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); | |
yhirano
2014/08/26 07:52:01
This parameter is not specified in net_log_event_t
Adam Rice
2014/08/26 13:38:42
Done.
| |
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 = RequiresValidation(); |
2118 | |
2119 bool skip_validation = required_validation == VALIDATION_NONE; | |
2120 | |
2121 if (required_validation == VALIDATION_ASYNCHRONOUS) { | |
2122 if (cache_ && cache_->use_stale_while_revalidate()) { | |
2123 TriggerAsyncValidation(); | |
2124 skip_validation = true; | |
2125 } else { | |
2126 skip_validation = false; | |
yhirano
2014/08/26 07:52:01
We don't need this statement: skip_validation is a
Adam Rice
2014/08/26 13:38:42
Oh good, that makes the code cleaner.
| |
2127 } | |
2128 } | |
2097 | 2129 |
2098 if (request_->method == "HEAD" && | 2130 if (request_->method == "HEAD" && |
2099 (truncated_ || response_.headers->response_code() == 206)) { | 2131 (truncated_ || response_.headers->response_code() == 206)) { |
2100 DCHECK(!partial_); | 2132 DCHECK(!partial_); |
2101 if (skip_validation) | 2133 if (skip_validation) |
2102 return SetupEntryForRead(); | 2134 return SetupEntryForRead(); |
2103 | 2135 |
2104 // Bail out! | 2136 // Bail out! |
2105 next_state_ = STATE_SEND_REQUEST; | 2137 next_state_ = STATE_SEND_REQUEST; |
2106 mode_ = NONE; | 2138 mode_ = NONE; |
2107 return OK; | 2139 return OK; |
2108 } | 2140 } |
2109 | 2141 |
2110 if (truncated_) { | 2142 if (truncated_) { |
2111 // Truncated entries can cause partial gets, so we shouldn't record this | 2143 // Truncated entries can cause partial gets, so we shouldn't record this |
2112 // load in histograms. | 2144 // load in histograms. |
2113 UpdateTransactionPattern(PATTERN_NOT_COVERED); | 2145 UpdateTransactionPattern(PATTERN_NOT_COVERED); |
2114 skip_validation = !partial_->initial_validation(); | 2146 skip_validation = !partial_->initial_validation(); |
2115 } | 2147 } |
2116 | 2148 |
2117 if (partial_.get() && (is_sparse_ || truncated_) && | 2149 if (partial_.get() && (is_sparse_ || truncated_) && |
2118 (!partial_->IsCurrentRangeCached() || invalid_range_)) { | 2150 (!partial_->IsCurrentRangeCached() || invalid_range_)) { |
2119 // Force revalidation for sparse or truncated entries. Note that we don't | 2151 // 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 | 2152 // want to ignore the regular validation logic just because a byte range was |
2121 // part of the request. | 2153 // part of the request. |
2122 skip_validation = false; | 2154 skip_validation = false; |
2123 } | 2155 } |
2124 | 2156 |
2125 if (skip_validation) { | 2157 if (skip_validation) { |
2158 // TODO(ricea): Is this pattern okay for asynchronous revalidations? | |
2126 UpdateTransactionPattern(PATTERN_ENTRY_USED); | 2159 UpdateTransactionPattern(PATTERN_ENTRY_USED); |
2127 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE); | 2160 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE); |
2128 return SetupEntryForRead(); | 2161 return SetupEntryForRead(); |
2129 } else { | 2162 } else { |
2130 // Make the network request conditional, to see if we may reuse our cached | 2163 // 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. | 2164 // 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 | 2165 // Our mode remains READ_WRITE for a conditional request. Even if the |
2133 // conditionalization fails, we don't switch to WRITE mode until we | 2166 // 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 | 2167 // know we won't be falling back to using the cache entry in the |
2135 // LOAD_FROM_CACHE_IF_OFFLINE case. | 2168 // LOAD_FROM_CACHE_IF_OFFLINE case. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2255 DCHECK(network_trans_.get()); | 2288 DCHECK(network_trans_.get()); |
2256 DCHECK_EQ(STATE_NONE, next_state_); | 2289 DCHECK_EQ(STATE_NONE, next_state_); |
2257 | 2290 |
2258 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 2291 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
2259 int rv = network_trans_->RestartWithAuth(credentials, io_callback_); | 2292 int rv = network_trans_->RestartWithAuth(credentials, io_callback_); |
2260 if (rv != ERR_IO_PENDING) | 2293 if (rv != ERR_IO_PENDING) |
2261 return DoLoop(rv); | 2294 return DoLoop(rv); |
2262 return rv; | 2295 return rv; |
2263 } | 2296 } |
2264 | 2297 |
2265 bool HttpCache::Transaction::RequiresValidation() { | 2298 ValidationType HttpCache::Transaction::RequiresValidation() { |
yhirano
2014/08/26 07:52:01
This function should be renamed because its return
Adam Rice
2014/08/26 13:38:42
Done.
| |
2266 // TODO(darin): need to do more work here: | 2299 // TODO(darin): need to do more work here: |
2267 // - make sure we have a matching request method | 2300 // - make sure we have a matching request method |
2268 // - watch out for cached responses that depend on authentication | 2301 // - watch out for cached responses that depend on authentication |
2269 | 2302 |
2270 // In playback mode, nothing requires validation. | 2303 // In playback mode, nothing requires validation. |
2271 if (cache_->mode() == net::HttpCache::PLAYBACK) | 2304 if (cache_->mode() == net::HttpCache::PLAYBACK) |
2272 return false; | 2305 return VALIDATION_NONE; |
2273 | 2306 |
2274 if (response_.vary_data.is_valid() && | 2307 if (response_.vary_data.is_valid() && |
2275 !response_.vary_data.MatchesRequest(*request_, | 2308 !response_.vary_data.MatchesRequest(*request_, |
2276 *response_.headers.get())) { | 2309 *response_.headers.get())) { |
2277 vary_mismatch_ = true; | 2310 vary_mismatch_ = true; |
2278 return true; | 2311 return VALIDATION_SYNCHRONOUS; |
2279 } | 2312 } |
2280 | 2313 |
2281 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) | 2314 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) |
2282 return false; | 2315 return VALIDATION_NONE; |
2283 | 2316 |
2284 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) | 2317 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) |
2285 return true; | 2318 return VALIDATION_SYNCHRONOUS; |
2286 | 2319 |
2287 if (request_->method == "PUT" || request_->method == "DELETE") | 2320 if (request_->method == "PUT" || request_->method == "DELETE") |
2288 return true; | 2321 return VALIDATION_SYNCHRONOUS; |
2289 | 2322 |
2290 if (response_.headers->RequiresValidation( | 2323 ValidationType validation_required_by_headers = |
2291 response_.request_time, response_.response_time, Time::Now())) { | 2324 response_.headers->RequiresValidation( |
2292 return true; | 2325 response_.request_time, response_.response_time, Time::Now()); |
2326 | |
2327 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) { | |
2328 // Asynchronous revalidation is only supported for GET and HEAD methods. | |
2329 if (request_->method != "GET" && request_->method != "HEAD") | |
2330 return VALIDATION_SYNCHRONOUS; | |
2331 | |
2332 // Asynchronous revalidation of GETs can only be used for completely cached | |
2333 // resources. | |
2334 if (request_->method == "GET" && (truncated_ || partial_)) | |
2335 return VALIDATION_SYNCHRONOUS; | |
2293 } | 2336 } |
2294 | 2337 |
2295 return false; | 2338 return validation_required_by_headers; |
2296 } | 2339 } |
2297 | 2340 |
2298 bool HttpCache::Transaction::ConditionalizeRequest() { | 2341 bool HttpCache::Transaction::ConditionalizeRequest() { |
2299 DCHECK(response_.headers.get()); | 2342 DCHECK(response_.headers.get()); |
2300 | 2343 |
2301 if (request_->method == "PUT" || request_->method == "DELETE") | 2344 if (request_->method == "PUT" || request_->method == "DELETE") |
2302 return false; | 2345 return false; |
2303 | 2346 |
2304 // This only makes sense for cached 200 or 206 responses. | 2347 // This only makes sense for cached 200 or 206 responses. |
2305 if (response_.headers->response_code() != 200 && | 2348 if (response_.headers->response_code() != 200 && |
(...skipping 28 matching lines...) Expand all Loading... | |
2334 } | 2377 } |
2335 DCHECK(custom_request_.get()); | 2378 DCHECK(custom_request_.get()); |
2336 | 2379 |
2337 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() && | 2380 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() && |
2338 !invalid_range_; | 2381 !invalid_range_; |
2339 | 2382 |
2340 if (!use_if_range) { | 2383 if (!use_if_range) { |
2341 // stale-while-revalidate is not useful when we only have a partial response | 2384 // stale-while-revalidate is not useful when we only have a partial response |
2342 // cached, so don't set the header in that case. | 2385 // cached, so don't set the header in that case. |
2343 TimeDelta stale_while_revalidate; | 2386 TimeDelta stale_while_revalidate; |
2387 TimeDelta max_age; | |
2344 if (response_.headers->GetStaleWhileRevalidateValue( | 2388 if (response_.headers->GetStaleWhileRevalidateValue( |
2345 &stale_while_revalidate) && | 2389 &stale_while_revalidate) && |
2390 response_.headers->GetFreshnessLifetime(response_.response_time, | |
2391 &max_age) != | |
2392 HttpResponseHeaders::NEVER_FRESH && | |
2346 stale_while_revalidate > TimeDelta()) { | 2393 stale_while_revalidate > TimeDelta()) { |
2347 TimeDelta max_age = | |
2348 response_.headers->GetFreshnessLifetime(response_.response_time); | |
2349 TimeDelta current_age = response_.headers->GetCurrentAge( | 2394 TimeDelta current_age = response_.headers->GetCurrentAge( |
2350 response_.request_time, response_.response_time, Time::Now()); | 2395 response_.request_time, response_.response_time, Time::Now()); |
2351 | 2396 |
2352 custom_request_->extra_headers.SetHeader( | 2397 custom_request_->extra_headers.SetHeader( |
2353 kFreshnessHeader, | 2398 kFreshnessHeader, |
2354 base::StringPrintf("max-age=%" PRId64 | 2399 base::StringPrintf("max-age=%" PRId64 |
2355 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64, | 2400 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64, |
2356 max_age.InSeconds(), | 2401 max_age.InSeconds(), |
2357 stale_while_revalidate.InSeconds(), | 2402 stale_while_revalidate.InSeconds(), |
2358 current_age.InSeconds())); | 2403 current_age.InSeconds())); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2516 } | 2561 } |
2517 | 2562 |
2518 void HttpCache::Transaction::FixHeadersForHead() { | 2563 void HttpCache::Transaction::FixHeadersForHead() { |
2519 if (response_.headers->response_code() == 206) { | 2564 if (response_.headers->response_code() == 206) { |
2520 response_.headers->RemoveHeader("Content-Length"); | 2565 response_.headers->RemoveHeader("Content-Length"); |
2521 response_.headers->RemoveHeader("Content-Range"); | 2566 response_.headers->RemoveHeader("Content-Range"); |
2522 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK"); | 2567 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK"); |
2523 } | 2568 } |
2524 } | 2569 } |
2525 | 2570 |
2571 void HttpCache::Transaction::TriggerAsyncValidation() { | |
2572 DCHECK(!request_->upload_data_stream); | |
2573 BoundNetLog async_revalidation_net_log( | |
2574 BoundNetLog::Make(net_log_.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION)); | |
2575 net_log_.AddEvent( | |
2576 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC, | |
2577 async_revalidation_net_log.source().ToEventParametersCallback()); | |
2578 async_revalidation_net_log.BeginEvent( | |
2579 NetLog::TYPE_ASYNC_REVALIDATION, | |
2580 base::Bind( | |
2581 &NetLogAsyncRevalidationInfoCallback, net_log_.source(), request_)); | |
2582 base::MessageLoop::current()->PostTask( | |
2583 FROM_HERE, | |
2584 base::Bind(&HttpCache::PerformAsyncValidation, | |
2585 cache_, | |
2586 *request_, | |
2587 async_revalidation_net_log)); | |
2588 } | |
2589 | |
2526 void HttpCache::Transaction::FailRangeRequest() { | 2590 void HttpCache::Transaction::FailRangeRequest() { |
2527 response_ = *new_response_; | 2591 response_ = *new_response_; |
2528 partial_->FixResponseHeaders(response_.headers.get(), false); | 2592 partial_->FixResponseHeaders(response_.headers.get(), false); |
2529 } | 2593 } |
2530 | 2594 |
2531 int HttpCache::Transaction::SetupEntryForRead() { | 2595 int HttpCache::Transaction::SetupEntryForRead() { |
2532 if (network_trans_) | 2596 if (network_trans_) |
2533 ResetNetworkTransaction(); | 2597 ResetNetworkTransaction(); |
2534 if (partial_.get()) { | 2598 if (partial_.get()) { |
2535 if (truncated_ || is_sparse_ || !invalid_range_) { | 2599 if (truncated_ || is_sparse_ || !invalid_range_) { |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2862 default: | 2926 default: |
2863 NOTREACHED(); | 2927 NOTREACHED(); |
2864 } | 2928 } |
2865 } | 2929 } |
2866 | 2930 |
2867 void HttpCache::Transaction::OnIOComplete(int result) { | 2931 void HttpCache::Transaction::OnIOComplete(int result) { |
2868 DoLoop(result); | 2932 DoLoop(result); |
2869 } | 2933 } |
2870 | 2934 |
2871 } // namespace net | 2935 } // namespace net |
OLD | NEW |