summaryrefslogtreecommitdiffstats
path: root/net/http/partial_data.cc
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-24 17:44:57 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-24 17:44:57 +0000
commit67fe45c45a76ab5f984ca6dfeb98945a668b97ae (patch)
tree95846805a9bf5d3480cb067de41219a69e99b2eb /net/http/partial_data.cc
parentb9eeba6713325a0fc67d6aeabc8475b5a2b37e0f (diff)
downloadchromium_src-67fe45c45a76ab5f984ca6dfeb98945a668b97ae.zip
chromium_src-67fe45c45a76ab5f984ca6dfeb98945a668b97ae.tar.gz
chromium_src-67fe45c45a76ab5f984ca6dfeb98945a668b97ae.tar.bz2
Http cache: Add support for ranges with an unknown start or end.
BUG=12258 TEST=unittest Review URL: http://codereview.chromium.org/147042 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19142 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http/partial_data.cc')
-rw-r--r--net/http/partial_data.cc50
1 files changed, 31 insertions, 19 deletions
diff --git a/net/http/partial_data.cc b/net/http/partial_data.cc
index 13cb377..8d56966 100644
--- a/net/http/partial_data.cc
+++ b/net/http/partial_data.cc
@@ -35,18 +35,16 @@ bool PartialData::Init(const std::string& headers,
extra_headers_ = new_headers;
resource_size_ = 0;
- // TODO(rvargas): Handle requests without explicit start or end.
- DCHECK(byte_range_.HasFirstBytePosition());
current_range_start_ = byte_range_.first_byte_position();
return true;
}
void PartialData::RestoreHeaders(std::string* headers) const {
- DCHECK(current_range_start_ >= 0);
+ DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange());
+ int64 end = byte_range_.IsSuffixByteRange() ?
+ byte_range_.suffix_length() : byte_range_.last_byte_position();
- // TODO(rvargas): Handle requests without explicit start or end.
- AddRangeHeader(current_range_start_, byte_range_.last_byte_position(),
- headers);
+ AddRangeHeader(current_range_start_, end, headers);
}
int PartialData::PrepareCacheValidation(disk_cache::Entry* entry,
@@ -102,18 +100,22 @@ bool PartialData::IsLastRange() const {
return final_range_;
}
-void PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers) {
+bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers) {
std::string length_value;
- if (!headers->GetNormalizedHeader(kLengthHeader, &length_value)) {
- // We must have stored the resource length.
- NOTREACHED();
- resource_size_ = 0;
- return;
- }
- if (!StringToInt64(length_value, &resource_size_)) {
- NOTREACHED();
- resource_size_ = 0;
- }
+ resource_size_ = 0;
+ if (!headers->GetNormalizedHeader(kLengthHeader, &length_value))
+ return false; // We must have stored the resource length.
+
+ if (!StringToInt64(length_value, &resource_size_))
+ return false;
+
+ if (resource_size_ && !byte_range_.ComputeBounds(resource_size_))
+ return false;
+
+ if (current_range_start_ < 0)
+ current_range_start_ = byte_range_.first_byte_position();
+
+ return current_range_start_ >= 0;
}
bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) {
@@ -126,8 +128,10 @@ bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) {
if (!resource_size_) {
// First response. Update our values with the ones provided by the server.
resource_size_ = total_length;
- if (!byte_range_.HasFirstBytePosition())
+ if (!byte_range_.HasFirstBytePosition()) {
byte_range_.set_first_byte_position(start);
+ current_range_start_ = start;
+ }
if (!byte_range_.HasLastBytePosition())
byte_range_.set_last_byte_position(end);
} else if (resource_size_ != total_length) {
@@ -199,7 +203,15 @@ void PartialData::OnNetworkReadCompleted(int result) {
// Static.
void PartialData::AddRangeHeader(int64 start, int64 end, std::string* headers) {
- headers->append(StringPrintf("Range: bytes=%lld-%lld\r\n", start, end));
+ DCHECK(start >= 0 || end >= 0);
+ std::string my_start, my_end;
+ if (start >= 0)
+ my_start = Int64ToString(start);
+ if (end >= 0)
+ my_end = Int64ToString(end);
+
+ headers->append(StringPrintf("Range: bytes=%s-%s\r\n", my_start.c_str(),
+ my_end.c_str()));
}