OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/test/webdriver/http_response.h" | 5 #include "chrome/test/webdriver/http_response.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 | 10 |
11 namespace webdriver { | 11 namespace webdriver { |
12 | 12 |
13 const int HttpResponse::kOk = 200; | 13 const int HttpResponse::kOk = 200; |
14 const int HttpResponse::kNoContent = 204; | 14 const int HttpResponse::kNoContent = 204; |
15 const int HttpResponse::kSeeOther = 303; | 15 const int HttpResponse::kSeeOther = 303; |
16 const int HttpResponse::kNotModified = 304; | 16 const int HttpResponse::kNotModified = 304; |
17 const int HttpResponse::kBadRequest = 400; | 17 const int HttpResponse::kBadRequest = 400; |
| 18 const int HttpResponse::kForbidden = 403; |
18 const int HttpResponse::kNotFound = 404; | 19 const int HttpResponse::kNotFound = 404; |
19 const int HttpResponse::kMethodNotAllowed = 405; | 20 const int HttpResponse::kMethodNotAllowed = 405; |
20 const int HttpResponse::kInternalServerError = 500; | 21 const int HttpResponse::kInternalServerError = 500; |
| 22 const int HttpResponse::kNotImplemented = 501; |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char* kContentLengthHeader = "content-length"; |
| 27 |
| 28 } // namespace |
21 | 29 |
22 HttpResponse::HttpResponse() | 30 HttpResponse::HttpResponse() |
23 : status_(kOk) { | 31 : status_(kOk) { |
24 } | 32 } |
25 | 33 |
| 34 HttpResponse::HttpResponse(int status) |
| 35 : status_(status) { |
| 36 } |
| 37 |
26 HttpResponse::~HttpResponse() { | 38 HttpResponse::~HttpResponse() { |
27 } | 39 } |
28 | 40 |
29 void HttpResponse::AddHeader(const std::string& name, | 41 void HttpResponse::AddHeader(const std::string& name, |
30 const std::string& value) { | 42 const std::string& value) { |
31 std::string lower_case_name(StringToLowerASCII(name)); | 43 std::string lower_case_name(StringToLowerASCII(name)); |
32 HeaderMap::iterator header = headers_.find(lower_case_name); | 44 HeaderMap::iterator header = headers_.find(lower_case_name); |
33 if (header == headers_.end()) { | 45 if (header == headers_.end()) { |
34 headers_[lower_case_name] = value; | 46 headers_[lower_case_name] = value; |
35 } else { | 47 } else { |
(...skipping 26 matching lines...) Expand all Loading... |
62 } | 74 } |
63 | 75 |
64 headers_.erase(header); | 76 headers_.erase(header); |
65 return true; | 77 return true; |
66 } | 78 } |
67 | 79 |
68 void HttpResponse::ClearHeaders() { | 80 void HttpResponse::ClearHeaders() { |
69 headers_.clear(); | 81 headers_.clear(); |
70 } | 82 } |
71 | 83 |
72 void HttpResponse::UpdateHeader(const std::string& name, | |
73 const std::string& new_value) { | |
74 RemoveHeader(name); | |
75 AddHeader(name, new_value); | |
76 } | |
77 | |
78 void HttpResponse::SetMimeType(const std::string& mime_type) { | 84 void HttpResponse::SetMimeType(const std::string& mime_type) { |
79 UpdateHeader("Content-Type", mime_type); | 85 UpdateHeader("Content-Type", mime_type); |
80 } | 86 } |
81 | 87 |
82 void HttpResponse::SetBody(const std::string& data) { | |
83 SetBody(data.data(), data.length()); | |
84 } | |
85 | |
86 void HttpResponse::SetBody(const char* const data, size_t length) { | |
87 data_ = std::string(data, length); | |
88 UpdateHeader("Content-Length", | |
89 base::StringPrintf("%"PRIuS"", data_.length())); | |
90 } | |
91 | |
92 std::string HttpResponse::GetReasonPhrase() const { | 88 std::string HttpResponse::GetReasonPhrase() const { |
93 switch (status_) { | 89 switch (status_) { |
94 case kOk: | 90 case kOk: |
95 return "OK"; | 91 return "OK"; |
96 case kNoContent: | 92 case kNoContent: |
97 return "No Content"; | 93 return "No Content"; |
98 case kSeeOther: | 94 case kSeeOther: |
99 return "See Other"; | 95 return "See Other"; |
100 case kNotModified: | 96 case kNotModified: |
101 return "Not Modified"; | 97 return "Not Modified"; |
102 case kBadRequest: | 98 case kBadRequest: |
103 return "Bad Request"; | 99 return "Bad Request"; |
| 100 case kForbidden: |
| 101 return "Forbidden"; |
104 case kNotFound: | 102 case kNotFound: |
105 return "Not Found"; | 103 return "Not Found"; |
106 case kMethodNotAllowed: | 104 case kMethodNotAllowed: |
107 return "Method Not Allowed"; | 105 return "Method Not Allowed"; |
108 case kInternalServerError: | 106 case kInternalServerError: |
109 return "Internal Server Error"; | 107 return "Internal Server Error"; |
| 108 case kNotImplemented: |
| 109 return "Not Implemented"; |
110 default: | 110 default: |
111 return "Unknown"; | 111 return "Unknown"; |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
| 115 void HttpResponse::GetData(std::string* data) const { |
| 116 *data += base::StringPrintf("HTTP/1.1 %d %s\r\n", |
| 117 status_, GetReasonPhrase().c_str()); |
| 118 |
| 119 typedef HttpResponse::HeaderMap::const_iterator HeaderIter; |
| 120 for (HeaderIter header = headers_.begin(); header != headers_.end(); |
| 121 ++header) { |
| 122 *data += header->first + ":" + header->second + "\r\n"; |
| 123 } |
| 124 std::string length; |
| 125 if (!GetHeader(kContentLengthHeader, &length)) { |
| 126 *data += base::StringPrintf( |
| 127 "%s:%"PRIuS"\r\n", |
| 128 kContentLengthHeader, body_.length()); |
| 129 } |
| 130 *data += "\r\n"; |
| 131 |
| 132 if (body_.length()) |
| 133 *data += body_; |
| 134 } |
| 135 |
115 int HttpResponse::status() const { | 136 int HttpResponse::status() const { |
116 return status_; | 137 return status_; |
117 } | 138 } |
118 | 139 |
119 void HttpResponse::set_status(int status) { | 140 void HttpResponse::set_status(int status) { |
120 status_ = status; | 141 status_ = status; |
121 } | 142 } |
122 | 143 |
123 const HttpResponse::HeaderMap* HttpResponse::headers() const { | 144 const std::string& HttpResponse::body() const { |
124 return &headers_; | 145 return body_; |
125 } | 146 } |
126 | 147 |
127 const char* HttpResponse::data() const { | 148 void HttpResponse::set_body(const std::string& body) { |
128 return data_.data(); | 149 body_ = body; |
129 } | 150 } |
130 | 151 |
131 size_t HttpResponse::length() const { | 152 void HttpResponse::UpdateHeader(const std::string& name, |
132 return data_.length(); | 153 const std::string& new_value) { |
| 154 RemoveHeader(name); |
| 155 AddHeader(name, new_value); |
133 } | 156 } |
134 | 157 |
135 } // namespace webdriver | 158 } // namespace webdriver |
OLD | NEW |