OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_transaction.h" | 5 #include "net/http/http_cache_transaction.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <string> | 14 #include <string> |
15 | 15 |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
18 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" |
20 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
21 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
22 #include "base/metrics/sparse_histogram.h" | 23 #include "base/metrics/sparse_histogram.h" |
23 #include "base/rand_util.h" | 24 #include "base/rand_util.h" |
24 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
25 #include "base/strings/string_piece.h" | 26 #include "base/strings/string_piece.h" |
26 #include "base/strings/string_util.h" | 27 #include "base/strings/string_util.h" |
27 #include "base/strings/stringprintf.h" | 28 #include "base/strings/stringprintf.h" |
28 #include "base/time/time.h" | 29 #include "base/time/time.h" |
| 30 #include "base/values.h" |
29 #include "net/base/completion_callback.h" | 31 #include "net/base/completion_callback.h" |
30 #include "net/base/io_buffer.h" | 32 #include "net/base/io_buffer.h" |
31 #include "net/base/load_flags.h" | 33 #include "net/base/load_flags.h" |
32 #include "net/base/load_timing_info.h" | 34 #include "net/base/load_timing_info.h" |
33 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
34 #include "net/base/net_log.h" | 36 #include "net/base/net_log.h" |
35 #include "net/base/upload_data_stream.h" | 37 #include "net/base/upload_data_stream.h" |
36 #include "net/cert/cert_status_flags.h" | 38 #include "net/cert/cert_status_flags.h" |
37 #include "net/disk_cache/disk_cache.h" | 39 #include "net/disk_cache/disk_cache.h" |
38 #include "net/http/disk_based_cert_cache.h" | 40 #include "net/http/disk_based_cert_cache.h" |
39 #include "net/http/http_network_session.h" | 41 #include "net/http/http_network_session.h" |
40 #include "net/http/http_request_info.h" | 42 #include "net/http/http_request_info.h" |
41 #include "net/http/http_response_headers.h" | 43 #include "net/http/http_response_headers.h" |
42 #include "net/http/http_transaction.h" | 44 #include "net/http/http_transaction.h" |
43 #include "net/http/http_util.h" | 45 #include "net/http/http_util.h" |
44 #include "net/http/partial_data.h" | 46 #include "net/http/partial_data.h" |
45 #include "net/ssl/ssl_cert_request_info.h" | 47 #include "net/ssl/ssl_cert_request_info.h" |
46 #include "net/ssl/ssl_config_service.h" | 48 #include "net/ssl/ssl_config_service.h" |
47 | 49 |
48 using base::Time; | 50 using base::Time; |
49 using base::TimeDelta; | 51 using base::TimeDelta; |
50 using base::TimeTicks; | 52 using base::TimeTicks; |
51 | 53 |
52 namespace { | 54 namespace { |
53 | 55 |
| 56 typedef net::HttpResponseHeaders::ValidationType ValidationType; |
| 57 const ValidationType VALIDATION_NONE = ValidationType::VALIDATION_NONE; |
| 58 const ValidationType VALIDATION_SYNCHRONOUS = |
| 59 ValidationType::VALIDATION_SYNCHRONOUS; |
| 60 const ValidationType VALIDATION_ASYNCHRONOUS = |
| 61 ValidationType::VALIDATION_ASYNCHRONOUS; |
| 62 |
54 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised. | 63 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised. |
55 static const char kFreshnessHeader[] = "Resource-Freshness"; | 64 static const char kFreshnessHeader[] = "Resource-Freshness"; |
56 | 65 |
57 // Stores data relevant to the statistics of writing and reading entire | 66 // Stores data relevant to the statistics of writing and reading entire |
58 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number | 67 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number |
59 // of certificates in the chain that have pending operations in the | 68 // of certificates in the chain that have pending operations in the |
60 // DiskBasedCertCache. |start_time| is the time that the read and write | 69 // DiskBasedCertCache. |start_time| is the time that the read and write |
61 // commands began being issued to the DiskBasedCertCache. | 70 // commands began being issued to the DiskBasedCertCache. |
62 // TODO(brandonsalmon): Remove this when it is no longer necessary to | 71 // TODO(brandonsalmon): Remove this when it is no longer necessary to |
63 // collect data. | 72 // collect data. |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 | 240 |
232 void RecordNoStoreHeaderHistogram(int load_flags, | 241 void RecordNoStoreHeaderHistogram(int load_flags, |
233 const net::HttpResponseInfo* response) { | 242 const net::HttpResponseInfo* response) { |
234 if (load_flags & net::LOAD_MAIN_FRAME) { | 243 if (load_flags & net::LOAD_MAIN_FRAME) { |
235 UMA_HISTOGRAM_BOOLEAN( | 244 UMA_HISTOGRAM_BOOLEAN( |
236 "Net.MainFrameNoStore", | 245 "Net.MainFrameNoStore", |
237 response->headers->HasHeaderValue("cache-control", "no-store")); | 246 response->headers->HasHeaderValue("cache-control", "no-store")); |
238 } | 247 } |
239 } | 248 } |
240 | 249 |
| 250 base::Value* NetLogAsyncRevalidationInfoCallback( |
| 251 const net::NetLog::Source& source, |
| 252 const net::HttpRequestInfo* request, |
| 253 net::NetLog::LogLevel log_level) { |
| 254 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 255 source.AddToEventParameters(dict); |
| 256 |
| 257 dict->SetString("url", request->url.possibly_invalid_spec()); |
| 258 dict->SetString("method", request->method); |
| 259 return dict; |
| 260 } |
| 261 |
241 } // namespace | 262 } // namespace |
242 | 263 |
243 namespace net { | 264 namespace net { |
244 | 265 |
245 struct HeaderNameAndValue { | 266 struct HeaderNameAndValue { |
246 const char* name; | 267 const char* name; |
247 const char* value; | 268 const char* value; |
248 }; | 269 }; |
249 | 270 |
250 // If the request includes one of these request headers, then avoid caching | 271 // If the request includes one of these request headers, then avoid caching |
(...skipping 1835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2086 | 2107 |
2087 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) | 2108 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) |
2088 next_state_ = STATE_CACHE_READ_METADATA; | 2109 next_state_ = STATE_CACHE_READ_METADATA; |
2089 | 2110 |
2090 return OK; | 2111 return OK; |
2091 } | 2112 } |
2092 | 2113 |
2093 int HttpCache::Transaction::BeginCacheValidation() { | 2114 int HttpCache::Transaction::BeginCacheValidation() { |
2094 DCHECK(mode_ == READ_WRITE); | 2115 DCHECK(mode_ == READ_WRITE); |
2095 | 2116 |
2096 bool skip_validation = !RequiresValidation(); | 2117 ValidationType required_validation = RequiredValidationType(); |
| 2118 |
| 2119 bool skip_validation = required_validation == VALIDATION_NONE; |
| 2120 |
| 2121 if (required_validation == VALIDATION_ASYNCHRONOUS && cache_ && |
| 2122 cache_->use_stale_while_revalidate()) { |
| 2123 TriggerAsyncValidation(); |
| 2124 skip_validation = true; |
| 2125 } |
2097 | 2126 |
2098 if (request_->method == "HEAD" && | 2127 if (request_->method == "HEAD" && |
2099 (truncated_ || response_.headers->response_code() == 206)) { | 2128 (truncated_ || response_.headers->response_code() == 206)) { |
2100 DCHECK(!partial_); | 2129 DCHECK(!partial_); |
2101 if (skip_validation) | 2130 if (skip_validation) |
2102 return SetupEntryForRead(); | 2131 return SetupEntryForRead(); |
2103 | 2132 |
2104 // Bail out! | 2133 // Bail out! |
2105 next_state_ = STATE_SEND_REQUEST; | 2134 next_state_ = STATE_SEND_REQUEST; |
2106 mode_ = NONE; | 2135 mode_ = NONE; |
2107 return OK; | 2136 return OK; |
2108 } | 2137 } |
2109 | 2138 |
2110 if (truncated_) { | 2139 if (truncated_) { |
2111 // Truncated entries can cause partial gets, so we shouldn't record this | 2140 // Truncated entries can cause partial gets, so we shouldn't record this |
2112 // load in histograms. | 2141 // load in histograms. |
2113 UpdateTransactionPattern(PATTERN_NOT_COVERED); | 2142 UpdateTransactionPattern(PATTERN_NOT_COVERED); |
2114 skip_validation = !partial_->initial_validation(); | 2143 skip_validation = !partial_->initial_validation(); |
2115 } | 2144 } |
2116 | 2145 |
2117 if (partial_.get() && (is_sparse_ || truncated_) && | 2146 if (partial_.get() && (is_sparse_ || truncated_) && |
2118 (!partial_->IsCurrentRangeCached() || invalid_range_)) { | 2147 (!partial_->IsCurrentRangeCached() || invalid_range_)) { |
2119 // Force revalidation for sparse or truncated entries. Note that we don't | 2148 // Force revalidation for sparse or truncated entries. Note that we don't |
2120 // want to ignore the regular validation logic just because a byte range was | 2149 // want to ignore the regular validation logic just because a byte range was |
2121 // part of the request. | 2150 // part of the request. |
2122 skip_validation = false; | 2151 skip_validation = false; |
2123 } | 2152 } |
2124 | 2153 |
2125 if (skip_validation) { | 2154 if (skip_validation) { |
| 2155 // TODO(ricea): Is this pattern okay for asynchronous revalidations? |
2126 UpdateTransactionPattern(PATTERN_ENTRY_USED); | 2156 UpdateTransactionPattern(PATTERN_ENTRY_USED); |
2127 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE); | 2157 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE); |
2128 return SetupEntryForRead(); | 2158 return SetupEntryForRead(); |
2129 } else { | 2159 } else { |
2130 // Make the network request conditional, to see if we may reuse our cached | 2160 // Make the network request conditional, to see if we may reuse our cached |
2131 // response. If we cannot do so, then we just resort to a normal fetch. | 2161 // response. If we cannot do so, then we just resort to a normal fetch. |
2132 // Our mode remains READ_WRITE for a conditional request. Even if the | 2162 // Our mode remains READ_WRITE for a conditional request. Even if the |
2133 // conditionalization fails, we don't switch to WRITE mode until we | 2163 // conditionalization fails, we don't switch to WRITE mode until we |
2134 // know we won't be falling back to using the cache entry in the | 2164 // know we won't be falling back to using the cache entry in the |
2135 // LOAD_FROM_CACHE_IF_OFFLINE case. | 2165 // LOAD_FROM_CACHE_IF_OFFLINE case. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2255 DCHECK(network_trans_.get()); | 2285 DCHECK(network_trans_.get()); |
2256 DCHECK_EQ(STATE_NONE, next_state_); | 2286 DCHECK_EQ(STATE_NONE, next_state_); |
2257 | 2287 |
2258 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 2288 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
2259 int rv = network_trans_->RestartWithAuth(credentials, io_callback_); | 2289 int rv = network_trans_->RestartWithAuth(credentials, io_callback_); |
2260 if (rv != ERR_IO_PENDING) | 2290 if (rv != ERR_IO_PENDING) |
2261 return DoLoop(rv); | 2291 return DoLoop(rv); |
2262 return rv; | 2292 return rv; |
2263 } | 2293 } |
2264 | 2294 |
2265 bool HttpCache::Transaction::RequiresValidation() { | 2295 ValidationType HttpCache::Transaction::RequiredValidationType() { |
2266 // TODO(darin): need to do more work here: | 2296 // TODO(darin): need to do more work here: |
2267 // - make sure we have a matching request method | 2297 // - make sure we have a matching request method |
2268 // - watch out for cached responses that depend on authentication | 2298 // - watch out for cached responses that depend on authentication |
2269 | 2299 |
2270 // In playback mode, nothing requires validation. | 2300 // In playback mode, nothing requires validation. |
2271 if (cache_->mode() == net::HttpCache::PLAYBACK) | 2301 if (cache_->mode() == net::HttpCache::PLAYBACK) |
2272 return false; | 2302 return VALIDATION_NONE; |
2273 | 2303 |
2274 if (response_.vary_data.is_valid() && | 2304 if (response_.vary_data.is_valid() && |
2275 !response_.vary_data.MatchesRequest(*request_, | 2305 !response_.vary_data.MatchesRequest(*request_, |
2276 *response_.headers.get())) { | 2306 *response_.headers.get())) { |
2277 vary_mismatch_ = true; | 2307 vary_mismatch_ = true; |
2278 return true; | 2308 return VALIDATION_SYNCHRONOUS; |
2279 } | 2309 } |
2280 | 2310 |
2281 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) | 2311 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) |
2282 return false; | 2312 return VALIDATION_NONE; |
2283 | 2313 |
2284 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) | 2314 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) |
2285 return true; | 2315 return VALIDATION_SYNCHRONOUS; |
2286 | 2316 |
2287 if (request_->method == "PUT" || request_->method == "DELETE") | 2317 if (request_->method == "PUT" || request_->method == "DELETE") |
2288 return true; | 2318 return VALIDATION_SYNCHRONOUS; |
2289 | 2319 |
2290 if (response_.headers->RequiresValidation( | 2320 ValidationType validation_required_by_headers = |
2291 response_.request_time, response_.response_time, Time::Now())) { | 2321 response_.headers->RequiresValidation( |
2292 return true; | 2322 response_.request_time, response_.response_time, Time::Now()); |
| 2323 |
| 2324 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) { |
| 2325 // Asynchronous revalidation is only supported for GET and HEAD methods. |
| 2326 if (request_->method != "GET" && request_->method != "HEAD") |
| 2327 return VALIDATION_SYNCHRONOUS; |
| 2328 |
| 2329 // Asynchronous revalidation of GETs can only be used for completely cached |
| 2330 // resources. |
| 2331 if (request_->method == "GET" && (truncated_ || partial_)) |
| 2332 return VALIDATION_SYNCHRONOUS; |
2293 } | 2333 } |
2294 | 2334 |
2295 return false; | 2335 return validation_required_by_headers; |
2296 } | 2336 } |
2297 | 2337 |
2298 bool HttpCache::Transaction::ConditionalizeRequest() { | 2338 bool HttpCache::Transaction::ConditionalizeRequest() { |
2299 DCHECK(response_.headers.get()); | 2339 DCHECK(response_.headers.get()); |
2300 | 2340 |
2301 if (request_->method == "PUT" || request_->method == "DELETE") | 2341 if (request_->method == "PUT" || request_->method == "DELETE") |
2302 return false; | 2342 return false; |
2303 | 2343 |
2304 // This only makes sense for cached 200 or 206 responses. | 2344 // This only makes sense for cached 200 or 206 responses. |
2305 if (response_.headers->response_code() != 200 && | 2345 if (response_.headers->response_code() != 200 && |
(...skipping 28 matching lines...) Expand all Loading... |
2334 } | 2374 } |
2335 DCHECK(custom_request_.get()); | 2375 DCHECK(custom_request_.get()); |
2336 | 2376 |
2337 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() && | 2377 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() && |
2338 !invalid_range_; | 2378 !invalid_range_; |
2339 | 2379 |
2340 if (!use_if_range) { | 2380 if (!use_if_range) { |
2341 // stale-while-revalidate is not useful when we only have a partial response | 2381 // stale-while-revalidate is not useful when we only have a partial response |
2342 // cached, so don't set the header in that case. | 2382 // cached, so don't set the header in that case. |
2343 TimeDelta stale_while_revalidate; | 2383 TimeDelta stale_while_revalidate; |
| 2384 TimeDelta max_age; |
2344 if (response_.headers->GetStaleWhileRevalidateValue( | 2385 if (response_.headers->GetStaleWhileRevalidateValue( |
2345 &stale_while_revalidate) && | 2386 &stale_while_revalidate) && |
| 2387 response_.headers->GetFreshnessLifetime(response_.response_time, |
| 2388 &max_age) != |
| 2389 HttpResponseHeaders::NEVER_FRESH && |
2346 stale_while_revalidate > TimeDelta()) { | 2390 stale_while_revalidate > TimeDelta()) { |
2347 TimeDelta max_age = | |
2348 response_.headers->GetFreshnessLifetime(response_.response_time); | |
2349 TimeDelta current_age = response_.headers->GetCurrentAge( | 2391 TimeDelta current_age = response_.headers->GetCurrentAge( |
2350 response_.request_time, response_.response_time, Time::Now()); | 2392 response_.request_time, response_.response_time, Time::Now()); |
2351 | 2393 |
2352 custom_request_->extra_headers.SetHeader( | 2394 custom_request_->extra_headers.SetHeader( |
2353 kFreshnessHeader, | 2395 kFreshnessHeader, |
2354 base::StringPrintf("max-age=%" PRId64 | 2396 base::StringPrintf("max-age=%" PRId64 |
2355 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64, | 2397 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64, |
2356 max_age.InSeconds(), | 2398 max_age.InSeconds(), |
2357 stale_while_revalidate.InSeconds(), | 2399 stale_while_revalidate.InSeconds(), |
2358 current_age.InSeconds())); | 2400 current_age.InSeconds())); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2516 } | 2558 } |
2517 | 2559 |
2518 void HttpCache::Transaction::FixHeadersForHead() { | 2560 void HttpCache::Transaction::FixHeadersForHead() { |
2519 if (response_.headers->response_code() == 206) { | 2561 if (response_.headers->response_code() == 206) { |
2520 response_.headers->RemoveHeader("Content-Length"); | 2562 response_.headers->RemoveHeader("Content-Length"); |
2521 response_.headers->RemoveHeader("Content-Range"); | 2563 response_.headers->RemoveHeader("Content-Range"); |
2522 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK"); | 2564 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK"); |
2523 } | 2565 } |
2524 } | 2566 } |
2525 | 2567 |
| 2568 void HttpCache::Transaction::TriggerAsyncValidation() { |
| 2569 DCHECK(!request_->upload_data_stream); |
| 2570 BoundNetLog async_revalidation_net_log( |
| 2571 BoundNetLog::Make(net_log_.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION)); |
| 2572 net_log_.AddEvent( |
| 2573 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC, |
| 2574 async_revalidation_net_log.source().ToEventParametersCallback()); |
| 2575 async_revalidation_net_log.BeginEvent( |
| 2576 NetLog::TYPE_ASYNC_REVALIDATION, |
| 2577 base::Bind( |
| 2578 &NetLogAsyncRevalidationInfoCallback, net_log_.source(), request_)); |
| 2579 base::MessageLoop::current()->PostTask( |
| 2580 FROM_HERE, |
| 2581 base::Bind(&HttpCache::PerformAsyncValidation, |
| 2582 cache_, |
| 2583 *request_, |
| 2584 async_revalidation_net_log)); |
| 2585 } |
| 2586 |
2526 void HttpCache::Transaction::FailRangeRequest() { | 2587 void HttpCache::Transaction::FailRangeRequest() { |
2527 response_ = *new_response_; | 2588 response_ = *new_response_; |
2528 partial_->FixResponseHeaders(response_.headers.get(), false); | 2589 partial_->FixResponseHeaders(response_.headers.get(), false); |
2529 } | 2590 } |
2530 | 2591 |
2531 int HttpCache::Transaction::SetupEntryForRead() { | 2592 int HttpCache::Transaction::SetupEntryForRead() { |
2532 if (network_trans_) | 2593 if (network_trans_) |
2533 ResetNetworkTransaction(); | 2594 ResetNetworkTransaction(); |
2534 if (partial_.get()) { | 2595 if (partial_.get()) { |
2535 if (truncated_ || is_sparse_ || !invalid_range_) { | 2596 if (truncated_ || is_sparse_ || !invalid_range_) { |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2862 default: | 2923 default: |
2863 NOTREACHED(); | 2924 NOTREACHED(); |
2864 } | 2925 } |
2865 } | 2926 } |
2866 | 2927 |
2867 void HttpCache::Transaction::OnIOComplete(int result) { | 2928 void HttpCache::Transaction::OnIOComplete(int result) { |
2868 DoLoop(result); | 2929 DoLoop(result); |
2869 } | 2930 } |
2870 | 2931 |
2871 } // namespace net | 2932 } // namespace net |
OLD | NEW |