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 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "net/http/http_status_code.h" |
13 | 15 |
14 namespace net { | 16 namespace net { |
15 namespace test_server { | 17 namespace test_server { |
16 | 18 |
17 enum ResponseCode { | |
18 SUCCESS = 200, | |
19 CREATED = 201, | |
20 NO_CONTENT = 204, | |
21 MOVED = 301, | |
22 RESUME_INCOMPLETE = 308, | |
23 NOT_FOUND = 404, | |
24 PRECONDITION = 412, | |
25 ACCESS_DENIED = 500, | |
26 }; | |
27 | |
28 // Interface for HTTP response implementations. | 19 // Interface for HTTP response implementations. |
29 class HttpResponse{ | 20 class HttpResponse{ |
30 public: | 21 public: |
31 virtual ~HttpResponse(); | 22 virtual ~HttpResponse(); |
32 | 23 |
33 // Returns raw contents to be written to the network socket | 24 // Returns raw contents to be written to the network socket |
34 // in response. If you intend to make this a valid HTTP response, | 25 // in response. If you intend to make this a valid HTTP response, |
35 // it should start with "HTTP/x.x" line, followed by response headers. | 26 // it should start with "HTTP/x.x" line, followed by response headers. |
36 virtual std::string ToResponseString() const = 0; | 27 virtual std::string ToResponseString() const = 0; |
37 }; | 28 }; |
38 | 29 |
39 // 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 |
40 // response headers such as "Content-Type". | 31 // response headers such as "Content-Type". |
41 class BasicHttpResponse : public HttpResponse { | 32 class BasicHttpResponse : public HttpResponse { |
42 public: | 33 public: |
43 BasicHttpResponse(); | 34 BasicHttpResponse(); |
44 virtual ~BasicHttpResponse(); | 35 virtual ~BasicHttpResponse(); |
45 | 36 |
46 // The response code. | 37 // The response code. |
47 ResponseCode code() const { return code_; } | 38 HttpStatusCode code() const { return code_; } |
48 void set_code(ResponseCode code) { code_ = code; } | 39 void set_code(HttpStatusCode code) { code_ = code; } |
49 | 40 |
50 // The content of the response. | 41 // The content of the response. |
51 const std::string& content() const { return content_; } | 42 const std::string& content() const { return content_; } |
52 void set_content(const std::string& content) { content_ = content; } | 43 void set_content(const std::string& content) { content_ = content; } |
53 | 44 |
54 // The content type. | 45 // The content type. |
55 const std::string& content_type() const { return content_type_; } | 46 const std::string& content_type() const { return content_type_; } |
56 void set_content_type(const std::string& content_type) { | 47 void set_content_type(const std::string& content_type) { |
57 content_type_ = content_type; | 48 content_type_ = content_type; |
58 } | 49 } |
59 | 50 |
60 // The custom headers to be sent to the client. | |
61 const std::map<std::string, std::string>& custom_headers() const { | |
62 return custom_headers_; | |
63 } | |
64 | |
65 // Adds a custom header. | 51 // Adds a custom header. |
66 void AddCustomHeader(const std::string& key, const std::string& value) { | 52 void AddCustomHeader(const std::string& key, const std::string& value) { |
67 custom_headers_[key] = value; | 53 custom_headers_.push_back(std::make_pair(key, value)); |
68 } | 54 } |
69 | 55 |
70 // Generates and returns a http response string. | 56 // Generates and returns a http response string. |
71 virtual std::string ToResponseString() const OVERRIDE; | 57 virtual std::string ToResponseString() const OVERRIDE; |
72 | 58 |
73 private: | 59 private: |
74 ResponseCode code_; | 60 HttpStatusCode code_; |
75 std::string content_; | 61 std::string content_; |
76 std::string content_type_; | 62 std::string content_type_; |
77 std::map<std::string, std::string> custom_headers_; | 63 std::vector<std::pair<std::string, std::string> > custom_headers_; |
78 | 64 |
79 DISALLOW_COPY_AND_ASSIGN(BasicHttpResponse); | 65 DISALLOW_COPY_AND_ASSIGN(BasicHttpResponse); |
80 }; | 66 }; |
81 | 67 |
82 } // namespace test_server | 68 } // namespace test_server |
83 } // namespace net | 69 } // namespace net |
84 | 70 |
85 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ | 71 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_RESPONSE_H_ |
OLD | NEW |