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. 4 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 9 10 #include "net/http/http_response_headers.h" 11 12 #include <algorithm> 13 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" 28 29 using base::StringPiece; 30 using base::Time; 31 using base::TimeDelta; 32 33 namespace net { 34 35 //----------------------------------------------------------------------------- 36 37 namespace { 38 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" 48 }; 49 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" 55 }; 56 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" 62 }; 63 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" 71 }; 72 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", 87 }; 88 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-" 96 }; 97 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; 103 } 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; 108 } 109 return true; 110 } 111 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); 117 } 118 119 } // namespace 120 121 const char HttpResponseHeaders::kContentRange[] = "Content-Range"; 122 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; } 127 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; 132 }; 133 134 //----------------------------------------------------------------------------- 135 136 HttpResponseHeaders::HttpResponseHeaders(const std::string& raw_input) 137 : response_code_(-1) { 138 Parse(raw_input); 139 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()); 157 } 158 159 HttpResponseHeaders::HttpResponseHeaders(const Pickle& pickle, 160 PickleIterator* iter) 161 : response_code_(-1) { 162 std::string raw_input; 163 if (pickle.ReadString(iter, &raw_input)) 164 Parse(raw_input); 165 } 166 167 void HttpResponseHeaders::Persist(Pickle* pickle, PersistOptions options) { 168 if (options == PERSIST_RAW) { 169 pickle->WriteString(raw_headers_); 170 return; // Done. 171 } 172 173 HeaderSet filter_headers; 174 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); 178 179 if ((options & PERSIST_SANS_COOKIES) == PERSIST_SANS_COOKIES) 180 AddCookieHeaders(&filter_headers); 181 182 if ((options & PERSIST_SANS_CHALLENGES) == PERSIST_SANS_CHALLENGES) 183 AddChallengeHeaders(&filter_headers); 184 185 if ((options & PERSIST_SANS_HOP_BY_HOP) == PERSIST_SANS_HOP_BY_HOP) 186 AddHopByHopHeaders(&filter_headers); 187 188 if ((options & PERSIST_SANS_RANGES) == PERSIST_SANS_RANGES) 189 AddHopContentRangeHeaders(&filter_headers); 190 191 if ((options & PERSIST_SANS_SECURITY_STATE) == PERSIST_SANS_SECURITY_STATE) 192 AddSecurityStateHeaders(&filter_headers); 193 194 std::string blob; 195 blob.reserve(raw_headers_.size()); 196 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); 201 202 for (size_t i = 0; i < parsed_.size(); ++i) { 203 DCHECK(!parsed_[i].is_continuation()); 204 205 // Locate the start of the next header. 206 size_t k = i; 207 while (++k < parsed_.size() && parsed_[k].is_continuation()) {} 208 --k; 209 210 std::string header_name(parsed_[i].name_begin, parsed_[i].name_end); 211 StringToLowerASCII(&header_name); 212 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'); 217 } 218 219 i = k; 220 } 221 blob.push_back('\0'); 222 223 pickle->WriteString(blob); 224 } 225 226 void HttpResponseHeaders::Update(const HttpResponseHeaders& new_headers) { 227 DCHECK(new_headers.response_code() == 304 || 228 new_headers.response_code() == 206); 229 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'); 233 234 HeaderSet updated_headers; 235 236 // NOTE: we write the new headers then the old headers for convenience. The 237 // order should not matter. 238 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_; 242 243 DCHECK(!new_parsed[i].is_continuation()); 244 245 // Locate the start of the next header. 246 size_t k = i; 247 while (++k < new_parsed.size() && new_parsed[k].is_continuation()) {} 248 --k; 249 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 StringToLowerASCII(&name); 255 updated_headers.insert(name); 256 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'); 261 } 262 263 i = k; 264 } 265 266 // Now, build the new raw headers. 267 MergeWithHeaders(new_raw_headers, updated_headers); 268 } 269 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()); 275 276 // Locate the start of the next header. 277 size_t k = i; 278 while (++k < parsed_.size() && parsed_[k].is_continuation()) {} 279 --k; 280 281 std::string name(parsed_[i].name_begin, parsed_[i].name_end); 282 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'); 287 } 288 289 i = k; 290 } 291 new_raw_headers.push_back('\0'); 292 293 // Make this object hold the new data. 294 raw_headers_.clear(); 295 parsed_.clear(); 296 Parse(new_raw_headers); 297 } 298 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'); 303 304 std::string lowercase_name(name); 305 StringToLowerASCII(&lowercase_name); 306 HeaderSet to_remove; 307 to_remove.insert(lowercase_name); 308 MergeWithHeaders(new_raw_headers, to_remove); 309 } 310 311 void HttpResponseHeaders::RemoveHeaderLine(const std::string& name, 312 const std::string& value) { 313 std::string name_lowercase(name); 314 StringToLowerASCII(&name_lowercase); 315 316 std::string new_raw_headers(GetStatusLine()); 317 new_raw_headers.push_back('\0'); 318 319 new_raw_headers.reserve(raw_headers_.size()); 320 321 void* iter = NULL; 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 StringToLowerASCII(&old_header_name_lowercase); 327 328 if (name_lowercase == old_header_name_lowercase && 329 value == old_header_value) 330 continue; 331 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'); 337 } 338 new_raw_headers.push_back('\0'); 339 340 // Make this object hold the new data. 341 raw_headers_.clear(); 342 parsed_.clear(); 343 Parse(new_raw_headers); 344 } 345 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'); 355 356 // Make this object hold the new data. 357 raw_headers_.clear(); 358 parsed_.clear(); 359 Parse(new_raw_headers); 360 } 361 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'); 367 368 HeaderSet empty_to_remove; 369 MergeWithHeaders(new_raw_headers, empty_to_remove); 370 } 371 372 void HttpResponseHeaders::UpdateWithNewRange( 373 const HttpByteRange& byte_range, 374 int64 resource_size, 375 bool replace_status_line) { 376 DCHECK(byte_range.IsValid()); 377 DCHECK(byte_range.HasFirstBytePosition()); 378 DCHECK(byte_range.HasLastBytePosition()); 379 380 const char kLengthHeader[] = "Content-Length"; 381 const char kRangeHeader[] = "Content-Range"; 382 383 RemoveHeader(kLengthHeader); 384 RemoveHeader(kRangeHeader); 385 386 int64 start = byte_range.first_byte_position(); 387 int64 end = byte_range.last_byte_position(); 388 int64 range_len = end - start + 1; 389 390 if (replace_status_line) 391 ReplaceStatusLine("HTTP/1.1 206 Partial Content"); 392 393 AddHeader(base::StringPrintf("%s: bytes %" PRId64 "-%" PRId64 "/%" PRId64, 394 kRangeHeader, start, end, resource_size)); 395 AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader, range_len)); 396 } 397 398 void HttpResponseHeaders::Parse(const std::string& raw_input) { 399 raw_headers_.reserve(raw_input.size()); 400 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. 412 413 if (line_end == raw_input.end()) { 414 raw_headers_.push_back('\0'); // Ensure the headers end with a double null. 415 416 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 2]); 417 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]); 418 return; 419 } 420 421 // Including a terminating null byte. 422 size_t status_line_len = raw_headers_.size(); 423 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()); 427 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'); 433 } 434 435 // Adjust to point at the null byte following the status line 436 line_end = raw_headers_.begin() + status_line_len - 1; 437 438 HttpUtil::HeadersIterator headers(line_end + 1, raw_headers_.end(), 439 std::string(1, '\0')); 440 while (headers.GetNext()) { 441 AddHeader(headers.name_begin(), 442 headers.name_end(), 443 headers.values_begin(), 444 headers.values_end()); 445 } 446 447 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 2]); 448 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]); 449 } 450 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()); 455 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. 461 // 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. 466 // 467 typedef base::hash_map<std::string, size_t> HeadersMap; 468 HeadersMap headers_map; 469 HeadersMap::iterator iter = headers_map.end(); 470 471 std::vector<std::string> headers; 472 473 for (size_t i = 0; i < parsed_.size(); ++i) { 474 DCHECK(!parsed_[i].is_continuation()); 475 476 std::string name(parsed_[i].name_begin, parsed_[i].name_end); 477 std::string lower_name = StringToLowerASCII(name); 478 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 + ": "); 484 } else { 485 headers[iter->second].append(", "); 486 } 487 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; 492 --i; 493 494 headers[iter->second].append(value_begin, value_end); 495 } 496 497 for (size_t i = 0; i < headers.size(); ++i) { 498 output->push_back('\n'); 499 output->append(headers[i]); 500 } 501 502 output->push_back('\n'); 503 } 504 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)); 509 510 value->clear(); 511 512 bool found = false; 513 size_t i = 0; 514 while (i < parsed_.size()) { 515 i = FindHeader(i, name); 516 if (i == std::string::npos) 517 break; 518 519 found = true; 520 521 if (!value->empty()) 522 value->append(", "); 523 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); 529 } 530 531 return found; 532 } 533 534 std::string HttpResponseHeaders::GetStatusLine() const { 535 // copy up to the null byte. 536 return std::string(raw_headers_.c_str()); 537 } 538 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); 548 } 549 550 bool HttpResponseHeaders::EnumerateHeaderLines(void** iter, 551 std::string* name, 552 std::string* value) const { 553 size_t i = reinterpret_cast<size_t>(*iter); 554 if (i == parsed_.size()) 555 return false; 556 557 DCHECK(!parsed_[i].is_continuation()); 558 559 name->assign(parsed_[i].name_begin, parsed_[i].name_end); 560 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; 565 566 value->assign(value_begin, value_end); 567 568 *iter = reinterpret_cast<void*>(i); 569 return true; 570 } 571 572 bool HttpResponseHeaders::EnumerateHeader(void** iter, 573 const base::StringPiece& name, 574 std::string* value) const { 575 size_t i; 576 if (!iter || !*iter) { 577 i = FindHeader(0, name); 578 } else { 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); 584 } 585 } 586 587 if (i == std::string::npos) { 588 value->clear(); 589 return false; 590 } 591 592 if (iter) 593 *iter = reinterpret_cast<void*>(i + 1); 594 value->assign(parsed_[i].value_begin, parsed_[i].value_end); 595 return true; 596 } 597 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"' 602 void* iter = NULL; 603 std::string temp; 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>())) 608 return true; 609 } 610 return false; 611 } 612 613 bool HttpResponseHeaders::HasHeader(const base::StringPiece& name) const { 614 return FindHeader(0, name) != std::string::npos; 615 } 616 617 HttpResponseHeaders::HttpResponseHeaders() : response_code_(-1) { 618 } 619 620 HttpResponseHeaders::~HttpResponseHeaders() { 621 } 622 623 // Note: this implementation implicitly assumes that line_end points at a valid 624 // sentinel character (such as '\0'). 625 // static 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; 630 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. 634 635 if ((line_end - p < 4) || !LowerCaseEqualsASCII(p, p + 4, "http")) { 636 DVLOG(1) << "missing status line"; 637 return HttpVersion(); 638 } 639 640 p += 4; 641 642 if (p >= line_end || *p != '/') { 643 DVLOG(1) << "missing version"; 644 return HttpVersion(); 645 } 646 647 std::string::const_iterator dot = std::find(p, line_end, '.'); 648 if (dot == line_end) { 649 DVLOG(1) << "malformed version"; 650 return HttpVersion(); 651 } 652 653 ++p; // from / to first digit. 654 ++dot; // from . to second digit. 655 656 if (!(*p >= '0' && *p <= '9' && *dot >= '0' && *dot <= '9')) { 657 DVLOG(1) << "malformed version number"; 658 return HttpVersion(); 659 } 660 661 uint16 major = *p - '0'; 662 uint16 minor = *dot - '0'; 663 664 return HttpVersion(major, minor); 665 } 666 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, 672 bool has_headers) { 673 // Extract the version number 674 parsed_http_version_ = ParseVersion(line_begin, line_end); 675 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"; 683 } else { 684 // Treat everything else like HTTP 1.0 685 http_version_ = HttpVersion(1, 0); 686 raw_headers_ = "HTTP/1.0"; 687 } 688 if (parsed_http_version_ != http_version_) { 689 DVLOG(1) << "assuming HTTP/" << http_version_.major_value() << "." 690 << http_version_.minor_value(); 691 } 692 693 // TODO(eroman): this doesn't make sense if ParseVersion failed. 694 std::string::const_iterator p = std::find(line_begin, line_end, ' '); 695 696 if (p == line_end) { 697 DVLOG(1) << "missing response status; assuming 200 OK"; 698 raw_headers_.append(" 200 OK"); 699 response_code_ = 200; 700 return; 701 } 702 703 // Skip whitespace. 704 while (*p == ' ') 705 ++p; 706 707 std::string::const_iterator code = p; 708 while (*p >= '0' && *p <= '9') 709 ++p; 710 711 if (p == code) { 712 DVLOG(1) << "missing response status number; assuming 200"; 713 raw_headers_.append(" 200 OK"); 714 response_code_ = 200; 715 return; 716 } 717 raw_headers_.push_back(' '); 718 raw_headers_.append(code, p); 719 raw_headers_.push_back(' '); 720 base::StringToInt(StringPiece(code, p), &response_code_); 721 722 // Skip whitespace. 723 while (*p == ' ') 724 ++p; 725 726 // Trim trailing whitespace. 727 while (line_end > p && line_end[-1] == ' ') 728 --line_end; 729 730 if (p == line_end) { 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"); 735 } else { 736 raw_headers_.append(p, line_end); 737 } 738 } 739 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()) 744 continue; 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>())) 750 return i; 751 } 752 753 return std::string::npos; 754 } 755 756 void HttpResponseHeaders::AddHeader(std::string::const_iterator name_begin, 757 std::string::const_iterator name_end, 758 std::string::const_iterator values_begin, 759 std::string::const_iterator values_end) { 760 // If the header can be coalesced, then we should split it up. 761 if (values_begin == values_end || 762 HttpUtil::IsNonCoalescingHeader(name_begin, name_end)) { 763 AddToParsed(name_begin, name_end, values_begin, values_end); 764 } else { 765 HttpUtil::ValuesIterator it(values_begin, values_end, ','); 766 while (it.GetNext()) { 767 AddToParsed(name_begin, name_end, it.value_begin(), it.value_end()); 768 // clobber these so that subsequent values are treated as continuations 769 name_begin = name_end = raw_headers_.end(); 770 } 771 } 772 } 773 774 void HttpResponseHeaders::AddToParsed(std::string::const_iterator name_begin, 775 std::string::const_iterator name_end, 776 std::string::const_iterator value_begin, 777 std::string::const_iterator value_end) { 778 ParsedHeader header; 779 header.name_begin = name_begin; 780 header.name_end = name_end; 781 header.value_begin = value_begin; 782 header.value_end = value_end; 783 parsed_.push_back(header); 784 } 785 786 void HttpResponseHeaders::AddNonCacheableHeaders(HeaderSet* result) const { 787 // Add server specified transients. Any 'cache-control: no-cache="foo,bar"' 788 // headers present in the response specify additional headers that we should 789 // not store in the cache. 790 const char kCacheControl[] = "cache-control"; 791 const char kPrefix[] = "no-cache=\""; 792 const size_t kPrefixLen = sizeof(kPrefix) - 1; 793 794 std::string value; 795 void* iter = NULL; 796 while (EnumerateHeader(&iter, kCacheControl, &value)) { 797 // If the value is smaller than the prefix and a terminal quote, skip 798 // it. 799 if (value.size() <= kPrefixLen || 800 value.compare(0, kPrefixLen, kPrefix) != 0) { 801 continue; 802 } 803 // if it doesn't end with a quote, then treat as malformed 804 if (value[value.size()-1] != '\"') 805 continue; 806 807 // process the value as a comma-separated list of items. Each 808 // item can be wrapped by linear white space. 809 std::string::const_iterator item = value.begin() + kPrefixLen; 810 std::string::const_iterator end = value.end() - 1; 811 while (item != end) { 812 // Find the comma to compute the length of the current item, 813 // and the position of the next one. 814 std::string::const_iterator item_next = std::find(item, end, ','); 815 std::string::const_iterator item_end = end; 816 if (item_next != end) { 817 // Skip over comma for next position. 818 item_end = item_next; 819 item_next++; 820 } 821 // trim off leading and trailing whitespace in this item. 822 HttpUtil::TrimLWS(&item, &item_end); 823 824 // assuming the header is not empty, lowercase and insert into set 825 if (item_end > item) { 826 std::string name(&*item, item_end - item); 827 StringToLowerASCII(&name); 828 result->insert(name); 829 } 830 831 // Continue to next item. 832 item = item_next; 833 } 834 } 835 } 836 837 void HttpResponseHeaders::AddHopByHopHeaders(HeaderSet* result) { 838 for (size_t i = 0; i < arraysize(kHopByHopResponseHeaders); ++i) 839 result->insert(std::string(kHopByHopResponseHeaders[i])); 840 } 841 842 void HttpResponseHeaders::AddCookieHeaders(HeaderSet* result) { 843 for (size_t i = 0; i < arraysize(kCookieResponseHeaders); ++i) 844 result->insert(std::string(kCookieResponseHeaders[i])); 845 } 846 847 void HttpResponseHeaders::AddChallengeHeaders(HeaderSet* result) { 848 for (size_t i = 0; i < arraysize(kChallengeResponseHeaders); ++i) 849 result->insert(std::string(kChallengeResponseHeaders[i])); 850 } 851 852 void HttpResponseHeaders::AddHopContentRangeHeaders(HeaderSet* result) { 853 result->insert(kContentRange); 854 } 855 856 void HttpResponseHeaders::AddSecurityStateHeaders(HeaderSet* result) { 857 for (size_t i = 0; i < arraysize(kSecurityStateHeaders); ++i) 858 result->insert(std::string(kSecurityStateHeaders[i])); 859 } 860 861 void HttpResponseHeaders::GetMimeTypeAndCharset(std::string* mime_type, 862 std::string* charset) const { 863 mime_type->clear(); 864 charset->clear(); 865 866 std::string name = "content-type"; 867 std::string value; 868 869 bool had_charset = false; 870 871 void* iter = NULL; 872 while (EnumerateHeader(&iter, name, &value)) 873 HttpUtil::ParseContentType(value, mime_type, charset, &had_charset, NULL); 874 } 875 876 bool HttpResponseHeaders::GetMimeType(std::string* mime_type) const { 877 std::string unused; 878 GetMimeTypeAndCharset(mime_type, &unused); 879 return !mime_type->empty(); 880 } 881 882 bool HttpResponseHeaders::GetCharset(std::string* charset) const { 883 std::string unused; 884 GetMimeTypeAndCharset(&unused, charset); 885 return !charset->empty(); 886 } 887 888 bool HttpResponseHeaders::IsRedirect(std::string* location) const { 889 if (!IsRedirectResponseCode(response_code_)) 890 return false; 891 892 // If we lack a Location header, then we can't treat this as a redirect. 893 // We assume that the first non-empty location value is the target URL that 894 // we want to follow. TODO(darin): Is this consistent with other browsers? 895 size_t i = std::string::npos; 896 do { 897 i = FindHeader(++i, "location"); 898 if (i == std::string::npos) 899 return false; 900 // If the location value is empty, then it doesn't count. 901 } while (parsed_[i].value_begin == parsed_[i].value_end); 902 903 if (location) { 904 // Escape any non-ASCII characters to preserve them. The server should 905 // only be returning ASCII here, but for compat we need to do this. 906 *location = EscapeNonASCII( 907 std::string(parsed_[i].value_begin, parsed_[i].value_end)); 908 } 909 910 return true; 911 } 912 913 // static 914 bool HttpResponseHeaders::IsRedirectResponseCode(int response_code) { 915 // Users probably want to see 300 (multiple choice) pages, so we don't count 916 // them as redirects that need to be followed. 917 return (response_code == 301 || 918 response_code == 302 || 919 response_code == 303 || 920 response_code == 307 || 921 response_code == 308); 922 } 923 924 // From RFC 2616 section 13.2.4: 925 // 926 // The calculation to determine if a response has expired is quite simple: 927 // 928 // response_is_fresh = (freshness_lifetime > current_age) 929 // 930 // Of course, there are other factors that can force a response to always be 931 // validated or re-fetched. 932 // 933 bool HttpResponseHeaders::RequiresValidation(const Time& request_time, 934 const Time& response_time, 935 const Time& current_time) const { 936 TimeDelta lifetime = 937 GetFreshnessLifetime(response_time); 938 if (lifetime == TimeDelta()) 939 return true; 940 941 return lifetime <= GetCurrentAge(request_time, response_time, current_time); 942 } 943 944 // From RFC 2616 section 13.2.4: 945 // 946 // The max-age directive takes priority over Expires, so if max-age is present 947 // in a response, the calculation is simply: 948 // 949 // freshness_lifetime = max_age_value 950 // 951 // Otherwise, if Expires is present in the response, the calculation is: 952 // 953 // freshness_lifetime = expires_value - date_value 954 // 955 // Note that neither of these calculations is vulnerable to clock skew, since 956 // all of the information comes from the origin server. 957 // 958 // Also, if the response does have a Last-Modified time, the heuristic 959 // expiration value SHOULD be no more than some fraction of the interval since 960 // that time. A typical setting of this fraction might be 10%: 961 // 962 // freshness_lifetime = (date_value - last_modified_value) * 0.10 963 // 964 TimeDelta HttpResponseHeaders::GetFreshnessLifetime( 965 const Time& response_time) const { 966 // Check for headers that force a response to never be fresh. For backwards 967 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: 968 // no-cache" even though RFC 2616 does not specify it. 969 if (HasHeaderValue("cache-control", "no-cache") || 970 HasHeaderValue("cache-control", "no-store") || 971 HasHeaderValue("pragma", "no-cache") || 972 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 973 return TimeDelta(); // not fresh 974 975 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the 976 // Expires header after checking for max-age in GetFreshnessLifetime. This 977 // is important since "Expires: <date in the past>" means not fresh, but 978 // it should not trump a max-age value. 979 980 TimeDelta max_age_value; 981 if (GetMaxAgeValue(&max_age_value)) 982 return max_age_value; 983 984 // If there is no Date header, then assume that the server response was 985 // generated at the time when we received the response. 986 Time date_value; 987 if (!GetDateValue(&date_value)) 988 date_value = response_time; 989 990 Time expires_value; 991 if (GetExpiresValue(&expires_value)) { 992 // The expires value can be a date in the past! 993 if (expires_value > date_value) 994 return expires_value - date_value; 995 996 return TimeDelta(); // not fresh 997 } 998 999 // From RFC 2616 section 13.4: 1000 // 1001 // A response received with a status code of 200, 203, 206, 300, 301 or 410 1002 // MAY be stored by a cache and used in reply to a subsequent request, 1003 // subject to the expiration mechanism, unless a cache-control directive 1004 // prohibits caching. 1005 // ... 1006 // A response received with any other status code (e.g. status codes 302 1007 // and 307) MUST NOT be returned in a reply to a subsequent request unless 1008 // there are cache-control directives or another header(s) that explicitly 1009 // allow it. 1010 // 1011 // From RFC 2616 section 14.9.4: 1012 // 1013 // When the must-revalidate directive is present in a response received by 1014 // a cache, that cache MUST NOT use the entry after it becomes stale to 1015 // respond to a subsequent request without first revalidating it with the 1016 // origin server. (I.e., the cache MUST do an end-to-end revalidation every 1017 // time, if, based solely on the origin server's Expires or max-age value, 1018 // the cached response is stale.) 1019 // 1020 // https://datatracker.ietf.org/doc/draft-reschke-http-status-308/ is an 1021 // experimental RFC that adds 308 permanent redirect as well, for which "any 1022 // future references ... SHOULD use one of the returned URIs." 1023 if ((response_code_ == 200 || response_code_ == 203 || 1024 response_code_ == 206) && 1025 !HasHeaderValue("cache-control", "must-revalidate")) { 1026 // TODO(darin): Implement a smarter heuristic. 1027 Time last_modified_value; 1028 if (GetLastModifiedValue(&last_modified_value)) { 1029 // The last-modified value can be a date in the past! 1030 if (last_modified_value <= date_value) 1031 return (date_value - last_modified_value) / 10; 1032 } 1033 } 1034 1035 // These responses are implicitly fresh (unless otherwise overruled): 1036 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || 1037 response_code_ == 410) { 1038 return TimeDelta::Max(); 1039 } 1040 1041 return TimeDelta(); // not fresh 1042 } 1043 1044 // From RFC 2616 section 13.2.3: 1045 // 1046 // Summary of age calculation algorithm, when a cache receives a response: 1047 // 1048 // /* 1049 // * age_value 1050 // * is the value of Age: header received by the cache with 1051 // * this response. 1052 // * date_value 1053 // * is the value of the origin server's Date: header 1054 // * request_time 1055 // * is the (local) time when the cache made the request 1056 // * that resulted in this cached response 1057 // * response_time 1058 // * is the (local) time when the cache received the 1059 // * response 1060 // * now 1061 // * is the current (local) time 1062 // */ 1063 // apparent_age = max(0, response_time - date_value); 1064 // corrected_received_age = max(apparent_age, age_value); 1065 // response_delay = response_time - request_time; 1066 // corrected_initial_age = corrected_received_age + response_delay; 1067 // resident_time = now - response_time; 1068 // current_age = corrected_initial_age + resident_time; 1069 // 1070 TimeDelta HttpResponseHeaders::GetCurrentAge(const Time& request_time, 1071 const Time& response_time, 1072 const Time& current_time) const { 1073 // If there is no Date header, then assume that the server response was 1074 // generated at the time when we received the response. 1075 Time date_value; 1076 if (!GetDateValue(&date_value)) 1077 date_value = response_time; 1078 1079 // If there is no Age header, then assume age is zero. GetAgeValue does not 1080 // modify its out param if the value does not exist. 1081 TimeDelta age_value; 1082 GetAgeValue(&age_value); 1083 1084 TimeDelta apparent_age = std::max(TimeDelta(), response_time - date_value); 1085 TimeDelta corrected_received_age = std::max(apparent_age, age_value); 1086 TimeDelta response_delay = response_time - request_time; 1087 TimeDelta corrected_initial_age = corrected_received_age + response_delay; 1088 TimeDelta resident_time = current_time - response_time; 1089 TimeDelta current_age = corrected_initial_age + resident_time; 1090 1091 return current_age; 1092 } 1093 1094 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { 1095 std::string name = "cache-control"; 1096 std::string value; 1097 1098 const char kMaxAgePrefix[] = "max-age="; 1099 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; 1100 1101 void* iter = NULL; 1102 while (EnumerateHeader(&iter, name, &value)) { 1103 if (value.size() > kMaxAgePrefixLen) { 1104 if (LowerCaseEqualsASCII(value.begin(), 1105 value.begin() + kMaxAgePrefixLen, 1106 kMaxAgePrefix)) { 1107 int64 seconds; 1108 base::StringToInt64(StringPiece(value.begin() + kMaxAgePrefixLen, 1109 value.end()), 1110 &seconds); 1111 *result = TimeDelta::FromSeconds(seconds); 1112 return true; 1113 } 1114 } 1115 } 1116 1117 return false; 1118 } 1119 1120 bool HttpResponseHeaders::GetAgeValue(TimeDelta* result) const { 1121 std::string value; 1122 if (!EnumerateHeader(NULL, "Age", &value)) 1123 return false; 1124 1125 int64 seconds; 1126 base::StringToInt64(value, &seconds); 1127 *result = TimeDelta::FromSeconds(seconds); 1128 return true; 1129 } 1130 1131 bool HttpResponseHeaders::GetDateValue(Time* result) const { 1132 return GetTimeValuedHeader("Date", result); 1133 } 1134 1135 bool HttpResponseHeaders::GetLastModifiedValue(Time* result) const { 1136 return GetTimeValuedHeader("Last-Modified", result); 1137 } 1138 1139 bool HttpResponseHeaders::GetExpiresValue(Time* result) const { 1140 return GetTimeValuedHeader("Expires", result); 1141 } 1142 1143 bool HttpResponseHeaders::GetTimeValuedHeader(const std::string& name, 1144 Time* result) const { 1145 std::string value; 1146 if (!EnumerateHeader(NULL, name, &value)) 1147 return false; 1148 1149 // When parsing HTTP dates it's beneficial to default to GMT because: 1150 // 1. RFC2616 3.3.1 says times should always be specified in GMT 1151 // 2. Only counter-example incorrectly appended "UTC" (crbug.com/153759) 1152 // 3. When adjusting cookie expiration times for clock skew 1153 // (crbug.com/135131) this better matches our cookie expiration 1154 // time parser which ignores timezone specifiers and assumes GMT. 1155 // 4. This is exactly what Firefox does. 1156 // TODO(pauljensen): The ideal solution would be to return false if the 1157 // timezone could not be understood so as to avoid makeing other calculations 1158 // based on an incorrect time. This would require modifying the time 1159 // library or duplicating the code. (http://crbug.com/158327) 1160 return Time::FromUTCString(value.c_str(), result); 1161 } 1162 1163 bool HttpResponseHeaders::IsKeepAlive() const { 1164 if (http_version_ < HttpVersion(1, 0)) 1165 return false; 1166 1167 // NOTE: It is perhaps risky to assume that a Proxy-Connection header is 1168 // meaningful when we don't know that this response was from a proxy, but 1169 // Mozilla also does this, so we'll do the same. 1170 std::string connection_val; 1171 if (!EnumerateHeader(NULL, "connection", &connection_val)) 1172 EnumerateHeader(NULL, "proxy-connection", &connection_val); 1173 1174 bool keep_alive; 1175 1176 if (http_version_ == HttpVersion(1, 0)) { 1177 // HTTP/1.0 responses default to NOT keep-alive 1178 keep_alive = LowerCaseEqualsASCII(connection_val, "keep-alive"); 1179 } else { 1180 // HTTP/1.1 responses default to keep-alive 1181 keep_alive = !LowerCaseEqualsASCII(connection_val, "close"); 1182 } 1183 1184 return keep_alive; 1185 } 1186 1187 bool HttpResponseHeaders::HasStrongValidators() const { 1188 std::string etag_header; 1189 EnumerateHeader(NULL, "etag", &etag_header); 1190 std::string last_modified_header; 1191 EnumerateHeader(NULL, "Last-Modified", &last_modified_header); 1192 std::string date_header; 1193 EnumerateHeader(NULL, "Date", &date_header); 1194 return HttpUtil::HasStrongValidators(GetHttpVersion(), 1195 etag_header, 1196 last_modified_header, 1197 date_header); 1198 } 1199 1200 // From RFC 2616: 1201 // Content-Length = "Content-Length" ":" 1*DIGIT 1202 int64 HttpResponseHeaders::GetContentLength() const { 1203 return GetInt64HeaderValue("content-length"); 1204 } 1205 1206 int64 HttpResponseHeaders::GetInt64HeaderValue( 1207 const std::string& header) const { 1208 void* iter = NULL; 1209 std::string content_length_val; 1210 if (!EnumerateHeader(&iter, header, &content_length_val)) 1211 return -1; 1212 1213 if (content_length_val.empty()) 1214 return -1; 1215 1216 if (content_length_val[0] == '+') 1217 return -1; 1218 1219 int64 result; 1220 bool ok = base::StringToInt64(content_length_val, &result); 1221 if (!ok || result < 0) 1222 return -1; 1223 1224 return result; 1225 } 1226 1227 // From RFC 2616 14.16: 1228 // content-range-spec = 1229 // bytes-unit SP byte-range-resp-spec "/" ( instance-length | "*" ) 1230 // byte-range-resp-spec = (first-byte-pos "-" last-byte-pos) | "*" 1231 // instance-length = 1*DIGIT 1232 // bytes-unit = "bytes" 1233 bool HttpResponseHeaders::GetContentRange(int64* first_byte_position, 1234 int64* last_byte_position, 1235 int64* instance_length) const { 1236 void* iter = NULL; 1237 std::string content_range_spec; 1238 *first_byte_position = *last_byte_position = *instance_length = -1; 1239 if (!EnumerateHeader(&iter, kContentRange, &content_range_spec)) 1240 return false; 1241 1242 // If the header value is empty, we have an invalid header. 1243 if (content_range_spec.empty()) 1244 return false; 1245 1246 size_t space_position = content_range_spec.find(' '); 1247 if (space_position == std::string::npos) 1248 return false; 1249 1250 // Invalid header if it doesn't contain "bytes-unit". 1251 std::string::const_iterator content_range_spec_begin = 1252 content_range_spec.begin(); 1253 std::string::const_iterator content_range_spec_end = 1254 content_range_spec.begin() + space_position; 1255 HttpUtil::TrimLWS(&content_range_spec_begin, &content_range_spec_end); 1256 if (!LowerCaseEqualsASCII(content_range_spec_begin, 1257 content_range_spec_end, 1258 "bytes")) { 1259 return false; 1260 } 1261 1262 size_t slash_position = content_range_spec.find('/', space_position + 1); 1263 if (slash_position == std::string::npos) 1264 return false; 1265 1266 // Obtain the part behind the space and before slash. 1267 std::string::const_iterator byte_range_resp_spec_begin = 1268 content_range_spec.begin() + space_position + 1; 1269 std::string::const_iterator byte_range_resp_spec_end = 1270 content_range_spec.begin() + slash_position; 1271 HttpUtil::TrimLWS(&byte_range_resp_spec_begin, &byte_range_resp_spec_end); 1272 1273 // Parse the byte-range-resp-spec part. 1274 std::string byte_range_resp_spec(byte_range_resp_spec_begin, 1275 byte_range_resp_spec_end); 1276 // If byte-range-resp-spec != "*". 1277 if (!LowerCaseEqualsASCII(byte_range_resp_spec, "*")) { 1278 size_t minus_position = byte_range_resp_spec.find('-'); 1279 if (minus_position != std::string::npos) { 1280 // Obtain first-byte-pos. 1281 std::string::const_iterator first_byte_pos_begin = 1282 byte_range_resp_spec.begin(); 1283 std::string::const_iterator first_byte_pos_end = 1284 byte_range_resp_spec.begin() + minus_position; 1285 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); 1286 1287 bool ok = base::StringToInt64(StringPiece(first_byte_pos_begin, 1288 first_byte_pos_end), 1289 first_byte_position); 1290 1291 // Obtain last-byte-pos. 1292 std::string::const_iterator last_byte_pos_begin = 1293 byte_range_resp_spec.begin() + minus_position + 1; 1294 std::string::const_iterator last_byte_pos_end = 1295 byte_range_resp_spec.end(); 1296 HttpUtil::TrimLWS(&last_byte_pos_begin, &last_byte_pos_end); 1297 1298 ok &= base::StringToInt64(StringPiece(last_byte_pos_begin, 1299 last_byte_pos_end), 1300 last_byte_position); 1301 if (!ok) { 1302 *first_byte_position = *last_byte_position = -1; 1303 return false; 1304 } 1305 if (*first_byte_position < 0 || *last_byte_position < 0 || 1306 *first_byte_position > *last_byte_position) 1307 return false; 1308 } else { 1309 return false; 1310 } 1311 } 1312 1313 // Parse the instance-length part. 1314 // If instance-length == "*". 1315 std::string::const_iterator instance_length_begin = 1316 content_range_spec.begin() + slash_position + 1; 1317 std::string::const_iterator instance_length_end = 1318 content_range_spec.end(); 1319 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end); 1320 1321 if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) { 1322 return false; 1323 } else if (!base::StringToInt64(StringPiece(instance_length_begin, 1324 instance_length_end), 1325 instance_length)) { 1326 *instance_length = -1; 1327 return false; 1328 } 1329 1330 // We have all the values; let's verify that they make sense for a 206 1331 // response. 1332 if (*first_byte_position < 0 || *last_byte_position < 0 || 1333 *instance_length < 0 || *instance_length - 1 < *last_byte_position) 1334 return false; 1335 1336 return true; 1337 } 1338 1339 base::Value* HttpResponseHeaders::NetLogCallback( 1340 NetLog::LogLevel log_level) const { 1341 base::DictionaryValue* dict = new base::DictionaryValue(); 1342 base::ListValue* headers = new base::ListValue(); 1343 headers->Append(new base::StringValue(GetStatusLine())); 1344 void* iterator = NULL; 1345 std::string name; 1346 std::string value; 1347 while (EnumerateHeaderLines(&iterator, &name, &value)) { 1348 std::string log_value = ElideHeaderValueForNetLog(log_level, name, value); 1349 headers->Append( 1350 new base::StringValue( 1351 base::StringPrintf("%s: %s", name.c_str(), log_value.c_str()))); 1352 } 1353 dict->Set("headers", headers); 1354 return dict; 1355 } 1356 1357 // static 1358 bool HttpResponseHeaders::FromNetLogParam( 1359 const base::Value* event_param, 1360 scoped_refptr<HttpResponseHeaders>* http_response_headers) { 1361 *http_response_headers = NULL; 1362 1363 const base::DictionaryValue* dict = NULL; 1364 const base::ListValue* header_list = NULL; 1365 1366 if (!event_param || 1367 !event_param->GetAsDictionary(&dict) || 1368 !dict->GetList("headers", &header_list)) { 1369 return false; 1370 } 1371 1372 std::string raw_headers; 1373 for (base::ListValue::const_iterator it = header_list->begin(); 1374 it != header_list->end(); 1375 ++it) { 1376 std::string header_line; 1377 if (!(*it)->GetAsString(&header_line)) 1378 return false; 1379 1380 raw_headers.append(header_line); 1381 raw_headers.push_back('\0'); 1382 } 1383 raw_headers.push_back('\0'); 1384 *http_response_headers = new HttpResponseHeaders(raw_headers); 1385 return true; 1386 } 1387 1388 bool HttpResponseHeaders::IsChunkEncoded() const { 1389 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. 1390 return GetHttpVersion() >= HttpVersion(1, 1) && 1391 HasHeaderValue("Transfer-Encoding", "chunked"); 1392 } 1393 1394 } // namespace net 1395