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