OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/string_util.h" | 9 #include "base/string_util.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 if (fail_requests_) | 120 if (fail_requests_) |
121 return net::ERR_CACHE_READ_FAILURE; | 121 return net::ERR_CACHE_READ_FAILURE; |
122 | 122 |
123 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 123 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
124 return net::ERR_FAILED; | 124 return net::ERR_FAILED; |
125 | 125 |
126 data_[index].resize(offset + buf_len); | 126 data_[index].resize(offset + buf_len); |
127 if (buf_len) | 127 if (buf_len) |
128 memcpy(&data_[index][offset], buf->data(), buf_len); | 128 memcpy(&data_[index][offset], buf->data(), buf_len); |
129 return buf_len; | 129 |
| 130 if (!callback || (test_mode_ & TEST_MODE_SYNC_CACHE_WRITE)) |
| 131 return buf_len; |
| 132 |
| 133 CallbackLater(callback, buf_len); |
| 134 return net::ERR_IO_PENDING; |
130 } | 135 } |
131 | 136 |
132 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 137 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
133 net::CompletionCallback* completion_callback) { | 138 net::CompletionCallback* completion_callback) { |
134 if (!sparse_) | 139 if (!sparse_) |
135 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 140 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
136 if (offset < 0) | 141 if (offset < 0) |
137 return net::ERR_FAILED; | 142 return net::ERR_FAILED; |
138 | 143 |
139 if (fail_requests_) | 144 if (fail_requests_) |
(...skipping 30 matching lines...) Expand all Loading... |
170 if (fail_requests_) | 175 if (fail_requests_) |
171 return net::ERR_CACHE_READ_FAILURE; | 176 return net::ERR_CACHE_READ_FAILURE; |
172 | 177 |
173 DCHECK(offset < kint32max); | 178 DCHECK(offset < kint32max); |
174 int real_offset = static_cast<int>(offset); | 179 int real_offset = static_cast<int>(offset); |
175 | 180 |
176 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) | 181 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) |
177 data_[1].resize(real_offset + buf_len); | 182 data_[1].resize(real_offset + buf_len); |
178 | 183 |
179 memcpy(&data_[1][real_offset], buf->data(), buf_len); | 184 memcpy(&data_[1][real_offset], buf->data(), buf_len); |
180 return buf_len; | 185 if (!completion_callback || (test_mode_ & TEST_MODE_SYNC_CACHE_WRITE)) |
| 186 return buf_len; |
| 187 |
| 188 CallbackLater(completion_callback, buf_len); |
| 189 return net::ERR_IO_PENDING; |
181 } | 190 } |
182 | 191 |
183 virtual int GetAvailableRange(int64 offset, int len, int64* start) { | 192 virtual int GetAvailableRange(int64 offset, int len, int64* start) { |
184 if (!sparse_) | 193 if (!sparse_) |
185 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 194 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
186 if (offset < 0) | 195 if (offset < 0) |
187 return net::ERR_FAILED; | 196 return net::ERR_FAILED; |
188 | 197 |
189 if (fail_requests_) | 198 if (fail_requests_) |
190 return net::ERR_CACHE_READ_FAILURE; | 199 return net::ERR_CACHE_READ_FAILURE; |
(...skipping 1771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1962 // Tests that we can cache range requests when the start or end is unknown. | 1971 // Tests that we can cache range requests when the start or end is unknown. |
1963 // We start with one request from a given point, followed by a suffix request. | 1972 // We start with one request from a given point, followed by a suffix request. |
1964 // We'll also verify that synchronous cache responses work as intended. | 1973 // We'll also verify that synchronous cache responses work as intended. |
1965 TEST(HttpCache, UnknownRangeGET_2) { | 1974 TEST(HttpCache, UnknownRangeGET_2) { |
1966 MockHttpCache cache; | 1975 MockHttpCache cache; |
1967 cache.http_cache()->set_enable_range_support(true); | 1976 cache.http_cache()->set_enable_range_support(true); |
1968 std::string headers; | 1977 std::string headers; |
1969 | 1978 |
1970 MockTransaction transaction(kRangeGET_TransactionOK); | 1979 MockTransaction transaction(kRangeGET_TransactionOK); |
1971 transaction.test_mode = TEST_MODE_SYNC_CACHE_START | | 1980 transaction.test_mode = TEST_MODE_SYNC_CACHE_START | |
1972 TEST_MODE_SYNC_CACHE_READ; | 1981 TEST_MODE_SYNC_CACHE_READ | |
| 1982 TEST_MODE_SYNC_CACHE_WRITE; |
1973 AddMockTransaction(&transaction); | 1983 AddMockTransaction(&transaction); |
1974 | 1984 |
1975 // Write to the cache (70-79). | 1985 // Write to the cache (70-79). |
1976 transaction.request_headers = "Range: bytes = 70-\r\n"; | 1986 transaction.request_headers = "Range: bytes = 70-\r\n"; |
1977 transaction.data = "rg: 70-79 "; | 1987 transaction.data = "rg: 70-79 "; |
1978 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 1988 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
1979 | 1989 |
1980 EXPECT_TRUE(Verify206Response(headers, 70, 79)); | 1990 EXPECT_TRUE(Verify206Response(headers, 70, 79)); |
1981 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1991 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1982 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1992 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2527 } | 2537 } |
2528 | 2538 |
2529 TEST(HttpCache, SyncRead) { | 2539 TEST(HttpCache, SyncRead) { |
2530 MockHttpCache cache; | 2540 MockHttpCache cache; |
2531 | 2541 |
2532 // This test ensures that a read that completes synchronously does not cause | 2542 // This test ensures that a read that completes synchronously does not cause |
2533 // any problems. | 2543 // any problems. |
2534 | 2544 |
2535 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 2545 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
2536 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | | 2546 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | |
2537 TEST_MODE_SYNC_CACHE_READ); | 2547 TEST_MODE_SYNC_CACHE_READ | |
| 2548 TEST_MODE_SYNC_CACHE_WRITE); |
2538 | 2549 |
2539 MockHttpRequest r1(transaction), | 2550 MockHttpRequest r1(transaction), |
2540 r2(transaction), | 2551 r2(transaction), |
2541 r3(transaction); | 2552 r3(transaction); |
2542 | 2553 |
2543 TestTransactionConsumer c1(cache.http_cache()), | 2554 TestTransactionConsumer c1(cache.http_cache()), |
2544 c2(cache.http_cache()), | 2555 c2(cache.http_cache()), |
2545 c3(cache.http_cache()); | 2556 c3(cache.http_cache()); |
2546 | 2557 |
2547 c1.Start(&r1, NULL); | 2558 c1.Start(&r1, NULL); |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2852 std::string headers; | 2863 std::string headers; |
2853 response.headers->GetNormalizedHeaders(&headers); | 2864 response.headers->GetNormalizedHeaders(&headers); |
2854 | 2865 |
2855 EXPECT_EQ("HTTP/1.1 200 OK\n" | 2866 EXPECT_EQ("HTTP/1.1 200 OK\n" |
2856 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 2867 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
2857 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 2868 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
2858 headers); | 2869 headers); |
2859 | 2870 |
2860 RemoveMockTransaction(&mock_network_response); | 2871 RemoveMockTransaction(&mock_network_response); |
2861 } | 2872 } |
OLD | NEW |