Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / net / http / http_response_headers.cc
blob3aef42a6b17a724925dbb23a71a341d7e903826e
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"
12 #include <algorithm>
14 #include "base/format_macros.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram.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;
30 using base::Time;
31 using base::TimeDelta;
33 namespace net {
35 //-----------------------------------------------------------------------------
37 namespace {
39 // These headers are RFC 2616 hop-by-hop headers;
40 // not to be stored by caches.
41 const char* const kHopByHopResponseHeaders[] = {
42 "connection",
43 "proxy-connection",
44 "keep-alive",
45 "trailer",
46 "transfer-encoding",
47 "upgrade"
50 // These headers are challenge response headers;
51 // not to be stored by caches.
52 const char* const kChallengeResponseHeaders[] = {
53 "www-authenticate",
54 "proxy-authenticate"
57 // These headers are cookie setting headers;
58 // not to be stored by caches or disclosed otherwise.
59 const char* const kCookieResponseHeaders[] = {
60 "set-cookie",
61 "set-cookie2"
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
67 // connections.
68 const char* const kSecurityStateHeaders[] = {
69 "strict-transport-security",
70 "public-key-pins"
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[] = {
76 "connection",
77 "proxy-connection",
78 "keep-alive",
79 "www-authenticate",
80 "proxy-authenticate",
81 "trailer",
82 "transfer-encoding",
83 "upgrade",
84 "etag",
85 "x-frame-options",
86 "x-xss-protection",
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
91 // this list:
92 const char* const kNonUpdatedHeaderPrefixes[] = {
93 "content-",
94 "x-content-",
95 "x-webkit-"
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 (LowerCaseEqualsASCII(name_begin, name_end, kNonUpdatedHeaders[i]))
102 return false;
104 for (size_t i = 0; i < arraysize(kNonUpdatedHeaderPrefixes); ++i) {
105 if (StartsWithASCII(std::string(name_begin, name_end),
106 kNonUpdatedHeaderPrefixes[i], false))
107 return false;
109 return true;
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);
119 } // namespace
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) {
138 Parse(raw_input);
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(
152 response_code_),
153 // Note the third argument is only
154 // evaluated once, see macro
155 // definition for details.
156 HttpUtil::GetStatusCodesForHistogram());
159 HttpResponseHeaders::HttpResponseHeaders(PickleIterator* iter)
160 : response_code_(-1) {
161 std::string raw_input;
162 if (iter->ReadString(&raw_input))
163 Parse(raw_input);
166 void HttpResponseHeaders::Persist(Pickle* pickle, PersistOptions options) {
167 if (options == PERSIST_RAW) {
168 pickle->WriteString(raw_headers_);
169 return; // Done.
172 HeaderSet filter_headers;
174 // Construct set of headers to filter out based on options.
175 if ((options & PERSIST_SANS_NON_CACHEABLE) == PERSIST_SANS_NON_CACHEABLE)
176 AddNonCacheableHeaders(&filter_headers);
178 if ((options & PERSIST_SANS_COOKIES) == PERSIST_SANS_COOKIES)
179 AddCookieHeaders(&filter_headers);
181 if ((options & PERSIST_SANS_CHALLENGES) == PERSIST_SANS_CHALLENGES)
182 AddChallengeHeaders(&filter_headers);
184 if ((options & PERSIST_SANS_HOP_BY_HOP) == PERSIST_SANS_HOP_BY_HOP)
185 AddHopByHopHeaders(&filter_headers);
187 if ((options & PERSIST_SANS_RANGES) == PERSIST_SANS_RANGES)
188 AddHopContentRangeHeaders(&filter_headers);
190 if ((options & PERSIST_SANS_SECURITY_STATE) == PERSIST_SANS_SECURITY_STATE)
191 AddSecurityStateHeaders(&filter_headers);
193 std::string blob;
194 blob.reserve(raw_headers_.size());
196 // This copies the status line w/ terminator null.
197 // Note raw_headers_ has embedded nulls instead of \n,
198 // so this just copies the first header line.
199 blob.assign(raw_headers_.c_str(), strlen(raw_headers_.c_str()) + 1);
201 for (size_t i = 0; i < parsed_.size(); ++i) {
202 DCHECK(!parsed_[i].is_continuation());
204 // Locate the start of the next header.
205 size_t k = i;
206 while (++k < parsed_.size() && parsed_[k].is_continuation()) {}
207 --k;
209 std::string header_name(parsed_[i].name_begin, parsed_[i].name_end);
210 base::StringToLowerASCII(&header_name);
212 if (filter_headers.find(header_name) == filter_headers.end()) {
213 // Make sure there is a null after the value.
214 blob.append(parsed_[i].name_begin, parsed_[k].value_end);
215 blob.push_back('\0');
218 i = k;
220 blob.push_back('\0');
222 pickle->WriteString(blob);
225 void HttpResponseHeaders::Update(const HttpResponseHeaders& new_headers) {
226 DCHECK(new_headers.response_code() == 304 ||
227 new_headers.response_code() == 206);
229 // Copy up to the null byte. This just copies the status line.
230 std::string new_raw_headers(raw_headers_.c_str());
231 new_raw_headers.push_back('\0');
233 HeaderSet updated_headers;
235 // NOTE: we write the new headers then the old headers for convenience. The
236 // order should not matter.
238 // Figure out which headers we want to take from new_headers:
239 for (size_t i = 0; i < new_headers.parsed_.size(); ++i) {
240 const HeaderList& new_parsed = new_headers.parsed_;
242 DCHECK(!new_parsed[i].is_continuation());
244 // Locate the start of the next header.
245 size_t k = i;
246 while (++k < new_parsed.size() && new_parsed[k].is_continuation()) {}
247 --k;
249 const std::string::const_iterator& name_begin = new_parsed[i].name_begin;
250 const std::string::const_iterator& name_end = new_parsed[i].name_end;
251 if (ShouldUpdateHeader(name_begin, name_end)) {
252 std::string name(name_begin, name_end);
253 base::StringToLowerASCII(&name);
254 updated_headers.insert(name);
256 // Preserve this header line in the merged result, making sure there is
257 // a null after the value.
258 new_raw_headers.append(name_begin, new_parsed[k].value_end);
259 new_raw_headers.push_back('\0');
262 i = k;
265 // Now, build the new raw headers.
266 MergeWithHeaders(new_raw_headers, updated_headers);
269 void HttpResponseHeaders::MergeWithHeaders(const std::string& raw_headers,
270 const HeaderSet& headers_to_remove) {
271 std::string new_raw_headers(raw_headers);
272 for (size_t i = 0; i < parsed_.size(); ++i) {
273 DCHECK(!parsed_[i].is_continuation());
275 // Locate the start of the next header.
276 size_t k = i;
277 while (++k < parsed_.size() && parsed_[k].is_continuation()) {}
278 --k;
280 std::string name(parsed_[i].name_begin, parsed_[i].name_end);
281 base::StringToLowerASCII(&name);
282 if (headers_to_remove.find(name) == headers_to_remove.end()) {
283 // It's ok to preserve this header in the final result.
284 new_raw_headers.append(parsed_[i].name_begin, parsed_[k].value_end);
285 new_raw_headers.push_back('\0');
288 i = k;
290 new_raw_headers.push_back('\0');
292 // Make this object hold the new data.
293 raw_headers_.clear();
294 parsed_.clear();
295 Parse(new_raw_headers);
298 void HttpResponseHeaders::RemoveHeader(const std::string& name) {
299 // Copy up to the null byte. This just copies the status line.
300 std::string new_raw_headers(raw_headers_.c_str());
301 new_raw_headers.push_back('\0');
303 std::string lowercase_name(name);
304 base::StringToLowerASCII(&lowercase_name);
305 HeaderSet to_remove;
306 to_remove.insert(lowercase_name);
307 MergeWithHeaders(new_raw_headers, to_remove);
310 void HttpResponseHeaders::RemoveHeaderLine(const std::string& name,
311 const std::string& value) {
312 std::string name_lowercase(name);
313 base::StringToLowerASCII(&name_lowercase);
315 std::string new_raw_headers(GetStatusLine());
316 new_raw_headers.push_back('\0');
318 new_raw_headers.reserve(raw_headers_.size());
320 void* iter = NULL;
321 std::string old_header_name;
322 std::string old_header_value;
323 while (EnumerateHeaderLines(&iter, &old_header_name, &old_header_value)) {
324 std::string old_header_name_lowercase(name);
325 base::StringToLowerASCII(&old_header_name_lowercase);
327 if (name_lowercase == old_header_name_lowercase &&
328 value == old_header_value)
329 continue;
331 new_raw_headers.append(old_header_name);
332 new_raw_headers.push_back(':');
333 new_raw_headers.push_back(' ');
334 new_raw_headers.append(old_header_value);
335 new_raw_headers.push_back('\0');
337 new_raw_headers.push_back('\0');
339 // Make this object hold the new data.
340 raw_headers_.clear();
341 parsed_.clear();
342 Parse(new_raw_headers);
345 void HttpResponseHeaders::AddHeader(const std::string& header) {
346 CheckDoesNotHaveEmbededNulls(header);
347 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 2]);
348 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]);
349 // Don't copy the last null.
350 std::string new_raw_headers(raw_headers_, 0, raw_headers_.size() - 1);
351 new_raw_headers.append(header);
352 new_raw_headers.push_back('\0');
353 new_raw_headers.push_back('\0');
355 // Make this object hold the new data.
356 raw_headers_.clear();
357 parsed_.clear();
358 Parse(new_raw_headers);
361 void HttpResponseHeaders::ReplaceStatusLine(const std::string& new_status) {
362 CheckDoesNotHaveEmbededNulls(new_status);
363 // Copy up to the null byte. This just copies the status line.
364 std::string new_raw_headers(new_status);
365 new_raw_headers.push_back('\0');
367 HeaderSet empty_to_remove;
368 MergeWithHeaders(new_raw_headers, empty_to_remove);
371 void HttpResponseHeaders::UpdateWithNewRange(
372 const HttpByteRange& byte_range,
373 int64 resource_size,
374 bool replace_status_line) {
375 DCHECK(byte_range.IsValid());
376 DCHECK(byte_range.HasFirstBytePosition());
377 DCHECK(byte_range.HasLastBytePosition());
379 const char kLengthHeader[] = "Content-Length";
380 const char kRangeHeader[] = "Content-Range";
382 RemoveHeader(kLengthHeader);
383 RemoveHeader(kRangeHeader);
385 int64 start = byte_range.first_byte_position();
386 int64 end = byte_range.last_byte_position();
387 int64 range_len = end - start + 1;
389 if (replace_status_line)
390 ReplaceStatusLine("HTTP/1.1 206 Partial Content");
392 AddHeader(base::StringPrintf("%s: bytes %" PRId64 "-%" PRId64 "/%" PRId64,
393 kRangeHeader, start, end, resource_size));
394 AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader, range_len));
397 void HttpResponseHeaders::Parse(const std::string& raw_input) {
398 raw_headers_.reserve(raw_input.size());
400 // ParseStatusLine adds a normalized status line to raw_headers_
401 std::string::const_iterator line_begin = raw_input.begin();
402 std::string::const_iterator line_end =
403 std::find(line_begin, raw_input.end(), '\0');
404 // has_headers = true, if there is any data following the status line.
405 // Used by ParseStatusLine() to decide if a HTTP/0.9 is really a HTTP/1.0.
406 bool has_headers = (line_end != raw_input.end() &&
407 (line_end + 1) != raw_input.end() &&
408 *(line_end + 1) != '\0');
409 ParseStatusLine(line_begin, line_end, has_headers);
410 raw_headers_.push_back('\0'); // Terminate status line with a null.
412 if (line_end == raw_input.end()) {
413 raw_headers_.push_back('\0'); // Ensure the headers end with a double null.
415 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 2]);
416 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]);
417 return;
420 // Including a terminating null byte.
421 size_t status_line_len = raw_headers_.size();
423 // Now, we add the rest of the raw headers to raw_headers_, and begin parsing
424 // it (to populate our parsed_ vector).
425 raw_headers_.append(line_end + 1, raw_input.end());
427 // Ensure the headers end with a double null.
428 while (raw_headers_.size() < 2 ||
429 raw_headers_[raw_headers_.size() - 2] != '\0' ||
430 raw_headers_[raw_headers_.size() - 1] != '\0') {
431 raw_headers_.push_back('\0');
434 // Adjust to point at the null byte following the status line
435 line_end = raw_headers_.begin() + status_line_len - 1;
437 HttpUtil::HeadersIterator headers(line_end + 1, raw_headers_.end(),
438 std::string(1, '\0'));
439 while (headers.GetNext()) {
440 AddHeader(headers.name_begin(),
441 headers.name_end(),
442 headers.values_begin(),
443 headers.values_end());
446 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 2]);
447 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]);
450 // Append all of our headers to the final output string.
451 void HttpResponseHeaders::GetNormalizedHeaders(std::string* output) const {
452 // copy up to the null byte. this just copies the status line.
453 output->assign(raw_headers_.c_str());
455 // headers may appear multiple times (not necessarily in succession) in the
456 // header data, so we build a map from header name to generated header lines.
457 // to preserve the order of the original headers, the actual values are kept
458 // in a separate list. finally, the list of headers is flattened to form
459 // the normalized block of headers.
461 // NOTE: We take special care to preserve the whitespace around any commas
462 // that may occur in the original response headers. Because our consumer may
463 // be a web app, we cannot be certain of the semantics of commas despite the
464 // fact that RFC 2616 says that they should be regarded as value separators.
466 typedef base::hash_map<std::string, size_t> HeadersMap;
467 HeadersMap headers_map;
468 HeadersMap::iterator iter = headers_map.end();
470 std::vector<std::string> headers;
472 for (size_t i = 0; i < parsed_.size(); ++i) {
473 DCHECK(!parsed_[i].is_continuation());
475 std::string name(parsed_[i].name_begin, parsed_[i].name_end);
476 std::string lower_name = base::StringToLowerASCII(name);
478 iter = headers_map.find(lower_name);
479 if (iter == headers_map.end()) {
480 iter = headers_map.insert(
481 HeadersMap::value_type(lower_name, headers.size())).first;
482 headers.push_back(name + ": ");
483 } else {
484 headers[iter->second].append(", ");
487 std::string::const_iterator value_begin = parsed_[i].value_begin;
488 std::string::const_iterator value_end = parsed_[i].value_end;
489 while (++i < parsed_.size() && parsed_[i].is_continuation())
490 value_end = parsed_[i].value_end;
491 --i;
493 headers[iter->second].append(value_begin, value_end);
496 for (size_t i = 0; i < headers.size(); ++i) {
497 output->push_back('\n');
498 output->append(headers[i]);
501 output->push_back('\n');
504 bool HttpResponseHeaders::GetNormalizedHeader(const std::string& name,
505 std::string* value) const {
506 // If you hit this assertion, please use EnumerateHeader instead!
507 DCHECK(!HttpUtil::IsNonCoalescingHeader(name));
509 value->clear();
511 bool found = false;
512 size_t i = 0;
513 while (i < parsed_.size()) {
514 i = FindHeader(i, name);
515 if (i == std::string::npos)
516 break;
518 found = true;
520 if (!value->empty())
521 value->append(", ");
523 std::string::const_iterator value_begin = parsed_[i].value_begin;
524 std::string::const_iterator value_end = parsed_[i].value_end;
525 while (++i < parsed_.size() && parsed_[i].is_continuation())
526 value_end = parsed_[i].value_end;
527 value->append(value_begin, value_end);
530 return found;
533 std::string HttpResponseHeaders::GetStatusLine() const {
534 // copy up to the null byte.
535 return std::string(raw_headers_.c_str());
538 std::string HttpResponseHeaders::GetStatusText() const {
539 // GetStatusLine() is already normalized, so it has the format:
540 // <http_version> SP <response_code> SP <status_text>
541 std::string status_text = GetStatusLine();
542 std::string::const_iterator begin = status_text.begin();
543 std::string::const_iterator end = status_text.end();
544 for (int i = 0; i < 2; ++i)
545 begin = std::find(begin, end, ' ') + 1;
546 return std::string(begin, end);
549 bool HttpResponseHeaders::EnumerateHeaderLines(void** iter,
550 std::string* name,
551 std::string* value) const {
552 size_t i = reinterpret_cast<size_t>(*iter);
553 if (i == parsed_.size())
554 return false;
556 DCHECK(!parsed_[i].is_continuation());
558 name->assign(parsed_[i].name_begin, parsed_[i].name_end);
560 std::string::const_iterator value_begin = parsed_[i].value_begin;
561 std::string::const_iterator value_end = parsed_[i].value_end;
562 while (++i < parsed_.size() && parsed_[i].is_continuation())
563 value_end = parsed_[i].value_end;
565 value->assign(value_begin, value_end);
567 *iter = reinterpret_cast<void*>(i);
568 return true;
571 bool HttpResponseHeaders::EnumerateHeader(void** iter,
572 const base::StringPiece& name,
573 std::string* value) const {
574 size_t i;
575 if (!iter || !*iter) {
576 i = FindHeader(0, name);
577 } else {
578 i = reinterpret_cast<size_t>(*iter);
579 if (i >= parsed_.size()) {
580 i = std::string::npos;
581 } else if (!parsed_[i].is_continuation()) {
582 i = FindHeader(i, name);
586 if (i == std::string::npos) {
587 value->clear();
588 return false;
591 if (iter)
592 *iter = reinterpret_cast<void*>(i + 1);
593 value->assign(parsed_[i].value_begin, parsed_[i].value_end);
594 return true;
597 bool HttpResponseHeaders::HasHeaderValue(const base::StringPiece& name,
598 const base::StringPiece& value) const {
599 // The value has to be an exact match. This is important since
600 // 'cache-control: no-cache' != 'cache-control: no-cache="foo"'
601 void* iter = NULL;
602 std::string temp;
603 while (EnumerateHeader(&iter, name, &temp)) {
604 if (value.size() == temp.size() &&
605 std::equal(temp.begin(), temp.end(), value.begin(),
606 base::CaseInsensitiveCompare<char>()))
607 return true;
609 return false;
612 bool HttpResponseHeaders::HasHeader(const base::StringPiece& name) const {
613 return FindHeader(0, name) != std::string::npos;
616 HttpResponseHeaders::HttpResponseHeaders() : response_code_(-1) {
619 HttpResponseHeaders::~HttpResponseHeaders() {
622 // Note: this implementation implicitly assumes that line_end points at a valid
623 // sentinel character (such as '\0').
624 // static
625 HttpVersion HttpResponseHeaders::ParseVersion(
626 std::string::const_iterator line_begin,
627 std::string::const_iterator line_end) {
628 std::string::const_iterator p = line_begin;
630 // RFC2616 sec 3.1: HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
631 // TODO: (1*DIGIT apparently means one or more digits, but we only handle 1).
632 // TODO: handle leading zeros, which is allowed by the rfc1616 sec 3.1.
634 if ((line_end - p < 4) || !LowerCaseEqualsASCII(p, p + 4, "http")) {
635 DVLOG(1) << "missing status line";
636 return HttpVersion();
639 p += 4;
641 if (p >= line_end || *p != '/') {
642 DVLOG(1) << "missing version";
643 return HttpVersion();
646 std::string::const_iterator dot = std::find(p, line_end, '.');
647 if (dot == line_end) {
648 DVLOG(1) << "malformed version";
649 return HttpVersion();
652 ++p; // from / to first digit.
653 ++dot; // from . to second digit.
655 if (!(*p >= '0' && *p <= '9' && *dot >= '0' && *dot <= '9')) {
656 DVLOG(1) << "malformed version number";
657 return HttpVersion();
660 uint16 major = *p - '0';
661 uint16 minor = *dot - '0';
663 return HttpVersion(major, minor);
666 // Note: this implementation implicitly assumes that line_end points at a valid
667 // sentinel character (such as '\0').
668 void HttpResponseHeaders::ParseStatusLine(
669 std::string::const_iterator line_begin,
670 std::string::const_iterator line_end,
671 bool has_headers) {
672 // Extract the version number
673 parsed_http_version_ = ParseVersion(line_begin, line_end);
675 // Clamp the version number to one of: {0.9, 1.0, 1.1}
676 if (parsed_http_version_ == HttpVersion(0, 9) && !has_headers) {
677 http_version_ = HttpVersion(0, 9);
678 raw_headers_ = "HTTP/0.9";
679 } else if (parsed_http_version_ >= HttpVersion(1, 1)) {
680 http_version_ = HttpVersion(1, 1);
681 raw_headers_ = "HTTP/1.1";
682 } else {
683 // Treat everything else like HTTP 1.0
684 http_version_ = HttpVersion(1, 0);
685 raw_headers_ = "HTTP/1.0";
687 if (parsed_http_version_ != http_version_) {
688 DVLOG(1) << "assuming HTTP/" << http_version_.major_value() << "."
689 << http_version_.minor_value();
692 // TODO(eroman): this doesn't make sense if ParseVersion failed.
693 std::string::const_iterator p = std::find(line_begin, line_end, ' ');
695 if (p == line_end) {
696 DVLOG(1) << "missing response status; assuming 200 OK";
697 raw_headers_.append(" 200 OK");
698 response_code_ = 200;
699 return;
702 // Skip whitespace.
703 while (*p == ' ')
704 ++p;
706 std::string::const_iterator code = p;
707 while (*p >= '0' && *p <= '9')
708 ++p;
710 if (p == code) {
711 DVLOG(1) << "missing response status number; assuming 200";
712 raw_headers_.append(" 200 OK");
713 response_code_ = 200;
714 return;
716 raw_headers_.push_back(' ');
717 raw_headers_.append(code, p);
718 raw_headers_.push_back(' ');
719 base::StringToInt(StringPiece(code, p), &response_code_);
721 // Skip whitespace.
722 while (*p == ' ')
723 ++p;
725 // Trim trailing whitespace.
726 while (line_end > p && line_end[-1] == ' ')
727 --line_end;
729 if (p == line_end) {
730 DVLOG(1) << "missing response status text; assuming OK";
731 // Not super critical what we put here. Just use "OK"
732 // even if it isn't descriptive of response_code_.
733 raw_headers_.append("OK");
734 } else {
735 raw_headers_.append(p, line_end);
739 size_t HttpResponseHeaders::FindHeader(size_t from,
740 const base::StringPiece& search) const {
741 for (size_t i = from; i < parsed_.size(); ++i) {
742 if (parsed_[i].is_continuation())
743 continue;
744 const std::string::const_iterator& name_begin = parsed_[i].name_begin;
745 const std::string::const_iterator& name_end = parsed_[i].name_end;
746 if (static_cast<size_t>(name_end - name_begin) == search.size() &&
747 std::equal(name_begin, name_end, search.begin(),
748 base::CaseInsensitiveCompare<char>()))
749 return i;
752 return std::string::npos;
755 bool HttpResponseHeaders::GetCacheControlDirective(const StringPiece& directive,
756 TimeDelta* result) const {
757 StringPiece name("cache-control");
758 std::string value;
760 size_t directive_size = directive.size();
762 void* iter = NULL;
763 while (EnumerateHeader(&iter, name, &value)) {
764 if (value.size() > directive_size + 1 &&
765 LowerCaseEqualsASCII(value.begin(),
766 value.begin() + directive_size,
767 directive.begin()) &&
768 value[directive_size] == '=') {
769 int64 seconds;
770 base::StringToInt64(
771 StringPiece(value.begin() + directive_size + 1, value.end()),
772 &seconds);
773 *result = TimeDelta::FromSeconds(seconds);
774 return true;
778 return false;
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);
789 } else {
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) {
803 ParsedHeader header;
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;
819 std::string value;
820 void* iter = NULL;
821 while (EnumerateHeader(&iter, kCacheControl, &value)) {
822 // If the value is smaller than the prefix and a terminal quote, skip
823 // it.
824 if (value.size() <= kPrefixLen ||
825 value.compare(0, kPrefixLen, kPrefix) != 0) {
826 continue;
828 // if it doesn't end with a quote, then treat as malformed
829 if (value[value.size()-1] != '\"')
830 continue;
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;
844 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.
857 item = item_next;
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 {
888 mime_type->clear();
889 charset->clear();
891 std::string name = "content-type";
892 std::string value;
894 bool had_charset = false;
896 void* iter = NULL;
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 {
902 std::string unused;
903 GetMimeTypeAndCharset(mime_type, &unused);
904 return !mime_type->empty();
907 bool HttpResponseHeaders::GetCharset(std::string* charset) const {
908 std::string unused;
909 GetMimeTypeAndCharset(&unused, charset);
910 return !charset->empty();
913 bool HttpResponseHeaders::IsRedirect(std::string* location) const {
914 if (!IsRedirectResponseCode(response_code_))
915 return false;
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;
921 do {
922 i = FindHeader(++i, "location");
923 if (i == std::string::npos)
924 return false;
925 // If the location value is empty, then it doesn't count.
926 } while (parsed_[i].value_begin == parsed_[i].value_end);
928 if (location) {
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));
935 return true;
938 // static
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", "*")) {
1016 return lifetimes;
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))
1031 return lifetimes;
1033 // If there is no Date header, then assume that the server response was
1034 // generated at the time when we received the response.
1035 Time date_value;
1036 if (!GetDateValue(&date_value))
1037 date_value = response_time;
1039 Time expires_value;
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;
1044 return lifetimes;
1047 DCHECK_EQ(TimeDelta(), lifetimes.freshness);
1048 return lifetimes;
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.
1057 // ...
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
1061 // allow it.
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;
1083 return lifetimes;
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.
1093 return lifetimes;
1096 // Our heuristic freshness estimate for this resource is 0 seconds, in
1097 // accordance with common browser behaviour. However, stale-while-revalidate
1098 // may still apply.
1099 DCHECK_EQ(TimeDelta(), lifetimes.freshness);
1100 return lifetimes;
1103 // From RFC 2616 section 13.2.3:
1105 // Summary of age calculation algorithm, when a cache receives a response:
1107 // /*
1108 // * age_value
1109 // * is the value of Age: header received by the cache with
1110 // * this response.
1111 // * date_value
1112 // * is the value of the origin server's Date: header
1113 // * request_time
1114 // * is the (local) time when the cache made the request
1115 // * that resulted in this cached response
1116 // * response_time
1117 // * is the (local) time when the cache received the
1118 // * response
1119 // * now
1120 // * is the current (local) time
1121 // */
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.
1134 Time date_value;
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;
1150 return current_age;
1153 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const {
1154 return GetCacheControlDirective("max-age", result);
1157 bool HttpResponseHeaders::GetAgeValue(TimeDelta* result) const {
1158 std::string value;
1159 if (!EnumerateHeader(NULL, "Age", &value))
1160 return false;
1162 int64 seconds;
1163 base::StringToInt64(value, &seconds);
1164 *result = TimeDelta::FromSeconds(seconds);
1165 return true;
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 {
1187 std::string value;
1188 if (!EnumerateHeader(NULL, name, &value))
1189 return false;
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
1209 // so.
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;
1221 bool keep_alive;
1223 static const KeepAliveToken kKeepAliveTokens[] = {{"keep-alive", true},
1224 {"close", false}};
1226 if (http_version_ < HttpVersion(1, 0))
1227 return false;
1229 for (const char* header : kConnectionHeaders) {
1230 void* iterator = nullptr;
1231 std::string token;
1232 while (EnumerateHeader(&iterator, header, &token)) {
1233 for (const KeepAliveToken& keep_alive_token : kKeepAliveTokens) {
1234 if (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(),
1250 etag_header,
1251 last_modified_header,
1252 date_header);
1255 // From RFC 2616:
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 {
1263 void* iter = NULL;
1264 std::string content_length_val;
1265 if (!EnumerateHeader(&iter, header, &content_length_val))
1266 return -1;
1268 if (content_length_val.empty())
1269 return -1;
1271 if (content_length_val[0] == '+')
1272 return -1;
1274 int64 result;
1275 bool ok = base::StringToInt64(content_length_val, &result);
1276 if (!ok || result < 0)
1277 return -1;
1279 return result;
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 {
1291 void* iter = NULL;
1292 std::string content_range_spec;
1293 *first_byte_position = *last_byte_position = *instance_length = -1;
1294 if (!EnumerateHeader(&iter, kContentRange, &content_range_spec))
1295 return false;
1297 // If the header value is empty, we have an invalid header.
1298 if (content_range_spec.empty())
1299 return false;
1301 size_t space_position = content_range_spec.find(' ');
1302 if (space_position == std::string::npos)
1303 return false;
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 (!LowerCaseEqualsASCII(content_range_spec_begin,
1312 content_range_spec_end,
1313 "bytes")) {
1314 return false;
1317 size_t slash_position = content_range_spec.find('/', space_position + 1);
1318 if (slash_position == std::string::npos)
1319 return false;
1321 // Obtain the part behind the space and before slash.
1322 std::string::const_iterator byte_range_resp_spec_begin =
1323 content_range_spec.begin() + space_position + 1;
1324 std::string::const_iterator byte_range_resp_spec_end =
1325 content_range_spec.begin() + slash_position;
1326 HttpUtil::TrimLWS(&byte_range_resp_spec_begin, &byte_range_resp_spec_end);
1328 // Parse the byte-range-resp-spec part.
1329 std::string byte_range_resp_spec(byte_range_resp_spec_begin,
1330 byte_range_resp_spec_end);
1331 // If byte-range-resp-spec != "*".
1332 if (!LowerCaseEqualsASCII(byte_range_resp_spec, "*")) {
1333 size_t minus_position = byte_range_resp_spec.find('-');
1334 if (minus_position != std::string::npos) {
1335 // Obtain first-byte-pos.
1336 std::string::const_iterator first_byte_pos_begin =
1337 byte_range_resp_spec.begin();
1338 std::string::const_iterator first_byte_pos_end =
1339 byte_range_resp_spec.begin() + minus_position;
1340 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end);
1342 bool ok = base::StringToInt64(StringPiece(first_byte_pos_begin,
1343 first_byte_pos_end),
1344 first_byte_position);
1346 // Obtain last-byte-pos.
1347 std::string::const_iterator last_byte_pos_begin =
1348 byte_range_resp_spec.begin() + minus_position + 1;
1349 std::string::const_iterator last_byte_pos_end =
1350 byte_range_resp_spec.end();
1351 HttpUtil::TrimLWS(&last_byte_pos_begin, &last_byte_pos_end);
1353 ok &= base::StringToInt64(StringPiece(last_byte_pos_begin,
1354 last_byte_pos_end),
1355 last_byte_position);
1356 if (!ok) {
1357 *first_byte_position = *last_byte_position = -1;
1358 return false;
1360 if (*first_byte_position < 0 || *last_byte_position < 0 ||
1361 *first_byte_position > *last_byte_position)
1362 return false;
1363 } else {
1364 return false;
1368 // Parse the instance-length part.
1369 // If instance-length == "*".
1370 std::string::const_iterator instance_length_begin =
1371 content_range_spec.begin() + slash_position + 1;
1372 std::string::const_iterator instance_length_end =
1373 content_range_spec.end();
1374 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end);
1376 if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) {
1377 return false;
1378 } else if (!base::StringToInt64(StringPiece(instance_length_begin,
1379 instance_length_end),
1380 instance_length)) {
1381 *instance_length = -1;
1382 return false;
1385 // We have all the values; let's verify that they make sense for a 206
1386 // response.
1387 if (*first_byte_position < 0 || *last_byte_position < 0 ||
1388 *instance_length < 0 || *instance_length - 1 < *last_byte_position)
1389 return false;
1391 return true;
1394 base::Value* HttpResponseHeaders::NetLogCallback(
1395 NetLog::LogLevel log_level) const {
1396 base::DictionaryValue* dict = new base::DictionaryValue();
1397 base::ListValue* headers = new base::ListValue();
1398 headers->Append(new base::StringValue(GetStatusLine()));
1399 void* iterator = NULL;
1400 std::string name;
1401 std::string value;
1402 while (EnumerateHeaderLines(&iterator, &name, &value)) {
1403 std::string log_value = ElideHeaderValueForNetLog(log_level, name, value);
1404 std::string escaped_name = EscapeNonASCII(name);
1405 std::string escaped_value = EscapeNonASCII(log_value);
1406 headers->Append(
1407 new base::StringValue(
1408 base::StringPrintf("%s: %s", escaped_name.c_str(),
1409 escaped_value.c_str())));
1411 dict->Set("headers", headers);
1412 return dict;
1415 // static
1416 bool HttpResponseHeaders::FromNetLogParam(
1417 const base::Value* event_param,
1418 scoped_refptr<HttpResponseHeaders>* http_response_headers) {
1419 *http_response_headers = NULL;
1421 const base::DictionaryValue* dict = NULL;
1422 const base::ListValue* header_list = NULL;
1424 if (!event_param ||
1425 !event_param->GetAsDictionary(&dict) ||
1426 !dict->GetList("headers", &header_list)) {
1427 return false;
1430 std::string raw_headers;
1431 for (base::ListValue::const_iterator it = header_list->begin();
1432 it != header_list->end();
1433 ++it) {
1434 std::string header_line;
1435 if (!(*it)->GetAsString(&header_line))
1436 return false;
1438 raw_headers.append(header_line);
1439 raw_headers.push_back('\0');
1441 raw_headers.push_back('\0');
1442 *http_response_headers = new HttpResponseHeaders(raw_headers);
1443 return true;
1446 bool HttpResponseHeaders::IsChunkEncoded() const {
1447 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies.
1448 return GetHttpVersion() >= HttpVersion(1, 1) &&
1449 HasHeaderValue("Transfer-Encoding", "chunked");
1452 } // namespace net