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_RESPONSE_H_ | 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ |
| 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ | 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 14 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 namespace test_server { | 18 namespace test_server { |
| 18 | 19 |
| 20 // Callback called when the response is done being sent. | |
| 21 typedef base::Callback<void(void)> SendDoneCallback; | |
|
davidben
2015/10/13 19:43:48
Nit: using SendDoneCallback = base::Callback<void(
svaldez
2015/10/13 20:54:44
Done.
| |
| 22 | |
| 23 // Callback called when the response is ready to be sent. | |
| 24 typedef base::Callback<void(std::string response, SendDoneCallback done)> | |
| 25 SendCallback; | |
| 26 | |
| 19 // Interface for HTTP response implementations. | 27 // Interface for HTTP response implementations. |
| 20 class HttpResponse{ | 28 class HttpResponse{ |
| 21 public: | 29 public: |
| 22 virtual ~HttpResponse(); | 30 virtual ~HttpResponse(); |
| 23 | 31 |
| 24 // Returns raw contents to be written to the network socket | 32 // Calls |send| with the raw contents to be written to the network socket |
| 25 // in response. If you intend to make this a valid HTTP response, | 33 // in response. If you intend to make this a valid HTTP response, |
| 26 // it should start with "HTTP/x.x" line, followed by response headers. | 34 // it should start with "HTTP/x.x" line, followed by response headers. |
|
davidben
2015/10/13 19:43:48
Should add "May call |send| reentrantly." or somet
svaldez
2015/10/13 20:54:44
Done.
| |
| 27 virtual std::string ToResponseString() const = 0; | 35 virtual void SendResponse(SendCallback send, SendDoneCallback done) = 0; |
|
davidben
2015/10/13 19:43:48
Why do you need both callbacks? It looks like Send
davidben
2015/10/13 19:43:48
Nit: I'd probably call this GetResponse or somethi
svaldez
2015/10/13 20:54:44
I guess that's kind of true in the base case, howe
svaldez
2015/10/13 20:54:44
Some subclasses (not implemented yet) will need to
| |
| 28 }; | 36 }; |
| 29 | 37 |
| 30 // This class is used to handle basic HTTP responses with commonly used | 38 // This class is used to handle basic HTTP responses with commonly used |
| 31 // response headers such as "Content-Type". | 39 // response headers such as "Content-Type". |
| 32 class BasicHttpResponse : public HttpResponse { | 40 class BasicHttpResponse : public HttpResponse { |
| 33 public: | 41 public: |
| 34 BasicHttpResponse(); | 42 BasicHttpResponse(); |
| 35 ~BasicHttpResponse() override; | 43 ~BasicHttpResponse() override; |
| 36 | 44 |
| 37 // The response code. | 45 // The response code. |
| 38 HttpStatusCode code() const { return code_; } | 46 HttpStatusCode code() const { return code_; } |
| 39 void set_code(HttpStatusCode code) { code_ = code; } | 47 void set_code(HttpStatusCode code) { code_ = code; } |
| 40 | 48 |
| 41 // The content of the response. | 49 // The content of the response. |
| 42 const std::string& content() const { return content_; } | 50 const std::string& content() const { return content_; } |
| 43 void set_content(const std::string& content) { content_ = content; } | 51 void set_content(const std::string& content) { content_ = content; } |
| 44 | 52 |
| 45 // The content type. | 53 // The content type. |
| 46 const std::string& content_type() const { return content_type_; } | 54 const std::string& content_type() const { return content_type_; } |
| 47 void set_content_type(const std::string& content_type) { | 55 void set_content_type(const std::string& content_type) { |
| 48 content_type_ = content_type; | 56 content_type_ = content_type; |
| 49 } | 57 } |
| 50 | 58 |
| 51 // Adds a custom header. | 59 // Adds a custom header. |
| 52 void AddCustomHeader(const std::string& key, const std::string& value) { | 60 void AddCustomHeader(const std::string& key, const std::string& value) { |
| 53 custom_headers_.push_back(std::make_pair(key, value)); | 61 custom_headers_.push_back(std::make_pair(key, value)); |
| 54 } | 62 } |
| 55 | 63 |
| 56 // Generates and returns a http response string. | 64 // Generates and returns a http response string. |
| 57 std::string ToResponseString() const override; | 65 std::string ToResponseString() const; |
| 66 | |
| 67 void SendResponse(SendCallback send, SendDoneCallback done) override; | |
| 58 | 68 |
| 59 private: | 69 private: |
| 60 HttpStatusCode code_; | 70 HttpStatusCode code_; |
| 61 std::string content_; | 71 std::string content_; |
| 62 std::string content_type_; | 72 std::string content_type_; |
| 63 base::StringPairs custom_headers_; | 73 base::StringPairs custom_headers_; |
| 64 | 74 |
| 65 DISALLOW_COPY_AND_ASSIGN(BasicHttpResponse); | 75 DISALLOW_COPY_AND_ASSIGN(BasicHttpResponse); |
| 66 }; | 76 }; |
| 67 | 77 |
| 68 } // namespace test_server | 78 } // namespace test_server |
| 69 } // namespace net | 79 } // namespace net |
| 70 | 80 |
| 71 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ | 81 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ |
| OLD | NEW |