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

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

Issue 356953003: Adding DiskBasedCertCache to HttpCache (+UMA). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@current
Patch Set: Moved CertChain writing and reading to new functions. Created 6 years, 5 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
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>
(...skipping 14 matching lines...) Expand all
25 #include "base/time/time.h" 25 #include "base/time/time.h"
26 #include "net/base/completion_callback.h" 26 #include "net/base/completion_callback.h"
27 #include "net/base/io_buffer.h" 27 #include "net/base/io_buffer.h"
28 #include "net/base/load_flags.h" 28 #include "net/base/load_flags.h"
29 #include "net/base/load_timing_info.h" 29 #include "net/base/load_timing_info.h"
30 #include "net/base/net_errors.h" 30 #include "net/base/net_errors.h"
31 #include "net/base/net_log.h" 31 #include "net/base/net_log.h"
32 #include "net/base/upload_data_stream.h" 32 #include "net/base/upload_data_stream.h"
33 #include "net/cert/cert_status_flags.h" 33 #include "net/cert/cert_status_flags.h"
34 #include "net/disk_cache/disk_cache.h" 34 #include "net/disk_cache/disk_cache.h"
35 #include "net/http/disk_based_cert_cache.h"
35 #include "net/http/http_network_session.h" 36 #include "net/http/http_network_session.h"
36 #include "net/http/http_request_info.h" 37 #include "net/http/http_request_info.h"
37 #include "net/http/http_response_headers.h" 38 #include "net/http/http_response_headers.h"
38 #include "net/http/http_transaction.h" 39 #include "net/http/http_transaction.h"
39 #include "net/http/http_util.h" 40 #include "net/http/http_util.h"
40 #include "net/http/partial_data.h" 41 #include "net/http/partial_data.h"
41 #include "net/ssl/ssl_cert_request_info.h" 42 #include "net/ssl/ssl_cert_request_info.h"
42 #include "net/ssl/ssl_config_service.h" 43 #include "net/ssl/ssl_config_service.h"
43 44
44 using base::Time; 45 using base::Time;
45 using base::TimeDelta; 46 using base::TimeDelta;
46 using base::TimeTicks; 47 using base::TimeTicks;
47 48
48 namespace { 49 namespace {
49 50
51 // Stores data relevant to the statistics of writing and reading entire
52 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
53 // of certificates in the chain that have pending operations in the
54 // DiskBasedCertCache. |start_time| is the time that the read and write
55 // commands began being issued to the DiskBasedCertCache.
56 // TODO(brandonsalmon): Remove this when it is no longer necessary to
57 // collect data.
58 class SharedChainData : public base::RefCounted<SharedChainData> {
59 public:
60 SharedChainData(int num_ops, TimeTicks start)
61 : num_pending_ops(num_ops), start_time(start) {}
62
63 int num_pending_ops;
64 TimeTicks start_time;
65
66 private:
67 friend class base::RefCounted<SharedChainData>;
68 ~SharedChainData() {}
69 DISALLOW_COPY_AND_ASSIGN(SharedChainData);
70 };
71
72 // Used to obtain a cache entry key for an OSCertHandle.
73 // TODO(brandonsalmon): Remove this when cache keys are stored
74 // and no longer have to be recomputed to retrieve the OSCertHandle
75 // from the disk.
76 std::string GetCacheKeyForCert(net::X509Certificate::OSCertHandle cert_handle) {
77 net::SHA1HashValue fingerprint =
78 net::X509Certificate::CalculateFingerprint(cert_handle);
79
80 return "cert:" +
81 base::HexEncode(fingerprint.data, arraysize(fingerprint.data));
82 }
83
84 // |dist_from_root| indicates the position of the read certificate in the
85 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
86 // whether or not the read certificate was the leaf of the chain.
87 // |shared_chain_data| contains data shared by each certificate in
88 // the chain.
89 void OnCertReadIOComplete(
90 int dist_from_root,
91 bool is_leaf,
92 const scoped_refptr<SharedChainData>& shared_chain_data,
93 net::X509Certificate::OSCertHandle cert_handle) {
94 // If |num_pending_ops| is one, this was the last pending read operation
95 // for this chain of certificates. The total time used to read the chain
96 // can be calculated by subtracting the starting time from Now().
97 shared_chain_data->num_pending_ops--;
98 if (!shared_chain_data->num_pending_ops) {
99 const TimeDelta read_chain_wait =
100 TimeTicks::Now() - shared_chain_data->start_time;
101 UMA_HISTOGRAM_TIMES("DiskBasedCertCache.ChainReadTime", read_chain_wait);
102 }
103
104 bool success = (cert_handle != NULL);
105 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.ReadSuccessTotal", success);
106
107 if (is_leaf)
108 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.ReadSuccessLeaf", success);
109 else if (dist_from_root == 0)
110 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.ReadSuccessRoot", success);
111 else if (dist_from_root == 1)
112 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.ReadSuccessInt1", success);
113 else if (dist_from_root == 2)
114 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.ReadSuccessInt2", success);
115 else
116 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.ReadSuccessIntN", success);
117 }
118
119 // |dist_from_root| indicates the position of the written certificate in the
120 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
121 // whether or not the written certificate was the leaf of the chain.
122 // |shared_chain_data| contains data shared by each certificate in
123 // the chain.
124 void OnCertWriteIOComplete(
125 int dist_from_root,
126 bool is_leaf,
127 const scoped_refptr<SharedChainData>& shared_chain_data,
128 const std::string& key) {
129 // If |num_pending_ops| is one, this was the last pending write operation
130 // for this chain of certificates. The total time used to write the chain
131 // can be calculated by subtracting the starting time from Now().
132 shared_chain_data->num_pending_ops--;
133 if (!shared_chain_data->num_pending_ops) {
134 const TimeDelta write_chain_wait =
135 TimeTicks::Now() - shared_chain_data->start_time;
136 UMA_HISTOGRAM_TIMES("DiskBasedCertCache.ChainWriteTime", write_chain_wait);
137 }
138
139 bool success = !key.empty();
140 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.WriteSuccessTotal", success);
141
142 if (is_leaf)
143 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.WriteSuccessLeaf", success);
144 else if (dist_from_root == 0)
145 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.WriteSuccessRoot", success);
146 else if (dist_from_root == 1)
147 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.WriteSuccessInt1", success);
148 else if (dist_from_root == 2)
149 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.WriteSuccessInt2", success);
150 else
151 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.WriteSuccessIntN", success);
152 }
153
50 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6 154 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
51 // a "non-error response" is one with a 2xx (Successful) or 3xx 155 // a "non-error response" is one with a 2xx (Successful) or 3xx
52 // (Redirection) status code. 156 // (Redirection) status code.
53 bool NonErrorResponse(int status_code) { 157 bool NonErrorResponse(int status_code) {
54 int status_code_range = status_code / 100; 158 int status_code_range = status_code / 100;
55 return status_code_range == 2 || status_code_range == 3; 159 return status_code_range == 2 || status_code_range == 3;
56 } 160 }
57 161
58 // Error codes that will be considered indicative of a page being offline/ 162 // Error codes that will be considered indicative of a page being offline/
59 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE. 163 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
(...skipping 1428 matching lines...) Expand 10 before | Expand all | Expand 10 after
1488 } 1592 }
1489 1593
1490 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { 1594 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1491 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); 1595 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1492 if (result != io_buf_len_ || 1596 if (result != io_buf_len_ ||
1493 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, 1597 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1494 &response_, &truncated_)) { 1598 &response_, &truncated_)) {
1495 return OnCacheReadError(result, true); 1599 return OnCacheReadError(result, true);
1496 } 1600 }
1497 1601
1602 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1603 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1604 DoCertChainRead();
1605
1498 // Some resources may have slipped in as truncated when they're not. 1606 // Some resources may have slipped in as truncated when they're not.
1499 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); 1607 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1500 if (response_.headers->GetContentLength() == current_size) 1608 if (response_.headers->GetContentLength() == current_size)
1501 truncated_ = false; 1609 truncated_ = false;
1502 1610
1503 // We now have access to the cache entry. 1611 // We now have access to the cache entry.
1504 // 1612 //
1505 // o if we are a reader for the transaction, then we can start reading the 1613 // o if we are a reader for the transaction, then we can start reading the
1506 // cache entry. 1614 // cache entry.
1507 // 1615 //
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
1698 // End of file. This may be the result of a connection problem so see if we 1806 // End of file. This may be the result of a connection problem so see if we
1699 // have to keep the entry around to be flagged as truncated later on. 1807 // have to keep the entry around to be flagged as truncated later on.
1700 if (done_reading_ || !entry_ || partial_.get() || 1808 if (done_reading_ || !entry_ || partial_.get() ||
1701 response_.headers->GetContentLength() <= 0) 1809 response_.headers->GetContentLength() <= 0)
1702 DoneWritingToEntry(true); 1810 DoneWritingToEntry(true);
1703 } 1811 }
1704 1812
1705 return result; 1813 return result;
1706 } 1814 }
1707 1815
1816 void HttpCache::Transaction::DoCertChainRead() {
1817 std::string key =
1818 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
1819 const X509Certificate::OSCertHandles& intermediates =
1820 response_.ssl_info.cert->GetIntermediateCertificates();
1821 int dist_from_root = intermediates.size();
1822
1823 scoped_refptr<SharedChainData> shared_chain_data(
1824 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1825 cache_->cert_cache()->Get(key,
1826 base::Bind(&OnCertReadIOComplete,
1827 dist_from_root,
1828 true /* is leaf */,
1829 shared_chain_data));
1830
1831 for (X509Certificate::OSCertHandles::const_iterator it =
1832 intermediates.begin();
1833 it != intermediates.end();
1834 ++it) {
1835 --dist_from_root;
1836 key = GetCacheKeyForCert(*it);
1837 cache_->cert_cache()->Get(key,
1838 base::Bind(&OnCertReadIOComplete,
1839 dist_from_root,
1840 false /* is not leaf */,
1841 shared_chain_data));
1842 }
1843 DCHECK_EQ(0, dist_from_root);
1844 }
1845
1846 void HttpCache::Transaction::DoCertChainWrite() {
1847 const X509Certificate::OSCertHandles& intermediates =
1848 response_.ssl_info.cert->GetIntermediateCertificates();
1849 int dist_from_root = intermediates.size();
1850
1851 scoped_refptr<SharedChainData> shared_chain_data(
1852 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1853 cache_->cert_cache()->Set(response_.ssl_info.cert->os_cert_handle(),
1854 base::Bind(&OnCertWriteIOComplete,
1855 dist_from_root,
1856 true /* is leaf */,
1857 shared_chain_data));
1858 for (X509Certificate::OSCertHandles::const_iterator it =
1859 intermediates.begin();
1860 it != intermediates.end();
1861 ++it) {
1862 --dist_from_root;
1863 cache_->cert_cache()->Set(*it,
1864 base::Bind(&OnCertWriteIOComplete,
1865 dist_from_root,
1866 false /* is not leaf */,
1867 shared_chain_data));
1868 }
1869 DCHECK_EQ(0, dist_from_root);
1870 }
1708 //----------------------------------------------------------------------------- 1871 //-----------------------------------------------------------------------------
rvargas (doing something else) 2014/07/09 01:25:36 nit: this line separates the state machine from ra
brandonsalmon 2014/07/09 01:50:43 Moved the methods outside of the state machine.
1709 1872
1710 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log, 1873 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1711 const HttpRequestInfo* request) { 1874 const HttpRequestInfo* request) {
1712 net_log_ = net_log; 1875 net_log_ = net_log;
1713 request_ = request; 1876 request_ = request;
1714 effective_load_flags_ = request_->load_flags; 1877 effective_load_flags_ = request_->load_flags;
1715 1878
1716 switch (cache_->mode()) { 1879 switch (cache_->mode()) {
1717 case NORMAL: 1880 case NORMAL:
1718 break; 1881 break;
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after
2320 // reverse-map the cert status to a net error and replay the net error. 2483 // reverse-map the cert status to a net error and replay the net error.
2321 if ((cache_->mode() != RECORD && 2484 if ((cache_->mode() != RECORD &&
2322 response_.headers->HasHeaderValue("cache-control", "no-store")) || 2485 response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2323 net::IsCertStatusError(response_.ssl_info.cert_status)) { 2486 net::IsCertStatusError(response_.ssl_info.cert_status)) {
2324 DoneWritingToEntry(false); 2487 DoneWritingToEntry(false);
2325 if (net_log_.IsLogging()) 2488 if (net_log_.IsLogging())
2326 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); 2489 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2327 return OK; 2490 return OK;
2328 } 2491 }
2329 2492
2493 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2494 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2495 DoCertChainWrite();
2496
2330 // When writing headers, we normally only write the non-transient 2497 // When writing headers, we normally only write the non-transient
2331 // headers; when in record mode, record everything. 2498 // headers; when in record mode, record everything.
2332 bool skip_transient_headers = (cache_->mode() != RECORD); 2499 bool skip_transient_headers = (cache_->mode() != RECORD);
2333 2500
2334 if (truncated) 2501 if (truncated)
2335 DCHECK_EQ(200, response_.headers->response_code()); 2502 DCHECK_EQ(200, response_.headers->response_code());
2336 2503
2337 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer()); 2504 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2338 response_.Persist(data->pickle(), skip_transient_headers, truncated); 2505 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2339 data->Done(); 2506 data->Done();
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
2576 default: 2743 default:
2577 NOTREACHED(); 2744 NOTREACHED();
2578 } 2745 }
2579 } 2746 }
2580 2747
2581 void HttpCache::Transaction::OnIOComplete(int result) { 2748 void HttpCache::Transaction::OnIOComplete(int result) {
2582 DoLoop(result); 2749 DoLoop(result);
2583 } 2750 }
2584 2751
2585 } // namespace net 2752 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698