OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/test/webdriver/http_response.h" | |
6 | |
7 #include "base/format_macros.h" | |
8 #include "base/strings/string_util.h" | |
9 #include "base/strings/stringprintf.h" | |
10 | |
11 namespace webdriver { | |
12 | |
13 const int HttpResponse::kOk = 200; | |
14 const int HttpResponse::kNoContent = 204; | |
15 const int HttpResponse::kSeeOther = 303; | |
16 const int HttpResponse::kNotModified = 304; | |
17 const int HttpResponse::kBadRequest = 400; | |
18 const int HttpResponse::kForbidden = 403; | |
19 const int HttpResponse::kNotFound = 404; | |
20 const int HttpResponse::kMethodNotAllowed = 405; | |
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 | |
29 | |
30 HttpResponse::HttpResponse() | |
31 : status_(kOk) { | |
32 } | |
33 | |
34 HttpResponse::HttpResponse(int status) | |
35 : status_(status) { | |
36 } | |
37 | |
38 HttpResponse::~HttpResponse() { | |
39 } | |
40 | |
41 void HttpResponse::AddHeader(const std::string& name, | |
42 const std::string& value) { | |
43 std::string lower_case_name(StringToLowerASCII(name)); | |
44 HeaderMap::iterator header = headers_.find(lower_case_name); | |
45 if (header == headers_.end()) { | |
46 headers_[lower_case_name] = value; | |
47 } else { | |
48 header->second.append("," + value); | |
49 } | |
50 } | |
51 | |
52 bool HttpResponse::GetHeader(const std::string& name, | |
53 std::string* value) const { | |
54 std::string lower_case_name(StringToLowerASCII(name)); | |
55 HeaderMap::const_iterator header = headers_.find(lower_case_name); | |
56 | |
57 if (header == headers_.end()) { | |
58 return false; | |
59 } | |
60 | |
61 if (value) { | |
62 *value = header->second; | |
63 } | |
64 | |
65 return true; | |
66 } | |
67 | |
68 bool HttpResponse::RemoveHeader(const std::string& name) { | |
69 std::string lower_case_name(StringToLowerASCII(name)); | |
70 HeaderMap::iterator header = headers_.find(lower_case_name); | |
71 | |
72 if (header == headers_.end()) { | |
73 return false; | |
74 } | |
75 | |
76 headers_.erase(header); | |
77 return true; | |
78 } | |
79 | |
80 void HttpResponse::ClearHeaders() { | |
81 headers_.clear(); | |
82 } | |
83 | |
84 void HttpResponse::SetMimeType(const std::string& mime_type) { | |
85 UpdateHeader("Content-Type", mime_type); | |
86 } | |
87 | |
88 std::string HttpResponse::GetReasonPhrase() const { | |
89 switch (status_) { | |
90 case kOk: | |
91 return "OK"; | |
92 case kNoContent: | |
93 return "No Content"; | |
94 case kSeeOther: | |
95 return "See Other"; | |
96 case kNotModified: | |
97 return "Not Modified"; | |
98 case kBadRequest: | |
99 return "Bad Request"; | |
100 case kForbidden: | |
101 return "Forbidden"; | |
102 case kNotFound: | |
103 return "Not Found"; | |
104 case kMethodNotAllowed: | |
105 return "Method Not Allowed"; | |
106 case kInternalServerError: | |
107 return "Internal Server Error"; | |
108 case kNotImplemented: | |
109 return "Not Implemented"; | |
110 default: | |
111 return "Unknown"; | |
112 } | |
113 } | |
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", kContentLengthHeader, body_.length()); | |
128 } | |
129 *data += "\r\n"; | |
130 | |
131 if (body_.length()) | |
132 *data += body_; | |
133 } | |
134 | |
135 int HttpResponse::status() const { | |
136 return status_; | |
137 } | |
138 | |
139 void HttpResponse::set_status(int status) { | |
140 status_ = status; | |
141 } | |
142 | |
143 const std::string& HttpResponse::body() const { | |
144 return body_; | |
145 } | |
146 | |
147 void HttpResponse::set_body(const std::string& body) { | |
148 body_ = body; | |
149 } | |
150 | |
151 void HttpResponse::UpdateHeader(const std::string& name, | |
152 const std::string& new_value) { | |
153 RemoveHeader(name); | |
154 AddHeader(name, new_value); | |
155 } | |
156 | |
157 } // namespace webdriver | |
OLD | NEW |