[go: nahoru, domu]

blob: 7b3bc39af929aa391eadc9985634da82570695cd [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
rvargas@google.com8bf26f49a2009-06-12 17:35:502// 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
avid0181f32015-12-10 19:41:477#include <limits>
Bence Béky74838e52018-08-03 19:25:238#include <utility>
avid0181f32015-12-10 19:41:479
evan@chromium.org34b2b002009-11-20 06:53:2810#include "base/format_macros.h"
Avi Drissman41c4a412023-01-11 22:45:3711#include "base/functional/bind.h"
12#include "base/functional/callback_helpers.h"
rvargas@google.com8bf26f49a2009-06-12 17:35:5013#include "base/logging.h"
avi@chromium.org125ef482013-06-11 18:32:4714#include "base/strings/string_number_conversions.h"
15#include "base/strings/string_util.h"
16#include "base/strings/stringprintf.h"
rvargas@google.com8bf26f49a2009-06-12 17:35:5017#include "net/base/net_errors.h"
18#include "net/disk_cache/disk_cache.h"
rvargas@google.com95792eb12009-06-22 21:30:4019#include "net/http/http_response_headers.h"
Feifei Wanged3689f2022-05-29 23:35:3820#include "net/http/http_status_code.h"
rvargas@google.com8bf26f49a2009-06-12 17:35:5021#include "net/http/http_util.h"
22
willchan@chromium.org8c76ae22010-04-20 22:15:4323namespace net {
24
rvargas@google.com95792eb12009-06-22 21:30:4025namespace {
26
27// The headers that we have to process.
28const char kLengthHeader[] = "Content-Length";
29const char kRangeHeader[] = "Content-Range";
rvargas@google.come5dad132009-08-18 00:53:4130const int kDataStream = 1;
rvargas@google.com95792eb12009-06-22 21:30:4031
willchan@chromium.org8c76ae22010-04-20 22:15:4332} // namespace
rvargas@google.com8bf26f49a2009-06-12 17:35:5033
Tsuyoshi Horo11e321eb2022-06-07 07:56:4034PartialData::PartialData() = default;
erg@google.comb104b502010-10-18 20:21:3135
Chris Watkins7a41d3552017-12-01 02:13:2736PartialData::~PartialData() = default;
rvargas@google.com034740a2010-06-11 17:16:4837
willchan@chromium.org8c76ae22010-04-20 22:15:4338bool PartialData::Init(const HttpRequestHeaders& headers) {
39 std::string range_header;
Maks Orlovich70039f12018-11-07 18:52:5740 if (!headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) {
41 range_requested_ = false;
willchan@chromium.org8c76ae22010-04-20 22:15:4342 return false;
Maks Orlovich70039f12018-11-07 18:52:5743 }
44 range_requested_ = true;
willchan@chromium.org8c76ae22010-04-20 22:15:4345
rvargas@google.com8bf26f49a2009-06-12 17:35:5046 std::vector<HttpByteRange> ranges;
willchan@chromium.org8c76ae22010-04-20 22:15:4347 if (!HttpUtil::ParseRangeHeader(range_header, &ranges) || ranges.size() != 1)
rvargas@google.com8bf26f49a2009-06-12 17:35:5048 return false;
49
50 // We can handle this range request.
51 byte_range_ = ranges[0];
52 if (!byte_range_.IsValid())
53 return false;
54
rvargas@google.com8bf26f49a2009-06-12 17:35:5055 current_range_start_ = byte_range_.first_byte_position();
rvargas@google.coma5c9d982010-10-12 20:48:0256
57 DVLOG(1) << "Range start: " << current_range_start_ << " end: " <<
58 byte_range_.last_byte_position();
rvargas@google.com8bf26f49a2009-06-12 17:35:5059 return true;
60}
61
willchan@chromium.org8c76ae22010-04-20 22:15:4362void PartialData::SetHeaders(const HttpRequestHeaders& headers) {
63 DCHECK(extra_headers_.IsEmpty());
64 extra_headers_.CopyFrom(headers);
rvargas@google.come75e8af2009-11-03 00:04:2065}
66
willchan@chromium.org8c76ae22010-04-20 22:15:4367void PartialData::RestoreHeaders(HttpRequestHeaders* headers) const {
rvargas@google.com67fe45c2009-06-24 17:44:5768 DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange());
avid0181f32015-12-10 19:41:4769 int64_t end = byte_range_.IsSuffixByteRange()
70 ? byte_range_.suffix_length()
71 : byte_range_.last_byte_position();
rvargas@google.com8bf26f49a2009-06-12 17:35:5072
willchan@chromium.org8c76ae22010-04-20 22:15:4373 headers->CopyFrom(extra_headers_);
tommycli@chromium.orgb7572ea2013-11-26 20:16:3874 if (truncated_ || !byte_range_.IsValid())
75 return;
76
77 if (current_range_start_ < 0) {
78 headers->SetHeader(HttpRequestHeaders::kRange,
79 HttpByteRange::Suffix(end).GetHeaderValue());
80 } else {
81 headers->SetHeader(HttpRequestHeaders::kRange,
82 HttpByteRange::Bounded(
83 current_range_start_, end).GetHeaderValue());
84 }
rvargas@google.com8bf26f49a2009-06-12 17:35:5085}
86
rvargas@google.com034740a2010-06-11 17:16:4887int PartialData::ShouldValidateCache(disk_cache::Entry* entry,
Bence Békya4a50932018-08-10 13:39:4188 CompletionOnceCallback callback) {
rvargas@google.com034740a2010-06-11 17:16:4889 DCHECK_GE(current_range_start_, 0);
rvargas@google.com8bf26f49a2009-06-12 17:35:5090
91 // Scan the disk cache for the first cached portion within this range.
rvargas@google.com034740a2010-06-11 17:16:4892 int len = GetNextRangeLen();
rvargas@google.com8bf26f49a2009-06-12 17:35:5093 if (!len)
94 return 0;
rvargas@google.com8bf26f49a2009-06-12 17:35:5095
rvargas@google.coma5c9d982010-10-12 20:48:0296 DVLOG(3) << "ShouldValidateCache len: " << len;
97
rvargas@google.come5dad132009-08-18 00:53:4198 if (sparse_entry_) {
jhawkins@chromium.org49639fa2011-12-20 23:22:4199 DCHECK(callback_.is_null());
Maks Orlovich04cd1ad2021-07-02 17:32:24100 disk_cache::RangeResultCallback cb = base::BindOnce(
101 &PartialData::GetAvailableRangeCompleted, weak_factory_.GetWeakPtr());
102 disk_cache::RangeResult range =
103 entry->GetAvailableRange(current_range_start_, len, std::move(cb));
rvargas@google.com034740a2010-06-11 17:16:48104
Maks Orlovich04cd1ad2021-07-02 17:32:24105 cached_min_len_ =
106 range.net_error == OK ? range.available_len : range.net_error;
rvargas@google.com034740a2010-06-11 17:16:48107 if (cached_min_len_ == ERR_IO_PENDING) {
Bence Békya4a50932018-08-10 13:39:41108 callback_ = std::move(callback);
rvargas@google.com034740a2010-06-11 17:16:48109 return ERR_IO_PENDING;
hubbe6cad67c2015-07-09 19:01:02110 } else {
Maks Orlovich04cd1ad2021-07-02 17:32:24111 cached_start_ = range.start;
rvargas@google.com034740a2010-06-11 17:16:48112 }
rvargas@google.com634739b2011-03-02 18:08:25113 } else if (!truncated_) {
rvargas@google.coma5c9d982010-10-12 20:48:02114 if (byte_range_.HasFirstBytePosition() &&
115 byte_range_.first_byte_position() >= resource_size_) {
116 // The caller should take care of this condition because we should have
117 // failed IsRequestedRangeOK(), but it's better to be consistent here.
118 len = 0;
119 }
rvargas@google.come5dad132009-08-18 00:53:41120 cached_min_len_ = len;
121 cached_start_ = current_range_start_;
122 }
123
rvargas@google.com034740a2010-06-11 17:16:48124 if (cached_min_len_ < 0)
rvargas@google.com8bf26f49a2009-06-12 17:35:50125 return cached_min_len_;
rvargas@google.com034740a2010-06-11 17:16:48126
127 // Return a positive number to indicate success (versus error or finished).
128 return 1;
129}
130
131void PartialData::PrepareCacheValidation(disk_cache::Entry* entry,
132 HttpRequestHeaders* headers) {
133 DCHECK_GE(current_range_start_, 0);
134 DCHECK_GE(cached_min_len_, 0);
135
136 int len = GetNextRangeLen();
137 DCHECK_NE(0, len);
138 range_present_ = false;
rvargas@google.com8bf26f49a2009-06-12 17:35:50139
willchan@chromium.org8c76ae22010-04-20 22:15:43140 headers->CopyFrom(extra_headers_);
rvargas@google.com8bf26f49a2009-06-12 17:35:50141
142 if (!cached_min_len_) {
143 // We don't have anything else stored.
144 final_range_ = true;
rvargas@google.com28accfe2009-09-04 23:36:33145 cached_start_ =
146 byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0;
rvargas@google.com8bf26f49a2009-06-12 17:35:50147 }
148
149 if (current_range_start_ == cached_start_) {
150 // The data lives in the cache.
151 range_present_ = true;
rvargas3b57e37a2015-01-06 00:56:34152 current_range_end_ = cached_start_ + cached_min_len_ - 1;
rvargas@google.com8bf26f49a2009-06-12 17:35:50153 if (len == cached_min_len_)
154 final_range_ = true;
rvargas@google.com8bf26f49a2009-06-12 17:35:50155 } else {
156 // This range is not in the cache.
rvargas3b57e37a2015-01-06 00:56:34157 current_range_end_ = cached_start_ - 1;
rvargas@google.com8bf26f49a2009-06-12 17:35:50158 }
hubbe70fbde6a2015-07-08 19:24:14159 headers->SetHeader(
160 HttpRequestHeaders::kRange,
161 HttpByteRange::Bounded(current_range_start_, current_range_end_)
162 .GetHeaderValue());
rvargas@google.com8bf26f49a2009-06-12 17:35:50163}
164
165bool PartialData::IsCurrentRangeCached() const {
166 return range_present_;
167}
168
169bool PartialData::IsLastRange() const {
170 return final_range_;
171}
172
rvargas@google.com44f873a62009-08-12 00:14:48173bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers,
rvargas@google.com28accfe2009-09-04 23:36:33174 disk_cache::Entry* entry,
Maks Orlovichd19c8272018-03-23 15:27:05175 bool truncated,
176 bool writing_in_progress) {
rvargas@google.com67fe45c2009-06-24 17:44:57177 resource_size_ = 0;
rvargas@google.com28accfe2009-09-04 23:36:33178 if (truncated) {
179 DCHECK_EQ(headers->response_code(), 200);
rvargas@google.com28accfe2009-09-04 23:36:33180 // We don't have the real length and the user may be trying to create a
181 // sparse entry so let's not write to this entry.
182 if (byte_range_.IsValid())
183 return false;
184
rvargas@google.com9f03cb7a2012-07-30 23:15:20185 if (!headers->HasStrongValidators())
186 return false;
187
rvargas@google.comdbd39fb2010-01-08 01:13:36188 // Now we avoid resume if there is no content length, but that was not
189 // always the case so double check here.
avid0181f32015-12-10 19:41:47190 int64_t total_length = headers->GetContentLength();
rvargas@google.combd069d72011-05-19 01:11:11191 if (total_length <= 0)
rvargas@google.comdbd39fb2010-01-08 01:13:36192 return false;
193
Maks Orlovich332bb11a2018-03-17 01:43:32194 // In case we see a truncated entry, we first send a network request for
195 // 1 byte range with If-Range: to probe server support for resumption.
196 // The setting of |current_range_start_| and |cached_start_| below (with any
197 // positive value of |cached_min_len_|) results in that.
198 //
199 // Setting |initial_validation_| to true is how this communicates to
200 // HttpCache::Transaction that we're doing that (and that it's not the user
201 // asking for one byte), so if it sees a 206 with that flag set it will call
202 // SetRangeToStartDownload(), and then restart the process looking for the
203 // entire file (which is what the user wanted), with the cache handling
204 // the previous portion, and then a second network request for the entire
205 // rest of the range. A 200 in response to the probe request can be simply
206 // returned directly to the user.
hclam@chromium.orgecd8becb2009-10-02 17:57:45207 truncated_ = true;
rvargas@google.com634739b2011-03-02 18:08:25208 initial_validation_ = true;
hclam@chromium.orgecd8becb2009-10-02 17:57:45209 sparse_entry_ = false;
rvargas@google.com634739b2011-03-02 18:08:25210 int current_len = entry->GetDataSize(kDataStream);
211 byte_range_.set_first_byte_position(current_len);
rvargas@google.comdbd39fb2010-01-08 01:13:36212 resource_size_ = total_length;
rvargas@google.com634739b2011-03-02 18:08:25213 current_range_start_ = current_len;
214 cached_min_len_ = current_len;
215 cached_start_ = current_len + 1;
rvargas@google.com28accfe2009-09-04 23:36:33216 return true;
217 }
218
Feifei Wanged3689f2022-05-29 23:35:38219 sparse_entry_ = (headers->response_code() == net::HTTP_PARTIAL_CONTENT);
Maks Orlovichd19c8272018-03-23 15:27:05220
221 if (writing_in_progress || sparse_entry_) {
222 // |writing_in_progress| means another Transaction is still fetching the
223 // body, so the only way we can see the length is if the server sent it
224 // in Content-Length -- GetDataSize would just return what got written
225 // thus far.
226 //
227 // |sparse_entry_| means a 206, and for those FixContentLength arranges it
228 // so that Content-Length written to the cache has the full length (on wire
229 // it's for a particular range only); while GetDataSize would be unusable
230 // since the data is stored using WriteSparseData, and not in the usual data
231 // stream.
232 resource_size_ = headers->GetContentLength();
233 if (resource_size_ <= 0)
234 return false;
235 } else {
236 // If we can safely use GetDataSize, it's preferrable since it's usable for
237 // things w/o Content-Length, such as chunked content.
rvargas@google.come5dad132009-08-18 00:53:41238 resource_size_ = entry->GetDataSize(kDataStream);
rvargas@google.come5dad132009-08-18 00:53:41239 }
240
Maks Orlovichd19c8272018-03-23 15:27:05241 DVLOG(2) << "UpdateFromStoredHeaders size: " << resource_size_;
rvargas43dc8fd2015-01-07 23:03:25242
Maks Orlovichd19c8272018-03-23 15:27:05243 if (sparse_entry_) {
244 // If our previous is a 206, we need strong validators as we may be
245 // stiching the cached data and network data together.
246 if (!headers->HasStrongValidators())
247 return false;
248 // Make sure that this is really a sparse entry.
249 return entry->CouldBeSparse();
250 }
251 return true;
rvargas@google.come5dad132009-08-18 00:53:41252}
253
rvargas@google.com634739b2011-03-02 18:08:25254void PartialData::SetRangeToStartDownload() {
255 DCHECK(truncated_);
256 DCHECK(!sparse_entry_);
257 current_range_start_ = 0;
258 cached_start_ = 0;
259 initial_validation_ = false;
260}
261
rvargas@google.come5dad132009-08-18 00:53:41262bool PartialData::IsRequestedRangeOK() {
rvargas@google.com44f873a62009-08-12 00:14:48263 if (byte_range_.IsValid()) {
264 if (!byte_range_.ComputeBounds(resource_size_))
265 return false;
rvargas@google.com634739b2011-03-02 18:08:25266 if (truncated_)
267 return true;
rvargas@google.com67fe45c2009-06-24 17:44:57268
rvargas@google.com44f873a62009-08-12 00:14:48269 if (current_range_start_ < 0)
270 current_range_start_ = byte_range_.first_byte_position();
271 } else {
272 // This is not a range request but we have partial data stored.
273 current_range_start_ = 0;
274 byte_range_.set_last_byte_position(resource_size_ - 1);
275 }
276
rvargas@google.come5dad132009-08-18 00:53:41277 bool rv = current_range_start_ >= 0;
278 if (!rv)
279 current_range_start_ = 0;
rvargas@google.com67fe45c2009-06-24 17:44:57280
rvargas@google.come5dad132009-08-18 00:53:41281 return rv;
rvargas@google.com95792eb12009-06-22 21:30:40282}
283
284bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) {
Feifei Wanged3689f2022-05-29 23:35:38285 if (headers->response_code() == net::HTTP_NOT_MODIFIED) {
hclam@chromium.orgd9adff2c2009-09-05 01:15:45286 if (!byte_range_.IsValid() || truncated_)
rvargas@google.com28accfe2009-09-04 23:36:33287 return true;
288
rvargas@google.come5dad132009-08-18 00:53:41289 // We must have a complete range here.
290 return byte_range_.HasFirstBytePosition() &&
cbentzel@chromium.org2227c692010-05-04 15:36:11291 byte_range_.HasLastBytePosition();
rvargas@google.come5dad132009-08-18 00:53:41292 }
293
avid0181f32015-12-10 19:41:47294 int64_t start, end, total_length;
sclittled43b2fb2016-12-17 03:20:47295 if (!headers->GetContentRangeFor206(&start, &end, &total_length))
rvargas@google.com95792eb12009-06-22 21:30:40296 return false;
297 if (total_length <= 0)
298 return false;
299
rvargas@chromium.org9f10ec32013-11-01 00:51:53300 DCHECK_EQ(headers->response_code(), 206);
301
302 // A server should return a valid content length with a 206 (per the standard)
303 // but relax the requirement because some servers don't do that.
avid0181f32015-12-10 19:41:47304 int64_t content_length = headers->GetContentLength();
rvargas@chromium.org9f10ec32013-11-01 00:51:53305 if (content_length > 0 && content_length != end - start + 1)
rvargas@google.com7eab0d2262009-10-14 22:05:54306 return false;
307
rvargas@google.com95792eb12009-06-22 21:30:40308 if (!resource_size_) {
309 // First response. Update our values with the ones provided by the server.
310 resource_size_ = total_length;
rvargas@google.com67fe45c2009-06-24 17:44:57311 if (!byte_range_.HasFirstBytePosition()) {
rvargas@google.com95792eb12009-06-22 21:30:40312 byte_range_.set_first_byte_position(start);
rvargas@google.com67fe45c2009-06-24 17:44:57313 current_range_start_ = start;
314 }
rvargas@google.com95792eb12009-06-22 21:30:40315 if (!byte_range_.HasLastBytePosition())
316 byte_range_.set_last_byte_position(end);
317 } else if (resource_size_ != total_length) {
318 return false;
319 }
320
rvargas@google.comdbd39fb2010-01-08 01:13:36321 if (truncated_) {
322 if (!byte_range_.HasLastBytePosition())
323 byte_range_.set_last_byte_position(end);
324 }
325
rvargas@google.com95792eb12009-06-22 21:30:40326 if (start != current_range_start_)
327 return false;
328
rvargas3b57e37a2015-01-06 00:56:34329 if (!current_range_end_) {
330 // There is nothing in the cache.
331 DCHECK(byte_range_.HasLastBytePosition());
332 current_range_end_ = byte_range_.last_byte_position();
333 if (current_range_end_ >= resource_size_) {
334 // We didn't know the real file size, and the server is saying that the
335 // requested range goes beyond the size. Fix it.
336 current_range_end_ = end;
337 byte_range_.set_last_byte_position(end);
338 }
339 }
340
341 // If we received a range, but it's not exactly the range we asked for, avoid
342 // trouble and signal an error.
343 if (end != current_range_end_)
rvargas@google.com95792eb12009-06-22 21:30:40344 return false;
345
346 return true;
347}
348
349// We are making multiple requests to complete the range requested by the user.
350// Just assume that everything is fine and say that we are returning what was
351// requested.
rvargas@google.coma5c9d982010-10-12 20:48:02352void PartialData::FixResponseHeaders(HttpResponseHeaders* headers,
353 bool success) {
rvargas@google.com28accfe2009-09-04 23:36:33354 if (truncated_)
355 return;
356
kinuko@chromium.org3d47dac9622014-03-10 07:28:58357 if (byte_range_.IsValid() && success) {
358 headers->UpdateWithNewRange(byte_range_, resource_size_, !sparse_entry_);
359 return;
360 }
361
rvargas@google.com44f873a62009-08-12 00:14:48362 if (byte_range_.IsValid()) {
kinuko@chromium.org3d47dac9622014-03-10 07:28:58363 headers->ReplaceStatusLine("HTTP/1.1 416 Requested Range Not Satisfiable");
Matt Menke73ed38f2020-04-10 21:29:11364 headers->SetHeader(
365 kRangeHeader, base::StringPrintf("bytes 0-0/%" PRId64, resource_size_));
366 headers->SetHeader(kLengthHeader, "0");
rvargas@google.com44f873a62009-08-12 00:14:48367 } else {
368 // TODO(rvargas): Is it safe to change the protocol version?
369 headers->ReplaceStatusLine("HTTP/1.1 200 OK");
370 DCHECK_NE(resource_size_, 0);
Matt Menke73ed38f2020-04-10 21:29:11371 headers->RemoveHeader(kRangeHeader);
372 headers->SetHeader(kLengthHeader,
373 base::StringPrintf("%" PRId64, resource_size_));
rvargas@google.com44f873a62009-08-12 00:14:48374 }
rvargas@google.com95792eb12009-06-22 21:30:40375}
376
377void PartialData::FixContentLength(HttpResponseHeaders* headers) {
Matt Menke73ed38f2020-04-10 21:29:11378 headers->SetHeader(kLengthHeader,
379 base::StringPrintf("%" PRId64, resource_size_));
rvargas@google.com95792eb12009-06-22 21:30:40380}
381
ttuttle859dc7a2015-04-23 19:42:29382int PartialData::CacheRead(disk_cache::Entry* entry,
383 IOBuffer* data,
384 int data_len,
Bence Békya4a50932018-08-10 13:39:41385 CompletionOnceCallback callback) {
rvargas@google.com8bf26f49a2009-06-12 17:35:50386 int read_len = std::min(data_len, cached_min_len_);
hclam@chromium.org8f28d632009-10-01 22:09:21387 if (!read_len)
388 return 0;
389
rvargas@google.come5dad132009-08-18 00:53:41390 int rv = 0;
391 if (sparse_entry_) {
392 rv = entry->ReadSparseData(current_range_start_, data, read_len,
Bence Békya4a50932018-08-10 13:39:41393 std::move(callback));
rvargas@google.come5dad132009-08-18 00:53:41394 } else {
avid0181f32015-12-10 19:41:47395 if (current_range_start_ > std::numeric_limits<int32_t>::max())
rvargas@google.come5dad132009-08-18 00:53:41396 return ERR_INVALID_ARGUMENT;
397
398 rv = entry->ReadData(kDataStream, static_cast<int>(current_range_start_),
Bence Békya4a50932018-08-10 13:39:41399 data, read_len, std::move(callback));
rvargas@google.come5dad132009-08-18 00:53:41400 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50401 return rv;
402}
403
ttuttle859dc7a2015-04-23 19:42:29404int PartialData::CacheWrite(disk_cache::Entry* entry,
405 IOBuffer* data,
406 int data_len,
Bence Békya4a50932018-08-10 13:39:41407 CompletionOnceCallback callback) {
rvargas@google.coma5c9d982010-10-12 20:48:02408 DVLOG(3) << "To write: " << data_len;
rvargas@google.come5dad132009-08-18 00:53:41409 if (sparse_entry_) {
Bence Békya4a50932018-08-10 13:39:41410 return entry->WriteSparseData(current_range_start_, data, data_len,
411 std::move(callback));
rvargas@google.come5dad132009-08-18 00:53:41412 } else {
avid0181f32015-12-10 19:41:47413 if (current_range_start_ > std::numeric_limits<int32_t>::max())
rvargas@google.come5dad132009-08-18 00:53:41414 return ERR_INVALID_ARGUMENT;
415
416 return entry->WriteData(kDataStream, static_cast<int>(current_range_start_),
Bence Békya4a50932018-08-10 13:39:41417 data, data_len, std::move(callback), true);
rvargas@google.come5dad132009-08-18 00:53:41418 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50419}
420
421void PartialData::OnCacheReadCompleted(int result) {
rvargas@google.coma5c9d982010-10-12 20:48:02422 DVLOG(3) << "Read: " << result;
rvargas@google.com8bf26f49a2009-06-12 17:35:50423 if (result > 0) {
424 current_range_start_ += result;
425 cached_min_len_ -= result;
rvargas@google.com034740a2010-06-11 17:16:48426 DCHECK_GE(cached_min_len_, 0);
rvargas@google.com8bf26f49a2009-06-12 17:35:50427 }
428}
429
430void PartialData::OnNetworkReadCompleted(int result) {
431 if (result > 0)
432 current_range_start_ += result;
433}
434
rvargas@google.com034740a2010-06-11 17:16:48435int PartialData::GetNextRangeLen() {
avid0181f32015-12-10 19:41:47436 int64_t range_len =
437 byte_range_.HasLastBytePosition()
438 ? byte_range_.last_byte_position() - current_range_start_ + 1
439 : std::numeric_limits<int32_t>::max();
440 if (range_len > std::numeric_limits<int32_t>::max())
441 range_len = std::numeric_limits<int32_t>::max();
442 return static_cast<int32_t>(range_len);
rvargas@google.com034740a2010-06-11 17:16:48443}
444
Maks Orlovich04cd1ad2021-07-02 17:32:24445void PartialData::GetAvailableRangeCompleted(
446 const disk_cache::RangeResult& result) {
jhawkins@chromium.org49639fa2011-12-20 23:22:41447 DCHECK(!callback_.is_null());
Maks Orlovich04cd1ad2021-07-02 17:32:24448 DCHECK_NE(ERR_IO_PENDING, result.net_error);
rvargas@google.com034740a2010-06-11 17:16:48449
Maks Orlovich04cd1ad2021-07-02 17:32:24450 int len_or_error =
451 result.net_error == OK ? result.available_len : result.net_error;
452 cached_start_ = result.start;
453 cached_min_len_ = len_or_error;
rvargas@google.com034740a2010-06-11 17:16:48454
Maks Orlovich04cd1ad2021-07-02 17:32:24455 // ShouldValidateCache has an unusual convention where 0 denotes EOF,
456 // so convert end of range to success (since there may be things that need
457 // fetching from network or other ranges).
458 std::move(callback_).Run(len_or_error >= 0 ? 1 : len_or_error);
rvargas@google.com034740a2010-06-11 17:16:48459}
460
rvargas@google.com8bf26f49a2009-06-12 17:35:50461} // namespace net