Chromium Code Reviews| 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 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> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/format_macros.h" | |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 16 #include "base/strings/string_tokenizer.h" | 17 #include "base/strings/string_tokenizer.h" |
| 17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "net/http/http_response_headers.h" | |
| 20 | 22 |
| 21 using std::string; | 23 using std::string; |
| 22 | 24 |
| 23 namespace net { | 25 namespace net { |
| 24 | 26 |
| 25 //----------------------------------------------------------------------------- | 27 //----------------------------------------------------------------------------- |
| 26 | 28 |
| 27 // Return the index of the closing quote of the string, if any. | 29 // Return the index of the closing quote of the string, if any. |
| 28 static size_t FindStringEnd(const string& line, size_t start, char delim) { | 30 static size_t FindStringEnd(const string& line, size_t start, char delim) { |
| 29 DCHECK(start < line.length() && line[start] == delim && | 31 DCHECK(start < line.length() && line[start] == delim && |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 | 284 |
| 283 // Do a final check on the HttpByteRange object. | 285 // Do a final check on the HttpByteRange object. |
| 284 if (!range.IsValid()) | 286 if (!range.IsValid()) |
| 285 return false; | 287 return false; |
| 286 ranges->push_back(range); | 288 ranges->push_back(range); |
| 287 } | 289 } |
| 288 return !ranges->empty(); | 290 return !ranges->empty(); |
| 289 } | 291 } |
| 290 | 292 |
| 291 // static | 293 // static |
| 294 bool HttpUtil::UpdateResponseHeadersWithRange( | |
| 295 const HttpByteRange& byte_range, | |
| 296 int64 resource_size, | |
| 297 bool replace_status_line, | |
| 298 HttpResponseHeaders* headers) { | |
| 299 DCHECK(headers); | |
| 300 if (!byte_range.IsValid()) | |
|
rvargas (doing something else)
2014/03/05 19:47:33
We should probably dcheck this and return void...
kinuko
2014/03/06 04:11:29
Done.
| |
| 301 return false; | |
| 302 | |
| 303 const char kLengthHeader[] = "Content-Length"; | |
| 304 const char kRangeHeader[] = "Content-Range"; | |
| 305 | |
| 306 headers->RemoveHeader(kLengthHeader); | |
| 307 headers->RemoveHeader(kRangeHeader); | |
| 308 | |
| 309 int64 start = byte_range.first_byte_position(); | |
| 310 int64 end = byte_range.last_byte_position(); | |
| 311 int64 range_len = end - start + 1; | |
| 312 | |
| 313 if (replace_status_line) | |
| 314 headers->ReplaceStatusLine("HTTP/1.1 206 Partial Content"); | |
| 315 | |
| 316 headers->AddHeader( | |
| 317 base::StringPrintf("%s: bytes %" PRId64 "-%" PRId64 "/%" PRId64, | |
| 318 kRangeHeader, start, end, resource_size)); | |
| 319 headers->AddHeader( | |
| 320 base::StringPrintf("%s: %" PRId64, kLengthHeader, range_len)); | |
| 321 return true; | |
| 322 } | |
| 323 | |
| 324 // static | |
| 292 bool HttpUtil::HasHeader(const std::string& headers, const char* name) { | 325 bool HttpUtil::HasHeader(const std::string& headers, const char* name) { |
| 293 size_t name_len = strlen(name); | 326 size_t name_len = strlen(name); |
| 294 string::const_iterator it = | 327 string::const_iterator it = |
| 295 std::search(headers.begin(), | 328 std::search(headers.begin(), |
| 296 headers.end(), | 329 headers.end(), |
| 297 name, | 330 name, |
| 298 name + name_len, | 331 name + name_len, |
| 299 base::CaseInsensitiveCompareASCII<char>()); | 332 base::CaseInsensitiveCompareASCII<char>()); |
| 300 if (it == headers.end()) | 333 if (it == headers.end()) |
| 301 return false; | 334 return false; |
| (...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 911 value_is_quoted_ = true; | 944 value_is_quoted_ = true; |
| 912 // Do not store iterators into this. See declaration of unquoted_value_. | 945 // Do not store iterators into this. See declaration of unquoted_value_. |
| 913 unquoted_value_ = HttpUtil::Unquote(value_begin_, value_end_); | 946 unquoted_value_ = HttpUtil::Unquote(value_begin_, value_end_); |
| 914 } | 947 } |
| 915 } | 948 } |
| 916 | 949 |
| 917 return true; | 950 return true; |
| 918 } | 951 } |
| 919 | 952 |
| 920 } // namespace net | 953 } // namespace net |
| OLD | NEW |