blob: 6bbe4a9fb93a5acd371eb68c40de62ee81c9d40e (
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
|
// 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::kNotFound = 404;
const int HttpResponse::kMethodNotAllowed = 405;
const int HttpResponse::kInternalServerError = 500;
HttpResponse::HttpResponse()
: status_(kOk) {
}
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::UpdateHeader(const std::string& name,
const std::string& new_value) {
RemoveHeader(name);
AddHeader(name, new_value);
}
void HttpResponse::SetMimeType(const std::string& mime_type) {
UpdateHeader("Content-Type", mime_type);
}
void HttpResponse::SetBody(const std::string& data) {
SetBody(data.data(), data.length());
}
void HttpResponse::SetBody(const char* const data, size_t length) {
data_ = std::string(data, length);
UpdateHeader("Content-Length",
base::StringPrintf("%"PRIuS"", data_.length()));
}
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 kNotFound:
return "Not Found";
case kMethodNotAllowed:
return "Method Not Allowed";
case kInternalServerError:
return "Internal Server Error";
default:
return "Unknown";
}
}
int HttpResponse::status() const {
return status_;
}
void HttpResponse::set_status(int status) {
status_ = status;
}
const HttpResponse::HeaderMap* HttpResponse::headers() const {
return &headers_;
}
const char* HttpResponse::data() const {
return data_.data();
}
size_t HttpResponse::length() const {
return data_.length();
}
} // namespace webdriver
|