Returning scoped_ptr instead of raw pointer in QuicInfoToValue in net/
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blob2b96817e56ccb6ecfc17e161b52067e6c9bd13ee
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
3 // found in the LICENSE file.
5 #include "net/http/http_cache_transaction.h"
7 #include "build/build_config.h"
9 #if defined(OS_POSIX)
10 #include <unistd.h>
11 #endif
13 #include <algorithm>
14 #include <string>
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/format_macros.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/metrics/field_trial.h"
22 #include "base/metrics/histogram.h"
23 #include "base/metrics/sparse_histogram.h"
24 #include "base/profiler/scoped_tracker.h"
25 #include "base/rand_util.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_piece.h"
28 #include "base/strings/string_util.h"
29 #include "base/strings/stringprintf.h"
30 #include "base/time/clock.h"
31 #include "base/time/time.h"
32 #include "base/values.h"
33 #include "net/base/completion_callback.h"
34 #include "net/base/io_buffer.h"
35 #include "net/base/load_flags.h"
36 #include "net/base/load_timing_info.h"
37 #include "net/base/net_errors.h"
38 #include "net/base/upload_data_stream.h"
39 #include "net/cert/cert_status_flags.h"
40 #include "net/disk_cache/disk_cache.h"
41 #include "net/http/disk_based_cert_cache.h"
42 #include "net/http/http_network_session.h"
43 #include "net/http/http_request_info.h"
44 #include "net/http/http_response_headers.h"
45 #include "net/http/http_transaction.h"
46 #include "net/http/http_util.h"
47 #include "net/http/partial_data.h"
48 #include "net/log/net_log.h"
49 #include "net/ssl/ssl_cert_request_info.h"
50 #include "net/ssl/ssl_config_service.h"
52 using base::Time;
53 using base::TimeDelta;
54 using base::TimeTicks;
56 namespace net {
58 namespace {
60 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
61 static const char kFreshnessHeader[] = "Resource-Freshness";
63 // Stores data relevant to the statistics of writing and reading entire
64 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
65 // of certificates in the chain that have pending operations in the
66 // DiskBasedCertCache. |start_time| is the time that the read and write
67 // commands began being issued to the DiskBasedCertCache.
68 // TODO(brandonsalmon): Remove this when it is no longer necessary to
69 // collect data.
70 class SharedChainData : public base::RefCounted<SharedChainData> {
71 public:
72 SharedChainData(int num_ops, TimeTicks start)
73 : num_pending_ops(num_ops), start_time(start) {}
75 int num_pending_ops;
76 TimeTicks start_time;
78 private:
79 friend class base::RefCounted<SharedChainData>;
80 ~SharedChainData() {}
81 DISALLOW_COPY_AND_ASSIGN(SharedChainData);
84 // Used to obtain a cache entry key for an OSCertHandle.
85 // TODO(brandonsalmon): Remove this when cache keys are stored
86 // and no longer have to be recomputed to retrieve the OSCertHandle
87 // from the disk.
88 std::string GetCacheKeyForCert(X509Certificate::OSCertHandle cert_handle) {
89 SHA1HashValue fingerprint =
90 X509Certificate::CalculateFingerprint(cert_handle);
92 return "cert:" +
93 base::HexEncode(fingerprint.data, arraysize(fingerprint.data));
96 // |dist_from_root| indicates the position of the read certificate in the
97 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
98 // whether or not the read certificate was the leaf of the chain.
99 // |shared_chain_data| contains data shared by each certificate in
100 // the chain.
101 void OnCertReadIOComplete(
102 int dist_from_root,
103 bool is_leaf,
104 const scoped_refptr<SharedChainData>& shared_chain_data,
105 X509Certificate::OSCertHandle cert_handle) {
106 // If |num_pending_ops| is one, this was the last pending read operation
107 // for this chain of certificates. The total time used to read the chain
108 // can be calculated by subtracting the starting time from Now().
109 shared_chain_data->num_pending_ops--;
110 if (!shared_chain_data->num_pending_ops) {
111 const TimeDelta read_chain_wait =
112 TimeTicks::Now() - shared_chain_data->start_time;
113 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
114 read_chain_wait,
115 base::TimeDelta::FromMilliseconds(1),
116 base::TimeDelta::FromMinutes(10),
117 50);
120 bool success = (cert_handle != NULL);
121 if (is_leaf)
122 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success);
124 if (success)
125 UMA_HISTOGRAM_CUSTOM_COUNTS(
126 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root, 0, 10, 7);
127 else
128 UMA_HISTOGRAM_CUSTOM_COUNTS(
129 "DiskBasedCertCache.CertIoReadFailure", dist_from_root, 0, 10, 7);
132 // |dist_from_root| indicates the position of the written certificate in the
133 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
134 // whether or not the written certificate was the leaf of the chain.
135 // |shared_chain_data| contains data shared by each certificate in
136 // the chain.
137 void OnCertWriteIOComplete(
138 int dist_from_root,
139 bool is_leaf,
140 const scoped_refptr<SharedChainData>& shared_chain_data,
141 const std::string& key) {
142 // If |num_pending_ops| is one, this was the last pending write operation
143 // for this chain of certificates. The total time used to write the chain
144 // can be calculated by subtracting the starting time from Now().
145 shared_chain_data->num_pending_ops--;
146 if (!shared_chain_data->num_pending_ops) {
147 const TimeDelta write_chain_wait =
148 TimeTicks::Now() - shared_chain_data->start_time;
149 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
150 write_chain_wait,
151 base::TimeDelta::FromMilliseconds(1),
152 base::TimeDelta::FromMinutes(10),
153 50);
156 bool success = !key.empty();
157 if (is_leaf)
158 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success);
160 if (success)
161 UMA_HISTOGRAM_CUSTOM_COUNTS(
162 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root, 0, 10, 7);
163 else
164 UMA_HISTOGRAM_CUSTOM_COUNTS(
165 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root, 0, 10, 7);
168 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
169 // a "non-error response" is one with a 2xx (Successful) or 3xx
170 // (Redirection) status code.
171 bool NonErrorResponse(int status_code) {
172 int status_code_range = status_code / 100;
173 return status_code_range == 2 || status_code_range == 3;
176 void RecordNoStoreHeaderHistogram(int load_flags,
177 const HttpResponseInfo* response) {
178 if (load_flags & LOAD_MAIN_FRAME) {
179 UMA_HISTOGRAM_BOOLEAN(
180 "Net.MainFrameNoStore",
181 response->headers->HasHeaderValue("cache-control", "no-store"));
185 scoped_ptr<base::Value> NetLogAsyncRevalidationInfoCallback(
186 const NetLog::Source& source,
187 const HttpRequestInfo* request,
188 NetLogCaptureMode capture_mode) {
189 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
190 source.AddToEventParameters(dict.get());
192 dict->SetString("url", request->url.possibly_invalid_spec());
193 dict->SetString("method", request->method);
194 return dict.Pass();
197 enum ExternallyConditionalizedType {
198 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION,
199 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE,
200 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS,
201 EXTERNALLY_CONDITIONALIZED_MAX
204 } // namespace
206 struct HeaderNameAndValue {
207 const char* name;
208 const char* value;
211 // If the request includes one of these request headers, then avoid caching
212 // to avoid getting confused.
213 static const HeaderNameAndValue kPassThroughHeaders[] = {
214 { "if-unmodified-since", NULL }, // causes unexpected 412s
215 { "if-match", NULL }, // causes unexpected 412s
216 { "if-range", NULL },
217 { NULL, NULL }
220 struct ValidationHeaderInfo {
221 const char* request_header_name;
222 const char* related_response_header_name;
225 static const ValidationHeaderInfo kValidationHeaders[] = {
226 { "if-modified-since", "last-modified" },
227 { "if-none-match", "etag" },
230 // If the request includes one of these request headers, then avoid reusing
231 // our cached copy if any.
232 static const HeaderNameAndValue kForceFetchHeaders[] = {
233 { "cache-control", "no-cache" },
234 { "pragma", "no-cache" },
235 { NULL, NULL }
238 // If the request includes one of these request headers, then force our
239 // cached copy (if any) to be revalidated before reusing it.
240 static const HeaderNameAndValue kForceValidateHeaders[] = {
241 { "cache-control", "max-age=0" },
242 { NULL, NULL }
245 static bool HeaderMatches(const HttpRequestHeaders& headers,
246 const HeaderNameAndValue* search) {
247 for (; search->name; ++search) {
248 std::string header_value;
249 if (!headers.GetHeader(search->name, &header_value))
250 continue;
252 if (!search->value)
253 return true;
255 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
256 while (v.GetNext()) {
257 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
258 return true;
261 return false;
264 //-----------------------------------------------------------------------------
266 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
267 : next_state_(STATE_NONE),
268 request_(NULL),
269 priority_(priority),
270 cache_(cache->GetWeakPtr()),
271 entry_(NULL),
272 new_entry_(NULL),
273 new_response_(NULL),
274 mode_(NONE),
275 target_state_(STATE_NONE),
276 reading_(false),
277 invalid_range_(false),
278 truncated_(false),
279 is_sparse_(false),
280 range_requested_(false),
281 handling_206_(false),
282 cache_pending_(false),
283 done_reading_(false),
284 vary_mismatch_(false),
285 couldnt_conditionalize_request_(false),
286 bypass_lock_for_test_(false),
287 fail_conditionalization_for_test_(false),
288 io_buf_len_(0),
289 read_offset_(0),
290 effective_load_flags_(0),
291 write_len_(0),
292 transaction_pattern_(PATTERN_UNDEFINED),
293 total_received_bytes_(0),
294 websocket_handshake_stream_base_create_helper_(NULL),
295 weak_factory_(this) {
296 static_assert(HttpCache::Transaction::kNumValidationHeaders ==
297 arraysize(kValidationHeaders),
298 "invalid number of validation headers");
300 io_callback_ = base::Bind(&Transaction::OnIOComplete,
301 weak_factory_.GetWeakPtr());
304 HttpCache::Transaction::~Transaction() {
305 // We may have to issue another IO, but we should never invoke the callback_
306 // after this point.
307 callback_.Reset();
309 if (cache_) {
310 if (entry_) {
311 bool cancel_request = reading_ && response_.headers.get();
312 if (cancel_request) {
313 if (partial_) {
314 entry_->disk_entry->CancelSparseIO();
315 } else {
316 cancel_request &= (response_.headers->response_code() == 200);
320 cache_->DoneWithEntry(entry_, this, cancel_request);
321 } else if (cache_pending_) {
322 cache_->RemovePendingTransaction(this);
327 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
328 const CompletionCallback& callback) {
329 DCHECK(buf);
330 DCHECK_GT(buf_len, 0);
331 DCHECK(!callback.is_null());
332 if (!cache_.get() || !entry_)
333 return ERR_UNEXPECTED;
335 // We don't need to track this operation for anything.
336 // It could be possible to check if there is something already written and
337 // avoid writing again (it should be the same, right?), but let's allow the
338 // caller to "update" the contents with something new.
339 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
340 callback, true);
343 bool HttpCache::Transaction::AddTruncatedFlag() {
344 DCHECK(mode_ & WRITE || mode_ == NONE);
346 // Don't set the flag for sparse entries.
347 if (partial_.get() && !truncated_)
348 return true;
350 if (!CanResume(true))
351 return false;
353 // We may have received the whole resource already.
354 if (done_reading_)
355 return true;
357 truncated_ = true;
358 target_state_ = STATE_NONE;
359 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
360 DoLoop(OK);
361 return true;
364 LoadState HttpCache::Transaction::GetWriterLoadState() const {
365 if (network_trans_.get())
366 return network_trans_->GetLoadState();
367 if (entry_ || !request_)
368 return LOAD_STATE_IDLE;
369 return LOAD_STATE_WAITING_FOR_CACHE;
372 const BoundNetLog& HttpCache::Transaction::net_log() const {
373 return net_log_;
376 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
377 const CompletionCallback& callback,
378 const BoundNetLog& net_log) {
379 DCHECK(request);
380 DCHECK(!callback.is_null());
382 // Ensure that we only have one asynchronous call at a time.
383 DCHECK(callback_.is_null());
384 DCHECK(!reading_);
385 DCHECK(!network_trans_.get());
386 DCHECK(!entry_);
388 if (!cache_.get())
389 return ERR_UNEXPECTED;
391 SetRequest(net_log, request);
393 // We have to wait until the backend is initialized so we start the SM.
394 next_state_ = STATE_GET_BACKEND;
395 int rv = DoLoop(OK);
397 // Setting this here allows us to check for the existence of a callback_ to
398 // determine if we are still inside Start.
399 if (rv == ERR_IO_PENDING)
400 callback_ = callback;
402 return rv;
405 int HttpCache::Transaction::RestartIgnoringLastError(
406 const CompletionCallback& callback) {
407 DCHECK(!callback.is_null());
409 // Ensure that we only have one asynchronous call at a time.
410 DCHECK(callback_.is_null());
412 if (!cache_.get())
413 return ERR_UNEXPECTED;
415 int rv = RestartNetworkRequest();
417 if (rv == ERR_IO_PENDING)
418 callback_ = callback;
420 return rv;
423 int HttpCache::Transaction::RestartWithCertificate(
424 X509Certificate* client_cert,
425 const CompletionCallback& callback) {
426 DCHECK(!callback.is_null());
428 // Ensure that we only have one asynchronous call at a time.
429 DCHECK(callback_.is_null());
431 if (!cache_.get())
432 return ERR_UNEXPECTED;
434 int rv = RestartNetworkRequestWithCertificate(client_cert);
436 if (rv == ERR_IO_PENDING)
437 callback_ = callback;
439 return rv;
442 int HttpCache::Transaction::RestartWithAuth(
443 const AuthCredentials& credentials,
444 const CompletionCallback& callback) {
445 DCHECK(auth_response_.headers.get());
446 DCHECK(!callback.is_null());
448 // Ensure that we only have one asynchronous call at a time.
449 DCHECK(callback_.is_null());
451 if (!cache_.get())
452 return ERR_UNEXPECTED;
454 // Clear the intermediate response since we are going to start over.
455 auth_response_ = HttpResponseInfo();
457 int rv = RestartNetworkRequestWithAuth(credentials);
459 if (rv == ERR_IO_PENDING)
460 callback_ = callback;
462 return rv;
465 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
466 if (!network_trans_.get())
467 return false;
468 return network_trans_->IsReadyToRestartForAuth();
471 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
472 const CompletionCallback& callback) {
473 DCHECK(buf);
474 DCHECK_GT(buf_len, 0);
475 DCHECK(!callback.is_null());
477 DCHECK(callback_.is_null());
479 if (!cache_.get())
480 return ERR_UNEXPECTED;
482 // If we have an intermediate auth response at this point, then it means the
483 // user wishes to read the network response (the error page). If there is a
484 // previous response in the cache then we should leave it intact.
485 if (auth_response_.headers.get() && mode_ != NONE) {
486 UpdateTransactionPattern(PATTERN_NOT_COVERED);
487 DCHECK(mode_ & WRITE);
488 DoneWritingToEntry(mode_ == READ_WRITE);
489 mode_ = NONE;
492 reading_ = true;
493 int rv;
495 switch (mode_) {
496 case READ_WRITE:
497 DCHECK(partial_.get());
498 if (!network_trans_.get()) {
499 // We are just reading from the cache, but we may be writing later.
500 rv = ReadFromEntry(buf, buf_len);
501 break;
503 case NONE:
504 case WRITE:
505 DCHECK(network_trans_.get());
506 rv = ReadFromNetwork(buf, buf_len);
507 break;
508 case READ:
509 rv = ReadFromEntry(buf, buf_len);
510 break;
511 default:
512 NOTREACHED();
513 rv = ERR_FAILED;
516 if (rv == ERR_IO_PENDING) {
517 DCHECK(callback_.is_null());
518 callback_ = callback;
520 return rv;
523 void HttpCache::Transaction::StopCaching() {
524 // We really don't know where we are now. Hopefully there is no operation in
525 // progress, but nothing really prevents this method to be called after we
526 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
527 // point because we need the state machine for that (and even if we are really
528 // free, that would be an asynchronous operation). In other words, keep the
529 // entry how it is (it will be marked as truncated at destruction), and let
530 // the next piece of code that executes know that we are now reading directly
531 // from the net.
532 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
533 // future request for the resource will be blocked on this one.
534 // Fix this.
535 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
536 !is_sparse_ && !range_requested_) {
537 mode_ = NONE;
541 bool HttpCache::Transaction::GetFullRequestHeaders(
542 HttpRequestHeaders* headers) const {
543 if (network_trans_)
544 return network_trans_->GetFullRequestHeaders(headers);
546 // TODO(ttuttle): Read headers from cache.
547 return false;
550 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
551 int64 total_received_bytes = total_received_bytes_;
552 if (network_trans_)
553 total_received_bytes += network_trans_->GetTotalReceivedBytes();
554 return total_received_bytes;
557 void HttpCache::Transaction::DoneReading() {
558 if (cache_.get() && entry_) {
559 DCHECK_NE(mode_, UPDATE);
560 if (mode_ & WRITE) {
561 DoneWritingToEntry(true);
562 } else if (mode_ & READ) {
563 // It is necessary to check mode_ & READ because it is possible
564 // for mode_ to be NONE and entry_ non-NULL with a write entry
565 // if StopCaching was called.
566 cache_->DoneReadingFromEntry(entry_, this);
567 entry_ = NULL;
572 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
573 // Null headers means we encountered an error or haven't a response yet
574 if (auth_response_.headers.get())
575 return &auth_response_;
576 return &response_;
579 LoadState HttpCache::Transaction::GetLoadState() const {
580 LoadState state = GetWriterLoadState();
581 if (state != LOAD_STATE_WAITING_FOR_CACHE)
582 return state;
584 if (cache_.get())
585 return cache_->GetLoadStateForPendingTransaction(this);
587 return LOAD_STATE_IDLE;
590 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
591 if (network_trans_.get())
592 return network_trans_->GetUploadProgress();
593 return final_upload_progress_;
596 void HttpCache::Transaction::SetQuicServerInfo(
597 QuicServerInfo* quic_server_info) {}
599 bool HttpCache::Transaction::GetLoadTimingInfo(
600 LoadTimingInfo* load_timing_info) const {
601 if (network_trans_)
602 return network_trans_->GetLoadTimingInfo(load_timing_info);
604 if (old_network_trans_load_timing_) {
605 *load_timing_info = *old_network_trans_load_timing_;
606 return true;
609 if (first_cache_access_since_.is_null())
610 return false;
612 // If the cache entry was opened, return that time.
613 load_timing_info->send_start = first_cache_access_since_;
614 // This time doesn't make much sense when reading from the cache, so just use
615 // the same time as send_start.
616 load_timing_info->send_end = first_cache_access_since_;
617 return true;
620 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
621 priority_ = priority;
622 if (network_trans_)
623 network_trans_->SetPriority(priority_);
626 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
627 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
628 websocket_handshake_stream_base_create_helper_ = create_helper;
629 if (network_trans_)
630 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
633 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
634 const BeforeNetworkStartCallback& callback) {
635 DCHECK(!network_trans_);
636 before_network_start_callback_ = callback;
639 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
640 const BeforeProxyHeadersSentCallback& callback) {
641 DCHECK(!network_trans_);
642 before_proxy_headers_sent_callback_ = callback;
645 int HttpCache::Transaction::ResumeNetworkStart() {
646 if (network_trans_)
647 return network_trans_->ResumeNetworkStart();
648 return ERR_UNEXPECTED;
651 void HttpCache::Transaction::GetConnectionAttempts(
652 ConnectionAttempts* out) const {
653 ConnectionAttempts new_connection_attempts;
654 if (network_trans_)
655 network_trans_->GetConnectionAttempts(&new_connection_attempts);
657 out->swap(new_connection_attempts);
658 out->insert(out->begin(), old_connection_attempts_.begin(),
659 old_connection_attempts_.end());
662 //-----------------------------------------------------------------------------
664 void HttpCache::Transaction::DoCallback(int rv) {
665 DCHECK(rv != ERR_IO_PENDING);
666 DCHECK(!callback_.is_null());
668 read_buf_ = NULL; // Release the buffer before invoking the callback.
670 // Since Run may result in Read being called, clear callback_ up front.
671 CompletionCallback c = callback_;
672 callback_.Reset();
673 c.Run(rv);
676 int HttpCache::Transaction::HandleResult(int rv) {
677 DCHECK(rv != ERR_IO_PENDING);
678 if (!callback_.is_null())
679 DoCallback(rv);
681 return rv;
684 // A few common patterns: (Foo* means Foo -> FooComplete)
686 // 1. Not-cached entry:
687 // Start():
688 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
689 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
690 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
691 // PartialHeadersReceived
693 // Read():
694 // NetworkRead* -> CacheWriteData*
696 // 2. Cached entry, no validation:
697 // Start():
698 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
699 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
700 // BeginCacheValidation() -> SetupEntryForRead()
702 // Read():
703 // CacheReadData*
705 // 3. Cached entry, validation (304):
706 // Start():
707 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
708 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
709 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
710 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
711 // -> OverwriteCachedResponse -> PartialHeadersReceived
713 // Read():
714 // CacheReadData*
716 // 4. Cached entry, validation and replace (200):
717 // Start():
718 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
719 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
720 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
721 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
722 // TruncateCachedMetadata* -> PartialHeadersReceived
724 // Read():
725 // NetworkRead* -> CacheWriteData*
727 // 5. Sparse entry, partially cached, byte range request:
728 // Start():
729 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
730 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
731 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
732 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
733 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
734 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
735 // -> OverwriteCachedResponse -> PartialHeadersReceived
737 // Read() 1:
738 // NetworkRead* -> CacheWriteData*
740 // Read() 2:
741 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
742 // CompletePartialCacheValidation -> CacheReadData* ->
744 // Read() 3:
745 // CacheReadData* -> StartPartialCacheValidation ->
746 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
747 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
748 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
750 // 6. HEAD. Not-cached entry:
751 // Pass through. Don't save a HEAD by itself.
752 // Start():
753 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
755 // 7. HEAD. Cached entry, no validation:
756 // Start():
757 // The same flow as for a GET request (example #2)
759 // Read():
760 // CacheReadData (returns 0)
762 // 8. HEAD. Cached entry, validation (304):
763 // The request updates the stored headers.
764 // Start(): Same as for a GET request (example #3)
766 // Read():
767 // CacheReadData (returns 0)
769 // 9. HEAD. Cached entry, validation and replace (200):
770 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
771 // itself.
772 // Start():
773 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
774 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
775 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
776 // OverwriteCachedResponse
778 // 10. HEAD. Sparse entry, partially cached:
779 // Serve the request from the cache, as long as it doesn't require
780 // revalidation. Ignore missing ranges when deciding to revalidate. If the
781 // entry requires revalidation, ignore the whole request and go to full pass
782 // through (the result of the HEAD request will NOT update the entry).
784 // Start(): Basically the same as example 7, as we never create a partial_
785 // object for this request.
787 // 11. Prefetch, not-cached entry:
788 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
789 // UpdateCachedResponse.
791 // 12. Prefetch, cached entry:
792 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
793 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
794 // bit is unset.
796 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
797 // Skip validation, similar to example 2.
798 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
799 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
800 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
801 // SetupEntryForRead()
803 // Read():
804 // CacheReadData*
806 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
807 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
808 // CacheReadResponse* and CacheDispatchValidation.
809 int HttpCache::Transaction::DoLoop(int result) {
810 DCHECK(next_state_ != STATE_NONE);
812 int rv = result;
813 do {
814 State state = next_state_;
815 next_state_ = STATE_NONE;
816 switch (state) {
817 case STATE_GET_BACKEND:
818 DCHECK_EQ(OK, rv);
819 rv = DoGetBackend();
820 break;
821 case STATE_GET_BACKEND_COMPLETE:
822 rv = DoGetBackendComplete(rv);
823 break;
824 case STATE_SEND_REQUEST:
825 DCHECK_EQ(OK, rv);
826 rv = DoSendRequest();
827 break;
828 case STATE_SEND_REQUEST_COMPLETE:
829 rv = DoSendRequestComplete(rv);
830 break;
831 case STATE_SUCCESSFUL_SEND_REQUEST:
832 DCHECK_EQ(OK, rv);
833 rv = DoSuccessfulSendRequest();
834 break;
835 case STATE_NETWORK_READ:
836 DCHECK_EQ(OK, rv);
837 rv = DoNetworkRead();
838 break;
839 case STATE_NETWORK_READ_COMPLETE:
840 rv = DoNetworkReadComplete(rv);
841 break;
842 case STATE_INIT_ENTRY:
843 DCHECK_EQ(OK, rv);
844 rv = DoInitEntry();
845 break;
846 case STATE_OPEN_ENTRY:
847 DCHECK_EQ(OK, rv);
848 rv = DoOpenEntry();
849 break;
850 case STATE_OPEN_ENTRY_COMPLETE:
851 rv = DoOpenEntryComplete(rv);
852 break;
853 case STATE_CREATE_ENTRY:
854 DCHECK_EQ(OK, rv);
855 rv = DoCreateEntry();
856 break;
857 case STATE_CREATE_ENTRY_COMPLETE:
858 rv = DoCreateEntryComplete(rv);
859 break;
860 case STATE_DOOM_ENTRY:
861 DCHECK_EQ(OK, rv);
862 rv = DoDoomEntry();
863 break;
864 case STATE_DOOM_ENTRY_COMPLETE:
865 rv = DoDoomEntryComplete(rv);
866 break;
867 case STATE_ADD_TO_ENTRY:
868 DCHECK_EQ(OK, rv);
869 rv = DoAddToEntry();
870 break;
871 case STATE_ADD_TO_ENTRY_COMPLETE:
872 rv = DoAddToEntryComplete(rv);
873 break;
874 case STATE_START_PARTIAL_CACHE_VALIDATION:
875 DCHECK_EQ(OK, rv);
876 rv = DoStartPartialCacheValidation();
877 break;
878 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
879 rv = DoCompletePartialCacheValidation(rv);
880 break;
881 case STATE_UPDATE_CACHED_RESPONSE:
882 DCHECK_EQ(OK, rv);
883 rv = DoUpdateCachedResponse();
884 break;
885 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
886 rv = DoUpdateCachedResponseComplete(rv);
887 break;
888 case STATE_OVERWRITE_CACHED_RESPONSE:
889 DCHECK_EQ(OK, rv);
890 rv = DoOverwriteCachedResponse();
891 break;
892 case STATE_TRUNCATE_CACHED_DATA:
893 DCHECK_EQ(OK, rv);
894 rv = DoTruncateCachedData();
895 break;
896 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
897 rv = DoTruncateCachedDataComplete(rv);
898 break;
899 case STATE_TRUNCATE_CACHED_METADATA:
900 DCHECK_EQ(OK, rv);
901 rv = DoTruncateCachedMetadata();
902 break;
903 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
904 rv = DoTruncateCachedMetadataComplete(rv);
905 break;
906 case STATE_PARTIAL_HEADERS_RECEIVED:
907 DCHECK_EQ(OK, rv);
908 rv = DoPartialHeadersReceived();
909 break;
910 case STATE_CACHE_READ_RESPONSE:
911 DCHECK_EQ(OK, rv);
912 rv = DoCacheReadResponse();
913 break;
914 case STATE_CACHE_READ_RESPONSE_COMPLETE:
915 rv = DoCacheReadResponseComplete(rv);
916 break;
917 case STATE_CACHE_DISPATCH_VALIDATION:
918 DCHECK_EQ(OK, rv);
919 rv = DoCacheDispatchValidation();
920 break;
921 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH:
922 DCHECK_EQ(OK, rv);
923 rv = DoCacheToggleUnusedSincePrefetch();
924 break;
925 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE:
926 rv = DoCacheToggleUnusedSincePrefetchComplete(rv);
927 break;
928 case STATE_CACHE_WRITE_RESPONSE:
929 DCHECK_EQ(OK, rv);
930 rv = DoCacheWriteResponse();
931 break;
932 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
933 DCHECK_EQ(OK, rv);
934 rv = DoCacheWriteTruncatedResponse();
935 break;
936 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
937 rv = DoCacheWriteResponseComplete(rv);
938 break;
939 case STATE_CACHE_READ_METADATA:
940 DCHECK_EQ(OK, rv);
941 rv = DoCacheReadMetadata();
942 break;
943 case STATE_CACHE_READ_METADATA_COMPLETE:
944 rv = DoCacheReadMetadataComplete(rv);
945 break;
946 case STATE_CACHE_QUERY_DATA:
947 DCHECK_EQ(OK, rv);
948 rv = DoCacheQueryData();
949 break;
950 case STATE_CACHE_QUERY_DATA_COMPLETE:
951 rv = DoCacheQueryDataComplete(rv);
952 break;
953 case STATE_CACHE_READ_DATA:
954 DCHECK_EQ(OK, rv);
955 rv = DoCacheReadData();
956 break;
957 case STATE_CACHE_READ_DATA_COMPLETE:
958 rv = DoCacheReadDataComplete(rv);
959 break;
960 case STATE_CACHE_WRITE_DATA:
961 rv = DoCacheWriteData(rv);
962 break;
963 case STATE_CACHE_WRITE_DATA_COMPLETE:
964 rv = DoCacheWriteDataComplete(rv);
965 break;
966 default:
967 NOTREACHED() << "bad state";
968 rv = ERR_FAILED;
969 break;
971 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
973 if (rv != ERR_IO_PENDING)
974 HandleResult(rv);
976 return rv;
979 int HttpCache::Transaction::DoGetBackend() {
980 cache_pending_ = true;
981 next_state_ = STATE_GET_BACKEND_COMPLETE;
982 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
983 return cache_->GetBackendForTransaction(this);
986 int HttpCache::Transaction::DoGetBackendComplete(int result) {
987 DCHECK(result == OK || result == ERR_FAILED);
988 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
989 result);
990 cache_pending_ = false;
992 if (!ShouldPassThrough()) {
993 cache_key_ = cache_->GenerateCacheKey(request_);
995 // Requested cache access mode.
996 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
997 mode_ = READ;
998 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
999 mode_ = WRITE;
1000 } else {
1001 mode_ = READ_WRITE;
1004 // Downgrade to UPDATE if the request has been externally conditionalized.
1005 if (external_validation_.initialized) {
1006 if (mode_ & WRITE) {
1007 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1008 // in case READ was off).
1009 mode_ = UPDATE;
1010 } else {
1011 mode_ = NONE;
1016 // Use PUT and DELETE only to invalidate existing stored entries.
1017 if ((request_->method == "PUT" || request_->method == "DELETE") &&
1018 mode_ != READ_WRITE && mode_ != WRITE) {
1019 mode_ = NONE;
1022 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1023 // transaction behaves the same for GET and HEAD requests at this point: if it
1024 // was not modified, the entry is updated and a response is not returned from
1025 // the cache. If we receive 200, it doesn't matter if there was a validation
1026 // header or not.
1027 if (request_->method == "HEAD" && mode_ == WRITE)
1028 mode_ = NONE;
1030 // If must use cache, then we must fail. This can happen for back/forward
1031 // navigations to a page generated via a form post.
1032 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
1033 return ERR_CACHE_MISS;
1035 if (mode_ == NONE) {
1036 if (partial_.get()) {
1037 partial_->RestoreHeaders(&custom_request_->extra_headers);
1038 partial_.reset();
1040 next_state_ = STATE_SEND_REQUEST;
1041 } else {
1042 next_state_ = STATE_INIT_ENTRY;
1045 // This is only set if we have something to do with the response.
1046 range_requested_ = (partial_.get() != NULL);
1048 return OK;
1051 int HttpCache::Transaction::DoSendRequest() {
1052 DCHECK(mode_ & WRITE || mode_ == NONE);
1053 DCHECK(!network_trans_.get());
1055 send_request_since_ = TimeTicks::Now();
1057 // Create a network transaction.
1058 int rv = cache_->network_layer_->CreateTransaction(priority_,
1059 &network_trans_);
1060 if (rv != OK)
1061 return rv;
1062 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1063 network_trans_->SetBeforeProxyHeadersSentCallback(
1064 before_proxy_headers_sent_callback_);
1066 // Old load timing information, if any, is now obsolete.
1067 old_network_trans_load_timing_.reset();
1069 if (websocket_handshake_stream_base_create_helper_)
1070 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1071 websocket_handshake_stream_base_create_helper_);
1073 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1074 rv = network_trans_->Start(request_, io_callback_, net_log_);
1075 return rv;
1078 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1079 if (!cache_.get())
1080 return ERR_UNEXPECTED;
1082 // If we tried to conditionalize the request and failed, we know
1083 // we won't be reading from the cache after this point.
1084 if (couldnt_conditionalize_request_)
1085 mode_ = WRITE;
1087 if (result == OK) {
1088 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
1089 return OK;
1092 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1093 response_.network_accessed = response->network_accessed;
1095 // Do not record requests that have network errors or restarts.
1096 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1097 if (IsCertificateError(result)) {
1098 // If we get a certificate error, then there is a certificate in ssl_info,
1099 // so GetResponseInfo() should never return NULL here.
1100 DCHECK(response);
1101 response_.ssl_info = response->ssl_info;
1102 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1103 DCHECK(response);
1104 response_.cert_request_info = response->cert_request_info;
1105 } else if (response_.was_cached) {
1106 DoneWritingToEntry(true);
1109 return result;
1112 // We received the response headers and there is no error.
1113 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1114 DCHECK(!new_response_);
1115 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1116 bool authentication_failure = false;
1118 if (new_response->headers->response_code() == 401 ||
1119 new_response->headers->response_code() == 407) {
1120 auth_response_ = *new_response;
1121 if (!reading_)
1122 return OK;
1124 // We initiated a second request the caller doesn't know about. We should be
1125 // able to authenticate this request because we should have authenticated
1126 // this URL moments ago.
1127 if (IsReadyToRestartForAuth()) {
1128 DCHECK(!response_.auth_challenge.get());
1129 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1130 // In theory we should check to see if there are new cookies, but there
1131 // is no way to do that from here.
1132 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1135 // We have to perform cleanup at this point so that at least the next
1136 // request can succeed.
1137 authentication_failure = true;
1138 if (entry_)
1139 DoomPartialEntry(false);
1140 mode_ = NONE;
1141 partial_.reset();
1144 new_response_ = new_response;
1145 if (authentication_failure ||
1146 (!ValidatePartialResponse() && !auth_response_.headers.get())) {
1147 // Something went wrong with this request and we have to restart it.
1148 // If we have an authentication response, we are exposed to weird things
1149 // hapenning if the user cancels the authentication before we receive
1150 // the new response.
1151 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST);
1152 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1153 response_ = HttpResponseInfo();
1154 ResetNetworkTransaction();
1155 new_response_ = NULL;
1156 next_state_ = STATE_SEND_REQUEST;
1157 return OK;
1160 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1161 // We have stored the full entry, but it changed and the server is
1162 // sending a range. We have to delete the old entry.
1163 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1164 DoneWritingToEntry(false);
1167 if (mode_ == WRITE &&
1168 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1169 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1172 // Invalidate any cached GET with a successful PUT or DELETE.
1173 if (mode_ == WRITE &&
1174 (request_->method == "PUT" || request_->method == "DELETE")) {
1175 if (NonErrorResponse(new_response->headers->response_code())) {
1176 int ret = cache_->DoomEntry(cache_key_, NULL);
1177 DCHECK_EQ(OK, ret);
1179 cache_->DoneWritingToEntry(entry_, true);
1180 entry_ = NULL;
1181 mode_ = NONE;
1184 // Invalidate any cached GET with a successful POST.
1185 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1186 request_->method == "POST" &&
1187 NonErrorResponse(new_response->headers->response_code())) {
1188 cache_->DoomMainEntryForUrl(request_->url);
1191 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1193 if (new_response_->headers->response_code() == 416 &&
1194 (request_->method == "GET" || request_->method == "POST")) {
1195 // If there is an active entry it may be destroyed with this transaction.
1196 response_ = *new_response_;
1197 return OK;
1200 // Are we expecting a response to a conditional query?
1201 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1202 if (new_response->headers->response_code() == 304 || handling_206_) {
1203 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1204 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1205 return OK;
1207 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1208 mode_ = WRITE;
1211 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1212 return OK;
1215 int HttpCache::Transaction::DoNetworkRead() {
1216 next_state_ = STATE_NETWORK_READ_COMPLETE;
1217 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1220 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1221 DCHECK(mode_ & WRITE || mode_ == NONE);
1223 if (!cache_.get())
1224 return ERR_UNEXPECTED;
1226 // If there is an error or we aren't saving the data, we are done; just wait
1227 // until the destructor runs to see if we can keep the data.
1228 if (mode_ == NONE || result < 0)
1229 return result;
1231 next_state_ = STATE_CACHE_WRITE_DATA;
1232 return result;
1235 int HttpCache::Transaction::DoInitEntry() {
1236 DCHECK(!new_entry_);
1238 if (!cache_.get())
1239 return ERR_UNEXPECTED;
1241 if (mode_ == WRITE) {
1242 next_state_ = STATE_DOOM_ENTRY;
1243 return OK;
1246 next_state_ = STATE_OPEN_ENTRY;
1247 return OK;
1250 int HttpCache::Transaction::DoOpenEntry() {
1251 DCHECK(!new_entry_);
1252 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1253 cache_pending_ = true;
1254 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1255 first_cache_access_since_ = TimeTicks::Now();
1256 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1259 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1260 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1261 // OK, otherwise the cache will end up with an active entry without any
1262 // transaction attached.
1263 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1264 cache_pending_ = false;
1265 if (result == OK) {
1266 next_state_ = STATE_ADD_TO_ENTRY;
1267 return OK;
1270 if (result == ERR_CACHE_RACE) {
1271 next_state_ = STATE_INIT_ENTRY;
1272 return OK;
1275 if (request_->method == "PUT" || request_->method == "DELETE" ||
1276 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1277 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1278 mode_ = NONE;
1279 next_state_ = STATE_SEND_REQUEST;
1280 return OK;
1283 if (mode_ == READ_WRITE) {
1284 mode_ = WRITE;
1285 next_state_ = STATE_CREATE_ENTRY;
1286 return OK;
1288 if (mode_ == UPDATE) {
1289 // There is no cache entry to update; proceed without caching.
1290 mode_ = NONE;
1291 next_state_ = STATE_SEND_REQUEST;
1292 return OK;
1295 // The entry does not exist, and we are not permitted to create a new entry,
1296 // so we must fail.
1297 return ERR_CACHE_MISS;
1300 int HttpCache::Transaction::DoCreateEntry() {
1301 DCHECK(!new_entry_);
1302 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1303 cache_pending_ = true;
1304 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1305 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1308 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1309 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1310 // OK, otherwise the cache will end up with an active entry without any
1311 // transaction attached.
1312 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1313 result);
1314 cache_pending_ = false;
1315 next_state_ = STATE_ADD_TO_ENTRY;
1317 if (result == ERR_CACHE_RACE) {
1318 next_state_ = STATE_INIT_ENTRY;
1319 return OK;
1322 if (result != OK) {
1323 // We have a race here: Maybe we failed to open the entry and decided to
1324 // create one, but by the time we called create, another transaction already
1325 // created the entry. If we want to eliminate this issue, we need an atomic
1326 // OpenOrCreate() method exposed by the disk cache.
1327 DLOG(WARNING) << "Unable to create cache entry";
1328 mode_ = NONE;
1329 if (partial_.get())
1330 partial_->RestoreHeaders(&custom_request_->extra_headers);
1331 next_state_ = STATE_SEND_REQUEST;
1333 return OK;
1336 int HttpCache::Transaction::DoDoomEntry() {
1337 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1338 cache_pending_ = true;
1339 if (first_cache_access_since_.is_null())
1340 first_cache_access_since_ = TimeTicks::Now();
1341 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1342 return cache_->DoomEntry(cache_key_, this);
1345 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1346 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1347 next_state_ = STATE_CREATE_ENTRY;
1348 cache_pending_ = false;
1349 if (result == ERR_CACHE_RACE)
1350 next_state_ = STATE_INIT_ENTRY;
1351 return OK;
1354 int HttpCache::Transaction::DoAddToEntry() {
1355 DCHECK(new_entry_);
1356 cache_pending_ = true;
1357 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1358 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1359 DCHECK(entry_lock_waiting_since_.is_null());
1360 entry_lock_waiting_since_ = TimeTicks::Now();
1361 int rv = cache_->AddTransactionToEntry(new_entry_, this);
1362 if (rv == ERR_IO_PENDING) {
1363 if (bypass_lock_for_test_) {
1364 OnAddToEntryTimeout(entry_lock_waiting_since_);
1365 } else {
1366 int timeout_milliseconds = 20 * 1000;
1367 if (partial_ && new_entry_->writer &&
1368 new_entry_->writer->range_requested_) {
1369 // Quickly timeout and bypass the cache if we're a range request and
1370 // we're blocked by the reader/writer lock. Doing so eliminates a long
1371 // running issue, http://crbug.com/31014, where two of the same media
1372 // resources could not be played back simultaneously due to one locking
1373 // the cache entry until the entire video was downloaded.
1375 // Bypassing the cache is not ideal, as we are now ignoring the cache
1376 // entirely for all range requests to a resource beyond the first. This
1377 // is however a much more succinct solution than the alternatives, which
1378 // would require somewhat significant changes to the http caching logic.
1380 // Allow some timeout slack for the entry addition to complete in case
1381 // the writer lock is imminently released; we want to avoid skipping
1382 // the cache if at all possible. See http://crbug.com/408765
1383 timeout_milliseconds = 25;
1385 base::MessageLoop::current()->PostDelayedTask(
1386 FROM_HERE,
1387 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout,
1388 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_),
1389 TimeDelta::FromMilliseconds(timeout_milliseconds));
1392 return rv;
1395 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1396 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1397 result);
1398 const TimeDelta entry_lock_wait =
1399 TimeTicks::Now() - entry_lock_waiting_since_;
1400 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1402 entry_lock_waiting_since_ = TimeTicks();
1403 DCHECK(new_entry_);
1404 cache_pending_ = false;
1406 if (result == OK)
1407 entry_ = new_entry_;
1409 // If there is a failure, the cache should have taken care of new_entry_.
1410 new_entry_ = NULL;
1412 if (result == ERR_CACHE_RACE) {
1413 next_state_ = STATE_INIT_ENTRY;
1414 return OK;
1417 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1418 // The cache is busy, bypass it for this transaction.
1419 mode_ = NONE;
1420 next_state_ = STATE_SEND_REQUEST;
1421 if (partial_) {
1422 partial_->RestoreHeaders(&custom_request_->extra_headers);
1423 partial_.reset();
1425 return OK;
1428 if (result != OK) {
1429 NOTREACHED();
1430 return result;
1433 if (mode_ == WRITE) {
1434 if (partial_.get())
1435 partial_->RestoreHeaders(&custom_request_->extra_headers);
1436 next_state_ = STATE_SEND_REQUEST;
1437 } else {
1438 // We have to read the headers from the cached entry.
1439 DCHECK(mode_ & READ_META);
1440 next_state_ = STATE_CACHE_READ_RESPONSE;
1442 return OK;
1445 // We may end up here multiple times for a given request.
1446 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1447 if (mode_ == NONE)
1448 return OK;
1450 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1451 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1454 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1455 if (!result) {
1456 // This is the end of the request.
1457 if (mode_ & WRITE) {
1458 DoneWritingToEntry(true);
1459 } else {
1460 cache_->DoneReadingFromEntry(entry_, this);
1461 entry_ = NULL;
1463 return result;
1466 if (result < 0)
1467 return result;
1469 partial_->PrepareCacheValidation(entry_->disk_entry,
1470 &custom_request_->extra_headers);
1472 if (reading_ && partial_->IsCurrentRangeCached()) {
1473 next_state_ = STATE_CACHE_READ_DATA;
1474 return OK;
1477 return BeginCacheValidation();
1480 // We received 304 or 206 and we want to update the cached response headers.
1481 int HttpCache::Transaction::DoUpdateCachedResponse() {
1482 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1483 int rv = OK;
1484 // Update cached response based on headers in new_response.
1485 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1486 response_.headers->Update(*new_response_->headers.get());
1487 response_.response_time = new_response_->response_time;
1488 response_.request_time = new_response_->request_time;
1489 response_.network_accessed = new_response_->network_accessed;
1490 response_.unused_since_prefetch = new_response_->unused_since_prefetch;
1491 if (new_response_->vary_data.is_valid()) {
1492 response_.vary_data = new_response_->vary_data;
1493 } else if (response_.vary_data.is_valid()) {
1494 // There is a vary header in the stored response but not in the current one.
1495 // Update the data with the new request headers.
1496 HttpVaryData new_vary_data;
1497 new_vary_data.Init(*request_, *response_.headers.get());
1498 response_.vary_data = new_vary_data;
1501 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1502 if (!entry_->doomed) {
1503 int ret = cache_->DoomEntry(cache_key_, NULL);
1504 DCHECK_EQ(OK, ret);
1506 } else {
1507 // If we are already reading, we already updated the headers for this
1508 // request; doing it again will change Content-Length.
1509 if (!reading_) {
1510 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1511 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1512 rv = OK;
1515 return rv;
1518 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1519 if (mode_ == UPDATE) {
1520 DCHECK(!handling_206_);
1521 // We got a "not modified" response and already updated the corresponding
1522 // cache entry above.
1524 // By closing the cached entry now, we make sure that the 304 rather than
1525 // the cached 200 response, is what will be returned to the user.
1526 DoneWritingToEntry(true);
1527 } else if (entry_ && !handling_206_) {
1528 DCHECK_EQ(READ_WRITE, mode_);
1529 if (!partial_.get() || partial_->IsLastRange()) {
1530 cache_->ConvertWriterToReader(entry_);
1531 mode_ = READ;
1533 // We no longer need the network transaction, so destroy it.
1534 final_upload_progress_ = network_trans_->GetUploadProgress();
1535 ResetNetworkTransaction();
1536 } else if (entry_ && handling_206_ && truncated_ &&
1537 partial_->initial_validation()) {
1538 // We just finished the validation of a truncated entry, and the server
1539 // is willing to resume the operation. Now we go back and start serving
1540 // the first part to the user.
1541 ResetNetworkTransaction();
1542 new_response_ = NULL;
1543 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1544 partial_->SetRangeToStartDownload();
1545 return OK;
1547 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1548 return OK;
1551 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1552 if (mode_ & READ) {
1553 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1554 return OK;
1557 // We change the value of Content-Length for partial content.
1558 if (handling_206_ && partial_.get())
1559 partial_->FixContentLength(new_response_->headers.get());
1561 response_ = *new_response_;
1563 if (request_->method == "HEAD") {
1564 // This response is replacing the cached one.
1565 DoneWritingToEntry(false);
1566 mode_ = NONE;
1567 new_response_ = NULL;
1568 return OK;
1571 if (handling_206_ && !CanResume(false)) {
1572 // There is no point in storing this resource because it will never be used.
1573 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1574 DoneWritingToEntry(false);
1575 if (partial_.get())
1576 partial_->FixResponseHeaders(response_.headers.get(), true);
1577 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1578 return OK;
1581 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1582 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1583 STATE_CACHE_WRITE_RESPONSE;
1584 return OK;
1587 int HttpCache::Transaction::DoTruncateCachedData() {
1588 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1589 if (!entry_)
1590 return OK;
1591 if (net_log_.IsCapturing())
1592 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1593 // Truncate the stream.
1594 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1597 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1598 if (entry_) {
1599 if (net_log_.IsCapturing()) {
1600 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1601 result);
1605 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1606 return OK;
1609 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1610 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1611 if (!entry_)
1612 return OK;
1614 if (net_log_.IsCapturing())
1615 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1616 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1619 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1620 if (entry_) {
1621 if (net_log_.IsCapturing()) {
1622 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1623 result);
1627 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1628 return OK;
1631 int HttpCache::Transaction::DoPartialHeadersReceived() {
1632 new_response_ = NULL;
1633 if (entry_ && !partial_.get() &&
1634 entry_->disk_entry->GetDataSize(kMetadataIndex))
1635 next_state_ = STATE_CACHE_READ_METADATA;
1637 if (!partial_.get())
1638 return OK;
1640 if (reading_) {
1641 if (network_trans_.get()) {
1642 next_state_ = STATE_NETWORK_READ;
1643 } else {
1644 next_state_ = STATE_CACHE_READ_DATA;
1646 } else if (mode_ != NONE) {
1647 // We are about to return the headers for a byte-range request to the user,
1648 // so let's fix them.
1649 partial_->FixResponseHeaders(response_.headers.get(), true);
1651 return OK;
1654 int HttpCache::Transaction::DoCacheReadResponse() {
1655 DCHECK(entry_);
1656 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1658 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1659 read_buf_ = new IOBuffer(io_buf_len_);
1661 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1662 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1663 io_buf_len_, io_callback_);
1666 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1667 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1668 if (result != io_buf_len_ ||
1669 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1670 &response_, &truncated_)) {
1671 return OnCacheReadError(result, true);
1674 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1675 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1676 ReadCertChain();
1678 // Some resources may have slipped in as truncated when they're not.
1679 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1680 if (response_.headers->GetContentLength() == current_size)
1681 truncated_ = false;
1683 if ((response_.unused_since_prefetch &&
1684 !(request_->load_flags & LOAD_PREFETCH)) ||
1685 (!response_.unused_since_prefetch &&
1686 (request_->load_flags & LOAD_PREFETCH))) {
1687 // Either this is the first use of an entry since it was prefetched or
1688 // this is a prefetch. The value of response.unused_since_prefetch is valid
1689 // for this transaction but the bit needs to be flipped in storage.
1690 next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH;
1691 return OK;
1694 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1695 return OK;
1698 int HttpCache::Transaction::DoCacheDispatchValidation() {
1699 // We now have access to the cache entry.
1701 // o if we are a reader for the transaction, then we can start reading the
1702 // cache entry.
1704 // o if we can read or write, then we should check if the cache entry needs
1705 // to be validated and then issue a network request if needed or just read
1706 // from the cache if the cache entry is already valid.
1708 // o if we are set to UPDATE, then we are handling an externally
1709 // conditionalized request (if-modified-since / if-none-match). We check
1710 // if the request headers define a validation request.
1712 int result = ERR_FAILED;
1713 switch (mode_) {
1714 case READ:
1715 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1716 result = BeginCacheRead();
1717 break;
1718 case READ_WRITE:
1719 result = BeginPartialCacheValidation();
1720 break;
1721 case UPDATE:
1722 result = BeginExternallyConditionalizedRequest();
1723 break;
1724 case WRITE:
1725 default:
1726 NOTREACHED();
1728 return result;
1731 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1732 // Write back the toggled value for the next use of this entry.
1733 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1735 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1736 // transaction then metadata will be written to cache twice. If prefetching
1737 // becomes more common, consider combining the writes.
1738 target_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE;
1739 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1740 return OK;
1743 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1744 int result) {
1745 // Restore the original value for this transaction.
1746 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1747 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1748 return OK;
1751 int HttpCache::Transaction::DoCacheWriteResponse() {
1752 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1753 tracked_objects::ScopedTracker tracking_profile(
1754 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1755 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1757 if (entry_) {
1758 if (net_log_.IsCapturing())
1759 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1761 return WriteResponseInfoToEntry(false);
1764 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1765 if (entry_) {
1766 if (net_log_.IsCapturing())
1767 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1769 return WriteResponseInfoToEntry(true);
1772 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1773 next_state_ = target_state_;
1774 target_state_ = STATE_NONE;
1775 if (!entry_)
1776 return OK;
1777 if (net_log_.IsCapturing()) {
1778 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1779 result);
1782 // Balance the AddRef from WriteResponseInfoToEntry.
1783 if (result != io_buf_len_) {
1784 DLOG(ERROR) << "failed to write response info to cache";
1785 DoneWritingToEntry(false);
1787 return OK;
1790 int HttpCache::Transaction::DoCacheReadMetadata() {
1791 DCHECK(entry_);
1792 DCHECK(!response_.metadata.get());
1793 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1795 response_.metadata =
1796 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1798 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1799 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1800 response_.metadata.get(),
1801 response_.metadata->size(),
1802 io_callback_);
1805 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1806 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1807 if (result != response_.metadata->size())
1808 return OnCacheReadError(result, false);
1809 return OK;
1812 int HttpCache::Transaction::DoCacheQueryData() {
1813 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1814 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1817 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1818 DCHECK_EQ(OK, result);
1819 if (!cache_.get())
1820 return ERR_UNEXPECTED;
1822 return ValidateEntryHeadersAndContinue();
1825 int HttpCache::Transaction::DoCacheReadData() {
1826 DCHECK(entry_);
1827 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1829 if (net_log_.IsCapturing())
1830 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1831 if (partial_.get()) {
1832 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1833 io_callback_);
1836 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1837 read_buf_.get(), io_buf_len_,
1838 io_callback_);
1841 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1842 if (net_log_.IsCapturing()) {
1843 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1844 result);
1847 if (!cache_.get())
1848 return ERR_UNEXPECTED;
1850 if (partial_.get()) {
1851 // Partial requests are confusing to report in histograms because they may
1852 // have multiple underlying requests.
1853 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1854 return DoPartialCacheReadCompleted(result);
1857 if (result > 0) {
1858 read_offset_ += result;
1859 } else if (result == 0) { // End of file.
1860 RecordHistograms();
1861 cache_->DoneReadingFromEntry(entry_, this);
1862 entry_ = NULL;
1863 } else {
1864 return OnCacheReadError(result, false);
1866 return result;
1869 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1870 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1871 write_len_ = num_bytes;
1872 if (entry_) {
1873 if (net_log_.IsCapturing())
1874 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1877 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1880 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1881 if (entry_) {
1882 if (net_log_.IsCapturing()) {
1883 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1884 result);
1887 // Balance the AddRef from DoCacheWriteData.
1888 if (!cache_.get())
1889 return ERR_UNEXPECTED;
1891 if (result != write_len_) {
1892 DLOG(ERROR) << "failed to write response data to cache";
1893 DoneWritingToEntry(false);
1895 // We want to ignore errors writing to disk and just keep reading from
1896 // the network.
1897 result = write_len_;
1898 } else if (!done_reading_ && entry_) {
1899 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1900 int64 body_size = response_.headers->GetContentLength();
1901 if (body_size >= 0 && body_size <= current_size)
1902 done_reading_ = true;
1905 if (partial_.get()) {
1906 // This may be the last request.
1907 if (!(result == 0 && !truncated_ &&
1908 (partial_->IsLastRange() || mode_ == WRITE)))
1909 return DoPartialNetworkReadCompleted(result);
1912 if (result == 0) {
1913 // End of file. This may be the result of a connection problem so see if we
1914 // have to keep the entry around to be flagged as truncated later on.
1915 if (done_reading_ || !entry_ || partial_.get() ||
1916 response_.headers->GetContentLength() <= 0)
1917 DoneWritingToEntry(true);
1920 return result;
1923 //-----------------------------------------------------------------------------
1925 void HttpCache::Transaction::ReadCertChain() {
1926 std::string key =
1927 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
1928 const X509Certificate::OSCertHandles& intermediates =
1929 response_.ssl_info.cert->GetIntermediateCertificates();
1930 int dist_from_root = intermediates.size();
1932 scoped_refptr<SharedChainData> shared_chain_data(
1933 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1934 cache_->cert_cache()->GetCertificate(key,
1935 base::Bind(&OnCertReadIOComplete,
1936 dist_from_root,
1937 true /* is leaf */,
1938 shared_chain_data));
1940 for (X509Certificate::OSCertHandles::const_iterator it =
1941 intermediates.begin();
1942 it != intermediates.end();
1943 ++it) {
1944 --dist_from_root;
1945 key = GetCacheKeyForCert(*it);
1946 cache_->cert_cache()->GetCertificate(key,
1947 base::Bind(&OnCertReadIOComplete,
1948 dist_from_root,
1949 false /* is not leaf */,
1950 shared_chain_data));
1952 DCHECK_EQ(0, dist_from_root);
1955 void HttpCache::Transaction::WriteCertChain() {
1956 const X509Certificate::OSCertHandles& intermediates =
1957 response_.ssl_info.cert->GetIntermediateCertificates();
1958 int dist_from_root = intermediates.size();
1960 scoped_refptr<SharedChainData> shared_chain_data(
1961 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1962 cache_->cert_cache()->SetCertificate(
1963 response_.ssl_info.cert->os_cert_handle(),
1964 base::Bind(&OnCertWriteIOComplete,
1965 dist_from_root,
1966 true /* is leaf */,
1967 shared_chain_data));
1968 for (X509Certificate::OSCertHandles::const_iterator it =
1969 intermediates.begin();
1970 it != intermediates.end();
1971 ++it) {
1972 --dist_from_root;
1973 cache_->cert_cache()->SetCertificate(*it,
1974 base::Bind(&OnCertWriteIOComplete,
1975 dist_from_root,
1976 false /* is not leaf */,
1977 shared_chain_data));
1979 DCHECK_EQ(0, dist_from_root);
1982 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1983 const HttpRequestInfo* request) {
1984 net_log_ = net_log;
1985 request_ = request;
1986 effective_load_flags_ = request_->load_flags;
1988 if (cache_->mode() == DISABLE)
1989 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1991 // Some headers imply load flags. The order here is significant.
1993 // LOAD_DISABLE_CACHE : no cache read or write
1994 // LOAD_BYPASS_CACHE : no cache read
1995 // LOAD_VALIDATE_CACHE : no cache read unless validation
1997 // The former modes trump latter modes, so if we find a matching header we
1998 // can stop iterating kSpecialHeaders.
2000 static const struct {
2001 const HeaderNameAndValue* search;
2002 int load_flag;
2003 } kSpecialHeaders[] = {
2004 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
2005 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
2006 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
2009 bool range_found = false;
2010 bool external_validation_error = false;
2011 bool special_headers = false;
2013 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
2014 range_found = true;
2016 for (size_t i = 0; i < arraysize(kSpecialHeaders); ++i) {
2017 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
2018 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
2019 special_headers = true;
2020 break;
2024 // Check for conditionalization headers which may correspond with a
2025 // cache validation request.
2026 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
2027 const ValidationHeaderInfo& info = kValidationHeaders[i];
2028 std::string validation_value;
2029 if (request_->extra_headers.GetHeader(
2030 info.request_header_name, &validation_value)) {
2031 if (!external_validation_.values[i].empty() ||
2032 validation_value.empty()) {
2033 external_validation_error = true;
2035 external_validation_.values[i] = validation_value;
2036 external_validation_.initialized = true;
2040 if (range_found || special_headers || external_validation_.initialized) {
2041 // Log the headers before request_ is modified.
2042 std::string empty;
2043 net_log_.AddEvent(
2044 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS,
2045 base::Bind(&HttpRequestHeaders::NetLogCallback,
2046 base::Unretained(&request_->extra_headers), &empty));
2049 // We don't support ranges and validation headers.
2050 if (range_found && external_validation_.initialized) {
2051 LOG(WARNING) << "Byte ranges AND validation headers found.";
2052 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2055 // If there is more than one validation header, we can't treat this request as
2056 // a cache validation, since we don't know for sure which header the server
2057 // will give us a response for (and they could be contradictory).
2058 if (external_validation_error) {
2059 LOG(WARNING) << "Multiple or malformed validation headers found.";
2060 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2063 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2064 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2065 partial_.reset(new PartialData);
2066 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
2067 // We will be modifying the actual range requested to the server, so
2068 // let's remove the header here.
2069 custom_request_.reset(new HttpRequestInfo(*request_));
2070 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2071 request_ = custom_request_.get();
2072 partial_->SetHeaders(custom_request_->extra_headers);
2073 } else {
2074 // The range is invalid or we cannot handle it properly.
2075 VLOG(1) << "Invalid byte range found.";
2076 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2077 partial_.reset(NULL);
2082 bool HttpCache::Transaction::ShouldPassThrough() {
2083 // We may have a null disk_cache if there is an error we cannot recover from,
2084 // like not enough disk space, or sharing violations.
2085 if (!cache_->disk_cache_.get())
2086 return true;
2088 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2089 return true;
2091 if (request_->method == "GET" || request_->method == "HEAD")
2092 return false;
2094 if (request_->method == "POST" && request_->upload_data_stream &&
2095 request_->upload_data_stream->identifier()) {
2096 return false;
2099 if (request_->method == "PUT" && request_->upload_data_stream)
2100 return false;
2102 if (request_->method == "DELETE")
2103 return false;
2105 return true;
2108 int HttpCache::Transaction::BeginCacheRead() {
2109 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2110 if (response_.headers->response_code() == 206 || partial_.get()) {
2111 NOTREACHED();
2112 return ERR_CACHE_MISS;
2115 if (request_->method == "HEAD")
2116 FixHeadersForHead();
2118 // We don't have the whole resource.
2119 if (truncated_)
2120 return ERR_CACHE_MISS;
2122 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2123 next_state_ = STATE_CACHE_READ_METADATA;
2125 return OK;
2128 int HttpCache::Transaction::BeginCacheValidation() {
2129 DCHECK(mode_ == READ_WRITE);
2131 ValidationType required_validation = RequiresValidation();
2133 bool skip_validation = (required_validation == VALIDATION_NONE);
2135 if (required_validation == VALIDATION_ASYNCHRONOUS &&
2136 !(request_->method == "GET" && (truncated_ || partial_)) && cache_ &&
2137 cache_->use_stale_while_revalidate()) {
2138 TriggerAsyncValidation();
2139 skip_validation = true;
2142 if (request_->method == "HEAD" &&
2143 (truncated_ || response_.headers->response_code() == 206)) {
2144 DCHECK(!partial_);
2145 if (skip_validation)
2146 return SetupEntryForRead();
2148 // Bail out!
2149 next_state_ = STATE_SEND_REQUEST;
2150 mode_ = NONE;
2151 return OK;
2154 if (truncated_) {
2155 // Truncated entries can cause partial gets, so we shouldn't record this
2156 // load in histograms.
2157 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2158 skip_validation = !partial_->initial_validation();
2161 if (partial_.get() && (is_sparse_ || truncated_) &&
2162 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
2163 // Force revalidation for sparse or truncated entries. Note that we don't
2164 // want to ignore the regular validation logic just because a byte range was
2165 // part of the request.
2166 skip_validation = false;
2169 if (skip_validation) {
2170 // TODO(ricea): Is this pattern okay for asynchronous revalidations?
2171 UpdateTransactionPattern(PATTERN_ENTRY_USED);
2172 return SetupEntryForRead();
2173 } else {
2174 // Make the network request conditional, to see if we may reuse our cached
2175 // response. If we cannot do so, then we just resort to a normal fetch.
2176 // Our mode remains READ_WRITE for a conditional request. Even if the
2177 // conditionalization fails, we don't switch to WRITE mode until we
2178 // know we won't be falling back to using the cache entry in the
2179 // LOAD_FROM_CACHE_IF_OFFLINE case.
2180 if (!ConditionalizeRequest()) {
2181 couldnt_conditionalize_request_ = true;
2182 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
2183 if (partial_.get())
2184 return DoRestartPartialRequest();
2186 DCHECK_NE(206, response_.headers->response_code());
2188 next_state_ = STATE_SEND_REQUEST;
2190 return OK;
2193 int HttpCache::Transaction::BeginPartialCacheValidation() {
2194 DCHECK(mode_ == READ_WRITE);
2196 if (response_.headers->response_code() != 206 && !partial_.get() &&
2197 !truncated_) {
2198 return BeginCacheValidation();
2201 // Partial requests should not be recorded in histograms.
2202 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2203 if (range_requested_) {
2204 next_state_ = STATE_CACHE_QUERY_DATA;
2205 return OK;
2208 // The request is not for a range, but we have stored just ranges.
2210 if (request_->method == "HEAD")
2211 return BeginCacheValidation();
2213 partial_.reset(new PartialData());
2214 partial_->SetHeaders(request_->extra_headers);
2215 if (!custom_request_.get()) {
2216 custom_request_.reset(new HttpRequestInfo(*request_));
2217 request_ = custom_request_.get();
2220 return ValidateEntryHeadersAndContinue();
2223 // This should only be called once per request.
2224 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2225 DCHECK(mode_ == READ_WRITE);
2227 if (!partial_->UpdateFromStoredHeaders(
2228 response_.headers.get(), entry_->disk_entry, truncated_)) {
2229 return DoRestartPartialRequest();
2232 if (response_.headers->response_code() == 206)
2233 is_sparse_ = true;
2235 if (!partial_->IsRequestedRangeOK()) {
2236 // The stored data is fine, but the request may be invalid.
2237 invalid_range_ = true;
2240 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2241 return OK;
2244 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2245 DCHECK_EQ(UPDATE, mode_);
2246 DCHECK(external_validation_.initialized);
2248 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
2249 if (external_validation_.values[i].empty())
2250 continue;
2251 // Retrieve either the cached response's "etag" or "last-modified" header.
2252 std::string validator;
2253 response_.headers->EnumerateHeader(
2254 NULL,
2255 kValidationHeaders[i].related_response_header_name,
2256 &validator);
2258 if (response_.headers->response_code() != 200 || truncated_ ||
2259 validator.empty() || validator != external_validation_.values[i]) {
2260 // The externally conditionalized request is not a validation request
2261 // for our existing cache entry. Proceed with caching disabled.
2262 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2263 DoneWritingToEntry(true);
2267 // TODO(ricea): This calculation is expensive to perform just to collect
2268 // statistics. Either remove it or use the result, depending on the result of
2269 // the experiment.
2270 ExternallyConditionalizedType type =
2271 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE;
2272 if (mode_ == NONE)
2273 type = EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS;
2274 else if (RequiresValidation())
2275 type = EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION;
2277 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2278 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2279 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2280 type,
2281 EXTERNALLY_CONDITIONALIZED_MAX);
2283 next_state_ = STATE_SEND_REQUEST;
2284 return OK;
2287 int HttpCache::Transaction::RestartNetworkRequest() {
2288 DCHECK(mode_ & WRITE || mode_ == NONE);
2289 DCHECK(network_trans_.get());
2290 DCHECK_EQ(STATE_NONE, next_state_);
2292 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2293 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
2294 if (rv != ERR_IO_PENDING)
2295 return DoLoop(rv);
2296 return rv;
2299 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2300 X509Certificate* client_cert) {
2301 DCHECK(mode_ & WRITE || mode_ == NONE);
2302 DCHECK(network_trans_.get());
2303 DCHECK_EQ(STATE_NONE, next_state_);
2305 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2306 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
2307 if (rv != ERR_IO_PENDING)
2308 return DoLoop(rv);
2309 return rv;
2312 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2313 const AuthCredentials& credentials) {
2314 DCHECK(mode_ & WRITE || mode_ == NONE);
2315 DCHECK(network_trans_.get());
2316 DCHECK_EQ(STATE_NONE, next_state_);
2318 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2319 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
2320 if (rv != ERR_IO_PENDING)
2321 return DoLoop(rv);
2322 return rv;
2325 ValidationType HttpCache::Transaction::RequiresValidation() {
2326 // TODO(darin): need to do more work here:
2327 // - make sure we have a matching request method
2328 // - watch out for cached responses that depend on authentication
2330 if (response_.vary_data.is_valid() &&
2331 !response_.vary_data.MatchesRequest(*request_,
2332 *response_.headers.get())) {
2333 vary_mismatch_ = true;
2334 return VALIDATION_SYNCHRONOUS;
2337 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
2338 return VALIDATION_NONE;
2340 if (response_.unused_since_prefetch &&
2341 !(effective_load_flags_ & LOAD_PREFETCH) &&
2342 response_.headers->GetCurrentAge(
2343 response_.request_time, response_.response_time,
2344 cache_->clock_->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins)) {
2345 // The first use of a resource after prefetch within a short window skips
2346 // validation.
2347 return VALIDATION_NONE;
2350 if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION))
2351 return VALIDATION_SYNCHRONOUS;
2353 if (request_->method == "PUT" || request_->method == "DELETE")
2354 return VALIDATION_SYNCHRONOUS;
2356 ValidationType validation_required_by_headers =
2357 response_.headers->RequiresValidation(response_.request_time,
2358 response_.response_time,
2359 cache_->clock_->Now());
2361 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) {
2362 // Asynchronous revalidation is only supported for GET and HEAD methods.
2363 if (request_->method != "GET" && request_->method != "HEAD")
2364 return VALIDATION_SYNCHRONOUS;
2367 return validation_required_by_headers;
2370 bool HttpCache::Transaction::ConditionalizeRequest() {
2371 DCHECK(response_.headers.get());
2373 if (request_->method == "PUT" || request_->method == "DELETE")
2374 return false;
2376 // This only makes sense for cached 200 or 206 responses.
2377 if (response_.headers->response_code() != 200 &&
2378 response_.headers->response_code() != 206) {
2379 return false;
2382 if (fail_conditionalization_for_test_)
2383 return false;
2385 DCHECK(response_.headers->response_code() != 206 ||
2386 response_.headers->HasStrongValidators());
2388 // Just use the first available ETag and/or Last-Modified header value.
2389 // TODO(darin): Or should we use the last?
2391 std::string etag_value;
2392 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2393 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2395 std::string last_modified_value;
2396 if (!vary_mismatch_) {
2397 response_.headers->EnumerateHeader(NULL, "last-modified",
2398 &last_modified_value);
2401 if (etag_value.empty() && last_modified_value.empty())
2402 return false;
2404 if (!partial_.get()) {
2405 // Need to customize the request, so this forces us to allocate :(
2406 custom_request_.reset(new HttpRequestInfo(*request_));
2407 request_ = custom_request_.get();
2409 DCHECK(custom_request_.get());
2411 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2412 !invalid_range_;
2414 if (!use_if_range) {
2415 // stale-while-revalidate is not useful when we only have a partial response
2416 // cached, so don't set the header in that case.
2417 HttpResponseHeaders::FreshnessLifetimes lifetimes =
2418 response_.headers->GetFreshnessLifetimes(response_.response_time);
2419 if (lifetimes.staleness > TimeDelta()) {
2420 TimeDelta current_age = response_.headers->GetCurrentAge(
2421 response_.request_time, response_.response_time,
2422 cache_->clock_->Now());
2424 custom_request_->extra_headers.SetHeader(
2425 kFreshnessHeader,
2426 base::StringPrintf("max-age=%" PRId64
2427 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2428 lifetimes.freshness.InSeconds(),
2429 lifetimes.staleness.InSeconds(),
2430 current_age.InSeconds()));
2434 if (!etag_value.empty()) {
2435 if (use_if_range) {
2436 // We don't want to switch to WRITE mode if we don't have this block of a
2437 // byte-range request because we may have other parts cached.
2438 custom_request_->extra_headers.SetHeader(
2439 HttpRequestHeaders::kIfRange, etag_value);
2440 } else {
2441 custom_request_->extra_headers.SetHeader(
2442 HttpRequestHeaders::kIfNoneMatch, etag_value);
2444 // For byte-range requests, make sure that we use only one way to validate
2445 // the request.
2446 if (partial_.get() && !partial_->IsCurrentRangeCached())
2447 return true;
2450 if (!last_modified_value.empty()) {
2451 if (use_if_range) {
2452 custom_request_->extra_headers.SetHeader(
2453 HttpRequestHeaders::kIfRange, last_modified_value);
2454 } else {
2455 custom_request_->extra_headers.SetHeader(
2456 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2460 return true;
2463 // We just received some headers from the server. We may have asked for a range,
2464 // in which case partial_ has an object. This could be the first network request
2465 // we make to fulfill the original request, or we may be already reading (from
2466 // the net and / or the cache). If we are not expecting a certain response, we
2467 // just bypass the cache for this request (but again, maybe we are reading), and
2468 // delete partial_ (so we are not able to "fix" the headers that we return to
2469 // the user). This results in either a weird response for the caller (we don't
2470 // expect it after all), or maybe a range that was not exactly what it was asked
2471 // for.
2473 // If the server is simply telling us that the resource has changed, we delete
2474 // the cached entry and restart the request as the caller intended (by returning
2475 // false from this method). However, we may not be able to do that at any point,
2476 // for instance if we already returned the headers to the user.
2478 // WARNING: Whenever this code returns false, it has to make sure that the next
2479 // time it is called it will return true so that we don't keep retrying the
2480 // request.
2481 bool HttpCache::Transaction::ValidatePartialResponse() {
2482 const HttpResponseHeaders* headers = new_response_->headers.get();
2483 int response_code = headers->response_code();
2484 bool partial_response = (response_code == 206);
2485 handling_206_ = false;
2487 if (!entry_ || request_->method != "GET")
2488 return true;
2490 if (invalid_range_) {
2491 // We gave up trying to match this request with the stored data. If the
2492 // server is ok with the request, delete the entry, otherwise just ignore
2493 // this request
2494 DCHECK(!reading_);
2495 if (partial_response || response_code == 200) {
2496 DoomPartialEntry(true);
2497 mode_ = NONE;
2498 } else {
2499 if (response_code == 304)
2500 FailRangeRequest();
2501 IgnoreRangeRequest();
2503 return true;
2506 if (!partial_.get()) {
2507 // We are not expecting 206 but we may have one.
2508 if (partial_response)
2509 IgnoreRangeRequest();
2511 return true;
2514 // TODO(rvargas): Do we need to consider other results here?.
2515 bool failure = response_code == 200 || response_code == 416;
2517 if (partial_->IsCurrentRangeCached()) {
2518 // We asked for "If-None-Match: " so a 206 means a new object.
2519 if (partial_response)
2520 failure = true;
2522 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2523 return true;
2524 } else {
2525 // We asked for "If-Range: " so a 206 means just another range.
2526 if (partial_response) {
2527 if (partial_->ResponseHeadersOK(headers)) {
2528 handling_206_ = true;
2529 return true;
2530 } else {
2531 failure = true;
2535 if (!reading_ && !is_sparse_ && !partial_response) {
2536 // See if we can ignore the fact that we issued a byte range request.
2537 // If the server sends 200, just store it. If it sends an error, redirect
2538 // or something else, we may store the response as long as we didn't have
2539 // anything already stored.
2540 if (response_code == 200 ||
2541 (!truncated_ && response_code != 304 && response_code != 416)) {
2542 // The server is sending something else, and we can save it.
2543 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2544 partial_.reset();
2545 truncated_ = false;
2546 return true;
2550 // 304 is not expected here, but we'll spare the entry (unless it was
2551 // truncated).
2552 if (truncated_)
2553 failure = true;
2556 if (failure) {
2557 // We cannot truncate this entry, it has to be deleted.
2558 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2559 mode_ = NONE;
2560 if (is_sparse_ || truncated_) {
2561 // There was something cached to start with, either sparsed data (206), or
2562 // a truncated 200, which means that we probably modified the request,
2563 // adding a byte range or modifying the range requested by the caller.
2564 if (!reading_ && !partial_->IsLastRange()) {
2565 // We have not returned anything to the caller yet so it should be safe
2566 // to issue another network request, this time without us messing up the
2567 // headers.
2568 ResetPartialState(true);
2569 return false;
2571 LOG(WARNING) << "Failed to revalidate partial entry";
2573 DoomPartialEntry(true);
2574 return true;
2577 IgnoreRangeRequest();
2578 return true;
2581 void HttpCache::Transaction::IgnoreRangeRequest() {
2582 // We have a problem. We may or may not be reading already (in which case we
2583 // returned the headers), but we'll just pretend that this request is not
2584 // using the cache and see what happens. Most likely this is the first
2585 // response from the server (it's not changing its mind midway, right?).
2586 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2587 if (mode_ & WRITE)
2588 DoneWritingToEntry(mode_ != WRITE);
2589 else if (mode_ & READ && entry_)
2590 cache_->DoneReadingFromEntry(entry_, this);
2592 partial_.reset(NULL);
2593 entry_ = NULL;
2594 mode_ = NONE;
2597 void HttpCache::Transaction::FixHeadersForHead() {
2598 if (response_.headers->response_code() == 206) {
2599 response_.headers->RemoveHeader("Content-Range");
2600 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2604 void HttpCache::Transaction::TriggerAsyncValidation() {
2605 DCHECK(!request_->upload_data_stream);
2606 BoundNetLog async_revalidation_net_log(
2607 BoundNetLog::Make(net_log_.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION));
2608 net_log_.AddEvent(
2609 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC,
2610 async_revalidation_net_log.source().ToEventParametersCallback());
2611 async_revalidation_net_log.BeginEvent(
2612 NetLog::TYPE_ASYNC_REVALIDATION,
2613 base::Bind(
2614 &NetLogAsyncRevalidationInfoCallback, net_log_.source(), request_));
2615 base::MessageLoop::current()->PostTask(
2616 FROM_HERE,
2617 base::Bind(&HttpCache::PerformAsyncValidation,
2618 cache_, // cache_ is a weak pointer.
2619 *request_,
2620 async_revalidation_net_log));
2623 void HttpCache::Transaction::FailRangeRequest() {
2624 response_ = *new_response_;
2625 partial_->FixResponseHeaders(response_.headers.get(), false);
2628 int HttpCache::Transaction::SetupEntryForRead() {
2629 if (network_trans_)
2630 ResetNetworkTransaction();
2631 if (partial_.get()) {
2632 if (truncated_ || is_sparse_ || !invalid_range_) {
2633 // We are going to return the saved response headers to the caller, so
2634 // we may need to adjust them first.
2635 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2636 return OK;
2637 } else {
2638 partial_.reset();
2641 cache_->ConvertWriterToReader(entry_);
2642 mode_ = READ;
2644 if (request_->method == "HEAD")
2645 FixHeadersForHead();
2647 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2648 next_state_ = STATE_CACHE_READ_METADATA;
2649 return OK;
2653 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2654 read_buf_ = data;
2655 io_buf_len_ = data_len;
2656 next_state_ = STATE_NETWORK_READ;
2657 return DoLoop(OK);
2660 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2661 if (request_->method == "HEAD")
2662 return 0;
2664 read_buf_ = data;
2665 io_buf_len_ = data_len;
2666 next_state_ = STATE_CACHE_READ_DATA;
2667 return DoLoop(OK);
2670 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2671 IOBuffer* data, int data_len,
2672 const CompletionCallback& callback) {
2673 if (!entry_)
2674 return data_len;
2676 int rv = 0;
2677 if (!partial_.get() || !data_len) {
2678 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2679 true);
2680 } else {
2681 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2683 return rv;
2686 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2687 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2688 if (!entry_)
2689 return OK;
2691 // Do not cache no-store content. Do not cache content with cert errors
2692 // either. This is to prevent not reporting net errors when loading a
2693 // resource from the cache. When we load a page over HTTPS with a cert error
2694 // we show an SSL blocking page. If the user clicks proceed we reload the
2695 // resource ignoring the errors. The loaded resource is then cached. If that
2696 // resource is subsequently loaded from the cache, no net error is reported
2697 // (even though the cert status contains the actual errors) and no SSL
2698 // blocking page is shown. An alternative would be to reverse-map the cert
2699 // status to a net error and replay the net error.
2700 if ((response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2701 IsCertStatusError(response_.ssl_info.cert_status)) {
2702 DoneWritingToEntry(false);
2703 if (net_log_.IsCapturing())
2704 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2705 return OK;
2708 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2709 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2710 WriteCertChain();
2712 if (truncated)
2713 DCHECK_EQ(200, response_.headers->response_code());
2715 // When writing headers, we normally only write the non-transient headers.
2716 bool skip_transient_headers = true;
2717 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2718 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2719 data->Done();
2721 io_buf_len_ = data->pickle()->size();
2722 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2723 io_buf_len_, io_callback_, true);
2726 int HttpCache::Transaction::AppendResponseDataToEntry(
2727 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2728 if (!entry_ || !data_len)
2729 return data_len;
2731 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2732 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2733 callback);
2736 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2737 if (!entry_)
2738 return;
2740 RecordHistograms();
2742 cache_->DoneWritingToEntry(entry_, success);
2743 entry_ = NULL;
2744 mode_ = NONE; // switch to 'pass through' mode
2747 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2748 DLOG(ERROR) << "ReadData failed: " << result;
2749 const int result_for_histogram = std::max(0, -result);
2750 if (restart) {
2751 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2752 result_for_histogram);
2753 } else {
2754 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2755 result_for_histogram);
2758 // Avoid using this entry in the future.
2759 if (cache_.get())
2760 cache_->DoomActiveEntry(cache_key_);
2762 if (restart) {
2763 DCHECK(!reading_);
2764 DCHECK(!network_trans_.get());
2765 cache_->DoneWithEntry(entry_, this, false);
2766 entry_ = NULL;
2767 is_sparse_ = false;
2768 partial_.reset();
2769 next_state_ = STATE_GET_BACKEND;
2770 return OK;
2773 return ERR_CACHE_READ_FAILURE;
2776 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
2777 if (entry_lock_waiting_since_ != start_time)
2778 return;
2780 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
2782 if (!cache_)
2783 return;
2785 cache_->RemovePendingTransaction(this);
2786 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
2789 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2790 DVLOG(2) << "DoomPartialEntry";
2791 int rv = cache_->DoomEntry(cache_key_, NULL);
2792 DCHECK_EQ(OK, rv);
2793 cache_->DoneWithEntry(entry_, this, false);
2794 entry_ = NULL;
2795 is_sparse_ = false;
2796 truncated_ = false;
2797 if (delete_object)
2798 partial_.reset(NULL);
2801 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2802 partial_->OnNetworkReadCompleted(result);
2804 if (result == 0) {
2805 // We need to move on to the next range.
2806 ResetNetworkTransaction();
2807 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2809 return result;
2812 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2813 partial_->OnCacheReadCompleted(result);
2815 if (result == 0 && mode_ == READ_WRITE) {
2816 // We need to move on to the next range.
2817 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2818 } else if (result < 0) {
2819 return OnCacheReadError(result, false);
2821 return result;
2824 int HttpCache::Transaction::DoRestartPartialRequest() {
2825 // The stored data cannot be used. Get rid of it and restart this request.
2826 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST);
2828 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
2829 // to Doom the entry again).
2830 mode_ = WRITE;
2831 ResetPartialState(!range_requested_);
2832 next_state_ = STATE_CREATE_ENTRY;
2833 return OK;
2836 void HttpCache::Transaction::ResetPartialState(bool delete_object) {
2837 partial_->RestoreHeaders(&custom_request_->extra_headers);
2838 DoomPartialEntry(delete_object);
2840 if (!delete_object) {
2841 // The simplest way to re-initialize partial_ is to create a new object.
2842 partial_.reset(new PartialData());
2843 if (partial_->Init(request_->extra_headers))
2844 partial_->SetHeaders(custom_request_->extra_headers);
2845 else
2846 partial_.reset();
2850 void HttpCache::Transaction::ResetNetworkTransaction() {
2851 DCHECK(!old_network_trans_load_timing_);
2852 DCHECK(network_trans_);
2853 LoadTimingInfo load_timing;
2854 if (network_trans_->GetLoadTimingInfo(&load_timing))
2855 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2856 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2857 ConnectionAttempts attempts;
2858 network_trans_->GetConnectionAttempts(&attempts);
2859 for (const auto& attempt : attempts)
2860 old_connection_attempts_.push_back(attempt);
2861 network_trans_.reset();
2864 // Histogram data from the end of 2010 show the following distribution of
2865 // response headers:
2867 // Content-Length............... 87%
2868 // Date......................... 98%
2869 // Last-Modified................ 49%
2870 // Etag......................... 19%
2871 // Accept-Ranges: bytes......... 25%
2872 // Accept-Ranges: none.......... 0.4%
2873 // Strong Validator............. 50%
2874 // Strong Validator + ranges.... 24%
2875 // Strong Validator + CL........ 49%
2877 bool HttpCache::Transaction::CanResume(bool has_data) {
2878 // Double check that there is something worth keeping.
2879 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2880 return false;
2882 if (request_->method != "GET")
2883 return false;
2885 // Note that if this is a 206, content-length was already fixed after calling
2886 // PartialData::ResponseHeadersOK().
2887 if (response_.headers->GetContentLength() <= 0 ||
2888 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2889 !response_.headers->HasStrongValidators()) {
2890 return false;
2893 return true;
2896 void HttpCache::Transaction::UpdateTransactionPattern(
2897 TransactionPattern new_transaction_pattern) {
2898 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2899 return;
2900 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2901 new_transaction_pattern == PATTERN_NOT_COVERED);
2902 transaction_pattern_ = new_transaction_pattern;
2905 void HttpCache::Transaction::RecordHistograms() {
2906 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2907 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2908 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2909 cache_->mode() != NORMAL || request_->method != "GET") {
2910 return;
2912 UMA_HISTOGRAM_ENUMERATION(
2913 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2914 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2915 return;
2916 DCHECK(!range_requested_);
2917 DCHECK(!first_cache_access_since_.is_null());
2919 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2921 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2923 bool did_send_request = !send_request_since_.is_null();
2924 DCHECK(
2925 (did_send_request &&
2926 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2927 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2928 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2929 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2930 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2932 if (!did_send_request) {
2933 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2934 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2935 return;
2938 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2939 int64 before_send_percent = (total_time.ToInternalValue() == 0) ?
2940 0 : before_send_time * 100 / total_time;
2941 DCHECK_GE(before_send_percent, 0);
2942 DCHECK_LE(before_send_percent, 100);
2943 base::HistogramBase::Sample before_send_sample =
2944 static_cast<base::HistogramBase::Sample>(before_send_percent);
2946 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2947 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2948 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample);
2950 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2951 // below this comment after we have received initial data.
2952 switch (transaction_pattern_) {
2953 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2954 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2955 before_send_time);
2956 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2957 before_send_sample);
2958 break;
2960 case PATTERN_ENTRY_NOT_CACHED: {
2961 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2962 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2963 before_send_sample);
2964 break;
2966 case PATTERN_ENTRY_VALIDATED: {
2967 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2968 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2969 before_send_sample);
2970 break;
2972 case PATTERN_ENTRY_UPDATED: {
2973 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2974 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2975 before_send_sample);
2976 break;
2978 default:
2979 NOTREACHED();
2983 void HttpCache::Transaction::OnIOComplete(int result) {
2984 DoLoop(result);
2987 } // namespace net