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