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