Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / test / chromedriver / server / http_response.cc
blobd1d75651cb1175461b118ddeba0ca6be429cffbb
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;
22 namespace {
24 const char* kContentLengthHeader = "content-length";
26 } // namespace
28 HttpResponse::HttpResponse()
29 : status_(kOk) {
32 HttpResponse::HttpResponse(int status)
33 : status_(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;
45 } else {
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()) {
56 return false;
59 if (value) {
60 *value = header->second;
63 return true;
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()) {
71 return false;
74 headers_.erase(header);
75 return true;
78 void HttpResponse::ClearHeaders() {
79 headers_.clear();
82 void HttpResponse::SetMimeType(const std::string& mime_type) {
83 UpdateHeader("Content-Type", mime_type);
86 std::string HttpResponse::GetReasonPhrase() const {
87 switch (status_) {
88 case kOk:
89 return "OK";
90 case kNoContent:
91 return "No Content";
92 case kSeeOther:
93 return "See Other";
94 case kNotModified:
95 return "Not Modified";
96 case kBadRequest:
97 return "Bad Request";
98 case kForbidden:
99 return "Forbidden";
100 case kNotFound:
101 return "Not Found";
102 case kMethodNotAllowed:
103 return "Method Not Allowed";
104 case kInternalServerError:
105 return "Internal Server Error";
106 case kNotImplemented:
107 return "Not Implemented";
108 default:
109 return "Unknown";
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();
119 ++header) {
120 *data += header->first + ":" + header->second + "\r\n";
122 std::string length;
123 if (!GetHeader(kContentLengthHeader, &length)) {
124 *data += base::StringPrintf(
125 "%s:%"PRIuS"\r\n",
126 kContentLengthHeader, body_.length());
128 *data += "\r\n";
130 if (body_.length())
131 *data += body_;
134 int HttpResponse::status() const {
135 return status_;
138 void HttpResponse::set_status(int status) {
139 status_ = status;
142 const std::string& HttpResponse::body() const {
143 return body_;
146 void HttpResponse::set_body(const std::string& body) {
147 body_ = body;
150 void HttpResponse::UpdateHeader(const std::string& name,
151 const std::string& new_value) {
152 RemoveHeader(name);
153 AddHeader(name, new_value);