diff options
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(); +} |