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 3 // found in the LICENSE file. 4 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ 7 8 #include <string> 9 10 #include "base/callback.h" 11 #include "base/compiler_specific.h" 12 #include "base/macros.h" 13 #include "base/strings/string_split.h" 14 #include "base/time/time.h" 15 #include "net/http/http_status_code.h" 16 17 namespace net { 18 namespace test_server { 19 20 // Callback called when the response is done being sent. 21 using SendCompleteCallback = base::OnceClosure; 22 23 // Callback called when the response is ready to be sent that takes the 24 // |response| that is being sent along with the callback |write_done| that is 25 // called when the response has been fully written. 26 using SendBytesCallback = 27 base::RepeatingCallback<void(const std::string& response, 28 SendCompleteCallback write_done)>; 29 30 // Interface for HTTP response implementations. 31 class HttpResponse{ 32 public: 33 virtual ~HttpResponse(); 34 35 // |send| will send the specified data to the network socket, and invoke 36 // |write_done| when complete. When the entire response has been sent, 37 // |done| must be called. Invoking |done| will delete the HttpResponse object. 38 virtual void SendResponse(const SendBytesCallback& send, 39 SendCompleteCallback done) = 0; 40 }; 41 42 // This class is used to handle basic HTTP responses with commonly used 43 // response headers such as "Content-Type". Sends the response immediately. 44 class BasicHttpResponse : public HttpResponse { 45 public: 46 BasicHttpResponse(); 47 ~BasicHttpResponse() override; 48 49 // The response code. code()50 HttpStatusCode code() const { return code_; } set_code(HttpStatusCode code)51 void set_code(HttpStatusCode code) { code_ = code; } 52 53 // The content of the response. content()54 const std::string& content() const { return content_; } set_content(const std::string & content)55 void set_content(const std::string& content) { content_ = content; } 56 57 // The content type. content_type()58 const std::string& content_type() const { return content_type_; } set_content_type(const std::string & content_type)59 void set_content_type(const std::string& content_type) { 60 content_type_ = content_type; 61 } 62 63 // Adds a custom header. AddCustomHeader(const std::string & key,const std::string & value)64 void AddCustomHeader(const std::string& key, const std::string& value) { 65 custom_headers_.push_back(std::make_pair(key, value)); 66 } 67 68 // Generates and returns a http response string. 69 std::string ToResponseString() const; 70 71 void SendResponse(const SendBytesCallback& send, 72 SendCompleteCallback done) override; 73 74 private: 75 HttpStatusCode code_; 76 std::string content_; 77 std::string content_type_; 78 base::StringPairs custom_headers_; 79 80 DISALLOW_COPY_AND_ASSIGN(BasicHttpResponse); 81 }; 82 83 class DelayedHttpResponse : public BasicHttpResponse { 84 public: 85 DelayedHttpResponse(const base::TimeDelta delay); 86 ~DelayedHttpResponse() override; 87 88 // Issues a delayed send to the to the task runner. 89 void SendResponse(const SendBytesCallback& send, 90 SendCompleteCallback done) override; 91 92 private: 93 // The delay time for the response. 94 const base::TimeDelta delay_; 95 96 DISALLOW_COPY_AND_ASSIGN(DelayedHttpResponse); 97 }; 98 99 class RawHttpResponse : public HttpResponse { 100 public: 101 RawHttpResponse(const std::string& headers, const std::string& contents); 102 ~RawHttpResponse() override; 103 104 void SendResponse(const SendBytesCallback& send, 105 SendCompleteCallback done) override; 106 107 void AddHeader(const std::string& key_value_pair); 108 109 private: 110 std::string headers_; 111 const std::string contents_; 112 113 DISALLOW_COPY_AND_ASSIGN(RawHttpResponse); 114 }; 115 116 // "Response" where the server doesn't actually respond until the server is 117 // destroyed. 118 class HungResponse : public HttpResponse { 119 public: HungResponse()120 HungResponse() {} ~HungResponse()121 ~HungResponse() override {} 122 123 void SendResponse(const SendBytesCallback& send, 124 SendCompleteCallback done) override; 125 126 private: 127 DISALLOW_COPY_AND_ASSIGN(HungResponse); 128 }; 129 130 } // namespace test_server 131 } // namespace net 132 133 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ 134