diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-18 23:27:36 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-18 23:27:36 +0000 |
commit | c85316f83dc5d24b593fc9d082375bfb7dc49260 (patch) | |
tree | 613350680ffea3ba7a9b066fa753b152b95b0f06 /net/http | |
parent | e1d16eb9348270bb806f9b9622b84d0df842e2e4 (diff) | |
download | chromium_src-c85316f83dc5d24b593fc9d082375bfb7dc49260.zip chromium_src-c85316f83dc5d24b593fc9d082375bfb7dc49260.tar.gz chromium_src-c85316f83dc5d24b593fc9d082375bfb7dc49260.tar.bz2 |
Http Cache: Detect Content_length mismatches and mark entries as
truncated when we do.
BUG=92944
TEST=net_unittests
Review URL: http://codereview.chromium.org/7650026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97390 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http')
-rw-r--r-- | net/http/http_cache_transaction.cc | 12 | ||||
-rw-r--r-- | net/http/http_cache_unittest.cc | 47 |
2 files changed, 55 insertions, 4 deletions
diff --git a/net/http/http_cache_transaction.cc b/net/http/http_cache_transaction.cc index 4b51cd5..9b3b6e8 100644 --- a/net/http/http_cache_transaction.cc +++ b/net/http/http_cache_transaction.cc @@ -1369,7 +1369,8 @@ int HttpCache::Transaction::DoCacheWriteDataComplete(int result) { result = write_len_; } else if (!done_reading_ && entry_) { int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); - if (response_.headers->GetContentLength() == current_size) + int64 body_size = response_.headers->GetContentLength(); + if (body_size >= 0 && body_size <= current_size) done_reading_ = true; } @@ -1380,8 +1381,13 @@ int HttpCache::Transaction::DoCacheWriteDataComplete(int result) { return DoPartialNetworkReadCompleted(result); } - if (result == 0) // End of file. - DoneWritingToEntry(true); + if (result == 0) { + // End of file. This may be the result of a connection problem so see if we + // have to keep the entry around to be flagged as truncated later on. + if (done_reading_ || !entry_ || partial_.get() || + response_.headers->GetContentLength() <= 0) + DoneWritingToEntry(true); + } return result; } diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc index 9fbd49e..dceabf8 100644 --- a/net/http/http_cache_unittest.cc +++ b/net/http/http_cache_unittest.cc @@ -5125,7 +5125,7 @@ TEST(HttpCache, FilterCompletion) { trans->DoneReading(); } - // Make sure that teh ActiveEntry is gone. + // Make sure that the ActiveEntry is gone. MessageLoop::current()->RunAllPending(); // Read from the cache. @@ -5135,3 +5135,48 @@ TEST(HttpCache, FilterCompletion) { EXPECT_EQ(1, cache.disk_cache()->open_count()); EXPECT_EQ(1, cache.disk_cache()->create_count()); } + +// Tests that we detect truncated rersources from the net when there is +// a Content-Length header. +TEST(HttpCache, TruncatedByContentLength) { + MockHttpCache cache; + TestCompletionCallback callback; + + MockTransaction transaction(kSimpleGET_Transaction); + AddMockTransaction(&transaction); + transaction.response_headers = "Cache-Control: max-age=10000\n" + "Content-Length: 100\n"; + RunTransactionTest(cache.http_cache(), transaction); + RemoveMockTransaction(&transaction); + + // Read from the cache. + RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); + + EXPECT_EQ(2, cache.network_layer()->transaction_count()); + EXPECT_EQ(0, cache.disk_cache()->open_count()); + EXPECT_EQ(2, cache.disk_cache()->create_count()); +} + +// Tests that we actually flag entries as truncated when we detect an error +// from the net. +TEST(HttpCache, TruncatedByContentLength2) { + MockHttpCache cache; + TestCompletionCallback callback; + + MockTransaction transaction(kSimpleGET_Transaction); + AddMockTransaction(&transaction); + transaction.response_headers = "Cache-Control: max-age=10000\n" + "Content-Length: 100\n" + "Etag: foo\n"; + RunTransactionTest(cache.http_cache(), transaction); + RemoveMockTransaction(&transaction); + + // Verify that the entry is marked as incomplete. + disk_cache::Entry* entry; + ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); + net::HttpResponseInfo response; + bool truncated = false; + EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); + EXPECT_TRUE(truncated); + entry->Close(); +} |