1 // Copyright (c) 2012 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 "net/http/http_request_headers.h"
7 #include "base/logging.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/values.h"
12 #include "net/http/http_log_util.h"
13 #include "net/http/http_util.h"
17 const char HttpRequestHeaders::kGetMethod
[] = "GET";
18 const char HttpRequestHeaders::kAcceptCharset
[] = "Accept-Charset";
19 const char HttpRequestHeaders::kAcceptEncoding
[] = "Accept-Encoding";
20 const char HttpRequestHeaders::kAcceptLanguage
[] = "Accept-Language";
21 const char HttpRequestHeaders::kAuthorization
[] = "Authorization";
22 const char HttpRequestHeaders::kCacheControl
[] = "Cache-Control";
23 const char HttpRequestHeaders::kConnection
[] = "Connection";
24 const char HttpRequestHeaders::kContentLength
[] = "Content-Length";
25 const char HttpRequestHeaders::kContentType
[] = "Content-Type";
26 const char HttpRequestHeaders::kCookie
[] = "Cookie";
27 const char HttpRequestHeaders::kHost
[] = "Host";
28 const char HttpRequestHeaders::kIfModifiedSince
[] = "If-Modified-Since";
29 const char HttpRequestHeaders::kIfNoneMatch
[] = "If-None-Match";
30 const char HttpRequestHeaders::kIfRange
[] = "If-Range";
31 const char HttpRequestHeaders::kOrigin
[] = "Origin";
32 const char HttpRequestHeaders::kPragma
[] = "Pragma";
33 const char HttpRequestHeaders::kProxyAuthorization
[] = "Proxy-Authorization";
34 const char HttpRequestHeaders::kProxyConnection
[] = "Proxy-Connection";
35 const char HttpRequestHeaders::kRange
[] = "Range";
36 const char HttpRequestHeaders::kReferer
[] = "Referer";
37 const char HttpRequestHeaders::kUserAgent
[] = "User-Agent";
38 const char HttpRequestHeaders::kTransferEncoding
[] = "Transfer-Encoding";
40 HttpRequestHeaders::HeaderKeyValuePair::HeaderKeyValuePair() {
43 HttpRequestHeaders::HeaderKeyValuePair::HeaderKeyValuePair(
44 const base::StringPiece
& key
, const base::StringPiece
& value
)
45 : key(key
.data(), key
.size()), value(value
.data(), value
.size()) {
49 HttpRequestHeaders::Iterator::Iterator(const HttpRequestHeaders
& headers
)
51 curr_(headers
.headers_
.begin()),
52 end_(headers
.headers_
.end()) {}
54 HttpRequestHeaders::Iterator::~Iterator() {}
56 bool HttpRequestHeaders::Iterator::GetNext() {
69 HttpRequestHeaders::HttpRequestHeaders() {}
70 HttpRequestHeaders::~HttpRequestHeaders() {}
72 bool HttpRequestHeaders::GetHeader(const base::StringPiece
& key
,
73 std::string
* out
) const {
74 HeaderVector::const_iterator it
= FindHeader(key
);
75 if (it
== headers_
.end())
77 out
->assign(it
->value
);
81 void HttpRequestHeaders::Clear() {
85 void HttpRequestHeaders::SetHeader(const base::StringPiece
& key
,
86 const base::StringPiece
& value
) {
87 HeaderVector::iterator it
= FindHeader(key
);
88 if (it
!= headers_
.end())
89 it
->value
.assign(value
.data(), value
.size());
91 headers_
.push_back(HeaderKeyValuePair(key
, value
));
94 void HttpRequestHeaders::SetHeaderIfMissing(const base::StringPiece
& key
,
95 const base::StringPiece
& value
) {
96 HeaderVector::iterator it
= FindHeader(key
);
97 if (it
== headers_
.end())
98 headers_
.push_back(HeaderKeyValuePair(key
, value
));
101 void HttpRequestHeaders::RemoveHeader(const base::StringPiece
& key
) {
102 HeaderVector::iterator it
= FindHeader(key
);
103 if (it
!= headers_
.end())
107 void HttpRequestHeaders::AddHeaderFromString(
108 const base::StringPiece
& header_line
) {
109 DCHECK_EQ(std::string::npos
, header_line
.find("\r\n"))
110 << "\"" << header_line
<< "\" contains CRLF.";
112 const std::string::size_type key_end_index
= header_line
.find(":");
113 if (key_end_index
== std::string::npos
) {
114 LOG(DFATAL
) << "\"" << header_line
<< "\" is missing colon delimiter.";
118 if (key_end_index
== 0) {
119 LOG(DFATAL
) << "\"" << header_line
<< "\" is missing header key.";
123 const base::StringPiece
header_key(header_line
.data(), key_end_index
);
125 const std::string::size_type value_index
= key_end_index
+ 1;
127 if (value_index
< header_line
.size()) {
128 std::string
header_value(header_line
.data() + value_index
,
129 header_line
.size() - value_index
);
130 std::string::const_iterator header_value_begin
=
131 header_value
.begin();
132 std::string::const_iterator header_value_end
=
134 HttpUtil::TrimLWS(&header_value_begin
, &header_value_end
);
136 if (header_value_begin
== header_value_end
) {
137 // Value was all LWS.
138 SetHeader(header_key
, "");
140 SetHeader(header_key
,
141 base::StringPiece(&*header_value_begin
,
142 header_value_end
- header_value_begin
));
144 } else if (value_index
== header_line
.size()) {
145 SetHeader(header_key
, "");
151 void HttpRequestHeaders::AddHeadersFromString(
152 const base::StringPiece
& headers
) {
153 // TODO(willchan): Consider adding more StringPiece support in string_util.h
154 // to eliminate copies.
155 std::vector
<std::string
> header_line_vector
;
156 base::SplitStringUsingSubstr(headers
.as_string(), "\r\n",
157 &header_line_vector
);
158 for (std::vector
<std::string
>::const_iterator it
= header_line_vector
.begin();
159 it
!= header_line_vector
.end(); ++it
) {
161 AddHeaderFromString(*it
);
165 void HttpRequestHeaders::MergeFrom(const HttpRequestHeaders
& other
) {
166 for (HeaderVector::const_iterator it
= other
.headers_
.begin();
167 it
!= other
.headers_
.end(); ++it
) {
168 SetHeader(it
->key
, it
->value
);
172 std::string
HttpRequestHeaders::ToString() const {
174 for (HeaderVector::const_iterator it
= headers_
.begin();
175 it
!= headers_
.end(); ++it
) {
176 if (!it
->value
.empty()) {
177 base::StringAppendF(&output
, "%s: %s\r\n",
178 it
->key
.c_str(), it
->value
.c_str());
180 base::StringAppendF(&output
, "%s:\r\n", it
->key
.c_str());
183 output
.append("\r\n");
187 base::Value
* HttpRequestHeaders::NetLogCallback(
188 const std::string
* request_line
,
189 NetLog::LogLevel log_level
) const {
190 base::DictionaryValue
* dict
= new base::DictionaryValue();
191 dict
->SetString("line", *request_line
);
192 base::ListValue
* headers
= new base::ListValue();
193 for (HeaderVector::const_iterator it
= headers_
.begin();
194 it
!= headers_
.end(); ++it
) {
195 std::string log_value
= ElideHeaderValueForNetLog(
196 log_level
, it
->key
, it
->value
);
197 headers
->Append(new base::StringValue(
198 base::StringPrintf("%s: %s",
199 it
->key
.c_str(), log_value
.c_str())));
201 dict
->Set("headers", headers
);
206 bool HttpRequestHeaders::FromNetLogParam(const base::Value
* event_param
,
207 HttpRequestHeaders
* headers
,
208 std::string
* request_line
) {
212 const base::DictionaryValue
* dict
= NULL
;
213 const base::ListValue
* header_list
= NULL
;
216 !event_param
->GetAsDictionary(&dict
) ||
217 !dict
->GetList("headers", &header_list
) ||
218 !dict
->GetString("line", request_line
)) {
222 for (base::ListValue::const_iterator it
= header_list
->begin();
223 it
!= header_list
->end();
225 std::string header_line
;
226 if (!(*it
)->GetAsString(&header_line
)) {
231 headers
->AddHeaderFromString(header_line
);
236 HttpRequestHeaders::HeaderVector::iterator
237 HttpRequestHeaders::FindHeader(const base::StringPiece
& key
) {
238 for (HeaderVector::iterator it
= headers_
.begin();
239 it
!= headers_
.end(); ++it
) {
240 if (key
.length() == it
->key
.length() &&
241 !base::strncasecmp(key
.data(), it
->key
.data(), key
.length()))
245 return headers_
.end();
248 HttpRequestHeaders::HeaderVector::const_iterator
249 HttpRequestHeaders::FindHeader(const base::StringPiece
& key
) const {
250 for (HeaderVector::const_iterator it
= headers_
.begin();
251 it
!= headers_
.end(); ++it
) {
252 if (key
.length() == it
->key
.length() &&
253 !base::strncasecmp(key
.data(), it
->key
.data(), key
.length()))
257 return headers_
.end();