| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "net/http/partial_data.h" | 5 #include "net/http/partial_data.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/disk_cache/disk_cache.h" | 11 #include "net/disk_cache/disk_cache.h" |
| 12 #include "net/http/http_response_headers.h" | 12 #include "net/http/http_response_headers.h" |
| 13 #include "net/http/http_util.h" | 13 #include "net/http/http_util.h" |
| 14 | 14 |
| 15 namespace net { |
| 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 // The headers that we have to process. | 19 // The headers that we have to process. |
| 18 const char kLengthHeader[] = "Content-Length"; | 20 const char kLengthHeader[] = "Content-Length"; |
| 19 const char kRangeHeader[] = "Content-Range"; | 21 const char kRangeHeader[] = "Content-Range"; |
| 20 const int kDataStream = 1; | 22 const int kDataStream = 1; |
| 21 | 23 |
| 24 void AddRangeHeader(int64 start, int64 end, HttpRequestHeaders* headers) { |
| 25 DCHECK(start >= 0 || end >= 0); |
| 26 std::string my_start, my_end; |
| 27 if (start >= 0) |
| 28 my_start = Int64ToString(start); |
| 29 if (end >= 0) |
| 30 my_end = Int64ToString(end); |
| 31 |
| 32 headers->SetHeader( |
| 33 HttpRequestHeaders::kRange, |
| 34 StringPrintf("bytes=%s-%s", my_start.c_str(), my_end.c_str())); |
| 22 } | 35 } |
| 23 | 36 |
| 24 namespace net { | 37 } // namespace |
| 25 | 38 |
| 26 bool PartialData::Init(const std::string& headers) { | 39 bool PartialData::Init(const HttpRequestHeaders& headers) { |
| 40 std::string range_header; |
| 41 if (!headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) |
| 42 return false; |
| 43 |
| 27 std::vector<HttpByteRange> ranges; | 44 std::vector<HttpByteRange> ranges; |
| 28 if (!HttpUtil::ParseRanges(headers, &ranges) || ranges.size() != 1) | 45 if (!HttpUtil::ParseRangeHeader(range_header, &ranges) || ranges.size() != 1) |
| 29 return false; | 46 return false; |
| 30 | 47 |
| 31 // We can handle this range request. | 48 // We can handle this range request. |
| 32 byte_range_ = ranges[0]; | 49 byte_range_ = ranges[0]; |
| 33 if (!byte_range_.IsValid()) | 50 if (!byte_range_.IsValid()) |
| 34 return false; | 51 return false; |
| 35 | 52 |
| 36 resource_size_ = 0; | 53 resource_size_ = 0; |
| 37 current_range_start_ = byte_range_.first_byte_position(); | 54 current_range_start_ = byte_range_.first_byte_position(); |
| 38 return true; | 55 return true; |
| 39 } | 56 } |
| 40 | 57 |
| 41 void PartialData::SetHeaders(const std::string& headers) { | 58 void PartialData::SetHeaders(const HttpRequestHeaders& headers) { |
| 42 DCHECK(extra_headers_.empty()); | 59 DCHECK(extra_headers_.IsEmpty()); |
| 43 extra_headers_ = headers; | 60 extra_headers_.CopyFrom(headers); |
| 44 } | 61 } |
| 45 | 62 |
| 46 void PartialData::RestoreHeaders(std::string* headers) const { | 63 void PartialData::RestoreHeaders(HttpRequestHeaders* headers) const { |
| 47 DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange()); | 64 DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange()); |
| 48 int64 end = byte_range_.IsSuffixByteRange() ? | 65 int64 end = byte_range_.IsSuffixByteRange() ? |
| 49 byte_range_.suffix_length() : byte_range_.last_byte_position(); | 66 byte_range_.suffix_length() : byte_range_.last_byte_position(); |
| 50 | 67 |
| 51 headers->assign(extra_headers_); | 68 headers->CopyFrom(extra_headers_); |
| 52 if (byte_range_.IsValid()) | 69 if (byte_range_.IsValid()) |
| 53 AddRangeHeader(current_range_start_, end, headers); | 70 AddRangeHeader(current_range_start_, end, headers); |
| 54 } | 71 } |
| 55 | 72 |
| 56 int PartialData::PrepareCacheValidation(disk_cache::Entry* entry, | 73 int PartialData::PrepareCacheValidation(disk_cache::Entry* entry, |
| 57 std::string* headers) { | 74 HttpRequestHeaders* headers) { |
| 58 DCHECK(current_range_start_ >= 0); | 75 DCHECK(current_range_start_ >= 0); |
| 59 | 76 |
| 60 // Scan the disk cache for the first cached portion within this range. | 77 // Scan the disk cache for the first cached portion within this range. |
| 61 int64 range_len = byte_range_.HasLastBytePosition() ? | 78 int64 range_len = byte_range_.HasLastBytePosition() ? |
| 62 byte_range_.last_byte_position() - current_range_start_ + 1 : kint32max; | 79 byte_range_.last_byte_position() - current_range_start_ + 1 : kint32max; |
| 63 if (range_len > kint32max) | 80 if (range_len > kint32max) |
| 64 range_len = kint32max; | 81 range_len = kint32max; |
| 65 int len = static_cast<int32>(range_len); | 82 int len = static_cast<int32>(range_len); |
| 66 if (!len) | 83 if (!len) |
| 67 return 0; | 84 return 0; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 79 } else { | 96 } else { |
| 80 cached_min_len_ = len; | 97 cached_min_len_ = len; |
| 81 cached_start_ = current_range_start_; | 98 cached_start_ = current_range_start_; |
| 82 } | 99 } |
| 83 | 100 |
| 84 if (cached_min_len_ < 0) { | 101 if (cached_min_len_ < 0) { |
| 85 DCHECK(cached_min_len_ != ERR_IO_PENDING); | 102 DCHECK(cached_min_len_ != ERR_IO_PENDING); |
| 86 return cached_min_len_; | 103 return cached_min_len_; |
| 87 } | 104 } |
| 88 | 105 |
| 89 headers->assign(extra_headers_); | 106 headers->CopyFrom(extra_headers_); |
| 90 | 107 |
| 91 if (!cached_min_len_) { | 108 if (!cached_min_len_) { |
| 92 // We don't have anything else stored. | 109 // We don't have anything else stored. |
| 93 final_range_ = true; | 110 final_range_ = true; |
| 94 cached_start_ = | 111 cached_start_ = |
| 95 byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0; | 112 byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0; |
| 96 } | 113 } |
| 97 | 114 |
| 98 if (current_range_start_ == cached_start_) { | 115 if (current_range_start_ == cached_start_) { |
| 99 // The data lives in the cache. | 116 // The data lives in the cache. |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 cached_min_len_ -= result; | 332 cached_min_len_ -= result; |
| 316 DCHECK(cached_min_len_ >= 0); | 333 DCHECK(cached_min_len_ >= 0); |
| 317 } | 334 } |
| 318 } | 335 } |
| 319 | 336 |
| 320 void PartialData::OnNetworkReadCompleted(int result) { | 337 void PartialData::OnNetworkReadCompleted(int result) { |
| 321 if (result > 0) | 338 if (result > 0) |
| 322 current_range_start_ += result; | 339 current_range_start_ += result; |
| 323 } | 340 } |
| 324 | 341 |
| 325 // Static. | |
| 326 void PartialData::AddRangeHeader(int64 start, int64 end, std::string* headers) { | |
| 327 DCHECK(start >= 0 || end >= 0); | |
| 328 std::string my_start, my_end; | |
| 329 if (start >= 0) | |
| 330 my_start = Int64ToString(start); | |
| 331 if (end >= 0) | |
| 332 my_end = Int64ToString(end); | |
| 333 | |
| 334 headers->append(StringPrintf("Range: bytes=%s-%s\r\n", my_start.c_str(), | |
| 335 my_end.c_str())); | |
| 336 } | |
| 337 | |
| 338 } // namespace net | 342 } // namespace net |
| OLD | NEW |