| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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/http_cache.h" | 5 #include "net/http/http_cache.h" |
| 6 | 6 |
| 7 #include "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_vector.h" | 9 #include "base/scoped_vector.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 }; | 293 }; |
| 294 | 294 |
| 295 ~MockDiskEntry() {} | 295 ~MockDiskEntry() {} |
| 296 | 296 |
| 297 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 297 // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
| 298 // if the consumer called Close on the MockDiskEntry. We achieve that by | 298 // if the consumer called Close on the MockDiskEntry. We achieve that by |
| 299 // leveraging the fact that this class is reference counted. | 299 // leveraging the fact that this class is reference counted. |
| 300 void CallbackLater(net::CompletionCallback* callback, int result) { | 300 void CallbackLater(net::CompletionCallback* callback, int result) { |
| 301 if (ignore_callbacks_) | 301 if (ignore_callbacks_) |
| 302 return StoreAndDeliverCallbacks(true, this, callback, result); | 302 return StoreAndDeliverCallbacks(true, this, callback, result); |
| 303 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, | 303 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 304 &MockDiskEntry::RunCallback, callback, result)); | 304 this, &MockDiskEntry::RunCallback, callback, result)); |
| 305 } | 305 } |
| 306 void RunCallback(net::CompletionCallback* callback, int result) { | 306 void RunCallback(net::CompletionCallback* callback, int result) { |
| 307 if (busy_) { | 307 if (busy_) { |
| 308 // This is kind of hacky, but controlling the behavior of just this entry | 308 // This is kind of hacky, but controlling the behavior of just this entry |
| 309 // from a test is sort of complicated. What we really want to do is | 309 // from a test is sort of complicated. What we really want to do is |
| 310 // delay the delivery of a sparse IO operation a little more so that the | 310 // delay the delivery of a sparse IO operation a little more so that the |
| 311 // request start operation (async) will finish without seeing the end of | 311 // request start operation (async) will finish without seeing the end of |
| 312 // this operation (already posted to the message loop)... and without | 312 // this operation (already posted to the message loop)... and without |
| 313 // just delaying for n mS (which may cause trouble with slow bots). So | 313 // just delaying for n mS (which may cause trouble with slow bots). So |
| 314 // we re-post this operation (all async sparse IO operations will take two | 314 // we re-post this operation (all async sparse IO operations will take two |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 | 524 |
| 525 class CallbackRunner : public Task { | 525 class CallbackRunner : public Task { |
| 526 public: | 526 public: |
| 527 CallbackRunner(net::CompletionCallback* callback, int result) | 527 CallbackRunner(net::CompletionCallback* callback, int result) |
| 528 : callback_(callback), result_(result) {} | 528 : callback_(callback), result_(result) {} |
| 529 virtual void Run() { | 529 virtual void Run() { |
| 530 callback_->Run(result_); | 530 callback_->Run(result_); |
| 531 } | 531 } |
| 532 | 532 |
| 533 private: | 533 private: |
| 534 net::CompletionCallback* callback_; | 534 net::CompletionCallback* callback_; |
| 535 int result_; | 535 int result_; |
| 536 DISALLOW_COPY_AND_ASSIGN(CallbackRunner); | 536 DISALLOW_COPY_AND_ASSIGN(CallbackRunner); |
| 537 }; | 537 }; |
| 538 | 538 |
| 539 void CallbackLater(net::CompletionCallback* callback, int result) { | 539 void CallbackLater(net::CompletionCallback* callback, int result) { |
| 540 MessageLoop::current()->PostTask(FROM_HERE, | 540 MessageLoop::current()->PostTask(FROM_HERE, |
| 541 new CallbackRunner(callback, result)); | 541 new CallbackRunner(callback, result)); |
| 542 } | 542 } |
| 543 | 543 |
| 544 EntryMap entries_; | 544 EntryMap entries_; |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); | 744 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
| 745 | 745 |
| 746 if (not_modified_) { | 746 if (not_modified_) { |
| 747 response_status->assign("HTTP/1.1 304 Not Modified"); | 747 response_status->assign("HTTP/1.1 304 Not Modified"); |
| 748 return; | 748 return; |
| 749 } | 749 } |
| 750 | 750 |
| 751 std::vector<net::HttpByteRange> ranges; | 751 std::vector<net::HttpByteRange> ranges; |
| 752 std::string range_header; | 752 std::string range_header; |
| 753 if (!request->extra_headers.GetHeader( | 753 if (!request->extra_headers.GetHeader( |
| 754 net::HttpRequestHeaders::kRange, &range_header) || | 754 net::HttpRequestHeaders::kRange, &range_header) || |
| 755 !net::HttpUtil::ParseRangeHeader(range_header, &ranges) || | 755 !net::HttpUtil::ParseRangeHeader(range_header, &ranges) || |
| 756 ranges.size() != 1) | 756 ranges.size() != 1) |
| 757 return; | 757 return; |
| 758 // We can handle this range request. | 758 // We can handle this range request. |
| 759 net::HttpByteRange byte_range = ranges[0]; | 759 net::HttpByteRange byte_range = ranges[0]; |
| 760 if (byte_range.first_byte_position() > 79) { | 760 if (byte_range.first_byte_position() > 79) { |
| 761 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); | 761 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
| 762 return; | 762 return; |
| 763 } | 763 } |
| 764 | 764 |
| (...skipping 1373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2138 // Second network response for |kUrl|. | 2138 // Second network response for |kUrl|. |
| 2139 static const Response kNetResponse2 = { | 2139 static const Response kNetResponse2 = { |
| 2140 "HTTP/1.1 200 OK", | 2140 "HTTP/1.1 200 OK", |
| 2141 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 2141 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2142 "Etag: \"Foo2\"\n" | 2142 "Etag: \"Foo2\"\n" |
| 2143 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | 2143 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2144 "body2" | 2144 "body2" |
| 2145 }; | 2145 }; |
| 2146 | 2146 |
| 2147 const char* kExtraRequestHeaders = | 2147 const char* kExtraRequestHeaders = |
| 2148 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" | 2148 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" |
| 2149 "If-None-Match: \"Foo1\"\r\n"; | 2149 "If-None-Match: \"Foo1\"\r\n"; |
| 2150 | 2150 |
| 2151 ConditionalizedRequestUpdatesCacheHelper( | 2151 ConditionalizedRequestUpdatesCacheHelper( |
| 2152 kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders); | 2152 kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders); |
| 2153 } | 2153 } |
| 2154 | 2154 |
| 2155 // Test that doing an externally conditionalized request with both if-none-match | 2155 // Test that doing an externally conditionalized request with both if-none-match |
| 2156 // and if-modified-since does not update the cache with only one match. | 2156 // and if-modified-since does not update the cache with only one match. |
| 2157 TEST(HttpCache, ConditionalizedRequestUpdatesCache9) { | 2157 TEST(HttpCache, ConditionalizedRequestUpdatesCache9) { |
| 2158 static const Response kNetResponse1 = { | 2158 static const Response kNetResponse1 = { |
| 2159 "HTTP/1.1 200 OK", | 2159 "HTTP/1.1 200 OK", |
| 2160 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | 2160 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2161 "Etag: \"Foo1\"\n" | 2161 "Etag: \"Foo1\"\n" |
| 2162 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 2162 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2163 "body1" | 2163 "body1" |
| 2164 }; | 2164 }; |
| 2165 | 2165 |
| 2166 // Second network response for |kUrl|. | 2166 // Second network response for |kUrl|. |
| 2167 static const Response kNetResponse2 = { | 2167 static const Response kNetResponse2 = { |
| 2168 "HTTP/1.1 200 OK", | 2168 "HTTP/1.1 200 OK", |
| 2169 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 2169 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2170 "Etag: \"Foo2\"\n" | 2170 "Etag: \"Foo2\"\n" |
| 2171 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | 2171 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2172 "body2" | 2172 "body2" |
| 2173 }; | 2173 }; |
| 2174 | 2174 |
| 2175 // The etag doesn't match what we have stored. | 2175 // The etag doesn't match what we have stored. |
| 2176 const char* kExtraRequestHeaders = | 2176 const char* kExtraRequestHeaders = |
| 2177 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n" | 2177 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n" |
| 2178 "If-None-Match: \"Foo2\"\n"; | 2178 "If-None-Match: \"Foo2\"\n"; |
| 2179 | 2179 |
| 2180 ConditionalizedRequestUpdatesCacheHelper( | 2180 ConditionalizedRequestUpdatesCacheHelper( |
| 2181 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); | 2181 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2182 } | 2182 } |
| 2183 | 2183 |
| 2184 // Test that doing an externally conditionalized request with both if-none-match | 2184 // Test that doing an externally conditionalized request with both if-none-match |
| 2185 // and if-modified-since does not update the cache with only one match. | 2185 // and if-modified-since does not update the cache with only one match. |
| 2186 TEST(HttpCache, ConditionalizedRequestUpdatesCache10) { | 2186 TEST(HttpCache, ConditionalizedRequestUpdatesCache10) { |
| 2187 static const Response kNetResponse1 = { | 2187 static const Response kNetResponse1 = { |
| 2188 "HTTP/1.1 200 OK", | 2188 "HTTP/1.1 200 OK", |
| 2189 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | 2189 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2190 "Etag: \"Foo1\"\n" | 2190 "Etag: \"Foo1\"\n" |
| 2191 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 2191 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2192 "body1" | 2192 "body1" |
| 2193 }; | 2193 }; |
| 2194 | 2194 |
| 2195 // Second network response for |kUrl|. | 2195 // Second network response for |kUrl|. |
| 2196 static const Response kNetResponse2 = { | 2196 static const Response kNetResponse2 = { |
| 2197 "HTTP/1.1 200 OK", | 2197 "HTTP/1.1 200 OK", |
| 2198 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 2198 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2199 "Etag: \"Foo2\"\n" | 2199 "Etag: \"Foo2\"\n" |
| 2200 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | 2200 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2201 "body2" | 2201 "body2" |
| 2202 }; | 2202 }; |
| 2203 | 2203 |
| 2204 // The modification date doesn't match what we have stored. | 2204 // The modification date doesn't match what we have stored. |
| 2205 const char* kExtraRequestHeaders = | 2205 const char* kExtraRequestHeaders = |
| 2206 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\n" | 2206 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\n" |
| 2207 "If-None-Match: \"Foo1\"\n"; | 2207 "If-None-Match: \"Foo1\"\n"; |
| 2208 | 2208 |
| 2209 ConditionalizedRequestUpdatesCacheHelper( | 2209 ConditionalizedRequestUpdatesCacheHelper( |
| 2210 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); | 2210 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2211 } | 2211 } |
| 2212 | 2212 |
| 2213 // Test that doing an externally conditionalized request with two conflicting | 2213 // Test that doing an externally conditionalized request with two conflicting |
| 2214 // headers does not update the cache. | 2214 // headers does not update the cache. |
| 2215 TEST(HttpCache, ConditionalizedRequestUpdatesCache11) { | 2215 TEST(HttpCache, ConditionalizedRequestUpdatesCache11) { |
| 2216 static const Response kNetResponse1 = { | 2216 static const Response kNetResponse1 = { |
| 2217 "HTTP/1.1 200 OK", | 2217 "HTTP/1.1 200 OK", |
| 2218 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | 2218 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" |
| 2219 "Etag: \"Foo1\"\n" | 2219 "Etag: \"Foo1\"\n" |
| 2220 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 2220 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2221 "body1" | 2221 "body1" |
| 2222 }; | 2222 }; |
| 2223 | 2223 |
| 2224 // Second network response for |kUrl|. | 2224 // Second network response for |kUrl|. |
| 2225 static const Response kNetResponse2 = { | 2225 static const Response kNetResponse2 = { |
| 2226 "HTTP/1.1 200 OK", | 2226 "HTTP/1.1 200 OK", |
| 2227 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 2227 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2228 "Etag: \"Foo2\"\n" | 2228 "Etag: \"Foo2\"\n" |
| 2229 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | 2229 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", |
| 2230 "body2" | 2230 "body2" |
| 2231 }; | 2231 }; |
| 2232 | 2232 |
| 2233 // Two dates, the second matches what we have stored. | 2233 // Two dates, the second matches what we have stored. |
| 2234 const char* kExtraRequestHeaders = | 2234 const char* kExtraRequestHeaders = |
| 2235 "If-Modified-Since: Mon, 04 Feb 2008 22:38:21 GMT\n" | 2235 "If-Modified-Since: Mon, 04 Feb 2008 22:38:21 GMT\n" |
| 2236 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n"; | 2236 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\n"; |
| 2237 | 2237 |
| 2238 ConditionalizedRequestUpdatesCacheHelper( | 2238 ConditionalizedRequestUpdatesCacheHelper( |
| 2239 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); | 2239 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); |
| 2240 } | 2240 } |
| 2241 | 2241 |
| 2242 TEST(HttpCache, UrlContainingHash) { | 2242 TEST(HttpCache, UrlContainingHash) { |
| 2243 MockHttpCache cache; | 2243 MockHttpCache cache; |
| 2244 | 2244 |
| 2245 // Do a typical GET request -- should write an entry into our cache. | 2245 // Do a typical GET request -- should write an entry into our cache. |
| 2246 MockTransaction trans(kTypicalGET_Transaction); | 2246 MockTransaction trans(kTypicalGET_Transaction); |
| (...skipping 1631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3878 | 3878 |
| 3879 // This test ensures that a read that completes synchronously does not cause | 3879 // This test ensures that a read that completes synchronously does not cause |
| 3880 // any problems. | 3880 // any problems. |
| 3881 | 3881 |
| 3882 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 3882 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 3883 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | | 3883 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | |
| 3884 TEST_MODE_SYNC_CACHE_READ | | 3884 TEST_MODE_SYNC_CACHE_READ | |
| 3885 TEST_MODE_SYNC_CACHE_WRITE); | 3885 TEST_MODE_SYNC_CACHE_WRITE); |
| 3886 | 3886 |
| 3887 MockHttpRequest r1(transaction), | 3887 MockHttpRequest r1(transaction), |
| 3888 r2(transaction), | 3888 r2(transaction), |
| 3889 r3(transaction); | 3889 r3(transaction); |
| 3890 | 3890 |
| 3891 TestTransactionConsumer c1(cache.http_cache()), | 3891 TestTransactionConsumer c1(cache.http_cache()), |
| 3892 c2(cache.http_cache()), | 3892 c2(cache.http_cache()), |
| 3893 c3(cache.http_cache()); | 3893 c3(cache.http_cache()); |
| 3894 | 3894 |
| 3895 c1.Start(&r1, net::BoundNetLog()); | 3895 c1.Start(&r1, net::BoundNetLog()); |
| 3896 | 3896 |
| 3897 r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 3897 r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 3898 c2.Start(&r2, net::BoundNetLog()); | 3898 c2.Start(&r2, net::BoundNetLog()); |
| 3899 | 3899 |
| 3900 r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 3900 r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 3901 c3.Start(&r3, net::BoundNetLog()); | 3901 c3.Start(&r3, net::BoundNetLog()); |
| 3902 | 3902 |
| 3903 MessageLoop::current()->Run(); | 3903 MessageLoop::current()->Run(); |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4335 // Now return 200 when validating the entry so the metadata will be lost. | 4335 // Now return 200 when validating the entry so the metadata will be lost. |
| 4336 MockTransaction trans2(kTypicalGET_Transaction); | 4336 MockTransaction trans2(kTypicalGET_Transaction); |
| 4337 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4337 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
| 4338 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4338 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 4339 EXPECT_TRUE(response.metadata.get() == NULL); | 4339 EXPECT_TRUE(response.metadata.get() == NULL); |
| 4340 | 4340 |
| 4341 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4341 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 4342 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4342 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 4343 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4343 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 4344 } | 4344 } |
| OLD | NEW |