tbreisacher@chromium.org | 9987dd7 | 2012-01-25 23:40:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/http/partial_data.h" |
| 6 | |
jhawkins@chromium.org | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 9 | #include "base/format_macros.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 10 | #include "base/logging.h" |
avi@chromium.org | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
| 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/strings/stringprintf.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 14 | #include "net/base/net_errors.h" |
| 15 | #include "net/disk_cache/disk_cache.h" |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 16 | #include "net/http/http_response_headers.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 17 | #include "net/http/http_util.h" |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 18 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 19 | namespace net { |
| 20 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // The headers that we have to process. |
| 24 | const char kLengthHeader[] = "Content-Length"; |
| 25 | const char kRangeHeader[] = "Content-Range"; |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 26 | const int kDataStream = 1; |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 27 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 28 | } // namespace |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 29 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 30 | // A core object that can be detached from the Partialdata object at destruction |
| 31 | // so that asynchronous operations cleanup can be performed. |
| 32 | class PartialData::Core { |
| 33 | public: |
| 34 | // Build a new core object. Lifetime management is automatic. |
| 35 | static Core* CreateCore(PartialData* owner) { |
| 36 | return new Core(owner); |
| 37 | } |
| 38 | |
| 39 | // Wrapper for Entry::GetAvailableRange. If this method returns ERR_IO_PENDING |
| 40 | // PartialData::GetAvailableRangeCompleted() will be invoked on the owner |
| 41 | // object when finished (unless Cancel() is called first). |
| 42 | int GetAvailableRange(disk_cache::Entry* entry, int64 offset, int len, |
| 43 | int64* start); |
| 44 | |
| 45 | // Cancels a pending operation. It is a mistake to call this method if there |
| 46 | // is no operation in progress; in fact, there will be no object to do so. |
| 47 | void Cancel(); |
| 48 | |
| 49 | private: |
| 50 | explicit Core(PartialData* owner); |
| 51 | ~Core(); |
| 52 | |
| 53 | // Pending io completion routine. |
| 54 | void OnIOComplete(int result); |
| 55 | |
| 56 | PartialData* owner_; |
| 57 | int64 start_; |
jhawkins@chromium.org | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 58 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 59 | DISALLOW_COPY_AND_ASSIGN(Core); |
| 60 | }; |
| 61 | |
| 62 | PartialData::Core::Core(PartialData* owner) |
tbreisacher@chromium.org | 9987dd7 | 2012-01-25 23:40:04 | [diff] [blame] | 63 | : owner_(owner), start_(0) { |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 64 | DCHECK(!owner_->core_); |
| 65 | owner_->core_ = this; |
| 66 | } |
| 67 | |
| 68 | PartialData::Core::~Core() { |
| 69 | if (owner_) |
| 70 | owner_->core_ = NULL; |
| 71 | } |
| 72 | |
| 73 | void PartialData::Core::Cancel() { |
| 74 | DCHECK(owner_); |
| 75 | owner_ = NULL; |
| 76 | } |
| 77 | |
| 78 | int PartialData::Core::GetAvailableRange(disk_cache::Entry* entry, int64 offset, |
| 79 | int len, int64* start) { |
jhawkins@chromium.org | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 80 | int rv = entry->GetAvailableRange( |
| 81 | offset, len, &start_, base::Bind(&PartialData::Core::OnIOComplete, |
| 82 | base::Unretained(this))); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 83 | if (rv != ERR_IO_PENDING) { |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 84 | // The callback will not be invoked. Lets cleanup. |
| 85 | *start = start_; |
| 86 | delete this; |
| 87 | } |
| 88 | return rv; |
| 89 | } |
| 90 | |
| 91 | void PartialData::Core::OnIOComplete(int result) { |
| 92 | if (owner_) |
| 93 | owner_->GetAvailableRangeCompleted(result, start_); |
| 94 | delete this; |
| 95 | } |
| 96 | |
| 97 | // ----------------------------------------------------------------------------- |
| 98 | |
erg@google.com | b104b50 | 2010-10-18 20:21:31 | [diff] [blame] | 99 | PartialData::PartialData() |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 100 | : current_range_start_(0), |
| 101 | current_range_end_(0), |
| 102 | cached_start_(0), |
| 103 | resource_size_(0), |
| 104 | cached_min_len_(0), |
| 105 | range_present_(false), |
erg@google.com | b104b50 | 2010-10-18 20:21:31 | [diff] [blame] | 106 | final_range_(false), |
| 107 | sparse_entry_(true), |
| 108 | truncated_(false), |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 109 | initial_validation_(false), |
jhawkins@chromium.org | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 110 | core_(NULL) { |
erg@google.com | b104b50 | 2010-10-18 20:21:31 | [diff] [blame] | 111 | } |
| 112 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 113 | PartialData::~PartialData() { |
| 114 | if (core_) |
| 115 | core_->Cancel(); |
| 116 | } |
| 117 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 118 | bool PartialData::Init(const HttpRequestHeaders& headers) { |
| 119 | std::string range_header; |
| 120 | if (!headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) |
| 121 | return false; |
| 122 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 123 | std::vector<HttpByteRange> ranges; |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 124 | if (!HttpUtil::ParseRangeHeader(range_header, &ranges) || ranges.size() != 1) |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 125 | return false; |
| 126 | |
| 127 | // We can handle this range request. |
| 128 | byte_range_ = ranges[0]; |
| 129 | if (!byte_range_.IsValid()) |
| 130 | return false; |
| 131 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 132 | current_range_start_ = byte_range_.first_byte_position(); |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 133 | |
| 134 | DVLOG(1) << "Range start: " << current_range_start_ << " end: " << |
| 135 | byte_range_.last_byte_position(); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 139 | void PartialData::SetHeaders(const HttpRequestHeaders& headers) { |
| 140 | DCHECK(extra_headers_.IsEmpty()); |
| 141 | extra_headers_.CopyFrom(headers); |
rvargas@google.com | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 142 | } |
| 143 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 144 | void PartialData::RestoreHeaders(HttpRequestHeaders* headers) const { |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 145 | DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange()); |
| 146 | int64 end = byte_range_.IsSuffixByteRange() ? |
| 147 | byte_range_.suffix_length() : byte_range_.last_byte_position(); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 148 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 149 | headers->CopyFrom(extra_headers_); |
tommycli@chromium.org | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 150 | if (truncated_ || !byte_range_.IsValid()) |
| 151 | return; |
| 152 | |
| 153 | if (current_range_start_ < 0) { |
| 154 | headers->SetHeader(HttpRequestHeaders::kRange, |
| 155 | HttpByteRange::Suffix(end).GetHeaderValue()); |
| 156 | } else { |
| 157 | headers->SetHeader(HttpRequestHeaders::kRange, |
| 158 | HttpByteRange::Bounded( |
| 159 | current_range_start_, end).GetHeaderValue()); |
| 160 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 161 | } |
| 162 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 163 | int PartialData::ShouldValidateCache(disk_cache::Entry* entry, |
jhawkins@chromium.org | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 164 | const CompletionCallback& callback) { |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 165 | DCHECK_GE(current_range_start_, 0); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 166 | |
| 167 | // Scan the disk cache for the first cached portion within this range. |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 168 | int len = GetNextRangeLen(); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 169 | if (!len) |
| 170 | return 0; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 171 | |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 172 | DVLOG(3) << "ShouldValidateCache len: " << len; |
| 173 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 174 | if (sparse_entry_) { |
jhawkins@chromium.org | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 175 | DCHECK(callback_.is_null()); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 176 | Core* core = Core::CreateCore(this); |
| 177 | cached_min_len_ = core->GetAvailableRange(entry, current_range_start_, len, |
| 178 | &cached_start_); |
| 179 | |
| 180 | if (cached_min_len_ == ERR_IO_PENDING) { |
| 181 | callback_ = callback; |
| 182 | return ERR_IO_PENDING; |
| 183 | } |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 184 | } else if (!truncated_) { |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 185 | if (byte_range_.HasFirstBytePosition() && |
| 186 | byte_range_.first_byte_position() >= resource_size_) { |
| 187 | // The caller should take care of this condition because we should have |
| 188 | // failed IsRequestedRangeOK(), but it's better to be consistent here. |
| 189 | len = 0; |
| 190 | } |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 191 | cached_min_len_ = len; |
| 192 | cached_start_ = current_range_start_; |
| 193 | } |
| 194 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 195 | if (cached_min_len_ < 0) |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 196 | return cached_min_len_; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 197 | |
| 198 | // Return a positive number to indicate success (versus error or finished). |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | void PartialData::PrepareCacheValidation(disk_cache::Entry* entry, |
| 203 | HttpRequestHeaders* headers) { |
| 204 | DCHECK_GE(current_range_start_, 0); |
| 205 | DCHECK_GE(cached_min_len_, 0); |
| 206 | |
| 207 | int len = GetNextRangeLen(); |
| 208 | DCHECK_NE(0, len); |
| 209 | range_present_ = false; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 210 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 211 | headers->CopyFrom(extra_headers_); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 212 | |
| 213 | if (!cached_min_len_) { |
| 214 | // We don't have anything else stored. |
| 215 | final_range_ = true; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 216 | cached_start_ = |
| 217 | byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (current_range_start_ == cached_start_) { |
| 221 | // The data lives in the cache. |
| 222 | range_present_ = true; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 223 | current_range_end_ = cached_start_ + cached_min_len_ - 1; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 224 | if (len == cached_min_len_) |
| 225 | final_range_ = true; |
tommycli@chromium.org | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 226 | headers->SetHeader( |
| 227 | HttpRequestHeaders::kRange, |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 228 | HttpByteRange::Bounded(current_range_start_, current_range_end_) |
| 229 | .GetHeaderValue()); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 230 | } else { |
| 231 | // This range is not in the cache. |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 232 | current_range_end_ = cached_start_ - 1; |
tommycli@chromium.org | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 233 | headers->SetHeader( |
| 234 | HttpRequestHeaders::kRange, |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 235 | HttpByteRange::Bounded(current_range_start_, current_range_end_) |
| 236 | .GetHeaderValue()); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 237 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool PartialData::IsCurrentRangeCached() const { |
| 241 | return range_present_; |
| 242 | } |
| 243 | |
| 244 | bool PartialData::IsLastRange() const { |
| 245 | return final_range_; |
| 246 | } |
| 247 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 248 | bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers, |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 249 | disk_cache::Entry* entry, |
| 250 | bool truncated) { |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 251 | resource_size_ = 0; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 252 | if (truncated) { |
| 253 | DCHECK_EQ(headers->response_code(), 200); |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 254 | // We don't have the real length and the user may be trying to create a |
| 255 | // sparse entry so let's not write to this entry. |
| 256 | if (byte_range_.IsValid()) |
rvargas@google.com | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 257 | return false; |
| 258 | |
| 259 | if (!headers->HasStrongValidators()) |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 260 | return false; |
| 261 | |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 262 | // Now we avoid resume if there is no content length, but that was not |
| 263 | // always the case so double check here. |
| 264 | int64 total_length = headers->GetContentLength(); |
rvargas@google.com | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 265 | if (total_length <= 0) |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 266 | return false; |
| 267 | |
hclam@chromium.org | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 268 | truncated_ = true; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 269 | initial_validation_ = true; |
hclam@chromium.org | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 270 | sparse_entry_ = false; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 271 | int current_len = entry->GetDataSize(kDataStream); |
| 272 | byte_range_.set_first_byte_position(current_len); |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 273 | resource_size_ = total_length; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 274 | current_range_start_ = current_len; |
| 275 | cached_min_len_ = current_len; |
| 276 | cached_start_ = current_len + 1; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
rvargas@google.com | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 280 | if (headers->response_code() != 206) { |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 281 | DCHECK(byte_range_.IsValid()); |
| 282 | sparse_entry_ = false; |
| 283 | resource_size_ = entry->GetDataSize(kDataStream); |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 284 | DVLOG(2) << "UpdateFromStoredHeaders size: " << resource_size_; |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 288 | if (!headers->HasStrongValidators()) |
| 289 | return false; |
| 290 | |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 291 | int64 length_value = headers->GetContentLength(); |
| 292 | if (length_value <= 0) |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 293 | return false; // We must have stored the resource length. |
| 294 | |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 295 | resource_size_ = length_value; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 296 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 297 | // Make sure that this is really a sparse entry. |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 298 | return entry->CouldBeSparse(); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 299 | } |
| 300 | |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 301 | void PartialData::SetRangeToStartDownload() { |
| 302 | DCHECK(truncated_); |
| 303 | DCHECK(!sparse_entry_); |
| 304 | current_range_start_ = 0; |
| 305 | cached_start_ = 0; |
| 306 | initial_validation_ = false; |
| 307 | } |
| 308 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 309 | bool PartialData::IsRequestedRangeOK() { |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 310 | if (byte_range_.IsValid()) { |
| 311 | if (!byte_range_.ComputeBounds(resource_size_)) |
| 312 | return false; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 313 | if (truncated_) |
| 314 | return true; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 315 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 316 | if (current_range_start_ < 0) |
| 317 | current_range_start_ = byte_range_.first_byte_position(); |
| 318 | } else { |
| 319 | // This is not a range request but we have partial data stored. |
| 320 | current_range_start_ = 0; |
| 321 | byte_range_.set_last_byte_position(resource_size_ - 1); |
| 322 | } |
| 323 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 324 | bool rv = current_range_start_ >= 0; |
| 325 | if (!rv) |
| 326 | current_range_start_ = 0; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 327 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 328 | return rv; |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) { |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 332 | if (headers->response_code() == 304) { |
hclam@chromium.org | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 333 | if (!byte_range_.IsValid() || truncated_) |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 334 | return true; |
| 335 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 336 | // We must have a complete range here. |
| 337 | return byte_range_.HasFirstBytePosition() && |
cbentzel@chromium.org | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 338 | byte_range_.HasLastBytePosition(); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 339 | } |
| 340 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 341 | int64 start, end, total_length; |
| 342 | if (!headers->GetContentRange(&start, &end, &total_length)) |
| 343 | return false; |
| 344 | if (total_length <= 0) |
| 345 | return false; |
| 346 | |
rvargas@chromium.org | 9f10ec3 | 2013-11-01 00:51:53 | [diff] [blame] | 347 | DCHECK_EQ(headers->response_code(), 206); |
| 348 | |
| 349 | // A server should return a valid content length with a 206 (per the standard) |
| 350 | // but relax the requirement because some servers don't do that. |
rvargas@google.com | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 351 | int64 content_length = headers->GetContentLength(); |
rvargas@chromium.org | 9f10ec3 | 2013-11-01 00:51:53 | [diff] [blame] | 352 | if (content_length > 0 && content_length != end - start + 1) |
rvargas@google.com | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 353 | return false; |
| 354 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 355 | if (!resource_size_) { |
| 356 | // First response. Update our values with the ones provided by the server. |
| 357 | resource_size_ = total_length; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 358 | if (!byte_range_.HasFirstBytePosition()) { |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 359 | byte_range_.set_first_byte_position(start); |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 360 | current_range_start_ = start; |
| 361 | } |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 362 | if (!byte_range_.HasLastBytePosition()) |
| 363 | byte_range_.set_last_byte_position(end); |
| 364 | } else if (resource_size_ != total_length) { |
| 365 | return false; |
| 366 | } |
| 367 | |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 368 | if (truncated_) { |
| 369 | if (!byte_range_.HasLastBytePosition()) |
| 370 | byte_range_.set_last_byte_position(end); |
| 371 | } |
| 372 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 373 | if (start != current_range_start_) |
| 374 | return false; |
| 375 | |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 376 | if (!current_range_end_) { |
| 377 | // There is nothing in the cache. |
| 378 | DCHECK(byte_range_.HasLastBytePosition()); |
| 379 | current_range_end_ = byte_range_.last_byte_position(); |
| 380 | if (current_range_end_ >= resource_size_) { |
| 381 | // We didn't know the real file size, and the server is saying that the |
| 382 | // requested range goes beyond the size. Fix it. |
| 383 | current_range_end_ = end; |
| 384 | byte_range_.set_last_byte_position(end); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // If we received a range, but it's not exactly the range we asked for, avoid |
| 389 | // trouble and signal an error. |
| 390 | if (end != current_range_end_) |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 391 | return false; |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | // We are making multiple requests to complete the range requested by the user. |
| 397 | // Just assume that everything is fine and say that we are returning what was |
| 398 | // requested. |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 399 | void PartialData::FixResponseHeaders(HttpResponseHeaders* headers, |
| 400 | bool success) { |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 401 | if (truncated_) |
| 402 | return; |
| 403 | |
kinuko@chromium.org | 3d47dac962 | 2014-03-10 07:28:58 | [diff] [blame] | 404 | if (byte_range_.IsValid() && success) { |
| 405 | headers->UpdateWithNewRange(byte_range_, resource_size_, !sparse_entry_); |
| 406 | return; |
| 407 | } |
| 408 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 409 | headers->RemoveHeader(kLengthHeader); |
| 410 | headers->RemoveHeader(kRangeHeader); |
| 411 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 412 | if (byte_range_.IsValid()) { |
kinuko@chromium.org | 3d47dac962 | 2014-03-10 07:28:58 | [diff] [blame] | 413 | headers->ReplaceStatusLine("HTTP/1.1 416 Requested Range Not Satisfiable"); |
| 414 | headers->AddHeader(base::StringPrintf("%s: bytes 0-0/%" PRId64, |
| 415 | kRangeHeader, resource_size_)); |
| 416 | headers->AddHeader(base::StringPrintf("%s: 0", kLengthHeader)); |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 417 | } else { |
| 418 | // TODO(rvargas): Is it safe to change the protocol version? |
| 419 | headers->ReplaceStatusLine("HTTP/1.1 200 OK"); |
| 420 | DCHECK_NE(resource_size_, 0); |
kinuko@chromium.org | 3d47dac962 | 2014-03-10 07:28:58 | [diff] [blame] | 421 | headers->AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader, |
| 422 | resource_size_)); |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 423 | } |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void PartialData::FixContentLength(HttpResponseHeaders* headers) { |
| 427 | headers->RemoveHeader(kLengthHeader); |
tfarina@chromium.org | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 428 | headers->AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader, |
| 429 | resource_size_)); |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 430 | } |
| 431 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 432 | int PartialData::CacheRead(disk_cache::Entry* entry, |
| 433 | IOBuffer* data, |
| 434 | int data_len, |
| 435 | const CompletionCallback& callback) { |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 436 | int read_len = std::min(data_len, cached_min_len_); |
hclam@chromium.org | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 437 | if (!read_len) |
| 438 | return 0; |
| 439 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 440 | int rv = 0; |
| 441 | if (sparse_entry_) { |
| 442 | rv = entry->ReadSparseData(current_range_start_, data, read_len, |
| 443 | callback); |
| 444 | } else { |
| 445 | if (current_range_start_ > kint32max) |
| 446 | return ERR_INVALID_ARGUMENT; |
| 447 | |
| 448 | rv = entry->ReadData(kDataStream, static_cast<int>(current_range_start_), |
| 449 | data, read_len, callback); |
| 450 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 451 | return rv; |
| 452 | } |
| 453 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 454 | int PartialData::CacheWrite(disk_cache::Entry* entry, |
| 455 | IOBuffer* data, |
| 456 | int data_len, |
| 457 | const CompletionCallback& callback) { |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 458 | DVLOG(3) << "To write: " << data_len; |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 459 | if (sparse_entry_) { |
jhawkins@chromium.org | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 460 | return entry->WriteSparseData( |
| 461 | current_range_start_, data, data_len, callback); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 462 | } else { |
| 463 | if (current_range_start_ > kint32max) |
| 464 | return ERR_INVALID_ARGUMENT; |
| 465 | |
| 466 | return entry->WriteData(kDataStream, static_cast<int>(current_range_start_), |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 467 | data, data_len, callback, true); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 468 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void PartialData::OnCacheReadCompleted(int result) { |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 472 | DVLOG(3) << "Read: " << result; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 473 | if (result > 0) { |
| 474 | current_range_start_ += result; |
| 475 | cached_min_len_ -= result; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 476 | DCHECK_GE(cached_min_len_, 0); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | void PartialData::OnNetworkReadCompleted(int result) { |
| 481 | if (result > 0) |
| 482 | current_range_start_ += result; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 483 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 484 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 485 | int PartialData::GetNextRangeLen() { |
| 486 | int64 range_len = |
| 487 | byte_range_.HasLastBytePosition() ? |
| 488 | byte_range_.last_byte_position() - current_range_start_ + 1 : |
| 489 | kint32max; |
| 490 | if (range_len > kint32max) |
| 491 | range_len = kint32max; |
| 492 | return static_cast<int32>(range_len); |
| 493 | } |
| 494 | |
| 495 | void PartialData::GetAvailableRangeCompleted(int result, int64 start) { |
jhawkins@chromium.org | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 496 | DCHECK(!callback_.is_null()); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 497 | DCHECK_NE(ERR_IO_PENDING, result); |
| 498 | |
| 499 | cached_start_ = start; |
| 500 | cached_min_len_ = result; |
| 501 | if (result >= 0) |
| 502 | result = 1; // Return success, go ahead and validate the entry. |
| 503 | |
jhawkins@chromium.org | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 504 | CompletionCallback cb = callback_; |
| 505 | callback_.Reset(); |
| 506 | cb.Run(result); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 507 | } |
| 508 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 509 | } // namespace net |