Chromium Code Reviews| 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 SendResponse(std::string response_string, SendCompleteCallback callback); |
|
mmenke
2015/10/19 18:07:40
const std::string&
mmenke
2015/10/19 18:07:40
Maybe SendResponse -> SendBytes or SendResponseByt
svaldez
2015/10/19 21:56:15
Done.
svaldez
2015/10/19 21:56:15
Done.
| |
| 42 const base::Closure& callback); | |
| 43 | 43 |
| 44 // Accepts raw chunk of data from the client. Internally, passes it to the | 44 // Accepts raw chunk of data from the client. Internally, passes it to the |
| 45 // HttpRequestParser class. If a request is parsed, then |callback_| is | 45 // HttpRequestParser class. If a request is parsed, then |callback_| is |
| 46 // called. | 46 // called. |
| 47 int ReadData(const CompletionCallback& callback); | 47 int ReadData(const CompletionCallback& callback); |
| 48 | 48 |
| 49 bool ConsumeData(int size); | 49 bool ConsumeData(int size); |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 friend class EmbeddedTestServer; | 52 friend class EmbeddedTestServer; |
| 53 | 53 |
| 54 void SendInternal(const base::Closure& callback, | 54 void SendInternal(const base::Closure& callback, |
| 55 scoped_refptr<DrainableIOBuffer> buffer); | 55 scoped_refptr<DrainableIOBuffer> buffer); |
| 56 void OnSendInternalDone(const base::Closure& callback, | 56 void OnSendInternalDone(const base::Closure& callback, |
| 57 scoped_refptr<DrainableIOBuffer> buffer, | 57 scoped_refptr<DrainableIOBuffer> buffer, |
| 58 int rv); | 58 int rv); |
| 59 | 59 |
| 60 scoped_ptr<StreamSocket> socket_; | 60 scoped_ptr<StreamSocket> socket_; |
| 61 const HandleRequestCallback callback_; | 61 const HandleRequestCallback callback_; |
| 62 HttpRequestParser request_parser_; | 62 HttpRequestParser request_parser_; |
| 63 scoped_refptr<IOBufferWithSize> read_buf_; | 63 scoped_refptr<IOBufferWithSize> read_buf_; |
| 64 | 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(HttpConnection); | 65 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 } // namespace test_server | 68 } // namespace test_server |
| 69 } // namespace net | 69 } // namespace net |
| 70 | 70 |
| 71 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ | 71 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ |
| OLD | NEW |