summaryrefslogtreecommitdiffstats
path: root/net/http
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-24 17:59:31 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-24 17:59:31 +0000
commit21f659de19e3ea874a0958372a311746a0e764c2 (patch)
treeed614625bb19dc6d981682b4af0e1462ccfb05e5 /net/http
parentee2eb89954c440c9249a3bf34e6960526e2f490b (diff)
downloadchromium_src-21f659de19e3ea874a0958372a311746a0e764c2.zip
chromium_src-21f659de19e3ea874a0958372a311746a0e764c2.tar.gz
chromium_src-21f659de19e3ea874a0958372a311746a0e764c2.tar.bz2
Http cache: Enable experimental support for byte range requests.
Requires --enable-byte-range-support BUG=12258 TEST=covered by unit tests. Review URL: http://codereview.chromium.org/173231 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24113 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http')
-rw-r--r--net/http/http_cache.cc45
-rw-r--r--net/http/http_cache.h5
-rw-r--r--net/http/http_cache_unittest.cc45
3 files changed, 56 insertions, 39 deletions
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index 4de8080..338a323 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -34,9 +34,6 @@
using base::Time;
-// Uncomment this to enable experimental byte-range support.
-// #define ENABLE_RANGE_SUPPORT
-
namespace net {
// disk cache entry data indices.
@@ -170,7 +167,7 @@ HttpCache::ActiveEntry::~ActiveEntry() {
class HttpCache::Transaction
: public HttpTransaction, public RevocableStore::Revocable {
public:
- explicit Transaction(HttpCache* cache)
+ Transaction(HttpCache* cache, bool enable_range_support)
: RevocableStore::Revocable(&cache->transactions_),
request_(NULL),
cache_(cache),
@@ -180,6 +177,7 @@ class HttpCache::Transaction
mode_(NONE),
reading_(false),
invalid_range_(false),
+ enable_range_support_(enable_range_support),
read_offset_(0),
effective_load_flags_(0),
final_upload_progress_(0),
@@ -386,6 +384,7 @@ class HttpCache::Transaction
Mode mode_;
bool reading_; // We are already reading.
bool invalid_range_; // We may bypass the cache for this request.
+ bool enable_range_support_;
scoped_refptr<IOBuffer> read_buf_;
int read_buf_len_;
int read_offset_;
@@ -777,12 +776,12 @@ void HttpCache::Transaction::SetRequest(LoadLog* load_log,
new_extra_headers.append(it.name_begin(), it.values_end());
new_extra_headers.append("\r\n");
} else {
-#ifdef ENABLE_RANGE_SUPPORT
- range_found = true;
-#else
- effective_load_flags_ |= LOAD_DISABLE_CACHE;
- continue;
-#endif
+ if (enable_range_support_) {
+ range_found = true;
+ } else {
+ effective_load_flags_ |= LOAD_DISABLE_CACHE;
+ continue;
+ }
}
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSpecialHeaders); ++i) {
if (HeaderMatches(it, kSpecialHeaders[i].search)) {
@@ -907,9 +906,8 @@ int HttpCache::Transaction::BeginPartialCacheValidation() {
if (response_.headers->response_code() != 206 && !partial_.get())
return BeginCacheValidation();
-#if !defined(ENABLE_RANGE_SUPPORT)
- return BeginCacheValidation();
-#endif
+ if (!enable_range_support_)
+ return BeginCacheValidation();
bool byte_range_requested = partial_.get() != NULL;
if (!byte_range_requested) {
@@ -1070,11 +1068,10 @@ bool HttpCache::Transaction::RequiresValidation() {
bool HttpCache::Transaction::ConditionalizeRequest() {
DCHECK(response_.headers);
-#if !defined(ENABLE_RANGE_SUPPORT)
- // This only makes sense for cached 200 responses.
- if (response_.headers->response_code() != 200)
+ if (!enable_range_support_ && response_.headers->response_code() != 200) {
+ // This only makes sense for cached 200 responses.
return false;
-#endif
+ }
// This only makes sense for cached 200 or 206 responses.
if (response_.headers->response_code() != 200 &&
@@ -1147,12 +1144,8 @@ bool HttpCache::Transaction::ConditionalizeRequest() {
// course, maybe we already returned the headers.
bool HttpCache::Transaction::ValidatePartialResponse(
const HttpResponseHeaders* headers) {
- int response_code = headers->response_code();
-#ifdef ENABLE_RANGE_SUPPORT
- bool partial_content = response_code == 206;
-#else
- bool partial_content = false;
-#endif
+ int response_code = headers->response_code();
+ bool partial_content = enable_range_support_ ? response_code == 206 : false;
if (invalid_range_) {
// We gave up trying to match this request with the stored data. If the
@@ -1546,6 +1539,7 @@ HttpCache::HttpCache(HostResolver* host_resolver,
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
in_memory_cache_(false),
deleted_(false),
+ enable_range_support_(false),
cache_size_(cache_size) {
}
@@ -1559,6 +1553,7 @@ HttpCache::HttpCache(HttpNetworkSession* session,
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
in_memory_cache_(false),
deleted_(false),
+ enable_range_support_(false),
cache_size_(cache_size) {
}
@@ -1573,6 +1568,7 @@ HttpCache::HttpCache(HostResolver* host_resolver,
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
in_memory_cache_(true),
deleted_(false),
+ enable_range_support_(false),
cache_size_(cache_size) {
}
@@ -1585,6 +1581,7 @@ HttpCache::HttpCache(HttpTransactionFactory* network_layer,
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
in_memory_cache_(false),
deleted_(false),
+ enable_range_support_(false),
cache_size_(0) {
}
@@ -1625,7 +1622,7 @@ HttpTransaction* HttpCache::CreateTransaction() {
disk_cache_dir_.clear(); // Reclaim memory.
}
}
- return new HttpCache::Transaction(this);
+ return new HttpCache::Transaction(this, enable_range_support_);
}
HttpCache* HttpCache::GetCache() {
diff --git a/net/http/http_cache.h b/net/http/http_cache.h
index 19c4331..af5c55e 100644
--- a/net/http/http_cache.h
+++ b/net/http/http_cache.h
@@ -116,6 +116,10 @@ class HttpCache : public HttpTransactionFactory {
// Close All Idle Sockets. This is for debugging.
void CloseIdleConnections();
+ void set_enable_range_support(bool value) {
+ enable_range_support_ = value;
+ }
+
private:
// Types --------------------------------------------------------------------
@@ -188,6 +192,7 @@ class HttpCache : public HttpTransactionFactory {
bool in_memory_cache_;
bool deleted_; // TODO(rvargas): remove this member. See bug 9952.
+ bool enable_range_support_;
int cache_size_;
typedef base::hash_map<std::string, int> PlaybackCacheMap;
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc
index 14299b0..90b0774 100644
--- a/net/http/http_cache_unittest.cc
+++ b/net/http/http_cache_unittest.cc
@@ -1639,6 +1639,7 @@ TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) {
TEST(HttpCache, RangeGET_SkipsCache) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
// Test that we skip the cache for range GET requests. Eventually, we will
// want to cache these, but we'll still have cases where skipping the cache
@@ -1670,6 +1671,7 @@ TEST(HttpCache, RangeGET_SkipsCache) {
// Tests that receiving 206 for a regular request is handled correctly.
TEST(HttpCache, GET_Crazy206) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
// Write to the cache.
MockTransaction transaction(kRangeGET_TransactionOK);
@@ -1686,15 +1688,16 @@ TEST(HttpCache, GET_Crazy206) {
RunTransactionTest(cache.http_cache(), transaction);
EXPECT_EQ(2, cache.network_layer()->transaction_count());
- EXPECT_EQ(1, cache.disk_cache()->open_count());
- EXPECT_EQ(1, cache.disk_cache()->create_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(2, cache.disk_cache()->create_count());
RemoveMockTransaction(&transaction);
}
// Tests that we can cache range requests and fetch random blocks from the
// cache and the network.
-TEST(HttpCache, DISABLED_RangeGET_OK) {
+TEST(HttpCache, RangeGET_OK) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
std::string headers;
@@ -1747,8 +1750,9 @@ TEST(HttpCache, DISABLED_RangeGET_OK) {
}
// Tests that we deal with 304s for range requests.
-TEST(HttpCache, DISABLED_RangeGET_304) {
+TEST(HttpCache, RangeGET_304) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
std::string headers;
@@ -1776,8 +1780,9 @@ TEST(HttpCache, DISABLED_RangeGET_304) {
}
// Tests that we deal with 206s when revalidating range requests.
-TEST(HttpCache, DISABLED_RangeGET_ModifiedResult) {
+TEST(HttpCache, RangeGET_ModifiedResult) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
std::string headers;
@@ -1812,8 +1817,9 @@ TEST(HttpCache, DISABLED_RangeGET_ModifiedResult) {
// Tests that we can cache range requests when the start or end is unknown.
// We start with one suffix request, followed by a request from a given point.
-TEST(HttpCache, DISABLED_UnknownRangeGET_1) {
+TEST(HttpCache, UnknownRangeGET_1) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
std::string headers;
@@ -1847,8 +1853,9 @@ TEST(HttpCache, DISABLED_UnknownRangeGET_1) {
// Tests that we can cache range requests when the start or end is unknown.
// We start with one request from a given point, followed by a suffix request.
// We'll also verify that synchronous cache responses work as intended.
-TEST(HttpCache, DISABLED_UnknownRangeGET_2) {
+TEST(HttpCache, UnknownRangeGET_2) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
std::string headers;
MockTransaction transaction(kRangeGET_TransactionOK);
@@ -1884,8 +1891,9 @@ TEST(HttpCache, DISABLED_UnknownRangeGET_2) {
// Tests that receiving Not Modified when asking for an open range doesn't mess
// up things.
-TEST(HttpCache, DISABLED_UnknownRangeGET_304) {
+TEST(HttpCache, UnknownRangeGET_304) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
std::string headers;
MockTransaction transaction(kRangeGET_TransactionOK);
@@ -1912,8 +1920,9 @@ TEST(HttpCache, DISABLED_UnknownRangeGET_304) {
}
// Tests that we can handle non-range requests when we have cached a range.
-TEST(HttpCache, DISABLED_GET_Previous206) {
+TEST(HttpCache, GET_Previous206) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
std::string headers;
@@ -1942,8 +1951,9 @@ TEST(HttpCache, DISABLED_GET_Previous206) {
}
// Tests that we can handle cached 206 responses that are not sparse.
-TEST(HttpCache, DISABLED_GET_Previous206_NotSparse) {
+TEST(HttpCache, GET_Previous206_NotSparse) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
// Create a disk cache entry that stores 206 headers while not being sparse.
disk_cache::Entry* entry;
@@ -1983,8 +1993,9 @@ TEST(HttpCache, DISABLED_GET_Previous206_NotSparse) {
// Tests that we can handle cached 206 responses that are not sparse. This time
// we issue a range request and expect to receive a range.
-TEST(HttpCache, DISABLED_RangeGET_Previous206_NotSparse_2) {
+TEST(HttpCache, RangeGET_Previous206_NotSparse_2) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
// Create a disk cache entry that stores 206 headers while not being sparse.
@@ -2023,8 +2034,9 @@ TEST(HttpCache, DISABLED_RangeGET_Previous206_NotSparse_2) {
}
// Tests that we can handle range requests with cached 200 responses.
-TEST(HttpCache, DISABLED_RangeGET_Previous200) {
+TEST(HttpCache, RangeGET_Previous200) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
// Store the whole thing with status 200.
MockTransaction transaction(kTypicalGET_Transaction);
@@ -2069,8 +2081,9 @@ TEST(HttpCache, DISABLED_RangeGET_Previous200) {
}
// Tests that we can handle a 200 response when dealing with sparse entries.
-TEST(HttpCache, DISABLED_RangeRequestResultsIn200) {
+TEST(HttpCache, RangeRequestResultsIn200) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
std::string headers;
@@ -2109,8 +2122,9 @@ TEST(HttpCache, DISABLED_RangeRequestResultsIn200) {
// Tests that a range request that falls outside of the size that we know about
// only deletes the entry if the resource has indeed changed.
-TEST(HttpCache, DISABLED_RangeGET_MoreThanCurrentSize) {
+TEST(HttpCache, RangeGET_MoreThanCurrentSize) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
std::string headers;
@@ -2142,8 +2156,9 @@ TEST(HttpCache, DISABLED_RangeGET_MoreThanCurrentSize) {
#ifdef NDEBUG
// This test hits a NOTREACHED so it is a release mode only test.
-TEST(HttpCache, DISABLED_RangeGET_OK_LoadOnlyFromCache) {
+TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) {
MockHttpCache cache;
+ cache.http_cache()->set_enable_range_support(true);
AddMockTransaction(&kRangeGET_TransactionOK);
// Write to the cache (40-49).