Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: net/http/http_cache_transaction.cc

Issue 391763002: Initial implementation of Chrome-Freshness header. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove const from directive_size. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/http/http_cache_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
19 #include "base/metrics/field_trial.h" 20 #include "base/metrics/field_trial.h"
20 #include "base/metrics/histogram.h" 21 #include "base/metrics/histogram.h"
21 #include "base/metrics/sparse_histogram.h" 22 #include "base/metrics/sparse_histogram.h"
22 #include "base/rand_util.h" 23 #include "base/rand_util.h"
23 #include "base/strings/string_number_conversions.h" 24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_piece.h"
24 #include "base/strings/string_util.h" 26 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h"
25 #include "base/time/time.h" 28 #include "base/time/time.h"
26 #include "net/base/completion_callback.h" 29 #include "net/base/completion_callback.h"
27 #include "net/base/io_buffer.h" 30 #include "net/base/io_buffer.h"
28 #include "net/base/load_flags.h" 31 #include "net/base/load_flags.h"
29 #include "net/base/load_timing_info.h" 32 #include "net/base/load_timing_info.h"
30 #include "net/base/net_errors.h" 33 #include "net/base/net_errors.h"
31 #include "net/base/net_log.h" 34 #include "net/base/net_log.h"
32 #include "net/base/upload_data_stream.h" 35 #include "net/base/upload_data_stream.h"
33 #include "net/cert/cert_status_flags.h" 36 #include "net/cert/cert_status_flags.h"
34 #include "net/disk_cache/disk_cache.h" 37 #include "net/disk_cache/disk_cache.h"
35 #include "net/http/disk_based_cert_cache.h" 38 #include "net/http/disk_based_cert_cache.h"
36 #include "net/http/http_network_session.h" 39 #include "net/http/http_network_session.h"
37 #include "net/http/http_request_info.h" 40 #include "net/http/http_request_info.h"
38 #include "net/http/http_response_headers.h" 41 #include "net/http/http_response_headers.h"
39 #include "net/http/http_transaction.h" 42 #include "net/http/http_transaction.h"
40 #include "net/http/http_util.h" 43 #include "net/http/http_util.h"
41 #include "net/http/partial_data.h" 44 #include "net/http/partial_data.h"
42 #include "net/ssl/ssl_cert_request_info.h" 45 #include "net/ssl/ssl_cert_request_info.h"
43 #include "net/ssl/ssl_config_service.h" 46 #include "net/ssl/ssl_config_service.h"
44 47
45 using base::Time; 48 using base::Time;
46 using base::TimeDelta; 49 using base::TimeDelta;
47 using base::TimeTicks; 50 using base::TimeTicks;
48 51
49 namespace { 52 namespace {
50 53
54 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
55 static const char kFreshnessHeader[] = "Chromium-Resource-Freshness";
56
51 // Stores data relevant to the statistics of writing and reading entire 57 // Stores data relevant to the statistics of writing and reading entire
52 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number 58 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
53 // of certificates in the chain that have pending operations in the 59 // of certificates in the chain that have pending operations in the
54 // DiskBasedCertCache. |start_time| is the time that the read and write 60 // DiskBasedCertCache. |start_time| is the time that the read and write
55 // commands began being issued to the DiskBasedCertCache. 61 // commands began being issued to the DiskBasedCertCache.
56 // TODO(brandonsalmon): Remove this when it is no longer necessary to 62 // TODO(brandonsalmon): Remove this when it is no longer necessary to
57 // collect data. 63 // collect data.
58 class SharedChainData : public base::RefCounted<SharedChainData> { 64 class SharedChainData : public base::RefCounted<SharedChainData> {
59 public: 65 public:
60 SharedChainData(int num_ops, TimeTicks start) 66 SharedChainData(int num_ops, TimeTicks start)
(...skipping 2189 matching lines...) Expand 10 before | Expand all | Expand 10 after
2250 if (!partial_.get()) { 2256 if (!partial_.get()) {
2251 // Need to customize the request, so this forces us to allocate :( 2257 // Need to customize the request, so this forces us to allocate :(
2252 custom_request_.reset(new HttpRequestInfo(*request_)); 2258 custom_request_.reset(new HttpRequestInfo(*request_));
2253 request_ = custom_request_.get(); 2259 request_ = custom_request_.get();
2254 } 2260 }
2255 DCHECK(custom_request_.get()); 2261 DCHECK(custom_request_.get());
2256 2262
2257 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() && 2263 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2258 !invalid_range_; 2264 !invalid_range_;
2259 2265
2266 if (!use_if_range) {
2267 // stale-while-revalidate is not useful when we only have a partial response
2268 // cached, so don't set the header in that case.
2269 TimeDelta stale_while_revalidate;
2270 if (response_.headers->GetStaleWhileRevalidateValue(
2271 &stale_while_revalidate) &&
2272 stale_while_revalidate > TimeDelta()) {
2273 TimeDelta max_age =
2274 response_.headers->GetFreshnessLifetime(response_.response_time);
2275 TimeDelta current_age = response_.headers->GetCurrentAge(
2276 response_.request_time, response_.response_time, Time::Now());
2277
2278 custom_request_->extra_headers.SetHeader(
2279 kFreshnessHeader,
2280 base::StringPrintf("max-age=%" PRId64
2281 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2282 max_age.InSeconds(),
2283 stale_while_revalidate.InSeconds(),
2284 current_age.InSeconds()));
2285 }
2286 }
2287
2260 if (!etag_value.empty()) { 2288 if (!etag_value.empty()) {
2261 if (use_if_range) { 2289 if (use_if_range) {
2262 // We don't want to switch to WRITE mode if we don't have this block of a 2290 // We don't want to switch to WRITE mode if we don't have this block of a
2263 // byte-range request because we may have other parts cached. 2291 // byte-range request because we may have other parts cached.
2264 custom_request_->extra_headers.SetHeader( 2292 custom_request_->extra_headers.SetHeader(
2265 HttpRequestHeaders::kIfRange, etag_value); 2293 HttpRequestHeaders::kIfRange, etag_value);
2266 } else { 2294 } else {
2267 custom_request_->extra_headers.SetHeader( 2295 custom_request_->extra_headers.SetHeader(
2268 HttpRequestHeaders::kIfNoneMatch, etag_value); 2296 HttpRequestHeaders::kIfNoneMatch, etag_value);
2269 } 2297 }
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
2746 default: 2774 default:
2747 NOTREACHED(); 2775 NOTREACHED();
2748 } 2776 }
2749 } 2777 }
2750 2778
2751 void HttpCache::Transaction::OnIOComplete(int result) { 2779 void HttpCache::Transaction::OnIOComplete(int result) {
2752 DoLoop(result); 2780 DoLoop(result);
2753 } 2781 }
2754 2782
2755 } // namespace net 2783 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/http/http_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698