OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/pickle.h" | 16 #include "base/pickle.h" |
17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
18 #include "base/string_piece.h" | |
19 #include "base/string_util.h" | 18 #include "base/string_util.h" |
20 #include "base/time.h" | 19 #include "base/time.h" |
21 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
22 #include "net/http/http_util.h" | 21 #include "net/http/http_util.h" |
23 | 22 |
24 using base::StringPiece; | |
25 using base::Time; | 23 using base::Time; |
26 using base::TimeDelta; | 24 using base::TimeDelta; |
27 | 25 |
28 namespace net { | 26 namespace net { |
29 | 27 |
30 //----------------------------------------------------------------------------- | 28 //----------------------------------------------------------------------------- |
31 | 29 |
32 namespace { | 30 namespace { |
33 | 31 |
34 // These headers are RFC 2616 hop-by-hop headers; | 32 // These headers are RFC 2616 hop-by-hop headers; |
(...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 | 691 |
694 if (p == code) { | 692 if (p == code) { |
695 DVLOG(1) << "missing response status number; assuming 200"; | 693 DVLOG(1) << "missing response status number; assuming 200"; |
696 raw_headers_.append(" 200 OK"); | 694 raw_headers_.append(" 200 OK"); |
697 response_code_ = 200; | 695 response_code_ = 200; |
698 return; | 696 return; |
699 } | 697 } |
700 raw_headers_.push_back(' '); | 698 raw_headers_.push_back(' '); |
701 raw_headers_.append(code, p); | 699 raw_headers_.append(code, p); |
702 raw_headers_.push_back(' '); | 700 raw_headers_.push_back(' '); |
703 base::StringToInt(StringPiece(code, p), &response_code_); | 701 base::StringToInt(code, p, &response_code_); |
704 | 702 |
705 // Skip whitespace. | 703 // Skip whitespace. |
706 while (*p == ' ') | 704 while (*p == ' ') |
707 ++p; | 705 ++p; |
708 | 706 |
709 // Trim trailing whitespace. | 707 // Trim trailing whitespace. |
710 while (line_end > p && line_end[-1] == ' ') | 708 while (line_end > p && line_end[-1] == ' ') |
711 --line_end; | 709 --line_end; |
712 | 710 |
713 if (p == line_end) { | 711 if (p == line_end) { |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1066 const char kMaxAgePrefix[] = "max-age="; | 1064 const char kMaxAgePrefix[] = "max-age="; |
1067 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; | 1065 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; |
1068 | 1066 |
1069 void* iter = NULL; | 1067 void* iter = NULL; |
1070 while (EnumerateHeader(&iter, name, &value)) { | 1068 while (EnumerateHeader(&iter, name, &value)) { |
1071 if (value.size() > kMaxAgePrefixLen) { | 1069 if (value.size() > kMaxAgePrefixLen) { |
1072 if (LowerCaseEqualsASCII(value.begin(), | 1070 if (LowerCaseEqualsASCII(value.begin(), |
1073 value.begin() + kMaxAgePrefixLen, | 1071 value.begin() + kMaxAgePrefixLen, |
1074 kMaxAgePrefix)) { | 1072 kMaxAgePrefix)) { |
1075 int64 seconds; | 1073 int64 seconds; |
1076 base::StringToInt64(StringPiece(value.begin() + kMaxAgePrefixLen, | 1074 base::StringToInt64(value.begin() + kMaxAgePrefixLen, |
1077 value.end()), | 1075 value.end(), |
1078 &seconds); | 1076 &seconds); |
1079 *result = TimeDelta::FromSeconds(seconds); | 1077 *result = TimeDelta::FromSeconds(seconds); |
1080 return true; | 1078 return true; |
1081 } | 1079 } |
1082 } | 1080 } |
1083 } | 1081 } |
1084 | 1082 |
1085 return false; | 1083 return false; |
1086 } | 1084 } |
1087 | 1085 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1245 if (!LowerCaseEqualsASCII(byte_range_resp_spec, "*")) { | 1243 if (!LowerCaseEqualsASCII(byte_range_resp_spec, "*")) { |
1246 size_t minus_position = byte_range_resp_spec.find('-'); | 1244 size_t minus_position = byte_range_resp_spec.find('-'); |
1247 if (minus_position != std::string::npos) { | 1245 if (minus_position != std::string::npos) { |
1248 // Obtain first-byte-pos. | 1246 // Obtain first-byte-pos. |
1249 std::string::const_iterator first_byte_pos_begin = | 1247 std::string::const_iterator first_byte_pos_begin = |
1250 byte_range_resp_spec.begin(); | 1248 byte_range_resp_spec.begin(); |
1251 std::string::const_iterator first_byte_pos_end = | 1249 std::string::const_iterator first_byte_pos_end = |
1252 byte_range_resp_spec.begin() + minus_position; | 1250 byte_range_resp_spec.begin() + minus_position; |
1253 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); | 1251 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); |
1254 | 1252 |
1255 bool ok = base::StringToInt64(StringPiece(first_byte_pos_begin, | 1253 bool ok = base::StringToInt64(first_byte_pos_begin, |
1256 first_byte_pos_end), | 1254 first_byte_pos_end, |
1257 first_byte_position); | 1255 first_byte_position); |
1258 | 1256 |
1259 // Obtain last-byte-pos. | 1257 // Obtain last-byte-pos. |
1260 std::string::const_iterator last_byte_pos_begin = | 1258 std::string::const_iterator last_byte_pos_begin = |
1261 byte_range_resp_spec.begin() + minus_position + 1; | 1259 byte_range_resp_spec.begin() + minus_position + 1; |
1262 std::string::const_iterator last_byte_pos_end = | 1260 std::string::const_iterator last_byte_pos_end = |
1263 byte_range_resp_spec.end(); | 1261 byte_range_resp_spec.end(); |
1264 HttpUtil::TrimLWS(&last_byte_pos_begin, &last_byte_pos_end); | 1262 HttpUtil::TrimLWS(&last_byte_pos_begin, &last_byte_pos_end); |
1265 | 1263 |
1266 ok &= base::StringToInt64(StringPiece(last_byte_pos_begin, | 1264 ok &= base::StringToInt64(last_byte_pos_begin, |
1267 last_byte_pos_end), | 1265 last_byte_pos_end, |
1268 last_byte_position); | 1266 last_byte_position); |
1269 if (!ok) { | 1267 if (!ok) { |
1270 *first_byte_position = *last_byte_position = -1; | 1268 *first_byte_position = *last_byte_position = -1; |
1271 return false; | 1269 return false; |
1272 } | 1270 } |
1273 if (*first_byte_position < 0 || *last_byte_position < 0 || | 1271 if (*first_byte_position < 0 || *last_byte_position < 0 || |
1274 *first_byte_position > *last_byte_position) | 1272 *first_byte_position > *last_byte_position) |
1275 return false; | 1273 return false; |
1276 } else { | 1274 } else { |
1277 return false; | 1275 return false; |
1278 } | 1276 } |
1279 } | 1277 } |
1280 | 1278 |
1281 // Parse the instance-length part. | 1279 // Parse the instance-length part. |
1282 // If instance-length == "*". | 1280 // If instance-length == "*". |
1283 std::string::const_iterator instance_length_begin = | 1281 std::string::const_iterator instance_length_begin = |
1284 content_range_spec.begin() + slash_position + 1; | 1282 content_range_spec.begin() + slash_position + 1; |
1285 std::string::const_iterator instance_length_end = | 1283 std::string::const_iterator instance_length_end = |
1286 content_range_spec.end(); | 1284 content_range_spec.end(); |
1287 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end); | 1285 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end); |
1288 | 1286 |
1289 if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) { | 1287 if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) { |
1290 return false; | 1288 return false; |
1291 } else if (!base::StringToInt64(StringPiece(instance_length_begin, | 1289 } else if (!base::StringToInt64(instance_length_begin, |
1292 instance_length_end), | 1290 instance_length_end, |
1293 instance_length)) { | 1291 instance_length)) { |
1294 *instance_length = -1; | 1292 *instance_length = -1; |
1295 return false; | 1293 return false; |
1296 } | 1294 } |
1297 | 1295 |
1298 // We have all the values; let's verify that they make sense for a 206 | 1296 // We have all the values; let's verify that they make sense for a 206 |
1299 // response. | 1297 // response. |
1300 if (*first_byte_position < 0 || *last_byte_position < 0 || | 1298 if (*first_byte_position < 0 || *last_byte_position < 0 || |
1301 *instance_length < 0 || *instance_length - 1 < *last_byte_position) | 1299 *instance_length < 0 || *instance_length - 1 < *last_byte_position) |
1302 return false; | 1300 return false; |
1303 | 1301 |
1304 return true; | 1302 return true; |
1305 } | 1303 } |
1306 | 1304 |
1307 bool HttpResponseHeaders::IsChunkEncoded() const { | 1305 bool HttpResponseHeaders::IsChunkEncoded() const { |
1308 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. | 1306 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. |
1309 return GetHttpVersion() >= HttpVersion(1, 1) && | 1307 return GetHttpVersion() >= HttpVersion(1, 1) && |
1310 HasHeaderValue("Transfer-Encoding", "chunked"); | 1308 HasHeaderValue("Transfer-Encoding", "chunked"); |
1311 } | 1309 } |
1312 | 1310 |
1313 } // namespace net | 1311 } // namespace net |
OLD | NEW |