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
3 // found in the LICENSE file.
5 #include "net/http/partial_data.h"
8 #include "base/bind_helpers.h"
9 #include "base/format_macros.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "net/base/net_errors.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/http/http_util.h"
23 // The headers that we have to process.
24 const char kLengthHeader
[] = "Content-Length";
25 const char kRangeHeader
[] = "Content-Range";
26 const int kDataStream
= 1;
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
{
34 // Build a new core object. Lifetime management is automatic.
35 static Core
* CreateCore(PartialData
* owner
) {
36 return new Core(owner
);
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
,
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.
50 explicit Core(PartialData
* owner
);
53 // Pending io completion routine.
54 void OnIOComplete(int result
);
59 DISALLOW_COPY_AND_ASSIGN(Core
);
62 PartialData::Core::Core(PartialData
* owner
)
63 : owner_(owner
), start_(0) {
64 DCHECK(!owner_
->core_
);
68 PartialData::Core::~Core() {
73 void PartialData::Core::Cancel() {
78 int PartialData::Core::GetAvailableRange(disk_cache::Entry
* entry
, int64 offset
,
79 int len
, int64
* start
) {
80 int rv
= entry
->GetAvailableRange(
81 offset
, len
, &start_
, base::Bind(&PartialData::Core::OnIOComplete
,
82 base::Unretained(this)));
83 if (rv
!= net::ERR_IO_PENDING
) {
84 // The callback will not be invoked. Lets cleanup.
91 void PartialData::Core::OnIOComplete(int result
) {
93 owner_
->GetAvailableRangeCompleted(result
, start_
);
97 // -----------------------------------------------------------------------------
99 PartialData::PartialData()
100 : range_present_(false),
104 initial_validation_(false),
108 PartialData::~PartialData() {
113 bool PartialData::Init(const HttpRequestHeaders
& headers
) {
114 std::string range_header
;
115 if (!headers
.GetHeader(HttpRequestHeaders::kRange
, &range_header
))
118 std::vector
<HttpByteRange
> ranges
;
119 if (!HttpUtil::ParseRangeHeader(range_header
, &ranges
) || ranges
.size() != 1)
122 // We can handle this range request.
123 byte_range_
= ranges
[0];
124 if (!byte_range_
.IsValid())
128 current_range_start_
= byte_range_
.first_byte_position();
130 DVLOG(1) << "Range start: " << current_range_start_
<< " end: " <<
131 byte_range_
.last_byte_position();
135 void PartialData::SetHeaders(const HttpRequestHeaders
& headers
) {
136 DCHECK(extra_headers_
.IsEmpty());
137 extra_headers_
.CopyFrom(headers
);
140 void PartialData::RestoreHeaders(HttpRequestHeaders
* headers
) const {
141 DCHECK(current_range_start_
>= 0 || byte_range_
.IsSuffixByteRange());
142 int64 end
= byte_range_
.IsSuffixByteRange() ?
143 byte_range_
.suffix_length() : byte_range_
.last_byte_position();
145 headers
->CopyFrom(extra_headers_
);
146 if (truncated_
|| !byte_range_
.IsValid())
149 if (current_range_start_
< 0) {
150 headers
->SetHeader(HttpRequestHeaders::kRange
,
151 HttpByteRange::Suffix(end
).GetHeaderValue());
153 headers
->SetHeader(HttpRequestHeaders::kRange
,
154 HttpByteRange::Bounded(
155 current_range_start_
, end
).GetHeaderValue());
159 int PartialData::ShouldValidateCache(disk_cache::Entry
* entry
,
160 const CompletionCallback
& callback
) {
161 DCHECK_GE(current_range_start_
, 0);
163 // Scan the disk cache for the first cached portion within this range.
164 int len
= GetNextRangeLen();
168 DVLOG(3) << "ShouldValidateCache len: " << len
;
171 DCHECK(callback_
.is_null());
172 Core
* core
= Core::CreateCore(this);
173 cached_min_len_
= core
->GetAvailableRange(entry
, current_range_start_
, len
,
176 if (cached_min_len_
== ERR_IO_PENDING
) {
177 callback_
= callback
;
178 return ERR_IO_PENDING
;
180 } else if (!truncated_
) {
181 if (byte_range_
.HasFirstBytePosition() &&
182 byte_range_
.first_byte_position() >= resource_size_
) {
183 // The caller should take care of this condition because we should have
184 // failed IsRequestedRangeOK(), but it's better to be consistent here.
187 cached_min_len_
= len
;
188 cached_start_
= current_range_start_
;
191 if (cached_min_len_
< 0)
192 return cached_min_len_
;
194 // Return a positive number to indicate success (versus error or finished).
198 void PartialData::PrepareCacheValidation(disk_cache::Entry
* entry
,
199 HttpRequestHeaders
* headers
) {
200 DCHECK_GE(current_range_start_
, 0);
201 DCHECK_GE(cached_min_len_
, 0);
203 int len
= GetNextRangeLen();
205 range_present_
= false;
207 headers
->CopyFrom(extra_headers_
);
209 if (!cached_min_len_
) {
210 // We don't have anything else stored.
213 byte_range_
.HasLastBytePosition() ? current_range_start_
+ len
: 0;
216 if (current_range_start_
== cached_start_
) {
217 // The data lives in the cache.
218 range_present_
= true;
219 if (len
== cached_min_len_
)
222 HttpRequestHeaders::kRange
,
223 net::HttpByteRange::Bounded(
224 current_range_start_
,
225 cached_start_
+ cached_min_len_
- 1).GetHeaderValue());
227 // This range is not in the cache.
229 HttpRequestHeaders::kRange
,
230 net::HttpByteRange::Bounded(
231 current_range_start_
, cached_start_
- 1).GetHeaderValue());
235 bool PartialData::IsCurrentRangeCached() const {
236 return range_present_
;
239 bool PartialData::IsLastRange() const {
243 bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders
* headers
,
244 disk_cache::Entry
* entry
,
248 DCHECK_EQ(headers
->response_code(), 200);
249 // We don't have the real length and the user may be trying to create a
250 // sparse entry so let's not write to this entry.
251 if (byte_range_
.IsValid())
254 if (!headers
->HasStrongValidators())
257 // Now we avoid resume if there is no content length, but that was not
258 // always the case so double check here.
259 int64 total_length
= headers
->GetContentLength();
260 if (total_length
<= 0)
264 initial_validation_
= true;
265 sparse_entry_
= false;
266 int current_len
= entry
->GetDataSize(kDataStream
);
267 byte_range_
.set_first_byte_position(current_len
);
268 resource_size_
= total_length
;
269 current_range_start_
= current_len
;
270 cached_min_len_
= current_len
;
271 cached_start_
= current_len
+ 1;
275 if (headers
->response_code() != 206) {
276 DCHECK(byte_range_
.IsValid());
277 sparse_entry_
= false;
278 resource_size_
= entry
->GetDataSize(kDataStream
);
279 DVLOG(2) << "UpdateFromStoredHeaders size: " << resource_size_
;
283 if (!headers
->HasStrongValidators())
286 int64 length_value
= headers
->GetContentLength();
287 if (length_value
<= 0)
288 return false; // We must have stored the resource length.
290 resource_size_
= length_value
;
292 // Make sure that this is really a sparse entry.
293 return entry
->CouldBeSparse();
296 void PartialData::SetRangeToStartDownload() {
298 DCHECK(!sparse_entry_
);
299 current_range_start_
= 0;
301 initial_validation_
= false;
304 bool PartialData::IsRequestedRangeOK() {
305 if (byte_range_
.IsValid()) {
306 if (!byte_range_
.ComputeBounds(resource_size_
))
311 if (current_range_start_
< 0)
312 current_range_start_
= byte_range_
.first_byte_position();
314 // This is not a range request but we have partial data stored.
315 current_range_start_
= 0;
316 byte_range_
.set_last_byte_position(resource_size_
- 1);
319 bool rv
= current_range_start_
>= 0;
321 current_range_start_
= 0;
326 bool PartialData::ResponseHeadersOK(const HttpResponseHeaders
* headers
) {
327 if (headers
->response_code() == 304) {
328 if (!byte_range_
.IsValid() || truncated_
)
331 // We must have a complete range here.
332 return byte_range_
.HasFirstBytePosition() &&
333 byte_range_
.HasLastBytePosition();
336 int64 start
, end
, total_length
;
337 if (!headers
->GetContentRange(&start
, &end
, &total_length
))
339 if (total_length
<= 0)
342 DCHECK_EQ(headers
->response_code(), 206);
344 // A server should return a valid content length with a 206 (per the standard)
345 // but relax the requirement because some servers don't do that.
346 int64 content_length
= headers
->GetContentLength();
347 if (content_length
> 0 && content_length
!= end
- start
+ 1)
350 if (!resource_size_
) {
351 // First response. Update our values with the ones provided by the server.
352 resource_size_
= total_length
;
353 if (!byte_range_
.HasFirstBytePosition()) {
354 byte_range_
.set_first_byte_position(start
);
355 current_range_start_
= start
;
357 if (!byte_range_
.HasLastBytePosition())
358 byte_range_
.set_last_byte_position(end
);
359 } else if (resource_size_
!= total_length
) {
364 if (!byte_range_
.HasLastBytePosition())
365 byte_range_
.set_last_byte_position(end
);
368 if (start
!= current_range_start_
)
371 if (byte_range_
.IsValid() && end
> byte_range_
.last_byte_position())
377 // We are making multiple requests to complete the range requested by the user.
378 // Just assume that everything is fine and say that we are returning what was
380 void PartialData::FixResponseHeaders(HttpResponseHeaders
* headers
,
385 if (byte_range_
.IsValid() && success
) {
386 headers
->UpdateWithNewRange(byte_range_
, resource_size_
, !sparse_entry_
);
390 headers
->RemoveHeader(kLengthHeader
);
391 headers
->RemoveHeader(kRangeHeader
);
393 if (byte_range_
.IsValid()) {
394 headers
->ReplaceStatusLine("HTTP/1.1 416 Requested Range Not Satisfiable");
395 headers
->AddHeader(base::StringPrintf("%s: bytes 0-0/%" PRId64
,
396 kRangeHeader
, resource_size_
));
397 headers
->AddHeader(base::StringPrintf("%s: 0", kLengthHeader
));
399 // TODO(rvargas): Is it safe to change the protocol version?
400 headers
->ReplaceStatusLine("HTTP/1.1 200 OK");
401 DCHECK_NE(resource_size_
, 0);
402 headers
->AddHeader(base::StringPrintf("%s: %" PRId64
, kLengthHeader
,
407 void PartialData::FixContentLength(HttpResponseHeaders
* headers
) {
408 headers
->RemoveHeader(kLengthHeader
);
409 headers
->AddHeader(base::StringPrintf("%s: %" PRId64
, kLengthHeader
,
413 int PartialData::CacheRead(
414 disk_cache::Entry
* entry
, IOBuffer
* data
, int data_len
,
415 const net::CompletionCallback
& callback
) {
416 int read_len
= std::min(data_len
, cached_min_len_
);
422 rv
= entry
->ReadSparseData(current_range_start_
, data
, read_len
,
425 if (current_range_start_
> kint32max
)
426 return ERR_INVALID_ARGUMENT
;
428 rv
= entry
->ReadData(kDataStream
, static_cast<int>(current_range_start_
),
429 data
, read_len
, callback
);
434 int PartialData::CacheWrite(
435 disk_cache::Entry
* entry
, IOBuffer
* data
, int data_len
,
436 const net::CompletionCallback
& callback
) {
437 DVLOG(3) << "To write: " << data_len
;
439 return entry
->WriteSparseData(
440 current_range_start_
, data
, data_len
, callback
);
442 if (current_range_start_
> kint32max
)
443 return ERR_INVALID_ARGUMENT
;
445 return entry
->WriteData(kDataStream
, static_cast<int>(current_range_start_
),
446 data
, data_len
, callback
, true);
450 void PartialData::OnCacheReadCompleted(int result
) {
451 DVLOG(3) << "Read: " << result
;
453 current_range_start_
+= result
;
454 cached_min_len_
-= result
;
455 DCHECK_GE(cached_min_len_
, 0);
459 void PartialData::OnNetworkReadCompleted(int result
) {
461 current_range_start_
+= result
;
464 int PartialData::GetNextRangeLen() {
466 byte_range_
.HasLastBytePosition() ?
467 byte_range_
.last_byte_position() - current_range_start_
+ 1 :
469 if (range_len
> kint32max
)
470 range_len
= kint32max
;
471 return static_cast<int32
>(range_len
);
474 void PartialData::GetAvailableRangeCompleted(int result
, int64 start
) {
475 DCHECK(!callback_
.is_null());
476 DCHECK_NE(ERR_IO_PENDING
, result
);
478 cached_start_
= start
;
479 cached_min_len_
= result
;
481 result
= 1; // Return success, go ahead and validate the entry.
483 CompletionCallback cb
= callback_
;