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, p), &response_code_); |
702 | 704 |
703 // Skip whitespace. | 705 // Skip whitespace. |
704 while (*p == ' ') | 706 while (*p == ' ') |
705 ++p; | 707 ++p; |
706 | 708 |
707 // Trim trailing whitespace. | 709 // Trim trailing whitespace. |
708 while (line_end > p && line_end[-1] == ' ') | 710 while (line_end > p && line_end[-1] == ' ') |
709 --line_end; | 711 --line_end; |
710 | 712 |
711 if (p == line_end) { | 713 if (p == line_end) { |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1064 const char kMaxAgePrefix[] = "max-age="; | 1066 const char kMaxAgePrefix[] = "max-age="; |
1065 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; | 1067 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; |
1066 | 1068 |
1067 void* iter = NULL; | 1069 void* iter = NULL; |
1068 while (EnumerateHeader(&iter, name, &value)) { | 1070 while (EnumerateHeader(&iter, name, &value)) { |
1069 if (value.size() > kMaxAgePrefixLen) { | 1071 if (value.size() > kMaxAgePrefixLen) { |
1070 if (LowerCaseEqualsASCII(value.begin(), | 1072 if (LowerCaseEqualsASCII(value.begin(), |
1071 value.begin() + kMaxAgePrefixLen, | 1073 value.begin() + kMaxAgePrefixLen, |
1072 kMaxAgePrefix)) { | 1074 kMaxAgePrefix)) { |
1073 int64 seconds; | 1075 int64 seconds; |
1074 base::StringToInt64(value.begin() + kMaxAgePrefixLen, | 1076 base::StringToInt64(StringPiece(value.begin() + kMaxAgePrefixLen, |
1075 value.end(), | 1077 value.end()), |
1076 &seconds); | 1078 &seconds); |
1077 *result = TimeDelta::FromSeconds(seconds); | 1079 *result = TimeDelta::FromSeconds(seconds); |
1078 return true; | 1080 return true; |
1079 } | 1081 } |
1080 } | 1082 } |
1081 } | 1083 } |
1082 | 1084 |
1083 return false; | 1085 return false; |
1084 } | 1086 } |
1085 | 1087 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1243 if (!LowerCaseEqualsASCII(byte_range_resp_spec, "*")) { | 1245 if (!LowerCaseEqualsASCII(byte_range_resp_spec, "*")) { |
1244 size_t minus_position = byte_range_resp_spec.find('-'); | 1246 size_t minus_position = byte_range_resp_spec.find('-'); |
1245 if (minus_position != std::string::npos) { | 1247 if (minus_position != std::string::npos) { |
1246 // Obtain first-byte-pos. | 1248 // Obtain first-byte-pos. |
1247 std::string::const_iterator first_byte_pos_begin = | 1249 std::string::const_iterator first_byte_pos_begin = |
1248 byte_range_resp_spec.begin(); | 1250 byte_range_resp_spec.begin(); |
1249 std::string::const_iterator first_byte_pos_end = | 1251 std::string::const_iterator first_byte_pos_end = |
1250 byte_range_resp_spec.begin() + minus_position; | 1252 byte_range_resp_spec.begin() + minus_position; |
1251 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); | 1253 HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); |
1252 | 1254 |
1253 bool ok = base::StringToInt64(first_byte_pos_begin, | 1255 bool ok = base::StringToInt64(StringPiece(first_byte_pos_begin, |
1254 first_byte_pos_end, | 1256 first_byte_pos_end), |
1255 first_byte_position); | 1257 first_byte_position); |
1256 | 1258 |
1257 // Obtain last-byte-pos. | 1259 // Obtain last-byte-pos. |
1258 std::string::const_iterator last_byte_pos_begin = | 1260 std::string::const_iterator last_byte_pos_begin = |
1259 byte_range_resp_spec.begin() + minus_position + 1; | 1261 byte_range_resp_spec.begin() + minus_position + 1; |
1260 std::string::const_iterator last_byte_pos_end = | 1262 std::string::const_iterator last_byte_pos_end = |
1261 byte_range_resp_spec.end(); | 1263 byte_range_resp_spec.end(); |
1262 HttpUtil::TrimLWS(&last_byte_pos_begin, &last_byte_pos_end); | 1264 HttpUtil::TrimLWS(&last_byte_pos_begin, &last_byte_pos_end); |
1263 | 1265 |
1264 ok &= base::StringToInt64(last_byte_pos_begin, | 1266 ok &= base::StringToInt64(StringPiece(last_byte_pos_begin, |
1265 last_byte_pos_end, | 1267 last_byte_pos_end), |
1266 last_byte_position); | 1268 last_byte_position); |
1267 if (!ok) { | 1269 if (!ok) { |
1268 *first_byte_position = *last_byte_position = -1; | 1270 *first_byte_position = *last_byte_position = -1; |
1269 return false; | 1271 return false; |
1270 } | 1272 } |
1271 if (*first_byte_position < 0 || *last_byte_position < 0 || | 1273 if (*first_byte_position < 0 || *last_byte_position < 0 || |
1272 *first_byte_position > *last_byte_position) | 1274 *first_byte_position > *last_byte_position) |
1273 return false; | 1275 return false; |
1274 } else { | 1276 } else { |
1275 return false; | 1277 return false; |
1276 } | 1278 } |
1277 } | 1279 } |
1278 | 1280 |
1279 // Parse the instance-length part. | 1281 // Parse the instance-length part. |
1280 // If instance-length == "*". | 1282 // If instance-length == "*". |
1281 std::string::const_iterator instance_length_begin = | 1283 std::string::const_iterator instance_length_begin = |
1282 content_range_spec.begin() + slash_position + 1; | 1284 content_range_spec.begin() + slash_position + 1; |
1283 std::string::const_iterator instance_length_end = | 1285 std::string::const_iterator instance_length_end = |
1284 content_range_spec.end(); | 1286 content_range_spec.end(); |
1285 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end); | 1287 HttpUtil::TrimLWS(&instance_length_begin, &instance_length_end); |
1286 | 1288 |
1287 if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) { | 1289 if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) { |
1288 return false; | 1290 return false; |
1289 } else if (!base::StringToInt64(instance_length_begin, | 1291 } else if (!base::StringToInt64(StringPiece(instance_length_begin, |
1290 instance_length_end, | 1292 instance_length_end), |
1291 instance_length)) { | 1293 instance_length)) { |
1292 *instance_length = -1; | 1294 *instance_length = -1; |
1293 return false; | 1295 return false; |
1294 } | 1296 } |
1295 | 1297 |
1296 // We have all the values; let's verify that they make sense for a 206 | 1298 // We have all the values; let's verify that they make sense for a 206 |
1297 // response. | 1299 // response. |
1298 if (*first_byte_position < 0 || *last_byte_position < 0 || | 1300 if (*first_byte_position < 0 || *last_byte_position < 0 || |
1299 *instance_length < 0 || *instance_length - 1 < *last_byte_position) | 1301 *instance_length < 0 || *instance_length - 1 < *last_byte_position) |
1300 return false; | 1302 return false; |
1301 | 1303 |
1302 return true; | 1304 return true; |
1303 } | 1305 } |
1304 | 1306 |
1305 bool HttpResponseHeaders::IsChunkEncoded() const { | 1307 bool HttpResponseHeaders::IsChunkEncoded() const { |
1306 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. | 1308 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. |
1307 return GetHttpVersion() >= HttpVersion(1, 1) && | 1309 return GetHttpVersion() >= HttpVersion(1, 1) && |
1308 HasHeaderValue("Transfer-Encoding", "chunked"); | 1310 HasHeaderValue("Transfer-Encoding", "chunked"); |
1309 } | 1311 } |
1310 | 1312 |
1311 } // namespace net | 1313 } // namespace net |
OLD | NEW |