1 // Copyright (c) 2013 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.
5 #include "chrome/test/chromedriver/server/http_response.h"
7 #include "base/format_macros.h"
8 #include "base/string_util.h"
9 #include "base/stringprintf.h"
11 const int HttpResponse::kOk
= 200;
12 const int HttpResponse::kNoContent
= 204;
13 const int HttpResponse::kSeeOther
= 303;
14 const int HttpResponse::kNotModified
= 304;
15 const int HttpResponse::kBadRequest
= 400;
16 const int HttpResponse::kForbidden
= 403;
17 const int HttpResponse::kNotFound
= 404;
18 const int HttpResponse::kMethodNotAllowed
= 405;
19 const int HttpResponse::kInternalServerError
= 500;
20 const int HttpResponse::kNotImplemented
= 501;
24 const char* kContentLengthHeader
= "content-length";
28 HttpResponse::HttpResponse()
32 HttpResponse::HttpResponse(int status
)
36 HttpResponse::~HttpResponse() {
39 void HttpResponse::AddHeader(const std::string
& name
,
40 const std::string
& value
) {
41 std::string
lower_case_name(StringToLowerASCII(name
));
42 HeaderMap::iterator header
= headers_
.find(lower_case_name
);
43 if (header
== headers_
.end()) {
44 headers_
[lower_case_name
] = value
;
46 header
->second
.append("," + value
);
50 bool HttpResponse::GetHeader(const std::string
& name
,
51 std::string
* value
) const {
52 std::string
lower_case_name(StringToLowerASCII(name
));
53 HeaderMap::const_iterator header
= headers_
.find(lower_case_name
);
55 if (header
== headers_
.end()) {
60 *value
= header
->second
;
66 bool HttpResponse::RemoveHeader(const std::string
& name
) {
67 std::string
lower_case_name(StringToLowerASCII(name
));
68 HeaderMap::iterator header
= headers_
.find(lower_case_name
);
70 if (header
== headers_
.end()) {
74 headers_
.erase(header
);
78 void HttpResponse::ClearHeaders() {
82 void HttpResponse::SetMimeType(const std::string
& mime_type
) {
83 UpdateHeader("Content-Type", mime_type
);
86 std::string
HttpResponse::GetReasonPhrase() const {
95 return "Not Modified";
102 case kMethodNotAllowed
:
103 return "Method Not Allowed";
104 case kInternalServerError
:
105 return "Internal Server Error";
106 case kNotImplemented
:
107 return "Not Implemented";
113 void HttpResponse::GetData(std::string
* data
) const {
114 *data
+= base::StringPrintf("HTTP/1.1 %d %s\r\n",
115 status_
, GetReasonPhrase().c_str());
117 typedef HttpResponse::HeaderMap::const_iterator HeaderIter
;
118 for (HeaderIter header
= headers_
.begin(); header
!= headers_
.end();
120 *data
+= header
->first
+ ":" + header
->second
+ "\r\n";
123 if (!GetHeader(kContentLengthHeader
, &length
)) {
124 *data
+= base::StringPrintf(
126 kContentLengthHeader
, body_
.length());
134 int HttpResponse::status() const {
138 void HttpResponse::set_status(int status
) {
142 const std::string
& HttpResponse::body() const {
146 void HttpResponse::set_body(const std::string
& body
) {
150 void HttpResponse::UpdateHeader(const std::string
& name
,
151 const std::string
& new_value
) {
153 AddHeader(name
, new_value
);