| 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 <memory> |
| 9 |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 14 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 16 #include "net/test/embedded_test_server/http_request.h" | 17 #include "net/test/embedded_test_server/http_request.h" |
| 17 #include "net/test/embedded_test_server/http_response.h" | 18 #include "net/test/embedded_test_server/http_response.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 | 21 |
| 21 class StreamSocket; | 22 class StreamSocket; |
| 22 | 23 |
| 23 namespace test_server { | 24 namespace test_server { |
| 24 | 25 |
| 25 class HttpConnection; | 26 class HttpConnection; |
| 26 class HttpResponse; | 27 class HttpResponse; |
| 27 | 28 |
| 28 // Calblack called when a request is parsed. Response should be sent | 29 // Calblack called when a request is parsed. Response should be sent |
| 29 // using HttpConnection::SendResponse() on the |connection| argument. | 30 // using HttpConnection::SendResponse() on the |connection| argument. |
| 30 typedef base::Callback<void(HttpConnection* connection, | 31 typedef base::Callback<void(HttpConnection* connection, |
| 31 scoped_ptr<HttpRequest> request)> | 32 std::unique_ptr<HttpRequest> request)> |
| 32 HandleRequestCallback; | 33 HandleRequestCallback; |
| 33 | 34 |
| 34 // Wraps the connection socket. Accepts incoming data and sends responses. | 35 // Wraps the connection socket. Accepts incoming data and sends responses. |
| 35 // If a valid request is parsed, then |callback_| is invoked. | 36 // If a valid request is parsed, then |callback_| is invoked. |
| 36 class HttpConnection { | 37 class HttpConnection { |
| 37 public: | 38 public: |
| 38 HttpConnection(scoped_ptr<StreamSocket> socket, | 39 HttpConnection(std::unique_ptr<StreamSocket> socket, |
| 39 const HandleRequestCallback& callback); | 40 const HandleRequestCallback& callback); |
| 40 ~HttpConnection(); | 41 ~HttpConnection(); |
| 41 | 42 |
| 42 // Sends the |response_string| to the client and calls |callback| once done. | 43 // Sends the |response_string| to the client and calls |callback| once done. |
| 43 void SendResponseBytes(const std::string& response_string, | 44 void SendResponseBytes(const std::string& response_string, |
| 44 const SendCompleteCallback& callback); | 45 const SendCompleteCallback& callback); |
| 45 | 46 |
| 46 // Accepts raw chunk of data from the client. Internally, passes it to the | 47 // Accepts raw chunk of data from the client. Internally, passes it to the |
| 47 // HttpRequestParser class. If a request is parsed, then |callback_| is | 48 // HttpRequestParser class. If a request is parsed, then |callback_| is |
| 48 // called. | 49 // called. |
| 49 int ReadData(const CompletionCallback& callback); | 50 int ReadData(const CompletionCallback& callback); |
| 50 | 51 |
| 51 bool ConsumeData(int size); | 52 bool ConsumeData(int size); |
| 52 | 53 |
| 53 private: | 54 private: |
| 54 friend class EmbeddedTestServer; | 55 friend class EmbeddedTestServer; |
| 55 | 56 |
| 56 void SendInternal(const base::Closure& callback, | 57 void SendInternal(const base::Closure& callback, |
| 57 scoped_refptr<DrainableIOBuffer> buffer); | 58 scoped_refptr<DrainableIOBuffer> buffer); |
| 58 void OnSendInternalDone(const base::Closure& callback, | 59 void OnSendInternalDone(const base::Closure& callback, |
| 59 scoped_refptr<DrainableIOBuffer> buffer, | 60 scoped_refptr<DrainableIOBuffer> buffer, |
| 60 int rv); | 61 int rv); |
| 61 | 62 |
| 62 base::WeakPtr<HttpConnection> GetWeakPtr(); | 63 base::WeakPtr<HttpConnection> GetWeakPtr(); |
| 63 | 64 |
| 64 scoped_ptr<StreamSocket> socket_; | 65 std::unique_ptr<StreamSocket> socket_; |
| 65 const HandleRequestCallback callback_; | 66 const HandleRequestCallback callback_; |
| 66 HttpRequestParser request_parser_; | 67 HttpRequestParser request_parser_; |
| 67 scoped_refptr<IOBufferWithSize> read_buf_; | 68 scoped_refptr<IOBufferWithSize> read_buf_; |
| 68 | 69 |
| 69 base::WeakPtrFactory<HttpConnection> weak_factory_; | 70 base::WeakPtrFactory<HttpConnection> weak_factory_; |
| 70 | 71 |
| 71 DISALLOW_COPY_AND_ASSIGN(HttpConnection); | 72 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 } // namespace test_server | 75 } // namespace test_server |
| 75 } // namespace net | 76 } // namespace net |
| 76 | 77 |
| 77 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ | 78 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ |
| OLD | NEW |