| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // The rules for header parsing were borrowed from Firefox: | 5 // The rules for header parsing were borrowed from Firefox: |
| 6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo
nseHead.cpp | 6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo
nseHead.cpp |
| 7 // The rules for parsing content-types were also borrowed from Firefox: | 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 | 8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 |
| 9 | 9 |
| 10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // this list: | 91 // this list: |
| 92 const char* const kNonUpdatedHeaderPrefixes[] = { | 92 const char* const kNonUpdatedHeaderPrefixes[] = { |
| 93 "content-", | 93 "content-", |
| 94 "x-content-", | 94 "x-content-", |
| 95 "x-webkit-" | 95 "x-webkit-" |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 bool ShouldUpdateHeader(const std::string::const_iterator& name_begin, | 98 bool ShouldUpdateHeader(const std::string::const_iterator& name_begin, |
| 99 const std::string::const_iterator& name_end) { | 99 const std::string::const_iterator& name_end) { |
| 100 for (size_t i = 0; i < arraysize(kNonUpdatedHeaders); ++i) { | 100 for (size_t i = 0; i < arraysize(kNonUpdatedHeaders); ++i) { |
| 101 if (LowerCaseEqualsASCII(name_begin, name_end, kNonUpdatedHeaders[i])) | 101 if (base::LowerCaseEqualsASCII(name_begin, name_end, kNonUpdatedHeaders[i])) |
| 102 return false; | 102 return false; |
| 103 } | 103 } |
| 104 for (size_t i = 0; i < arraysize(kNonUpdatedHeaderPrefixes); ++i) { | 104 for (size_t i = 0; i < arraysize(kNonUpdatedHeaderPrefixes); ++i) { |
| 105 if (StartsWithASCII(std::string(name_begin, name_end), | 105 if (StartsWithASCII(std::string(name_begin, name_end), |
| 106 kNonUpdatedHeaderPrefixes[i], false)) | 106 kNonUpdatedHeaderPrefixes[i], false)) |
| 107 return false; | 107 return false; |
| 108 } | 108 } |
| 109 return true; | 109 return true; |
| 110 } | 110 } |
| 111 | 111 |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 // static | 625 // static |
| 626 HttpVersion HttpResponseHeaders::ParseVersion( | 626 HttpVersion HttpResponseHeaders::ParseVersion( |
| 627 std::string::const_iterator line_begin, | 627 std::string::const_iterator line_begin, |
| 628 std::string::const_iterator line_end) { | 628 std::string::const_iterator line_end) { |
| 629 std::string::const_iterator p = line_begin; | 629 std::string::const_iterator p = line_begin; |
| 630 | 630 |
| 631 // RFC2616 sec 3.1: HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT | 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). | 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. | 633 // TODO: handle leading zeros, which is allowed by the rfc1616 sec 3.1. |
| 634 | 634 |
| 635 if ((line_end - p < 4) || !LowerCaseEqualsASCII(p, p + 4, "http")) { | 635 if ((line_end - p < 4) || !base::LowerCaseEqualsASCII(p, p + 4, "http")) { |
| 636 DVLOG(1) << "missing status line"; | 636 DVLOG(1) << "missing status line"; |
| 637 return HttpVersion(); | 637 return HttpVersion(); |
| 638 } | 638 } |
| 639 | 639 |
| 640 p += 4; | 640 p += 4; |
| 641 | 641 |
| 642 if (p >= line_end || *p != '/') { | 642 if (p >= line_end || *p != '/') { |
| 643 DVLOG(1) << "missing version"; | 643 DVLOG(1) << "missing version"; |
| 644 return HttpVersion(); | 644 return HttpVersion(); |
| 645 } | 645 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 bool HttpResponseHeaders::GetCacheControlDirective(const StringPiece& directive, | 756 bool HttpResponseHeaders::GetCacheControlDirective(const StringPiece& directive, |
| 757 TimeDelta* result) const { | 757 TimeDelta* result) const { |
| 758 StringPiece name("cache-control"); | 758 StringPiece name("cache-control"); |
| 759 std::string value; | 759 std::string value; |
| 760 | 760 |
| 761 size_t directive_size = directive.size(); | 761 size_t directive_size = directive.size(); |
| 762 | 762 |
| 763 void* iter = NULL; | 763 void* iter = NULL; |
| 764 while (EnumerateHeader(&iter, name, &value)) { | 764 while (EnumerateHeader(&iter, name, &value)) { |
| 765 if (value.size() > directive_size + 1 && | 765 if (value.size() > directive_size + 1 && |
| 766 LowerCaseEqualsASCII(value.begin(), | 766 base::LowerCaseEqualsASCII( |
| 767 value.begin() + directive_size, | 767 value.begin(), value.begin() + directive_size, directive.begin()) && |
| 768 directive.begin()) && | |
| 769 value[directive_size] == '=') { | 768 value[directive_size] == '=') { |
| 770 int64 seconds; | 769 int64 seconds; |
| 771 base::StringToInt64( | 770 base::StringToInt64( |
| 772 StringPiece(value.begin() + directive_size + 1, value.end()), | 771 StringPiece(value.begin() + directive_size + 1, value.end()), |
| 773 &seconds); | 772 &seconds); |
| 774 *result = TimeDelta::FromSeconds(seconds); | 773 *result = TimeDelta::FromSeconds(seconds); |
| 775 return true; | 774 return true; |
| 776 } | 775 } |
| 777 } | 776 } |
| 778 | 777 |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1225 {"close", false}}; | 1224 {"close", false}}; |
| 1226 | 1225 |
| 1227 if (http_version_ < HttpVersion(1, 0)) | 1226 if (http_version_ < HttpVersion(1, 0)) |
| 1228 return false; | 1227 return false; |
| 1229 | 1228 |
| 1230 for (const char* header : kConnectionHeaders) { | 1229 for (const char* header : kConnectionHeaders) { |
| 1231 void* iterator = nullptr; | 1230 void* iterator = nullptr; |
| 1232 std::string token; | 1231 std::string token; |
| 1233 while (EnumerateHeader(&iterator, header, &token)) { | 1232 while (EnumerateHeader(&iterator, header, &token)) { |
| 1234 for (const KeepAliveToken& keep_alive_token : kKeepAliveTokens) { | 1233 for (const KeepAliveToken& keep_alive_token : kKeepAliveTokens) { |
| 1235 if (LowerCaseEqualsASCII(token, keep_alive_token.token)) | 1234 if (base::LowerCaseEqualsASCII(token, keep_alive_token.token)) |
| 1236 return keep_alive_token.keep_alive; | 1235 return keep_alive_token.keep_alive; |
| 1237 } | 1236 } |
| 1238 } | 1237 } |
| 1239 } | 1238 } |
| 1240 return http_version_ != HttpVersion(1, 0); | 1239 return http_version_ != HttpVersion(1, 0); |
| 1241 } | 1240 } |
| 1242 | 1241 |
| 1243 bool HttpResponseHeaders::HasStrongValidators() const { | 1242 bool HttpResponseHeaders::HasStrongValidators() const { |
| 1244 std::string etag_header; | 1243 std::string etag_header; |
| 1245 EnumerateHeader(NULL, "etag", &etag_header); | 1244 EnumerateHeader(NULL, "etag", &etag_header); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1302 size_t space_position = content_range_spec.find(' '); | 1301 size_t space_position = content_range_spec.find(' '); |
| 1303 if (space_position == std::string::npos) | 1302 if (space_position == std::string::npos) |
| 1304 return false; | 1303 return false; |
| 1305 | 1304 |
| 1306 // Invalid header if it doesn't contain "bytes-unit". | 1305 // Invalid header if it doesn't contain "bytes-unit". |
| 1307 std::string::const_iterator content_range_spec_begin = | 1306 std::string::const_iterator content_range_spec_begin = |
| 1308 content_range_spec.begin(); | 1307 content_range_spec.begin(); |
| 1309 std::string::const_iterator content_range_spec_end = | 1308 std::string::const_iterator content_range_spec_end = |
| 1310 content_range_spec.begin() + space_position; | 1309 content_range_spec.begin() + space_position; |
| 1311 HttpUtil::TrimLWS(&content_range_spec_begin, &content_range_spec_end); | 1310 HttpUtil::TrimLWS(&content_range_spec_begin, &content_range_spec_end); |
| 1312 if (!LowerCaseEqualsASCII(content_range_spec_begin, | 1311 if (!base::LowerCaseEqualsASCII(content_range_spec_begin, |
| 1313 content_range_spec_end, | 1312 content_range_spec_end, "bytes")) { |
| 1314 "bytes")) { | |
| 1315 return false; | 1313 return false; |
| 1316 } | 1314 } |
| 1317 | 1315 |
| 1318 size_t slash_position = content_range_spec.find('/', space_position + 1); | 1316 size_t slash_position = content_range_spec.find('/', space_position + 1); |
| 1319 if (slash_position == std::string::npos) | 1317 if (slash_position == std::string::npos) |
| 1320 return false; | 1318 return false; |
| 1321 | 1319 |
| 1322 // Obtain the part behind the space and before slash. | 1320 // Obtain the part behind the space and before slash. |
| 1323 std::string::const_iterator byte_range_resp_spec_begin = | 1321 std::string::const_iterator byte_range_resp_spec_begin = |
| 1324 content_range_spec.begin() + space_position + 1; | 1322 content_range_spec.begin() + space_position + 1; |
| 1325 std::string::const_iterator byte_range_resp_spec_end = | 1323 std::string::const_iterator byte_range_resp_spec_end = |
| 1326 content_range_spec.begin() + slash_position; | 1324 content_range_spec.begin() + slash_position; |
| 1327 HttpUtil::TrimLWS(&byte_range_resp_spec_begin, &byte_range_resp_spec_end); | 1325 HttpUtil::TrimLWS(&byte_range_resp_spec_begin, &byte_range_resp_spec_end); |
| 1328 | 1326 |
| 1329 // Parse the byte-range-resp-spec part. | 1327 // Parse the byte-range-resp-spec part. |
| 1330 std::string byte_range_resp_spec(byte_range_resp_spec_begin, | 1328 std::string byte_range_resp_spec(byte_range_resp_spec_begin, |
| 1331 byte_range_resp_spec_end); | 1329 byte_range_resp_spec_end); |
| 1332 // If byte-range-resp-spec != "*". | 1330 // If byte-range-resp-spec != "*". |
| 1333 if (!LowerCaseEqualsASCII(byte_range_resp_spec, "*")) { | 1331 if (!base::LowerCaseEqualsASCII(byte_range_resp_spec, "*")) { |
| 1334 size_t minus_position = byte_range_resp_spec.find('-'); | 1332 size_t minus_position = byte_range_resp_spec.find('-'); |
| 1335 if (minus_position != std::string::npos) { | 1333 if (minus_position != std::string::npos) { |
| 1336 // Obtain first-byte-pos. | 1334 // Obtain first-byte-pos. |
| 1337 std::string::const_iterator first_byte_pos_begin = | 1335 std::string::const_iterator first_byte_pos_begin = |
| 1338 byte_range_resp_spec.begin(); | 1336 byte_range_resp_spec.begin(); |
| 1339 std::string::const_iterator first_byte_pos_end = | 1337 std::string::const_iterator first_byte_pos_end = |
| 1340 byte_range_resp_spec.begin() + minus_position; | 1338 byte_range_resp_spec.begin() + minus_position; |
| 1341 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); | 1339 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); |
| 1342 | 1340 |
| 1343 bool ok = base::StringToInt64(StringPiece(first_byte_pos_begin, | 1341 bool ok = base::StringToInt64(StringPiece(first_byte_pos_begin, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1367 } | 1365 } |
| 1368 | 1366 |
| 1369 // Parse the instance-length part. | 1367 // Parse the instance-length part. |
| 1370 // If instance-length == "*". | 1368 // If instance-length == "*". |
| 1371 std::string::const_iterator instance_length_begin = | 1369 std::string::const_iterator instance_length_begin = |
| 1372 content_range_spec.begin() + slash_position + 1; | 1370 content_range_spec.begin() + slash_position + 1; |
| 1373 std::string::const_iterator instance_length_end = | 1371 std::string::const_iterator instance_length_end = |
| 1374 content_range_spec.end(); | 1372 content_range_spec.end(); |
| 1375 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end); | 1373 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end); |
| 1376 | 1374 |
| 1377 if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) { | 1375 if (base::LowerCaseEqualsASCII(instance_length_begin, instance_length_end, |
| 1376 "*")) { |
| 1378 return false; | 1377 return false; |
| 1379 } else if (!base::StringToInt64(StringPiece(instance_length_begin, | 1378 } else if (!base::StringToInt64(StringPiece(instance_length_begin, |
| 1380 instance_length_end), | 1379 instance_length_end), |
| 1381 instance_length)) { | 1380 instance_length)) { |
| 1382 *instance_length = -1; | 1381 *instance_length = -1; |
| 1383 return false; | 1382 return false; |
| 1384 } | 1383 } |
| 1385 | 1384 |
| 1386 // We have all the values; let's verify that they make sense for a 206 | 1385 // We have all the values; let's verify that they make sense for a 206 |
| 1387 // response. | 1386 // response. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1445 return true; | 1444 return true; |
| 1446 } | 1445 } |
| 1447 | 1446 |
| 1448 bool HttpResponseHeaders::IsChunkEncoded() const { | 1447 bool HttpResponseHeaders::IsChunkEncoded() const { |
| 1449 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. | 1448 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. |
| 1450 return GetHttpVersion() >= HttpVersion(1, 1) && | 1449 return GetHttpVersion() >= HttpVersion(1, 1) && |
| 1451 HasHeaderValue("Transfer-Encoding", "chunked"); | 1450 HasHeaderValue("Transfer-Encoding", "chunked"); |
| 1452 } | 1451 } |
| 1453 | 1452 |
| 1454 } // namespace net | 1453 } // namespace net |
| OLD | NEW |