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 // The rules for header parsing were borrowed from Firefox:
6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpResponseHead.cpp
7 // The rules for parsing content-types were also borrowed from Firefox:
8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834
10 #include "net/http/http_response_headers.h"
14 #include "base/format_macros.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram_macros.h"
17 #include "base/pickle.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_piece.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/time/time.h"
23 #include "base/values.h"
24 #include "net/base/escape.h"
25 #include "net/http/http_byte_range.h"
26 #include "net/http/http_log_util.h"
27 #include "net/http/http_util.h"
29 using base::StringPiece
;
31 using base::TimeDelta
;
35 //-----------------------------------------------------------------------------
39 // These headers are RFC 2616 hop-by-hop headers;
40 // not to be stored by caches.
41 const char* const kHopByHopResponseHeaders
[] = {
50 // These headers are challenge response headers;
51 // not to be stored by caches.
52 const char* const kChallengeResponseHeaders
[] = {
57 // These headers are cookie setting headers;
58 // not to be stored by caches or disclosed otherwise.
59 const char* const kCookieResponseHeaders
[] = {
64 // By default, do not cache Strict-Transport-Security or Public-Key-Pins.
65 // This avoids erroneously re-processing them on page loads from cache ---
66 // they are defined to be valid only on live and error-free HTTPS
68 const char* const kSecurityStateHeaders
[] = {
69 "strict-transport-security",
73 // These response headers are not copied from a 304/206 response to the cached
74 // response headers. This list is based on Mozilla's nsHttpResponseHead.cpp.
75 const char* const kNonUpdatedHeaders
[] = {
89 // Some header prefixes mean "Don't copy this header from a 304 response.".
90 // Rather than listing all the relevant headers, we can consolidate them into
92 const char* const kNonUpdatedHeaderPrefixes
[] = {
98 bool ShouldUpdateHeader(const std::string::const_iterator
& name_begin
,
99 const std::string::const_iterator
& name_end
) {
100 for (size_t i
= 0; i
< arraysize(kNonUpdatedHeaders
); ++i
) {
101 if (base::LowerCaseEqualsASCII(name_begin
, name_end
, kNonUpdatedHeaders
[i
]))
104 for (size_t i
= 0; i
< arraysize(kNonUpdatedHeaderPrefixes
); ++i
) {
105 if (base::StartsWithASCII(std::string(name_begin
, name_end
),
106 kNonUpdatedHeaderPrefixes
[i
], false))
112 void CheckDoesNotHaveEmbededNulls(const std::string
& str
) {
113 // Care needs to be taken when adding values to the raw headers string to
114 // make sure it does not contain embeded NULLs. Any embeded '\0' may be
115 // understood as line terminators and change how header lines get tokenized.
116 CHECK(str
.find('\0') == std::string::npos
);
121 const char HttpResponseHeaders::kContentRange
[] = "Content-Range";
123 struct HttpResponseHeaders::ParsedHeader
{
124 // A header "continuation" contains only a subsequent value for the
125 // preceding header. (Header values are comma separated.)
126 bool is_continuation() const { return name_begin
== name_end
; }
128 std::string::const_iterator name_begin
;
129 std::string::const_iterator name_end
;
130 std::string::const_iterator value_begin
;
131 std::string::const_iterator value_end
;
134 //-----------------------------------------------------------------------------
136 HttpResponseHeaders::HttpResponseHeaders(const std::string
& raw_input
)
137 : response_code_(-1) {
140 // The most important thing to do with this histogram is find out
141 // the existence of unusual HTTP status codes. As it happens
142 // right now, there aren't double-constructions of response headers
143 // using this constructor, so our counts should also be accurate,
144 // without instantiating the histogram in two places. It is also
145 // important that this histogram not collect data in the other
146 // constructor, which rebuilds an histogram from a pickle, since
147 // that would actually create a double call between the original
148 // HttpResponseHeader that was serialized, and initialization of the
149 // new object from that pickle.
150 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Net.HttpResponseCode",
151 HttpUtil::MapStatusCodeForHistogram(
153 // Note the third argument is only
154 // evaluated once, see macro
155 // definition for details.
156 HttpUtil::GetStatusCodesForHistogram());
159 HttpResponseHeaders::HttpResponseHeaders(base::PickleIterator
* iter
)
160 : response_code_(-1) {
161 std::string raw_input
;
162 if (iter
->ReadString(&raw_input
))
166 void HttpResponseHeaders::Persist(base::Pickle
* pickle
,
167 PersistOptions options
) {
168 if (options
== PERSIST_RAW
) {
169 pickle
->WriteString(raw_headers_
);
173 HeaderSet filter_headers
;
175 // Construct set of headers to filter out based on options.
176 if ((options
& PERSIST_SANS_NON_CACHEABLE
) == PERSIST_SANS_NON_CACHEABLE
)
177 AddNonCacheableHeaders(&filter_headers
);
179 if ((options
& PERSIST_SANS_COOKIES
) == PERSIST_SANS_COOKIES
)
180 AddCookieHeaders(&filter_headers
);
182 if ((options
& PERSIST_SANS_CHALLENGES
) == PERSIST_SANS_CHALLENGES
)
183 AddChallengeHeaders(&filter_headers
);
185 if ((options
& PERSIST_SANS_HOP_BY_HOP
) == PERSIST_SANS_HOP_BY_HOP
)
186 AddHopByHopHeaders(&filter_headers
);
188 if ((options
& PERSIST_SANS_RANGES
) == PERSIST_SANS_RANGES
)
189 AddHopContentRangeHeaders(&filter_headers
);
191 if ((options
& PERSIST_SANS_SECURITY_STATE
) == PERSIST_SANS_SECURITY_STATE
)
192 AddSecurityStateHeaders(&filter_headers
);
195 blob
.reserve(raw_headers_
.size());
197 // This copies the status line w/ terminator null.
198 // Note raw_headers_ has embedded nulls instead of \n,
199 // so this just copies the first header line.
200 blob
.assign(raw_headers_
.c_str(), strlen(raw_headers_
.c_str()) + 1);
202 for (size_t i
= 0; i
< parsed_
.size(); ++i
) {
203 DCHECK(!parsed_
[i
].is_continuation());
205 // Locate the start of the next header.
207 while (++k
< parsed_
.size() && parsed_
[k
].is_continuation()) {}
210 std::string
header_name(parsed_
[i
].name_begin
, parsed_
[i
].name_end
);
211 base::StringToLowerASCII(&header_name
);
213 if (filter_headers
.find(header_name
) == filter_headers
.end()) {
214 // Make sure there is a null after the value.
215 blob
.append(parsed_
[i
].name_begin
, parsed_
[k
].value_end
);
216 blob
.push_back('\0');
221 blob
.push_back('\0');
223 pickle
->WriteString(blob
);
226 void HttpResponseHeaders::Update(const HttpResponseHeaders
& new_headers
) {
227 DCHECK(new_headers
.response_code() == 304 ||
228 new_headers
.response_code() == 206);
230 // Copy up to the null byte. This just copies the status line.
231 std::string
new_raw_headers(raw_headers_
.c_str());
232 new_raw_headers
.push_back('\0');
234 HeaderSet updated_headers
;
236 // NOTE: we write the new headers then the old headers for convenience. The
237 // order should not matter.
239 // Figure out which headers we want to take from new_headers:
240 for (size_t i
= 0; i
< new_headers
.parsed_
.size(); ++i
) {
241 const HeaderList
& new_parsed
= new_headers
.parsed_
;
243 DCHECK(!new_parsed
[i
].is_continuation());
245 // Locate the start of the next header.
247 while (++k
< new_parsed
.size() && new_parsed
[k
].is_continuation()) {}
250 const std::string::const_iterator
& name_begin
= new_parsed
[i
].name_begin
;
251 const std::string::const_iterator
& name_end
= new_parsed
[i
].name_end
;
252 if (ShouldUpdateHeader(name_begin
, name_end
)) {
253 std::string
name(name_begin
, name_end
);
254 base::StringToLowerASCII(&name
);
255 updated_headers
.insert(name
);
257 // Preserve this header line in the merged result, making sure there is
258 // a null after the value.
259 new_raw_headers
.append(name_begin
, new_parsed
[k
].value_end
);
260 new_raw_headers
.push_back('\0');
266 // Now, build the new raw headers.
267 MergeWithHeaders(new_raw_headers
, updated_headers
);
270 void HttpResponseHeaders::MergeWithHeaders(const std::string
& raw_headers
,
271 const HeaderSet
& headers_to_remove
) {
272 std::string
new_raw_headers(raw_headers
);
273 for (size_t i
= 0; i
< parsed_
.size(); ++i
) {
274 DCHECK(!parsed_
[i
].is_continuation());
276 // Locate the start of the next header.
278 while (++k
< parsed_
.size() && parsed_
[k
].is_continuation()) {}
281 std::string
name(parsed_
[i
].name_begin
, parsed_
[i
].name_end
);
282 base::StringToLowerASCII(&name
);
283 if (headers_to_remove
.find(name
) == headers_to_remove
.end()) {
284 // It's ok to preserve this header in the final result.
285 new_raw_headers
.append(parsed_
[i
].name_begin
, parsed_
[k
].value_end
);
286 new_raw_headers
.push_back('\0');
291 new_raw_headers
.push_back('\0');
293 // Make this object hold the new data.
294 raw_headers_
.clear();
296 Parse(new_raw_headers
);
299 void HttpResponseHeaders::RemoveHeader(const std::string
& name
) {
300 // Copy up to the null byte. This just copies the status line.
301 std::string
new_raw_headers(raw_headers_
.c_str());
302 new_raw_headers
.push_back('\0');
304 std::string
lowercase_name(name
);
305 base::StringToLowerASCII(&lowercase_name
);
307 to_remove
.insert(lowercase_name
);
308 MergeWithHeaders(new_raw_headers
, to_remove
);
311 void HttpResponseHeaders::RemoveHeaderLine(const std::string
& name
,
312 const std::string
& value
) {
313 std::string
name_lowercase(name
);
314 base::StringToLowerASCII(&name_lowercase
);
316 std::string
new_raw_headers(GetStatusLine());
317 new_raw_headers
.push_back('\0');
319 new_raw_headers
.reserve(raw_headers_
.size());
322 std::string old_header_name
;
323 std::string old_header_value
;
324 while (EnumerateHeaderLines(&iter
, &old_header_name
, &old_header_value
)) {
325 std::string
old_header_name_lowercase(name
);
326 base::StringToLowerASCII(&old_header_name_lowercase
);
328 if (name_lowercase
== old_header_name_lowercase
&&
329 value
== old_header_value
)
332 new_raw_headers
.append(old_header_name
);
333 new_raw_headers
.push_back(':');
334 new_raw_headers
.push_back(' ');
335 new_raw_headers
.append(old_header_value
);
336 new_raw_headers
.push_back('\0');
338 new_raw_headers
.push_back('\0');
340 // Make this object hold the new data.
341 raw_headers_
.clear();
343 Parse(new_raw_headers
);
346 void HttpResponseHeaders::AddHeader(const std::string
& header
) {
347 CheckDoesNotHaveEmbededNulls(header
);
348 DCHECK_EQ('\0', raw_headers_
[raw_headers_
.size() - 2]);
349 DCHECK_EQ('\0', raw_headers_
[raw_headers_
.size() - 1]);
350 // Don't copy the last null.
351 std::string
new_raw_headers(raw_headers_
, 0, raw_headers_
.size() - 1);
352 new_raw_headers
.append(header
);
353 new_raw_headers
.push_back('\0');
354 new_raw_headers
.push_back('\0');
356 // Make this object hold the new data.
357 raw_headers_
.clear();
359 Parse(new_raw_headers
);
362 void HttpResponseHeaders::ReplaceStatusLine(const std::string
& new_status
) {
363 CheckDoesNotHaveEmbededNulls(new_status
);
364 // Copy up to the null byte. This just copies the status line.
365 std::string
new_raw_headers(new_status
);
366 new_raw_headers
.push_back('\0');
368 HeaderSet empty_to_remove
;
369 MergeWithHeaders(new_raw_headers
, empty_to_remove
);
372 void HttpResponseHeaders::UpdateWithNewRange(
373 const HttpByteRange
& byte_range
,
375 bool replace_status_line
) {
376 DCHECK(byte_range
.IsValid());
377 DCHECK(byte_range
.HasFirstBytePosition());
378 DCHECK(byte_range
.HasLastBytePosition());
380 const char kLengthHeader
[] = "Content-Length";
381 const char kRangeHeader
[] = "Content-Range";
383 RemoveHeader(kLengthHeader
);
384 RemoveHeader(kRangeHeader
);
386 int64 start
= byte_range
.first_byte_position();
387 int64 end
= byte_range
.last_byte_position();
388 int64 range_len
= end
- start
+ 1;
390 if (replace_status_line
)
391 ReplaceStatusLine("HTTP/1.1 206 Partial Content");
393 AddHeader(base::StringPrintf("%s: bytes %" PRId64
"-%" PRId64
"/%" PRId64
,
394 kRangeHeader
, start
, end
, resource_size
));
395 AddHeader(base::StringPrintf("%s: %" PRId64
, kLengthHeader
, range_len
));
398 void HttpResponseHeaders::Parse(const std::string
& raw_input
) {
399 raw_headers_
.reserve(raw_input
.size());
401 // ParseStatusLine adds a normalized status line to raw_headers_
402 std::string::const_iterator line_begin
= raw_input
.begin();
403 std::string::const_iterator line_end
=
404 std::find(line_begin
, raw_input
.end(), '\0');
405 // has_headers = true, if there is any data following the status line.
406 // Used by ParseStatusLine() to decide if a HTTP/0.9 is really a HTTP/1.0.
407 bool has_headers
= (line_end
!= raw_input
.end() &&
408 (line_end
+ 1) != raw_input
.end() &&
409 *(line_end
+ 1) != '\0');
410 ParseStatusLine(line_begin
, line_end
, has_headers
);
411 raw_headers_
.push_back('\0'); // Terminate status line with a null.
413 if (line_end
== raw_input
.end()) {
414 raw_headers_
.push_back('\0'); // Ensure the headers end with a double null.
416 DCHECK_EQ('\0', raw_headers_
[raw_headers_
.size() - 2]);
417 DCHECK_EQ('\0', raw_headers_
[raw_headers_
.size() - 1]);
421 // Including a terminating null byte.
422 size_t status_line_len
= raw_headers_
.size();
424 // Now, we add the rest of the raw headers to raw_headers_, and begin parsing
425 // it (to populate our parsed_ vector).
426 raw_headers_
.append(line_end
+ 1, raw_input
.end());
428 // Ensure the headers end with a double null.
429 while (raw_headers_
.size() < 2 ||
430 raw_headers_
[raw_headers_
.size() - 2] != '\0' ||
431 raw_headers_
[raw_headers_
.size() - 1] != '\0') {
432 raw_headers_
.push_back('\0');
435 // Adjust to point at the null byte following the status line
436 line_end
= raw_headers_
.begin() + status_line_len
- 1;
438 HttpUtil::HeadersIterator
headers(line_end
+ 1, raw_headers_
.end(),
439 std::string(1, '\0'));
440 while (headers
.GetNext()) {
441 AddHeader(headers
.name_begin(),
443 headers
.values_begin(),
444 headers
.values_end());
447 DCHECK_EQ('\0', raw_headers_
[raw_headers_
.size() - 2]);
448 DCHECK_EQ('\0', raw_headers_
[raw_headers_
.size() - 1]);
451 // Append all of our headers to the final output string.
452 void HttpResponseHeaders::GetNormalizedHeaders(std::string
* output
) const {
453 // copy up to the null byte. this just copies the status line.
454 output
->assign(raw_headers_
.c_str());
456 // headers may appear multiple times (not necessarily in succession) in the
457 // header data, so we build a map from header name to generated header lines.
458 // to preserve the order of the original headers, the actual values are kept
459 // in a separate list. finally, the list of headers is flattened to form
460 // the normalized block of headers.
462 // NOTE: We take special care to preserve the whitespace around any commas
463 // that may occur in the original response headers. Because our consumer may
464 // be a web app, we cannot be certain of the semantics of commas despite the
465 // fact that RFC 2616 says that they should be regarded as value separators.
467 typedef base::hash_map
<std::string
, size_t> HeadersMap
;
468 HeadersMap headers_map
;
469 HeadersMap::iterator iter
= headers_map
.end();
471 std::vector
<std::string
> headers
;
473 for (size_t i
= 0; i
< parsed_
.size(); ++i
) {
474 DCHECK(!parsed_
[i
].is_continuation());
476 std::string
name(parsed_
[i
].name_begin
, parsed_
[i
].name_end
);
477 std::string lower_name
= base::StringToLowerASCII(name
);
479 iter
= headers_map
.find(lower_name
);
480 if (iter
== headers_map
.end()) {
481 iter
= headers_map
.insert(
482 HeadersMap::value_type(lower_name
, headers
.size())).first
;
483 headers
.push_back(name
+ ": ");
485 headers
[iter
->second
].append(", ");
488 std::string::const_iterator value_begin
= parsed_
[i
].value_begin
;
489 std::string::const_iterator value_end
= parsed_
[i
].value_end
;
490 while (++i
< parsed_
.size() && parsed_
[i
].is_continuation())
491 value_end
= parsed_
[i
].value_end
;
494 headers
[iter
->second
].append(value_begin
, value_end
);
497 for (size_t i
= 0; i
< headers
.size(); ++i
) {
498 output
->push_back('\n');
499 output
->append(headers
[i
]);
502 output
->push_back('\n');
505 bool HttpResponseHeaders::GetNormalizedHeader(const std::string
& name
,
506 std::string
* value
) const {
507 // If you hit this assertion, please use EnumerateHeader instead!
508 DCHECK(!HttpUtil::IsNonCoalescingHeader(name
));
514 while (i
< parsed_
.size()) {
515 i
= FindHeader(i
, name
);
516 if (i
== std::string::npos
)
524 std::string::const_iterator value_begin
= parsed_
[i
].value_begin
;
525 std::string::const_iterator value_end
= parsed_
[i
].value_end
;
526 while (++i
< parsed_
.size() && parsed_
[i
].is_continuation())
527 value_end
= parsed_
[i
].value_end
;
528 value
->append(value_begin
, value_end
);
534 std::string
HttpResponseHeaders::GetStatusLine() const {
535 // copy up to the null byte.
536 return std::string(raw_headers_
.c_str());
539 std::string
HttpResponseHeaders::GetStatusText() const {
540 // GetStatusLine() is already normalized, so it has the format:
541 // <http_version> SP <response_code> SP <status_text>
542 std::string status_text
= GetStatusLine();
543 std::string::const_iterator begin
= status_text
.begin();
544 std::string::const_iterator end
= status_text
.end();
545 for (int i
= 0; i
< 2; ++i
)
546 begin
= std::find(begin
, end
, ' ') + 1;
547 return std::string(begin
, end
);
550 bool HttpResponseHeaders::EnumerateHeaderLines(void** iter
,
552 std::string
* value
) const {
553 size_t i
= reinterpret_cast<size_t>(*iter
);
554 if (i
== parsed_
.size())
557 DCHECK(!parsed_
[i
].is_continuation());
559 name
->assign(parsed_
[i
].name_begin
, parsed_
[i
].name_end
);
561 std::string::const_iterator value_begin
= parsed_
[i
].value_begin
;
562 std::string::const_iterator value_end
= parsed_
[i
].value_end
;
563 while (++i
< parsed_
.size() && parsed_
[i
].is_continuation())
564 value_end
= parsed_
[i
].value_end
;
566 value
->assign(value_begin
, value_end
);
568 *iter
= reinterpret_cast<void*>(i
);
572 bool HttpResponseHeaders::EnumerateHeader(void** iter
,
573 const base::StringPiece
& name
,
574 std::string
* value
) const {
576 if (!iter
|| !*iter
) {
577 i
= FindHeader(0, name
);
579 i
= reinterpret_cast<size_t>(*iter
);
580 if (i
>= parsed_
.size()) {
581 i
= std::string::npos
;
582 } else if (!parsed_
[i
].is_continuation()) {
583 i
= FindHeader(i
, name
);
587 if (i
== std::string::npos
) {
593 *iter
= reinterpret_cast<void*>(i
+ 1);
594 value
->assign(parsed_
[i
].value_begin
, parsed_
[i
].value_end
);
598 bool HttpResponseHeaders::HasHeaderValue(const base::StringPiece
& name
,
599 const base::StringPiece
& value
) const {
600 // The value has to be an exact match. This is important since
601 // 'cache-control: no-cache' != 'cache-control: no-cache="foo"'
604 while (EnumerateHeader(&iter
, name
, &temp
)) {
605 if (value
.size() == temp
.size() &&
606 std::equal(temp
.begin(), temp
.end(), value
.begin(),
607 base::CaseInsensitiveCompare
<char>()))
613 bool HttpResponseHeaders::HasHeader(const base::StringPiece
& name
) const {
614 return FindHeader(0, name
) != std::string::npos
;
617 HttpResponseHeaders::HttpResponseHeaders() : response_code_(-1) {
620 HttpResponseHeaders::~HttpResponseHeaders() {
623 // Note: this implementation implicitly assumes that line_end points at a valid
624 // sentinel character (such as '\0').
626 HttpVersion
HttpResponseHeaders::ParseVersion(
627 std::string::const_iterator line_begin
,
628 std::string::const_iterator line_end
) {
629 std::string::const_iterator p
= line_begin
;
631 // RFC2616 sec 3.1: HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
632 // TODO: (1*DIGIT apparently means one or more digits, but we only handle 1).
633 // TODO: handle leading zeros, which is allowed by the rfc1616 sec 3.1.
635 if ((line_end
- p
< 4) || !base::LowerCaseEqualsASCII(p
, p
+ 4, "http")) {
636 DVLOG(1) << "missing status line";
637 return HttpVersion();
642 if (p
>= line_end
|| *p
!= '/') {
643 DVLOG(1) << "missing version";
644 return HttpVersion();
647 std::string::const_iterator dot
= std::find(p
, line_end
, '.');
648 if (dot
== line_end
) {
649 DVLOG(1) << "malformed version";
650 return HttpVersion();
653 ++p
; // from / to first digit.
654 ++dot
; // from . to second digit.
656 if (!(*p
>= '0' && *p
<= '9' && *dot
>= '0' && *dot
<= '9')) {
657 DVLOG(1) << "malformed version number";
658 return HttpVersion();
661 uint16 major
= *p
- '0';
662 uint16 minor
= *dot
- '0';
664 return HttpVersion(major
, minor
);
667 // Note: this implementation implicitly assumes that line_end points at a valid
668 // sentinel character (such as '\0').
669 void HttpResponseHeaders::ParseStatusLine(
670 std::string::const_iterator line_begin
,
671 std::string::const_iterator line_end
,
673 // Extract the version number
674 parsed_http_version_
= ParseVersion(line_begin
, line_end
);
676 // Clamp the version number to one of: {0.9, 1.0, 1.1}
677 if (parsed_http_version_
== HttpVersion(0, 9) && !has_headers
) {
678 http_version_
= HttpVersion(0, 9);
679 raw_headers_
= "HTTP/0.9";
680 } else if (parsed_http_version_
>= HttpVersion(1, 1)) {
681 http_version_
= HttpVersion(1, 1);
682 raw_headers_
= "HTTP/1.1";
684 // Treat everything else like HTTP 1.0
685 http_version_
= HttpVersion(1, 0);
686 raw_headers_
= "HTTP/1.0";
688 if (parsed_http_version_
!= http_version_
) {
689 DVLOG(1) << "assuming HTTP/" << http_version_
.major_value() << "."
690 << http_version_
.minor_value();
693 // TODO(eroman): this doesn't make sense if ParseVersion failed.
694 std::string::const_iterator p
= std::find(line_begin
, line_end
, ' ');
697 DVLOG(1) << "missing response status; assuming 200 OK";
698 raw_headers_
.append(" 200 OK");
699 response_code_
= 200;
707 std::string::const_iterator code
= p
;
708 while (*p
>= '0' && *p
<= '9')
712 DVLOG(1) << "missing response status number; assuming 200";
713 raw_headers_
.append(" 200 OK");
714 response_code_
= 200;
717 raw_headers_
.push_back(' ');
718 raw_headers_
.append(code
, p
);
719 raw_headers_
.push_back(' ');
720 base::StringToInt(StringPiece(code
, p
), &response_code_
);
726 // Trim trailing whitespace.
727 while (line_end
> p
&& line_end
[-1] == ' ')
731 DVLOG(1) << "missing response status text; assuming OK";
732 // Not super critical what we put here. Just use "OK"
733 // even if it isn't descriptive of response_code_.
734 raw_headers_
.append("OK");
736 raw_headers_
.append(p
, line_end
);
740 size_t HttpResponseHeaders::FindHeader(size_t from
,
741 const base::StringPiece
& search
) const {
742 for (size_t i
= from
; i
< parsed_
.size(); ++i
) {
743 if (parsed_
[i
].is_continuation())
745 const std::string::const_iterator
& name_begin
= parsed_
[i
].name_begin
;
746 const std::string::const_iterator
& name_end
= parsed_
[i
].name_end
;
747 if (static_cast<size_t>(name_end
- name_begin
) == search
.size() &&
748 std::equal(name_begin
, name_end
, search
.begin(),
749 base::CaseInsensitiveCompare
<char>()))
753 return std::string::npos
;
756 bool HttpResponseHeaders::GetCacheControlDirective(const StringPiece
& directive
,
757 TimeDelta
* result
) const {
758 StringPiece
name("cache-control");
761 size_t directive_size
= directive
.size();
764 while (EnumerateHeader(&iter
, name
, &value
)) {
765 if (value
.size() > directive_size
+ 1 &&
766 base::LowerCaseEqualsASCII(
767 value
.begin(), value
.begin() + directive_size
, directive
.begin()) &&
768 value
[directive_size
] == '=') {
771 StringPiece(value
.begin() + directive_size
+ 1, value
.end()),
773 *result
= TimeDelta::FromSeconds(seconds
);
781 void HttpResponseHeaders::AddHeader(std::string::const_iterator name_begin
,
782 std::string::const_iterator name_end
,
783 std::string::const_iterator values_begin
,
784 std::string::const_iterator values_end
) {
785 // If the header can be coalesced, then we should split it up.
786 if (values_begin
== values_end
||
787 HttpUtil::IsNonCoalescingHeader(name_begin
, name_end
)) {
788 AddToParsed(name_begin
, name_end
, values_begin
, values_end
);
790 HttpUtil::ValuesIterator
it(values_begin
, values_end
, ',');
791 while (it
.GetNext()) {
792 AddToParsed(name_begin
, name_end
, it
.value_begin(), it
.value_end());
793 // clobber these so that subsequent values are treated as continuations
794 name_begin
= name_end
= raw_headers_
.end();
799 void HttpResponseHeaders::AddToParsed(std::string::const_iterator name_begin
,
800 std::string::const_iterator name_end
,
801 std::string::const_iterator value_begin
,
802 std::string::const_iterator value_end
) {
804 header
.name_begin
= name_begin
;
805 header
.name_end
= name_end
;
806 header
.value_begin
= value_begin
;
807 header
.value_end
= value_end
;
808 parsed_
.push_back(header
);
811 void HttpResponseHeaders::AddNonCacheableHeaders(HeaderSet
* result
) const {
812 // Add server specified transients. Any 'cache-control: no-cache="foo,bar"'
813 // headers present in the response specify additional headers that we should
814 // not store in the cache.
815 const char kCacheControl
[] = "cache-control";
816 const char kPrefix
[] = "no-cache=\"";
817 const size_t kPrefixLen
= sizeof(kPrefix
) - 1;
821 while (EnumerateHeader(&iter
, kCacheControl
, &value
)) {
822 // If the value is smaller than the prefix and a terminal quote, skip
824 if (value
.size() <= kPrefixLen
||
825 value
.compare(0, kPrefixLen
, kPrefix
) != 0) {
828 // if it doesn't end with a quote, then treat as malformed
829 if (value
[value
.size()-1] != '\"')
832 // process the value as a comma-separated list of items. Each
833 // item can be wrapped by linear white space.
834 std::string::const_iterator item
= value
.begin() + kPrefixLen
;
835 std::string::const_iterator end
= value
.end() - 1;
836 while (item
!= end
) {
837 // Find the comma to compute the length of the current item,
838 // and the position of the next one.
839 std::string::const_iterator item_next
= std::find(item
, end
, ',');
840 std::string::const_iterator item_end
= end
;
841 if (item_next
!= end
) {
842 // Skip over comma for next position.
843 item_end
= item_next
;
846 // trim off leading and trailing whitespace in this item.
847 HttpUtil::TrimLWS(&item
, &item_end
);
849 // assuming the header is not empty, lowercase and insert into set
850 if (item_end
> item
) {
851 std::string
name(&*item
, item_end
- item
);
852 base::StringToLowerASCII(&name
);
853 result
->insert(name
);
856 // Continue to next item.
862 void HttpResponseHeaders::AddHopByHopHeaders(HeaderSet
* result
) {
863 for (size_t i
= 0; i
< arraysize(kHopByHopResponseHeaders
); ++i
)
864 result
->insert(std::string(kHopByHopResponseHeaders
[i
]));
867 void HttpResponseHeaders::AddCookieHeaders(HeaderSet
* result
) {
868 for (size_t i
= 0; i
< arraysize(kCookieResponseHeaders
); ++i
)
869 result
->insert(std::string(kCookieResponseHeaders
[i
]));
872 void HttpResponseHeaders::AddChallengeHeaders(HeaderSet
* result
) {
873 for (size_t i
= 0; i
< arraysize(kChallengeResponseHeaders
); ++i
)
874 result
->insert(std::string(kChallengeResponseHeaders
[i
]));
877 void HttpResponseHeaders::AddHopContentRangeHeaders(HeaderSet
* result
) {
878 result
->insert(kContentRange
);
881 void HttpResponseHeaders::AddSecurityStateHeaders(HeaderSet
* result
) {
882 for (size_t i
= 0; i
< arraysize(kSecurityStateHeaders
); ++i
)
883 result
->insert(std::string(kSecurityStateHeaders
[i
]));
886 void HttpResponseHeaders::GetMimeTypeAndCharset(std::string
* mime_type
,
887 std::string
* charset
) const {
891 std::string name
= "content-type";
894 bool had_charset
= false;
897 while (EnumerateHeader(&iter
, name
, &value
))
898 HttpUtil::ParseContentType(value
, mime_type
, charset
, &had_charset
, NULL
);
901 bool HttpResponseHeaders::GetMimeType(std::string
* mime_type
) const {
903 GetMimeTypeAndCharset(mime_type
, &unused
);
904 return !mime_type
->empty();
907 bool HttpResponseHeaders::GetCharset(std::string
* charset
) const {
909 GetMimeTypeAndCharset(&unused
, charset
);
910 return !charset
->empty();
913 bool HttpResponseHeaders::IsRedirect(std::string
* location
) const {
914 if (!IsRedirectResponseCode(response_code_
))
917 // If we lack a Location header, then we can't treat this as a redirect.
918 // We assume that the first non-empty location value is the target URL that
919 // we want to follow. TODO(darin): Is this consistent with other browsers?
920 size_t i
= std::string::npos
;
922 i
= FindHeader(++i
, "location");
923 if (i
== std::string::npos
)
925 // If the location value is empty, then it doesn't count.
926 } while (parsed_
[i
].value_begin
== parsed_
[i
].value_end
);
929 // Escape any non-ASCII characters to preserve them. The server should
930 // only be returning ASCII here, but for compat we need to do this.
931 *location
= EscapeNonASCII(
932 std::string(parsed_
[i
].value_begin
, parsed_
[i
].value_end
));
939 bool HttpResponseHeaders::IsRedirectResponseCode(int response_code
) {
940 // Users probably want to see 300 (multiple choice) pages, so we don't count
941 // them as redirects that need to be followed.
942 return (response_code
== 301 ||
943 response_code
== 302 ||
944 response_code
== 303 ||
945 response_code
== 307 ||
946 response_code
== 308);
949 // From RFC 2616 section 13.2.4:
951 // The calculation to determine if a response has expired is quite simple:
953 // response_is_fresh = (freshness_lifetime > current_age)
955 // Of course, there are other factors that can force a response to always be
956 // validated or re-fetched.
958 // From RFC 5861 section 3, a stale response may be used while revalidation is
959 // performed in the background if
961 // freshness_lifetime + stale_while_revalidate > current_age
963 ValidationType
HttpResponseHeaders::RequiresValidation(
964 const Time
& request_time
,
965 const Time
& response_time
,
966 const Time
& current_time
) const {
967 FreshnessLifetimes lifetimes
= GetFreshnessLifetimes(response_time
);
968 if (lifetimes
.freshness
== TimeDelta() && lifetimes
.staleness
== TimeDelta())
969 return VALIDATION_SYNCHRONOUS
;
971 TimeDelta age
= GetCurrentAge(request_time
, response_time
, current_time
);
973 if (lifetimes
.freshness
> age
)
974 return VALIDATION_NONE
;
976 if (lifetimes
.freshness
+ lifetimes
.staleness
> age
)
977 return VALIDATION_ASYNCHRONOUS
;
979 return VALIDATION_SYNCHRONOUS
;
982 // From RFC 2616 section 13.2.4:
984 // The max-age directive takes priority over Expires, so if max-age is present
985 // in a response, the calculation is simply:
987 // freshness_lifetime = max_age_value
989 // Otherwise, if Expires is present in the response, the calculation is:
991 // freshness_lifetime = expires_value - date_value
993 // Note that neither of these calculations is vulnerable to clock skew, since
994 // all of the information comes from the origin server.
996 // Also, if the response does have a Last-Modified time, the heuristic
997 // expiration value SHOULD be no more than some fraction of the interval since
998 // that time. A typical setting of this fraction might be 10%:
1000 // freshness_lifetime = (date_value - last_modified_value) * 0.10
1002 // If the stale-while-revalidate directive is present, then it is used to set
1003 // the |staleness| time, unless it overridden by another directive.
1005 HttpResponseHeaders::FreshnessLifetimes
1006 HttpResponseHeaders::GetFreshnessLifetimes(const Time
& response_time
) const {
1007 FreshnessLifetimes lifetimes
;
1008 // Check for headers that force a response to never be fresh. For backwards
1009 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control:
1010 // no-cache" even though RFC 2616 does not specify it.
1011 if (HasHeaderValue("cache-control", "no-cache") ||
1012 HasHeaderValue("cache-control", "no-store") ||
1013 HasHeaderValue("pragma", "no-cache") ||
1014 // Vary: * is never usable: see RFC 2616 section 13.6.
1015 HasHeaderValue("vary", "*")) {
1019 // Cache-Control directive must_revalidate overrides stale-while-revalidate.
1020 bool must_revalidate
= HasHeaderValue("cache-control", "must-revalidate");
1022 if (must_revalidate
|| !GetStaleWhileRevalidateValue(&lifetimes
.staleness
)) {
1023 DCHECK_EQ(TimeDelta(), lifetimes
.staleness
);
1026 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the
1027 // Expires header after checking for max-age in GetFreshnessLifetimes. This
1028 // is important since "Expires: <date in the past>" means not fresh, but
1029 // it should not trump a max-age value.
1030 if (GetMaxAgeValue(&lifetimes
.freshness
))
1033 // If there is no Date header, then assume that the server response was
1034 // generated at the time when we received the response.
1036 if (!GetDateValue(&date_value
))
1037 date_value
= response_time
;
1040 if (GetExpiresValue(&expires_value
)) {
1041 // The expires value can be a date in the past!
1042 if (expires_value
> date_value
) {
1043 lifetimes
.freshness
= expires_value
- date_value
;
1047 DCHECK_EQ(TimeDelta(), lifetimes
.freshness
);
1051 // From RFC 2616 section 13.4:
1053 // A response received with a status code of 200, 203, 206, 300, 301 or 410
1054 // MAY be stored by a cache and used in reply to a subsequent request,
1055 // subject to the expiration mechanism, unless a cache-control directive
1056 // prohibits caching.
1058 // A response received with any other status code (e.g. status codes 302
1059 // and 307) MUST NOT be returned in a reply to a subsequent request unless
1060 // there are cache-control directives or another header(s) that explicitly
1063 // From RFC 2616 section 14.9.4:
1065 // When the must-revalidate directive is present in a response received by
1066 // a cache, that cache MUST NOT use the entry after it becomes stale to
1067 // respond to a subsequent request without first revalidating it with the
1068 // origin server. (I.e., the cache MUST do an end-to-end revalidation every
1069 // time, if, based solely on the origin server's Expires or max-age value,
1070 // the cached response is stale.)
1072 // https://datatracker.ietf.org/doc/draft-reschke-http-status-308/ is an
1073 // experimental RFC that adds 308 permanent redirect as well, for which "any
1074 // future references ... SHOULD use one of the returned URIs."
1075 if ((response_code_
== 200 || response_code_
== 203 ||
1076 response_code_
== 206) && !must_revalidate
) {
1077 // TODO(darin): Implement a smarter heuristic.
1078 Time last_modified_value
;
1079 if (GetLastModifiedValue(&last_modified_value
)) {
1080 // The last-modified value can be a date in the future!
1081 if (last_modified_value
<= date_value
) {
1082 lifetimes
.freshness
= (date_value
- last_modified_value
) / 10;
1088 // These responses are implicitly fresh (unless otherwise overruled):
1089 if (response_code_
== 300 || response_code_
== 301 || response_code_
== 308 ||
1090 response_code_
== 410) {
1091 lifetimes
.freshness
= TimeDelta::Max();
1092 lifetimes
.staleness
= TimeDelta(); // It should never be stale.
1096 // Our heuristic freshness estimate for this resource is 0 seconds, in
1097 // accordance with common browser behaviour. However, stale-while-revalidate
1099 DCHECK_EQ(TimeDelta(), lifetimes
.freshness
);
1103 // From RFC 2616 section 13.2.3:
1105 // Summary of age calculation algorithm, when a cache receives a response:
1109 // * is the value of Age: header received by the cache with
1112 // * is the value of the origin server's Date: header
1114 // * is the (local) time when the cache made the request
1115 // * that resulted in this cached response
1117 // * is the (local) time when the cache received the
1120 // * is the current (local) time
1122 // apparent_age = max(0, response_time - date_value);
1123 // corrected_received_age = max(apparent_age, age_value);
1124 // response_delay = response_time - request_time;
1125 // corrected_initial_age = corrected_received_age + response_delay;
1126 // resident_time = now - response_time;
1127 // current_age = corrected_initial_age + resident_time;
1129 TimeDelta
HttpResponseHeaders::GetCurrentAge(const Time
& request_time
,
1130 const Time
& response_time
,
1131 const Time
& current_time
) const {
1132 // If there is no Date header, then assume that the server response was
1133 // generated at the time when we received the response.
1135 if (!GetDateValue(&date_value
))
1136 date_value
= response_time
;
1138 // If there is no Age header, then assume age is zero. GetAgeValue does not
1139 // modify its out param if the value does not exist.
1140 TimeDelta age_value
;
1141 GetAgeValue(&age_value
);
1143 TimeDelta apparent_age
= std::max(TimeDelta(), response_time
- date_value
);
1144 TimeDelta corrected_received_age
= std::max(apparent_age
, age_value
);
1145 TimeDelta response_delay
= response_time
- request_time
;
1146 TimeDelta corrected_initial_age
= corrected_received_age
+ response_delay
;
1147 TimeDelta resident_time
= current_time
- response_time
;
1148 TimeDelta current_age
= corrected_initial_age
+ resident_time
;
1153 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta
* result
) const {
1154 return GetCacheControlDirective("max-age", result
);
1157 bool HttpResponseHeaders::GetAgeValue(TimeDelta
* result
) const {
1159 if (!EnumerateHeader(NULL
, "Age", &value
))
1163 base::StringToInt64(value
, &seconds
);
1164 *result
= TimeDelta::FromSeconds(seconds
);
1168 bool HttpResponseHeaders::GetDateValue(Time
* result
) const {
1169 return GetTimeValuedHeader("Date", result
);
1172 bool HttpResponseHeaders::GetLastModifiedValue(Time
* result
) const {
1173 return GetTimeValuedHeader("Last-Modified", result
);
1176 bool HttpResponseHeaders::GetExpiresValue(Time
* result
) const {
1177 return GetTimeValuedHeader("Expires", result
);
1180 bool HttpResponseHeaders::GetStaleWhileRevalidateValue(
1181 TimeDelta
* result
) const {
1182 return GetCacheControlDirective("stale-while-revalidate", result
);
1185 bool HttpResponseHeaders::GetTimeValuedHeader(const std::string
& name
,
1186 Time
* result
) const {
1188 if (!EnumerateHeader(NULL
, name
, &value
))
1191 // When parsing HTTP dates it's beneficial to default to GMT because:
1192 // 1. RFC2616 3.3.1 says times should always be specified in GMT
1193 // 2. Only counter-example incorrectly appended "UTC" (crbug.com/153759)
1194 // 3. When adjusting cookie expiration times for clock skew
1195 // (crbug.com/135131) this better matches our cookie expiration
1196 // time parser which ignores timezone specifiers and assumes GMT.
1197 // 4. This is exactly what Firefox does.
1198 // TODO(pauljensen): The ideal solution would be to return false if the
1199 // timezone could not be understood so as to avoid makeing other calculations
1200 // based on an incorrect time. This would require modifying the time
1201 // library or duplicating the code. (http://crbug.com/158327)
1202 return Time::FromUTCString(value
.c_str(), result
);
1205 // We accept the first value of "close" or "keep-alive" in a Connection or
1206 // Proxy-Connection header, in that order. Obeying "keep-alive" in HTTP/1.1 or
1207 // "close" in 1.0 is not strictly standards-compliant, but we'd like to
1208 // avoid looking at the Proxy-Connection header whenever it is reasonable to do
1210 // TODO(ricea): Measure real-world usage of the "Proxy-Connection" header,
1211 // with a view to reducing support for it in order to make our Connection header
1212 // handling more RFC 7230 compliant.
1213 bool HttpResponseHeaders::IsKeepAlive() const {
1214 // NOTE: It is perhaps risky to assume that a Proxy-Connection header is
1215 // meaningful when we don't know that this response was from a proxy, but
1216 // Mozilla also does this, so we'll do the same.
1217 static const char* const kConnectionHeaders
[] = {
1218 "connection", "proxy-connection"};
1219 struct KeepAliveToken
{
1220 const char* const token
;
1223 static const KeepAliveToken kKeepAliveTokens
[] = {{"keep-alive", true},
1226 if (http_version_
< HttpVersion(1, 0))
1229 for (const char* header
: kConnectionHeaders
) {
1230 void* iterator
= nullptr;
1232 while (EnumerateHeader(&iterator
, header
, &token
)) {
1233 for (const KeepAliveToken
& keep_alive_token
: kKeepAliveTokens
) {
1234 if (base::LowerCaseEqualsASCII(token
, keep_alive_token
.token
))
1235 return keep_alive_token
.keep_alive
;
1239 return http_version_
!= HttpVersion(1, 0);
1242 bool HttpResponseHeaders::HasStrongValidators() const {
1243 std::string etag_header
;
1244 EnumerateHeader(NULL
, "etag", &etag_header
);
1245 std::string last_modified_header
;
1246 EnumerateHeader(NULL
, "Last-Modified", &last_modified_header
);
1247 std::string date_header
;
1248 EnumerateHeader(NULL
, "Date", &date_header
);
1249 return HttpUtil::HasStrongValidators(GetHttpVersion(),
1251 last_modified_header
,
1256 // Content-Length = "Content-Length" ":" 1*DIGIT
1257 int64
HttpResponseHeaders::GetContentLength() const {
1258 return GetInt64HeaderValue("content-length");
1261 int64
HttpResponseHeaders::GetInt64HeaderValue(
1262 const std::string
& header
) const {
1264 std::string content_length_val
;
1265 if (!EnumerateHeader(&iter
, header
, &content_length_val
))
1268 if (content_length_val
.empty())
1271 if (content_length_val
[0] == '+')
1275 bool ok
= base::StringToInt64(content_length_val
, &result
);
1276 if (!ok
|| result
< 0)
1282 // From RFC 2616 14.16:
1283 // content-range-spec =
1284 // bytes-unit SP byte-range-resp-spec "/" ( instance-length | "*" )
1285 // byte-range-resp-spec = (first-byte-pos "-" last-byte-pos) | "*"
1286 // instance-length = 1*DIGIT
1287 // bytes-unit = "bytes"
1288 bool HttpResponseHeaders::GetContentRange(int64
* first_byte_position
,
1289 int64
* last_byte_position
,
1290 int64
* instance_length
) const {
1292 std::string content_range_spec
;
1293 *first_byte_position
= *last_byte_position
= *instance_length
= -1;
1294 if (!EnumerateHeader(&iter
, kContentRange
, &content_range_spec
))
1297 // If the header value is empty, we have an invalid header.
1298 if (content_range_spec
.empty())
1301 size_t space_position
= content_range_spec
.find(' ');
1302 if (space_position
== std::string::npos
)
1305 // Invalid header if it doesn't contain "bytes-unit".
1306 std::string::const_iterator content_range_spec_begin
=
1307 content_range_spec
.begin();
1308 std::string::const_iterator content_range_spec_end
=
1309 content_range_spec
.begin() + space_position
;
1310 HttpUtil::TrimLWS(&content_range_spec_begin
, &content_range_spec_end
);
1311 if (!base::LowerCaseEqualsASCII(content_range_spec_begin
,
1312 content_range_spec_end
, "bytes")) {
1316 size_t slash_position
= content_range_spec
.find('/', space_position
+ 1);
1317 if (slash_position
== std::string::npos
)
1320 // Obtain the part behind the space and before slash.
1321 std::string::const_iterator byte_range_resp_spec_begin
=
1322 content_range_spec
.begin() + space_position
+ 1;
1323 std::string::const_iterator byte_range_resp_spec_end
=
1324 content_range_spec
.begin() + slash_position
;
1325 HttpUtil::TrimLWS(&byte_range_resp_spec_begin
, &byte_range_resp_spec_end
);
1327 // Parse the byte-range-resp-spec part.
1328 std::string
byte_range_resp_spec(byte_range_resp_spec_begin
,
1329 byte_range_resp_spec_end
);
1330 // If byte-range-resp-spec != "*".
1331 if (!base::LowerCaseEqualsASCII(byte_range_resp_spec
, "*")) {
1332 size_t minus_position
= byte_range_resp_spec
.find('-');
1333 if (minus_position
!= std::string::npos
) {
1334 // Obtain first-byte-pos.
1335 std::string::const_iterator first_byte_pos_begin
=
1336 byte_range_resp_spec
.begin();
1337 std::string::const_iterator first_byte_pos_end
=
1338 byte_range_resp_spec
.begin() + minus_position
;
1339 HttpUtil::TrimLWS(&first_byte_pos_begin
, &first_byte_pos_end
);
1341 bool ok
= base::StringToInt64(StringPiece(first_byte_pos_begin
,
1342 first_byte_pos_end
),
1343 first_byte_position
);
1345 // Obtain last-byte-pos.
1346 std::string::const_iterator last_byte_pos_begin
=
1347 byte_range_resp_spec
.begin() + minus_position
+ 1;
1348 std::string::const_iterator last_byte_pos_end
=
1349 byte_range_resp_spec
.end();
1350 HttpUtil::TrimLWS(&last_byte_pos_begin
, &last_byte_pos_end
);
1352 ok
&= base::StringToInt64(StringPiece(last_byte_pos_begin
,
1354 last_byte_position
);
1356 *first_byte_position
= *last_byte_position
= -1;
1359 if (*first_byte_position
< 0 || *last_byte_position
< 0 ||
1360 *first_byte_position
> *last_byte_position
)
1367 // Parse the instance-length part.
1368 // If instance-length == "*".
1369 std::string::const_iterator instance_length_begin
=
1370 content_range_spec
.begin() + slash_position
+ 1;
1371 std::string::const_iterator instance_length_end
=
1372 content_range_spec
.end();
1373 HttpUtil::TrimLWS(&instance_length_begin
, &instance_length_end
);
1375 if (base::LowerCaseEqualsASCII(instance_length_begin
, instance_length_end
,
1378 } else if (!base::StringToInt64(StringPiece(instance_length_begin
,
1379 instance_length_end
),
1381 *instance_length
= -1;
1385 // We have all the values; let's verify that they make sense for a 206
1387 if (*first_byte_position
< 0 || *last_byte_position
< 0 ||
1388 *instance_length
< 0 || *instance_length
- 1 < *last_byte_position
)
1394 scoped_ptr
<base::Value
> HttpResponseHeaders::NetLogCallback(
1395 NetLogCaptureMode capture_mode
) const {
1396 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
1397 base::ListValue
* headers
= new base::ListValue();
1398 headers
->Append(new base::StringValue(GetStatusLine()));
1399 void* iterator
= NULL
;
1402 while (EnumerateHeaderLines(&iterator
, &name
, &value
)) {
1403 std::string log_value
=
1404 ElideHeaderValueForNetLog(capture_mode
, name
, value
);
1405 std::string escaped_name
= EscapeNonASCII(name
);
1406 std::string escaped_value
= EscapeNonASCII(log_value
);
1408 new base::StringValue(
1409 base::StringPrintf("%s: %s", escaped_name
.c_str(),
1410 escaped_value
.c_str())));
1412 dict
->Set("headers", headers
);
1417 bool HttpResponseHeaders::FromNetLogParam(
1418 const base::Value
* event_param
,
1419 scoped_refptr
<HttpResponseHeaders
>* http_response_headers
) {
1420 *http_response_headers
= NULL
;
1422 const base::DictionaryValue
* dict
= NULL
;
1423 const base::ListValue
* header_list
= NULL
;
1426 !event_param
->GetAsDictionary(&dict
) ||
1427 !dict
->GetList("headers", &header_list
)) {
1431 std::string raw_headers
;
1432 for (base::ListValue::const_iterator it
= header_list
->begin();
1433 it
!= header_list
->end();
1435 std::string header_line
;
1436 if (!(*it
)->GetAsString(&header_line
))
1439 raw_headers
.append(header_line
);
1440 raw_headers
.push_back('\0');
1442 raw_headers
.push_back('\0');
1443 *http_response_headers
= new HttpResponseHeaders(raw_headers
);
1447 bool HttpResponseHeaders::IsChunkEncoded() const {
1448 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies.
1449 return GetHttpVersion() >= HttpVersion(1, 1) &&
1450 HasHeaderValue("Transfer-Encoding", "chunked");