Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / http / partial_data.cc
bloba05c21897542f12f7677a12e1cebedbec6914eab
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"
7 #include "base/bind.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"
19 namespace net {
21 namespace {
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;
28 } // namespace
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);
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);
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();
49 private:
50 explicit Core(PartialData* owner);
51 ~Core();
53 // Pending io completion routine.
54 void OnIOComplete(int result);
56 PartialData* owner_;
57 int64 start_;
59 DISALLOW_COPY_AND_ASSIGN(Core);
62 PartialData::Core::Core(PartialData* owner)
63 : owner_(owner), start_(0) {
64 DCHECK(!owner_->core_);
65 owner_->core_ = this;
68 PartialData::Core::~Core() {
69 if (owner_)
70 owner_->core_ = NULL;
73 void PartialData::Core::Cancel() {
74 DCHECK(owner_);
75 owner_ = NULL;
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.
85 *start = start_;
86 delete this;
88 return rv;
91 void PartialData::Core::OnIOComplete(int result) {
92 if (owner_)
93 owner_->GetAvailableRangeCompleted(result, start_);
94 delete this;
97 // -----------------------------------------------------------------------------
99 PartialData::PartialData()
100 : range_present_(false),
101 final_range_(false),
102 sparse_entry_(true),
103 truncated_(false),
104 initial_validation_(false),
105 core_(NULL) {
108 PartialData::~PartialData() {
109 if (core_)
110 core_->Cancel();
113 bool PartialData::Init(const HttpRequestHeaders& headers) {
114 std::string range_header;
115 if (!headers.GetHeader(HttpRequestHeaders::kRange, &range_header))
116 return false;
118 std::vector<HttpByteRange> ranges;
119 if (!HttpUtil::ParseRangeHeader(range_header, &ranges) || ranges.size() != 1)
120 return false;
122 // We can handle this range request.
123 byte_range_ = ranges[0];
124 if (!byte_range_.IsValid())
125 return false;
127 resource_size_ = 0;
128 current_range_start_ = byte_range_.first_byte_position();
130 DVLOG(1) << "Range start: " << current_range_start_ << " end: " <<
131 byte_range_.last_byte_position();
132 return true;
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())
147 return;
149 if (current_range_start_ < 0) {
150 headers->SetHeader(HttpRequestHeaders::kRange,
151 HttpByteRange::Suffix(end).GetHeaderValue());
152 } else {
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();
165 if (!len)
166 return 0;
168 DVLOG(3) << "ShouldValidateCache len: " << len;
170 if (sparse_entry_) {
171 DCHECK(callback_.is_null());
172 Core* core = Core::CreateCore(this);
173 cached_min_len_ = core->GetAvailableRange(entry, current_range_start_, len,
174 &cached_start_);
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.
185 len = 0;
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).
195 return 1;
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();
204 DCHECK_NE(0, len);
205 range_present_ = false;
207 headers->CopyFrom(extra_headers_);
209 if (!cached_min_len_) {
210 // We don't have anything else stored.
211 final_range_ = true;
212 cached_start_ =
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_)
220 final_range_ = true;
221 headers->SetHeader(
222 HttpRequestHeaders::kRange,
223 net::HttpByteRange::Bounded(
224 current_range_start_,
225 cached_start_ + cached_min_len_ - 1).GetHeaderValue());
226 } else {
227 // This range is not in the cache.
228 headers->SetHeader(
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 {
240 return final_range_;
243 bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers,
244 disk_cache::Entry* entry,
245 bool truncated) {
246 resource_size_ = 0;
247 if (truncated) {
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())
252 return false;
254 if (!headers->HasStrongValidators())
255 return false;
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)
261 return false;
263 truncated_ = true;
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;
272 return true;
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_;
280 return true;
283 if (!headers->HasStrongValidators())
284 return false;
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() {
297 DCHECK(truncated_);
298 DCHECK(!sparse_entry_);
299 current_range_start_ = 0;
300 cached_start_ = 0;
301 initial_validation_ = false;
304 bool PartialData::IsRequestedRangeOK() {
305 if (byte_range_.IsValid()) {
306 if (!byte_range_.ComputeBounds(resource_size_))
307 return false;
308 if (truncated_)
309 return true;
311 if (current_range_start_ < 0)
312 current_range_start_ = byte_range_.first_byte_position();
313 } else {
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;
320 if (!rv)
321 current_range_start_ = 0;
323 return rv;
326 bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) {
327 if (headers->response_code() == 304) {
328 if (!byte_range_.IsValid() || truncated_)
329 return true;
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))
338 return false;
339 if (total_length <= 0)
340 return false;
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)
348 return false;
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) {
360 return false;
363 if (truncated_) {
364 if (!byte_range_.HasLastBytePosition())
365 byte_range_.set_last_byte_position(end);
368 if (start != current_range_start_)
369 return false;
371 if (byte_range_.IsValid() && end > byte_range_.last_byte_position())
372 return false;
374 return true;
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
379 // requested.
380 void PartialData::FixResponseHeaders(HttpResponseHeaders* headers,
381 bool success) {
382 if (truncated_)
383 return;
385 if (byte_range_.IsValid() && success) {
386 headers->UpdateWithNewRange(byte_range_, resource_size_, !sparse_entry_);
387 return;
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));
398 } else {
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,
403 resource_size_));
407 void PartialData::FixContentLength(HttpResponseHeaders* headers) {
408 headers->RemoveHeader(kLengthHeader);
409 headers->AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader,
410 resource_size_));
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_);
417 if (!read_len)
418 return 0;
420 int rv = 0;
421 if (sparse_entry_) {
422 rv = entry->ReadSparseData(current_range_start_, data, read_len,
423 callback);
424 } else {
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);
431 return rv;
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;
438 if (sparse_entry_) {
439 return entry->WriteSparseData(
440 current_range_start_, data, data_len, callback);
441 } else {
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;
452 if (result > 0) {
453 current_range_start_ += result;
454 cached_min_len_ -= result;
455 DCHECK_GE(cached_min_len_, 0);
459 void PartialData::OnNetworkReadCompleted(int result) {
460 if (result > 0)
461 current_range_start_ += result;
464 int PartialData::GetNextRangeLen() {
465 int64 range_len =
466 byte_range_.HasLastBytePosition() ?
467 byte_range_.last_byte_position() - current_range_start_ + 1 :
468 kint32max;
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;
480 if (result >= 0)
481 result = 1; // Return success, go ahead and validate the entry.
483 CompletionCallback cb = callback_;
484 callback_.Reset();
485 cb.Run(result);
488 } // namespace net