| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/disk_cache/disk_cache.h" | 13 #include "net/disk_cache/disk_cache.h" |
| 13 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_util.h" | 15 #include "net/http/http_util.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // The headers that we have to process. | 21 // The headers that we have to process. |
| 21 const char kLengthHeader[] = "Content-Length"; | 22 const char kLengthHeader[] = "Content-Length"; |
| 22 const char kRangeHeader[] = "Content-Range"; | 23 const char kRangeHeader[] = "Content-Range"; |
| 23 const int kDataStream = 1; | 24 const int kDataStream = 1; |
| 24 | 25 |
| 25 void AddRangeHeader(int64 start, int64 end, HttpRequestHeaders* headers) { | 26 void AddRangeHeader(int64 start, int64 end, HttpRequestHeaders* headers) { |
| 26 DCHECK(start >= 0 || end >= 0); | 27 DCHECK(start >= 0 || end >= 0); |
| 27 std::string my_start, my_end; | 28 std::string my_start, my_end; |
| 28 if (start >= 0) | 29 if (start >= 0) |
| 29 my_start = base::Int64ToString(start); | 30 my_start = base::Int64ToString(start); |
| 30 if (end >= 0) | 31 if (end >= 0) |
| 31 my_end = base::Int64ToString(end); | 32 my_end = base::Int64ToString(end); |
| 32 | 33 |
| 33 headers->SetHeader( | 34 headers->SetHeader( |
| 34 HttpRequestHeaders::kRange, | 35 HttpRequestHeaders::kRange, |
| 35 StringPrintf("bytes=%s-%s", my_start.c_str(), my_end.c_str())); | 36 base::StringPrintf("bytes=%s-%s", my_start.c_str(), my_end.c_str())); |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 // A core object that can be detached from the Partialdata object at destruction | 41 // A core object that can be detached from the Partialdata object at destruction |
| 41 // so that asynchronous operations cleanup can be performed. | 42 // so that asynchronous operations cleanup can be performed. |
| 42 class PartialData::Core { | 43 class PartialData::Core { |
| 43 public: | 44 public: |
| 44 // Build a new core object. Lifetime management is automatic. | 45 // Build a new core object. Lifetime management is automatic. |
| 45 static Core* CreateCore(PartialData* owner) { | 46 static Core* CreateCore(PartialData* owner) { |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 headers->RemoveHeader(kRangeHeader); | 343 headers->RemoveHeader(kRangeHeader); |
| 343 | 344 |
| 344 int64 range_len; | 345 int64 range_len; |
| 345 if (byte_range_.IsValid()) { | 346 if (byte_range_.IsValid()) { |
| 346 if (!sparse_entry_) | 347 if (!sparse_entry_) |
| 347 headers->ReplaceStatusLine("HTTP/1.1 206 Partial Content"); | 348 headers->ReplaceStatusLine("HTTP/1.1 206 Partial Content"); |
| 348 | 349 |
| 349 DCHECK(byte_range_.HasFirstBytePosition()); | 350 DCHECK(byte_range_.HasFirstBytePosition()); |
| 350 DCHECK(byte_range_.HasLastBytePosition()); | 351 DCHECK(byte_range_.HasLastBytePosition()); |
| 351 headers->AddHeader( | 352 headers->AddHeader( |
| 352 StringPrintf("%s: bytes %" PRId64 "-%" PRId64 "/%" PRId64, | 353 base::StringPrintf("%s: bytes %" PRId64 "-%" PRId64 "/%" PRId64, |
| 353 kRangeHeader, | 354 kRangeHeader, |
| 354 byte_range_.first_byte_position(), | 355 byte_range_.first_byte_position(), |
| 355 byte_range_.last_byte_position(), | 356 byte_range_.last_byte_position(), |
| 356 resource_size_)); | 357 resource_size_)); |
| 357 range_len = byte_range_.last_byte_position() - | 358 range_len = byte_range_.last_byte_position() - |
| 358 byte_range_.first_byte_position() + 1; | 359 byte_range_.first_byte_position() + 1; |
| 359 } else { | 360 } else { |
| 360 // TODO(rvargas): Is it safe to change the protocol version? | 361 // TODO(rvargas): Is it safe to change the protocol version? |
| 361 headers->ReplaceStatusLine("HTTP/1.1 200 OK"); | 362 headers->ReplaceStatusLine("HTTP/1.1 200 OK"); |
| 362 DCHECK_NE(resource_size_, 0); | 363 DCHECK_NE(resource_size_, 0); |
| 363 range_len = resource_size_; | 364 range_len = resource_size_; |
| 364 } | 365 } |
| 365 | 366 |
| 366 headers->AddHeader(StringPrintf("%s: %" PRId64, kLengthHeader, range_len)); | 367 headers->AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader, |
| 368 range_len)); |
| 367 } | 369 } |
| 368 | 370 |
| 369 void PartialData::FixContentLength(HttpResponseHeaders* headers) { | 371 void PartialData::FixContentLength(HttpResponseHeaders* headers) { |
| 370 headers->RemoveHeader(kLengthHeader); | 372 headers->RemoveHeader(kLengthHeader); |
| 371 headers->AddHeader(StringPrintf("%s: %" PRId64, kLengthHeader, | 373 headers->AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader, |
| 372 resource_size_)); | 374 resource_size_)); |
| 373 } | 375 } |
| 374 | 376 |
| 375 int PartialData::CacheRead(disk_cache::Entry* entry, IOBuffer* data, | 377 int PartialData::CacheRead(disk_cache::Entry* entry, IOBuffer* data, |
| 376 int data_len, CompletionCallback* callback) { | 378 int data_len, CompletionCallback* callback) { |
| 377 int read_len = std::min(data_len, cached_min_len_); | 379 int read_len = std::min(data_len, cached_min_len_); |
| 378 if (!read_len) | 380 if (!read_len) |
| 379 return 0; | 381 return 0; |
| 380 | 382 |
| 381 int rv = 0; | 383 int rv = 0; |
| 382 if (sparse_entry_) { | 384 if (sparse_entry_) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 cached_min_len_ = result; | 439 cached_min_len_ = result; |
| 438 if (result >= 0) | 440 if (result >= 0) |
| 439 result = 1; // Return success, go ahead and validate the entry. | 441 result = 1; // Return success, go ahead and validate the entry. |
| 440 | 442 |
| 441 CompletionCallback* cb = callback_; | 443 CompletionCallback* cb = callback_; |
| 442 callback_ = NULL; | 444 callback_ = NULL; |
| 443 cb->Run(result); | 445 cb->Run(result); |
| 444 } | 446 } |
| 445 | 447 |
| 446 } // namespace net | 448 } // namespace net |
| OLD | NEW |