Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(607)

Side by Side Diff: net/http/http_util.cc

Issue 2545213002: Refactor Content-Range response header parsing into http_util (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 parsing content-types were borrowed from Firefox: 5 // The rules for parsing content-types were borrowed from Firefox:
6 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 6 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834
7 7
8 #include "net/http/http_util.h" 8 #include "net/http/http_util.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 258
259 // Do a final check on the HttpByteRange object. 259 // Do a final check on the HttpByteRange object.
260 if (!range.IsValid()) 260 if (!range.IsValid())
261 return false; 261 return false;
262 ranges->push_back(range); 262 ranges->push_back(range);
263 } 263 }
264 return !ranges->empty(); 264 return !ranges->empty();
265 } 265 }
266 266
267 // static 267 // static
268 // From RFC 2616 14.16:
269 // content-range-spec =
270 // bytes-unit SP byte-range-resp-spec "/" ( instance-length | "*" )
271 // byte-range-resp-spec = (first-byte-pos "-" last-byte-pos) | "*"
272 // instance-length = 1*DIGIT
273 // bytes-unit = "bytes"
274 bool HttpUtil::ParseContentRangeHeader(base::StringPiece content_range_spec,
275 int64_t* first_byte_position,
276 int64_t* last_byte_position,
277 int64_t* instance_length) {
278 *first_byte_position = *last_byte_position = *instance_length = -1;
279 content_range_spec = TrimLWS(content_range_spec);
280
281 size_t space_position = content_range_spec.find(' ');
282 if (space_position == base::StringPiece::npos)
283 return false;
284
285 // Invalid header if it doesn't contain "bytes-unit".
286 if (!base::LowerCaseEqualsASCII(
287 TrimLWS(content_range_spec.substr(0, space_position)), "bytes")) {
288 return false;
289 }
290
291 size_t slash_position = content_range_spec.find('/', space_position + 1);
292 if (slash_position == base::StringPiece::npos)
293 return false;
294
295 // Obtain the part behind the space and before slash.
296 base::StringPiece byte_range_resp_spec = TrimLWS(content_range_spec.substr(
297 space_position + 1, slash_position - (space_position + 1)));
298
299 if (byte_range_resp_spec != "*") {
300 size_t minus_position = byte_range_resp_spec.find('-');
301 if (minus_position == base::StringPiece::npos)
302 return false;
303
304 // Obtain first-byte-pos and last-byte-pos.
305 if (!base::StringToInt64(
306 TrimLWS(byte_range_resp_spec.substr(0, minus_position)),
307 first_byte_position) ||
308 !base::StringToInt64(
309 TrimLWS(byte_range_resp_spec.substr(minus_position + 1)),
310 last_byte_position)) {
311 *first_byte_position = *last_byte_position = -1;
312 return false;
313 }
314 if (*first_byte_position < 0 || *last_byte_position < 0 ||
315 *first_byte_position > *last_byte_position)
316 return false;
317 }
318
319 // Parse the instance-length part.
320 base::StringPiece instance_length_spec =
321 TrimLWS(content_range_spec.substr(slash_position + 1));
322
323 if (base::StartsWith(instance_length_spec, "*",
324 base::CompareCase::SENSITIVE)) {
325 return false;
326 } else if (!base::StringToInt64(instance_length_spec, instance_length)) {
327 *instance_length = -1;
328 return false;
329 }
330
331 // We have all the values; let's verify that they make sense for a 206
332 // response.
333 if (*first_byte_position < 0 || *last_byte_position < 0 ||
334 *instance_length < 0 || *instance_length - 1 < *last_byte_position)
335 return false;
336
337 return true;
338 }
339
340 // static
268 bool HttpUtil::ParseRetryAfterHeader(const std::string& retry_after_string, 341 bool HttpUtil::ParseRetryAfterHeader(const std::string& retry_after_string,
269 base::Time now, 342 base::Time now,
270 base::TimeDelta* retry_after) { 343 base::TimeDelta* retry_after) {
271 int seconds; 344 int seconds;
272 base::Time time; 345 base::Time time;
273 base::TimeDelta interval; 346 base::TimeDelta interval;
274 347
275 if (base::StringToInt(retry_after_string, &seconds)) { 348 if (base::StringToInt(retry_after_string, &seconds)) {
276 interval = base::TimeDelta::FromSeconds(seconds); 349 interval = base::TimeDelta::FromSeconds(seconds);
277 } else if (base::Time::FromUTCString(retry_after_string.c_str(), &time)) { 350 } else if (base::Time::FromUTCString(retry_after_string.c_str(), &time)) {
(...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after
1073 return true; 1146 return true;
1074 } 1147 }
1075 1148
1076 bool HttpUtil::NameValuePairsIterator::IsQuote(char c) const { 1149 bool HttpUtil::NameValuePairsIterator::IsQuote(char c) const {
1077 if (strict_quotes_) 1150 if (strict_quotes_)
1078 return c == '"'; 1151 return c == '"';
1079 return HttpUtil::IsQuote(c); 1152 return HttpUtil::IsQuote(c);
1080 } 1153 }
1081 1154
1082 } // namespace net 1155 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698