| 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> | |
| 9 #include <string> | 8 #include <string> |
| 10 | 9 |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 namespace test_server { | 17 namespace test_server { |
| 18 | 18 |
| 19 // Callback called when the response is done being sent. |
| 20 using SendCompleteCallback = base::Callback<void(void)>; |
| 21 |
| 22 // Callback called when the response is ready to be sent that takes the |
| 23 // |response| that is being sent along with the callback |write_done| that is |
| 24 // called when the response has been fully written. |
| 25 using SendBytesCallback = |
| 26 base::Callback<void(const std::string& response, |
| 27 const SendCompleteCallback& write_done)>; |
| 28 |
| 19 // Interface for HTTP response implementations. | 29 // Interface for HTTP response implementations. |
| 20 class HttpResponse{ | 30 class HttpResponse{ |
| 21 public: | 31 public: |
| 22 virtual ~HttpResponse(); | 32 virtual ~HttpResponse(); |
| 23 | 33 |
| 24 // Returns raw contents to be written to the network socket | 34 // |send| will send the specified data to the network socket, and invoke |
| 25 // in response. If you intend to make this a valid HTTP response, | 35 // |write_done| when complete. When the entire response has been sent, |
| 26 // it should start with "HTTP/x.x" line, followed by response headers. | 36 // |done| must be called. |
| 27 virtual std::string ToResponseString() const = 0; | 37 virtual void SendResponse(const SendBytesCallback& send, |
| 38 const SendCompleteCallback& done) = 0; |
| 28 }; | 39 }; |
| 29 | 40 |
| 30 // This class is used to handle basic HTTP responses with commonly used | 41 // This class is used to handle basic HTTP responses with commonly used |
| 31 // response headers such as "Content-Type". | 42 // response headers such as "Content-Type". Sends the response immediately. |
| 32 class BasicHttpResponse : public HttpResponse { | 43 class BasicHttpResponse : public HttpResponse { |
| 33 public: | 44 public: |
| 34 BasicHttpResponse(); | 45 BasicHttpResponse(); |
| 35 ~BasicHttpResponse() override; | 46 ~BasicHttpResponse() override; |
| 36 | 47 |
| 37 // The response code. | 48 // The response code. |
| 38 HttpStatusCode code() const { return code_; } | 49 HttpStatusCode code() const { return code_; } |
| 39 void set_code(HttpStatusCode code) { code_ = code; } | 50 void set_code(HttpStatusCode code) { code_ = code; } |
| 40 | 51 |
| 41 // The content of the response. | 52 // The content of the response. |
| 42 const std::string& content() const { return content_; } | 53 const std::string& content() const { return content_; } |
| 43 void set_content(const std::string& content) { content_ = content; } | 54 void set_content(const std::string& content) { content_ = content; } |
| 44 | 55 |
| 45 // The content type. | 56 // The content type. |
| 46 const std::string& content_type() const { return content_type_; } | 57 const std::string& content_type() const { return content_type_; } |
| 47 void set_content_type(const std::string& content_type) { | 58 void set_content_type(const std::string& content_type) { |
| 48 content_type_ = content_type; | 59 content_type_ = content_type; |
| 49 } | 60 } |
| 50 | 61 |
| 51 // Adds a custom header. | 62 // Adds a custom header. |
| 52 void AddCustomHeader(const std::string& key, const std::string& value) { | 63 void AddCustomHeader(const std::string& key, const std::string& value) { |
| 53 custom_headers_.push_back(std::make_pair(key, value)); | 64 custom_headers_.push_back(std::make_pair(key, value)); |
| 54 } | 65 } |
| 55 | 66 |
| 56 // Generates and returns a http response string. | 67 // Generates and returns a http response string. |
| 57 std::string ToResponseString() const override; | 68 std::string ToResponseString() const; |
| 69 |
| 70 void SendResponse(const SendBytesCallback& send, |
| 71 const SendCompleteCallback& done) override; |
| 58 | 72 |
| 59 private: | 73 private: |
| 60 HttpStatusCode code_; | 74 HttpStatusCode code_; |
| 61 std::string content_; | 75 std::string content_; |
| 62 std::string content_type_; | 76 std::string content_type_; |
| 63 base::StringPairs custom_headers_; | 77 base::StringPairs custom_headers_; |
| 64 | 78 |
| 65 DISALLOW_COPY_AND_ASSIGN(BasicHttpResponse); | 79 DISALLOW_COPY_AND_ASSIGN(BasicHttpResponse); |
| 66 }; | 80 }; |
| 67 | 81 |
| 82 class RawHttpResponse : public HttpResponse { |
| 83 public: |
| 84 RawHttpResponse(const std::string& headers, const std::string& contents); |
| 85 ~RawHttpResponse() override; |
| 86 |
| 87 void SendResponse(const SendBytesCallback& send, |
| 88 const SendCompleteCallback& done) override; |
| 89 |
| 90 void AddHeader(const std::string& key_value_pair); |
| 91 |
| 92 private: |
| 93 std::string headers_; |
| 94 const std::string contents_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(RawHttpResponse); |
| 97 }; |
| 98 |
| 68 } // namespace test_server | 99 } // namespace test_server |
| 69 } // namespace net | 100 } // namespace net |
| 70 | 101 |
| 71 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ | 102 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ |
| OLD | NEW |