| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ | 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ |
| 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ | 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/test/embedded_test_server/http_request.h" | 15 #include "net/test/embedded_test_server/http_request.h" |
| 16 #include "net/test/embedded_test_server/http_response.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 class StreamSocket; | 20 class StreamSocket; |
| 20 | 21 |
| 21 namespace test_server { | 22 namespace test_server { |
| 22 | 23 |
| 23 class HttpConnection; | 24 class HttpConnection; |
| 24 class HttpResponse; | 25 class HttpResponse; |
| 25 | 26 |
| 26 // Calblack called when a request is parsed. Response should be sent | 27 // Calblack called when a request is parsed. Response should be sent |
| 27 // using HttpConnection::SendResponse() on the |connection| argument. | 28 // using HttpConnection::SendResponse() on the |connection| argument. |
| 28 typedef base::Callback<void(HttpConnection* connection, | 29 typedef base::Callback<void(HttpConnection* connection, |
| 29 scoped_ptr<HttpRequest> request)> | 30 scoped_ptr<HttpRequest> request)> |
| 30 HandleRequestCallback; | 31 HandleRequestCallback; |
| 31 | 32 |
| 32 // Wraps the connection socket. Accepts incoming data and sends responses. | 33 // Wraps the connection socket. Accepts incoming data and sends responses. |
| 33 // If a valid request is parsed, then |callback_| is invoked. | 34 // If a valid request is parsed, then |callback_| is invoked. |
| 34 class HttpConnection { | 35 class HttpConnection { |
| 35 public: | 36 public: |
| 36 HttpConnection(scoped_ptr<StreamSocket> socket, | 37 HttpConnection(scoped_ptr<StreamSocket> socket, |
| 37 const HandleRequestCallback& callback); | 38 const HandleRequestCallback& callback); |
| 38 ~HttpConnection(); | 39 ~HttpConnection(); |
| 39 | 40 |
| 40 // Sends the HTTP response to the client. | 41 // Sends the |response_string| to the client and calls |callback| once done. |
| 41 void SendResponse(scoped_ptr<HttpResponse> response, | 42 void SendResponseBytes(const std::string& response_string, |
| 42 const base::Closure& callback); | 43 const SendCompleteCallback& callback); |
| 43 | 44 |
| 44 // Accepts raw chunk of data from the client. Internally, passes it to the | 45 // Accepts raw chunk of data from the client. Internally, passes it to the |
| 45 // HttpRequestParser class. If a request is parsed, then |callback_| is | 46 // HttpRequestParser class. If a request is parsed, then |callback_| is |
| 46 // called. | 47 // called. |
| 47 int ReadData(const CompletionCallback& callback); | 48 int ReadData(const CompletionCallback& callback); |
| 48 | 49 |
| 49 bool ConsumeData(int size); | 50 bool ConsumeData(int size); |
| 50 | 51 |
| 51 private: | 52 private: |
| 52 friend class EmbeddedTestServer; | 53 friend class EmbeddedTestServer; |
| 53 | 54 |
| 54 void SendInternal(const base::Closure& callback, | 55 void SendInternal(const base::Closure& callback, |
| 55 scoped_refptr<DrainableIOBuffer> buffer); | 56 scoped_refptr<DrainableIOBuffer> buffer); |
| 56 void OnSendInternalDone(const base::Closure& callback, | 57 void OnSendInternalDone(const base::Closure& callback, |
| 57 scoped_refptr<DrainableIOBuffer> buffer, | 58 scoped_refptr<DrainableIOBuffer> buffer, |
| 58 int rv); | 59 int rv); |
| 59 | 60 |
| 60 scoped_ptr<StreamSocket> socket_; | 61 scoped_ptr<StreamSocket> socket_; |
| 61 const HandleRequestCallback callback_; | 62 const HandleRequestCallback callback_; |
| 62 HttpRequestParser request_parser_; | 63 HttpRequestParser request_parser_; |
| 63 scoped_refptr<IOBufferWithSize> read_buf_; | 64 scoped_refptr<IOBufferWithSize> read_buf_; |
| 64 | 65 |
| 65 DISALLOW_COPY_AND_ASSIGN(HttpConnection); | 66 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 } // namespace test_server | 69 } // namespace test_server |
| 69 } // namespace net | 70 } // namespace net |
| 70 | 71 |
| 71 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ | 72 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ |
| OLD | NEW |