Move LowerCaseEqualsASCII to base namespace
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blobdd4b35731ff38016ec12c172858edefb85b86c12
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 (base::LowerCaseEqualsASCII(v.value_begin(), v.value_end(),
258 search->value))
259 return true;
262 return false;
265 //-----------------------------------------------------------------------------
267 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
268 : next_state_(STATE_NONE),
269 request_(NULL),
270 priority_(priority),
271 cache_(cache->GetWeakPtr()),
272 entry_(NULL),
273 new_entry_(NULL),
274 new_response_(NULL),
275 mode_(NONE),
276 target_state_(STATE_NONE),
277 reading_(false),
278 invalid_range_(false),
279 truncated_(false),
280 is_sparse_(false),
281 range_requested_(false),
282 handling_206_(false),
283 cache_pending_(false),
284 done_reading_(false),
285 vary_mismatch_(false),
286 couldnt_conditionalize_request_(false),
287 bypass_lock_for_test_(false),
288 fail_conditionalization_for_test_(false),
289 io_buf_len_(0),
290 read_offset_(0),
291 effective_load_flags_(0),
292 write_len_(0),
293 transaction_pattern_(PATTERN_UNDEFINED),
294 total_received_bytes_(0),
295 websocket_handshake_stream_base_create_helper_(NULL),
296 weak_factory_(this) {
297 static_assert(HttpCache::Transaction::kNumValidationHeaders ==
298 arraysize(kValidationHeaders),
299 "invalid number of validation headers");
301 io_callback_ = base::Bind(&Transaction::OnIOComplete,
302 weak_factory_.GetWeakPtr());
305 HttpCache::Transaction::~Transaction() {
306 // We may have to issue another IO, but we should never invoke the callback_
307 // after this point.
308 callback_.Reset();
310 if (cache_) {
311 if (entry_) {
312 bool cancel_request = reading_ && response_.headers.get();
313 if (cancel_request) {
314 if (partial_) {
315 entry_->disk_entry->CancelSparseIO();
316 } else {
317 cancel_request &= (response_.headers->response_code() == 200);
321 cache_->DoneWithEntry(entry_, this, cancel_request);
322 } else if (cache_pending_) {
323 cache_->RemovePendingTransaction(this);
328 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
329 const CompletionCallback& callback) {
330 DCHECK(buf);
331 DCHECK_GT(buf_len, 0);
332 DCHECK(!callback.is_null());
333 if (!cache_.get() || !entry_)
334 return ERR_UNEXPECTED;
336 // We don't need to track this operation for anything.
337 // It could be possible to check if there is something already written and
338 // avoid writing again (it should be the same, right?), but let's allow the
339 // caller to "update" the contents with something new.
340 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
341 callback, true);
344 bool HttpCache::Transaction::AddTruncatedFlag() {
345 DCHECK(mode_ & WRITE || mode_ == NONE);
347 // Don't set the flag for sparse entries.
348 if (partial_.get() && !truncated_)
349 return true;
351 if (!CanResume(true))
352 return false;
354 // We may have received the whole resource already.
355 if (done_reading_)
356 return true;
358 truncated_ = true;
359 target_state_ = STATE_NONE;
360 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
361 DoLoop(OK);
362 return true;
365 LoadState HttpCache::Transaction::GetWriterLoadState() const {
366 if (network_trans_.get())
367 return network_trans_->GetLoadState();
368 if (entry_ || !request_)
369 return LOAD_STATE_IDLE;
370 return LOAD_STATE_WAITING_FOR_CACHE;
373 const BoundNetLog& HttpCache::Transaction::net_log() const {
374 return net_log_;
377 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
378 const CompletionCallback& callback,
379 const BoundNetLog& net_log) {
380 DCHECK(request);
381 DCHECK(!callback.is_null());
383 // Ensure that we only have one asynchronous call at a time.
384 DCHECK(callback_.is_null());
385 DCHECK(!reading_);
386 DCHECK(!network_trans_.get());
387 DCHECK(!entry_);
389 if (!cache_.get())
390 return ERR_UNEXPECTED;
392 SetRequest(net_log, request);
394 // We have to wait until the backend is initialized so we start the SM.
395 next_state_ = STATE_GET_BACKEND;
396 int rv = DoLoop(OK);
398 // Setting this here allows us to check for the existence of a callback_ to
399 // determine if we are still inside Start.
400 if (rv == ERR_IO_PENDING)
401 callback_ = callback;
403 return rv;
406 int HttpCache::Transaction::RestartIgnoringLastError(
407 const CompletionCallback& callback) {
408 DCHECK(!callback.is_null());
410 // Ensure that we only have one asynchronous call at a time.
411 DCHECK(callback_.is_null());
413 if (!cache_.get())
414 return ERR_UNEXPECTED;
416 int rv = RestartNetworkRequest();
418 if (rv == ERR_IO_PENDING)
419 callback_ = callback;
421 return rv;
424 int HttpCache::Transaction::RestartWithCertificate(
425 X509Certificate* client_cert,
426 const CompletionCallback& callback) {
427 DCHECK(!callback.is_null());
429 // Ensure that we only have one asynchronous call at a time.
430 DCHECK(callback_.is_null());
432 if (!cache_.get())
433 return ERR_UNEXPECTED;
435 int rv = RestartNetworkRequestWithCertificate(client_cert);
437 if (rv == ERR_IO_PENDING)
438 callback_ = callback;
440 return rv;
443 int HttpCache::Transaction::RestartWithAuth(
444 const AuthCredentials& credentials,
445 const CompletionCallback& callback) {
446 DCHECK(auth_response_.headers.get());
447 DCHECK(!callback.is_null());
449 // Ensure that we only have one asynchronous call at a time.
450 DCHECK(callback_.is_null());
452 if (!cache_.get())
453 return ERR_UNEXPECTED;
455 // Clear the intermediate response since we are going to start over.
456 auth_response_ = HttpResponseInfo();
458 int rv = RestartNetworkRequestWithAuth(credentials);
460 if (rv == ERR_IO_PENDING)
461 callback_ = callback;
463 return rv;
466 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
467 if (!network_trans_.get())
468 return false;
469 return network_trans_->IsReadyToRestartForAuth();
472 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
473 const CompletionCallback& callback) {
474 DCHECK(buf);
475 DCHECK_GT(buf_len, 0);
476 DCHECK(!callback.is_null());
478 DCHECK(callback_.is_null());
480 if (!cache_.get())
481 return ERR_UNEXPECTED;
483 // If we have an intermediate auth response at this point, then it means the
484 // user wishes to read the network response (the error page). If there is a
485 // previous response in the cache then we should leave it intact.
486 if (auth_response_.headers.get() && mode_ != NONE) {
487 UpdateTransactionPattern(PATTERN_NOT_COVERED);
488 DCHECK(mode_ & WRITE);
489 DoneWritingToEntry(mode_ == READ_WRITE);
490 mode_ = NONE;
493 reading_ = true;
494 int rv;
496 switch (mode_) {
497 case READ_WRITE:
498 DCHECK(partial_.get());
499 if (!network_trans_.get()) {
500 // We are just reading from the cache, but we may be writing later.
501 rv = ReadFromEntry(buf, buf_len);
502 break;
504 case NONE:
505 case WRITE:
506 DCHECK(network_trans_.get());
507 rv = ReadFromNetwork(buf, buf_len);
508 break;
509 case READ:
510 rv = ReadFromEntry(buf, buf_len);
511 break;
512 default:
513 NOTREACHED();
514 rv = ERR_FAILED;
517 if (rv == ERR_IO_PENDING) {
518 DCHECK(callback_.is_null());
519 callback_ = callback;
521 return rv;
524 void HttpCache::Transaction::StopCaching() {
525 // We really don't know where we are now. Hopefully there is no operation in
526 // progress, but nothing really prevents this method to be called after we
527 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
528 // point because we need the state machine for that (and even if we are really
529 // free, that would be an asynchronous operation). In other words, keep the
530 // entry how it is (it will be marked as truncated at destruction), and let
531 // the next piece of code that executes know that we are now reading directly
532 // from the net.
533 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
534 // future request for the resource will be blocked on this one.
535 // Fix this.
536 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
537 !is_sparse_ && !range_requested_) {
538 mode_ = NONE;
542 bool HttpCache::Transaction::GetFullRequestHeaders(
543 HttpRequestHeaders* headers) const {
544 if (network_trans_)
545 return network_trans_->GetFullRequestHeaders(headers);
547 // TODO(ttuttle): Read headers from cache.
548 return false;
551 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
552 int64 total_received_bytes = total_received_bytes_;
553 if (network_trans_)
554 total_received_bytes += network_trans_->GetTotalReceivedBytes();
555 return total_received_bytes;
558 void HttpCache::Transaction::DoneReading() {
559 if (cache_.get() && entry_) {
560 DCHECK_NE(mode_, UPDATE);
561 if (mode_ & WRITE) {
562 DoneWritingToEntry(true);
563 } else if (mode_ & READ) {
564 // It is necessary to check mode_ & READ because it is possible
565 // for mode_ to be NONE and entry_ non-NULL with a write entry
566 // if StopCaching was called.
567 cache_->DoneReadingFromEntry(entry_, this);
568 entry_ = NULL;
573 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
574 // Null headers means we encountered an error or haven't a response yet
575 if (auth_response_.headers.get())
576 return &auth_response_;
577 return &response_;
580 LoadState HttpCache::Transaction::GetLoadState() const {
581 LoadState state = GetWriterLoadState();
582 if (state != LOAD_STATE_WAITING_FOR_CACHE)
583 return state;
585 if (cache_.get())
586 return cache_->GetLoadStateForPendingTransaction(this);
588 return LOAD_STATE_IDLE;
591 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
592 if (network_trans_.get())
593 return network_trans_->GetUploadProgress();
594 return final_upload_progress_;
597 void HttpCache::Transaction::SetQuicServerInfo(
598 QuicServerInfo* quic_server_info) {}
600 bool HttpCache::Transaction::GetLoadTimingInfo(
601 LoadTimingInfo* load_timing_info) const {
602 if (network_trans_)
603 return network_trans_->GetLoadTimingInfo(load_timing_info);
605 if (old_network_trans_load_timing_) {
606 *load_timing_info = *old_network_trans_load_timing_;
607 return true;
610 if (first_cache_access_since_.is_null())
611 return false;
613 // If the cache entry was opened, return that time.
614 load_timing_info->send_start = first_cache_access_since_;
615 // This time doesn't make much sense when reading from the cache, so just use
616 // the same time as send_start.
617 load_timing_info->send_end = first_cache_access_since_;
618 return true;
621 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
622 priority_ = priority;
623 if (network_trans_)
624 network_trans_->SetPriority(priority_);
627 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
628 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
629 websocket_handshake_stream_base_create_helper_ = create_helper;
630 if (network_trans_)
631 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
634 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
635 const BeforeNetworkStartCallback& callback) {
636 DCHECK(!network_trans_);
637 before_network_start_callback_ = callback;
640 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
641 const BeforeProxyHeadersSentCallback& callback) {
642 DCHECK(!network_trans_);
643 before_proxy_headers_sent_callback_ = callback;
646 int HttpCache::Transaction::ResumeNetworkStart() {
647 if (network_trans_)
648 return network_trans_->ResumeNetworkStart();
649 return ERR_UNEXPECTED;
652 void HttpCache::Transaction::GetConnectionAttempts(
653 ConnectionAttempts* out) const {
654 ConnectionAttempts new_connection_attempts;
655 if (network_trans_)
656 network_trans_->GetConnectionAttempts(&new_connection_attempts);
658 out->swap(new_connection_attempts);
659 out->insert(out->begin(), old_connection_attempts_.begin(),
660 old_connection_attempts_.end());
663 //-----------------------------------------------------------------------------
665 void HttpCache::Transaction::DoCallback(int rv) {
666 DCHECK(rv != ERR_IO_PENDING);
667 DCHECK(!callback_.is_null());
669 read_buf_ = NULL; // Release the buffer before invoking the callback.
671 // Since Run may result in Read being called, clear callback_ up front.
672 CompletionCallback c = callback_;
673 callback_.Reset();
674 c.Run(rv);
677 int HttpCache::Transaction::HandleResult(int rv) {
678 DCHECK(rv != ERR_IO_PENDING);
679 if (!callback_.is_null())
680 DoCallback(rv);
682 return rv;
685 // A few common patterns: (Foo* means Foo -> FooComplete)
687 // 1. Not-cached entry:
688 // Start():
689 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
690 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
691 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
692 // PartialHeadersReceived
694 // Read():
695 // NetworkRead* -> CacheWriteData*
697 // 2. Cached entry, no validation:
698 // Start():
699 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
700 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
701 // BeginCacheValidation() -> SetupEntryForRead()
703 // Read():
704 // CacheReadData*
706 // 3. Cached entry, validation (304):
707 // Start():
708 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
709 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
710 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
711 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
712 // -> OverwriteCachedResponse -> PartialHeadersReceived
714 // Read():
715 // CacheReadData*
717 // 4. Cached entry, validation and replace (200):
718 // Start():
719 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
720 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
721 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
722 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
723 // TruncateCachedMetadata* -> PartialHeadersReceived
725 // Read():
726 // NetworkRead* -> CacheWriteData*
728 // 5. Sparse entry, partially cached, byte range request:
729 // Start():
730 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
731 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
732 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
733 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
734 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
735 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
736 // -> OverwriteCachedResponse -> PartialHeadersReceived
738 // Read() 1:
739 // NetworkRead* -> CacheWriteData*
741 // Read() 2:
742 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
743 // CompletePartialCacheValidation -> CacheReadData* ->
745 // Read() 3:
746 // CacheReadData* -> StartPartialCacheValidation ->
747 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
748 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
749 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
751 // 6. HEAD. Not-cached entry:
752 // Pass through. Don't save a HEAD by itself.
753 // Start():
754 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
756 // 7. HEAD. Cached entry, no validation:
757 // Start():
758 // The same flow as for a GET request (example #2)
760 // Read():
761 // CacheReadData (returns 0)
763 // 8. HEAD. Cached entry, validation (304):
764 // The request updates the stored headers.
765 // Start(): Same as for a GET request (example #3)
767 // Read():
768 // CacheReadData (returns 0)
770 // 9. HEAD. Cached entry, validation and replace (200):
771 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
772 // itself.
773 // Start():
774 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
775 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
776 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
777 // OverwriteCachedResponse
779 // 10. HEAD. Sparse entry, partially cached:
780 // Serve the request from the cache, as long as it doesn't require
781 // revalidation. Ignore missing ranges when deciding to revalidate. If the
782 // entry requires revalidation, ignore the whole request and go to full pass
783 // through (the result of the HEAD request will NOT update the entry).
785 // Start(): Basically the same as example 7, as we never create a partial_
786 // object for this request.
788 // 11. Prefetch, not-cached entry:
789 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
790 // UpdateCachedResponse.
792 // 12. Prefetch, cached entry:
793 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
794 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
795 // bit is unset.
797 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
798 // Skip validation, similar to example 2.
799 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
800 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
801 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
802 // SetupEntryForRead()
804 // Read():
805 // CacheReadData*
807 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
808 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
809 // CacheReadResponse* and CacheDispatchValidation.
810 int HttpCache::Transaction::DoLoop(int result) {
811 DCHECK(next_state_ != STATE_NONE);
813 int rv = result;
814 do {
815 State state = next_state_;
816 next_state_ = STATE_NONE;
817 switch (state) {
818 case STATE_GET_BACKEND:
819 DCHECK_EQ(OK, rv);
820 rv = DoGetBackend();
821 break;
822 case STATE_GET_BACKEND_COMPLETE:
823 rv = DoGetBackendComplete(rv);
824 break;
825 case STATE_SEND_REQUEST:
826 DCHECK_EQ(OK, rv);
827 rv = DoSendRequest();
828 break;
829 case STATE_SEND_REQUEST_COMPLETE:
830 rv = DoSendRequestComplete(rv);
831 break;
832 case STATE_SUCCESSFUL_SEND_REQUEST:
833 DCHECK_EQ(OK, rv);
834 rv = DoSuccessfulSendRequest();
835 break;
836 case STATE_NETWORK_READ:
837 DCHECK_EQ(OK, rv);
838 rv = DoNetworkRead();
839 break;
840 case STATE_NETWORK_READ_COMPLETE:
841 rv = DoNetworkReadComplete(rv);
842 break;
843 case STATE_INIT_ENTRY:
844 DCHECK_EQ(OK, rv);
845 rv = DoInitEntry();
846 break;
847 case STATE_OPEN_ENTRY:
848 DCHECK_EQ(OK, rv);
849 rv = DoOpenEntry();
850 break;
851 case STATE_OPEN_ENTRY_COMPLETE:
852 rv = DoOpenEntryComplete(rv);
853 break;
854 case STATE_CREATE_ENTRY:
855 DCHECK_EQ(OK, rv);
856 rv = DoCreateEntry();
857 break;
858 case STATE_CREATE_ENTRY_COMPLETE:
859 rv = DoCreateEntryComplete(rv);
860 break;
861 case STATE_DOOM_ENTRY:
862 DCHECK_EQ(OK, rv);
863 rv = DoDoomEntry();
864 break;
865 case STATE_DOOM_ENTRY_COMPLETE:
866 rv = DoDoomEntryComplete(rv);
867 break;
868 case STATE_ADD_TO_ENTRY:
869 DCHECK_EQ(OK, rv);
870 rv = DoAddToEntry();
871 break;
872 case STATE_ADD_TO_ENTRY_COMPLETE:
873 rv = DoAddToEntryComplete(rv);
874 break;
875 case STATE_START_PARTIAL_CACHE_VALIDATION:
876 DCHECK_EQ(OK, rv);
877 rv = DoStartPartialCacheValidation();
878 break;
879 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
880 rv = DoCompletePartialCacheValidation(rv);
881 break;
882 case STATE_UPDATE_CACHED_RESPONSE:
883 DCHECK_EQ(OK, rv);
884 rv = DoUpdateCachedResponse();
885 break;
886 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
887 rv = DoUpdateCachedResponseComplete(rv);
888 break;
889 case STATE_OVERWRITE_CACHED_RESPONSE:
890 DCHECK_EQ(OK, rv);
891 rv = DoOverwriteCachedResponse();
892 break;
893 case STATE_TRUNCATE_CACHED_DATA:
894 DCHECK_EQ(OK, rv);
895 rv = DoTruncateCachedData();
896 break;
897 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
898 rv = DoTruncateCachedDataComplete(rv);
899 break;
900 case STATE_TRUNCATE_CACHED_METADATA:
901 DCHECK_EQ(OK, rv);
902 rv = DoTruncateCachedMetadata();
903 break;
904 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
905 rv = DoTruncateCachedMetadataComplete(rv);
906 break;
907 case STATE_PARTIAL_HEADERS_RECEIVED:
908 DCHECK_EQ(OK, rv);
909 rv = DoPartialHeadersReceived();
910 break;
911 case STATE_CACHE_READ_RESPONSE:
912 DCHECK_EQ(OK, rv);
913 rv = DoCacheReadResponse();
914 break;
915 case STATE_CACHE_READ_RESPONSE_COMPLETE:
916 rv = DoCacheReadResponseComplete(rv);
917 break;
918 case STATE_CACHE_DISPATCH_VALIDATION:
919 DCHECK_EQ(OK, rv);
920 rv = DoCacheDispatchValidation();
921 break;
922 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH:
923 DCHECK_EQ(OK, rv);
924 rv = DoCacheToggleUnusedSincePrefetch();
925 break;
926 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE:
927 rv = DoCacheToggleUnusedSincePrefetchComplete(rv);
928 break;
929 case STATE_CACHE_WRITE_RESPONSE:
930 DCHECK_EQ(OK, rv);
931 rv = DoCacheWriteResponse();
932 break;
933 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
934 DCHECK_EQ(OK, rv);
935 rv = DoCacheWriteTruncatedResponse();
936 break;
937 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
938 rv = DoCacheWriteResponseComplete(rv);
939 break;
940 case STATE_CACHE_READ_METADATA:
941 DCHECK_EQ(OK, rv);
942 rv = DoCacheReadMetadata();
943 break;
944 case STATE_CACHE_READ_METADATA_COMPLETE:
945 rv = DoCacheReadMetadataComplete(rv);
946 break;
947 case STATE_CACHE_QUERY_DATA:
948 DCHECK_EQ(OK, rv);
949 rv = DoCacheQueryData();
950 break;
951 case STATE_CACHE_QUERY_DATA_COMPLETE:
952 rv = DoCacheQueryDataComplete(rv);
953 break;
954 case STATE_CACHE_READ_DATA:
955 DCHECK_EQ(OK, rv);
956 rv = DoCacheReadData();
957 break;
958 case STATE_CACHE_READ_DATA_COMPLETE:
959 rv = DoCacheReadDataComplete(rv);
960 break;
961 case STATE_CACHE_WRITE_DATA:
962 rv = DoCacheWriteData(rv);
963 break;
964 case STATE_CACHE_WRITE_DATA_COMPLETE:
965 rv = DoCacheWriteDataComplete(rv);
966 break;
967 default:
968 NOTREACHED() << "bad state";
969 rv = ERR_FAILED;
970 break;
972 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
974 if (rv != ERR_IO_PENDING)
975 HandleResult(rv);
977 return rv;
980 int HttpCache::Transaction::DoGetBackend() {
981 cache_pending_ = true;
982 next_state_ = STATE_GET_BACKEND_COMPLETE;
983 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
984 return cache_->GetBackendForTransaction(this);
987 int HttpCache::Transaction::DoGetBackendComplete(int result) {
988 DCHECK(result == OK || result == ERR_FAILED);
989 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
990 result);
991 cache_pending_ = false;
993 if (!ShouldPassThrough()) {
994 cache_key_ = cache_->GenerateCacheKey(request_);
996 // Requested cache access mode.
997 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
998 mode_ = READ;
999 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
1000 mode_ = WRITE;
1001 } else {
1002 mode_ = READ_WRITE;
1005 // Downgrade to UPDATE if the request has been externally conditionalized.
1006 if (external_validation_.initialized) {
1007 if (mode_ & WRITE) {
1008 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1009 // in case READ was off).
1010 mode_ = UPDATE;
1011 } else {
1012 mode_ = NONE;
1017 // Use PUT and DELETE only to invalidate existing stored entries.
1018 if ((request_->method == "PUT" || request_->method == "DELETE") &&
1019 mode_ != READ_WRITE && mode_ != WRITE) {
1020 mode_ = NONE;
1023 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1024 // transaction behaves the same for GET and HEAD requests at this point: if it
1025 // was not modified, the entry is updated and a response is not returned from
1026 // the cache. If we receive 200, it doesn't matter if there was a validation
1027 // header or not.
1028 if (request_->method == "HEAD" && mode_ == WRITE)
1029 mode_ = NONE;
1031 // If must use cache, then we must fail. This can happen for back/forward
1032 // navigations to a page generated via a form post.
1033 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
1034 return ERR_CACHE_MISS;
1036 if (mode_ == NONE) {
1037 if (partial_.get()) {
1038 partial_->RestoreHeaders(&custom_request_->extra_headers);
1039 partial_.reset();
1041 next_state_ = STATE_SEND_REQUEST;
1042 } else {
1043 next_state_ = STATE_INIT_ENTRY;
1046 // This is only set if we have something to do with the response.
1047 range_requested_ = (partial_.get() != NULL);
1049 return OK;
1052 int HttpCache::Transaction::DoSendRequest() {
1053 DCHECK(mode_ & WRITE || mode_ == NONE);
1054 DCHECK(!network_trans_.get());
1056 send_request_since_ = TimeTicks::Now();
1058 // Create a network transaction.
1059 int rv = cache_->network_layer_->CreateTransaction(priority_,
1060 &network_trans_);
1061 if (rv != OK)
1062 return rv;
1063 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1064 network_trans_->SetBeforeProxyHeadersSentCallback(
1065 before_proxy_headers_sent_callback_);
1067 // Old load timing information, if any, is now obsolete.
1068 old_network_trans_load_timing_.reset();
1070 if (websocket_handshake_stream_base_create_helper_)
1071 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1072 websocket_handshake_stream_base_create_helper_);
1074 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1075 rv = network_trans_->Start(request_, io_callback_, net_log_);
1076 return rv;
1079 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1080 if (!cache_.get())
1081 return ERR_UNEXPECTED;
1083 // If we tried to conditionalize the request and failed, we know
1084 // we won't be reading from the cache after this point.
1085 if (couldnt_conditionalize_request_)
1086 mode_ = WRITE;
1088 if (result == OK) {
1089 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
1090 return OK;
1093 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1094 response_.network_accessed = response->network_accessed;
1096 // Do not record requests that have network errors or restarts.
1097 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1098 if (IsCertificateError(result)) {
1099 // If we get a certificate error, then there is a certificate in ssl_info,
1100 // so GetResponseInfo() should never return NULL here.
1101 DCHECK(response);
1102 response_.ssl_info = response->ssl_info;
1103 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1104 DCHECK(response);
1105 response_.cert_request_info = response->cert_request_info;
1106 } else if (response_.was_cached) {
1107 DoneWritingToEntry(true);
1110 return result;
1113 // We received the response headers and there is no error.
1114 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1115 DCHECK(!new_response_);
1116 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
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. We do not retry at this point, because data
1137 // has been read and we have no way to gather credentials. We would
1138 // fail again, and potentially loop. This can happen if the credentials
1139 // expire while chrome is suspended.
1140 if (entry_)
1141 DoomPartialEntry(false);
1142 mode_ = NONE;
1143 partial_.reset();
1144 ResetNetworkTransaction();
1145 return ERR_CACHE_AUTH_FAILURE_AFTER_READ;
1148 new_response_ = new_response;
1149 if (!ValidatePartialResponse() && !auth_response_.headers.get()) {
1150 // Something went wrong with this request and we have to restart it.
1151 // If we have an authentication response, we are exposed to weird things
1152 // hapenning if the user cancels the authentication before we receive
1153 // the new response.
1154 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST);
1155 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1156 response_ = HttpResponseInfo();
1157 ResetNetworkTransaction();
1158 new_response_ = NULL;
1159 next_state_ = STATE_SEND_REQUEST;
1160 return OK;
1163 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1164 // We have stored the full entry, but it changed and the server is
1165 // sending a range. We have to delete the old entry.
1166 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1167 DoneWritingToEntry(false);
1170 if (mode_ == WRITE &&
1171 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1172 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1175 // Invalidate any cached GET with a successful PUT or DELETE.
1176 if (mode_ == WRITE &&
1177 (request_->method == "PUT" || request_->method == "DELETE")) {
1178 if (NonErrorResponse(new_response->headers->response_code())) {
1179 int ret = cache_->DoomEntry(cache_key_, NULL);
1180 DCHECK_EQ(OK, ret);
1182 cache_->DoneWritingToEntry(entry_, true);
1183 entry_ = NULL;
1184 mode_ = NONE;
1187 // Invalidate any cached GET with a successful POST.
1188 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1189 request_->method == "POST" &&
1190 NonErrorResponse(new_response->headers->response_code())) {
1191 cache_->DoomMainEntryForUrl(request_->url);
1194 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1196 if (new_response_->headers->response_code() == 416 &&
1197 (request_->method == "GET" || request_->method == "POST")) {
1198 // If there is an active entry it may be destroyed with this transaction.
1199 response_ = *new_response_;
1200 return OK;
1203 // Are we expecting a response to a conditional query?
1204 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1205 if (new_response->headers->response_code() == 304 || handling_206_) {
1206 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1207 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1208 return OK;
1210 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1211 mode_ = WRITE;
1214 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1215 return OK;
1218 int HttpCache::Transaction::DoNetworkRead() {
1219 next_state_ = STATE_NETWORK_READ_COMPLETE;
1220 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1223 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1224 DCHECK(mode_ & WRITE || mode_ == NONE);
1226 if (!cache_.get())
1227 return ERR_UNEXPECTED;
1229 // If there is an error or we aren't saving the data, we are done; just wait
1230 // until the destructor runs to see if we can keep the data.
1231 if (mode_ == NONE || result < 0)
1232 return result;
1234 next_state_ = STATE_CACHE_WRITE_DATA;
1235 return result;
1238 int HttpCache::Transaction::DoInitEntry() {
1239 DCHECK(!new_entry_);
1241 if (!cache_.get())
1242 return ERR_UNEXPECTED;
1244 if (mode_ == WRITE) {
1245 next_state_ = STATE_DOOM_ENTRY;
1246 return OK;
1249 next_state_ = STATE_OPEN_ENTRY;
1250 return OK;
1253 int HttpCache::Transaction::DoOpenEntry() {
1254 DCHECK(!new_entry_);
1255 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1256 cache_pending_ = true;
1257 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1258 first_cache_access_since_ = TimeTicks::Now();
1259 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1262 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1263 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1264 // OK, otherwise the cache will end up with an active entry without any
1265 // transaction attached.
1266 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1267 cache_pending_ = false;
1268 if (result == OK) {
1269 next_state_ = STATE_ADD_TO_ENTRY;
1270 return OK;
1273 if (result == ERR_CACHE_RACE) {
1274 next_state_ = STATE_INIT_ENTRY;
1275 return OK;
1278 if (request_->method == "PUT" || request_->method == "DELETE" ||
1279 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1280 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1281 mode_ = NONE;
1282 next_state_ = STATE_SEND_REQUEST;
1283 return OK;
1286 if (mode_ == READ_WRITE) {
1287 mode_ = WRITE;
1288 next_state_ = STATE_CREATE_ENTRY;
1289 return OK;
1291 if (mode_ == UPDATE) {
1292 // There is no cache entry to update; proceed without caching.
1293 mode_ = NONE;
1294 next_state_ = STATE_SEND_REQUEST;
1295 return OK;
1298 // The entry does not exist, and we are not permitted to create a new entry,
1299 // so we must fail.
1300 return ERR_CACHE_MISS;
1303 int HttpCache::Transaction::DoCreateEntry() {
1304 DCHECK(!new_entry_);
1305 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1306 cache_pending_ = true;
1307 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1308 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1311 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1312 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1313 // OK, otherwise the cache will end up with an active entry without any
1314 // transaction attached.
1315 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1316 result);
1317 cache_pending_ = false;
1318 next_state_ = STATE_ADD_TO_ENTRY;
1320 if (result == ERR_CACHE_RACE) {
1321 next_state_ = STATE_INIT_ENTRY;
1322 return OK;
1325 if (result != OK) {
1326 // We have a race here: Maybe we failed to open the entry and decided to
1327 // create one, but by the time we called create, another transaction already
1328 // created the entry. If we want to eliminate this issue, we need an atomic
1329 // OpenOrCreate() method exposed by the disk cache.
1330 DLOG(WARNING) << "Unable to create cache entry";
1331 mode_ = NONE;
1332 if (partial_.get())
1333 partial_->RestoreHeaders(&custom_request_->extra_headers);
1334 next_state_ = STATE_SEND_REQUEST;
1336 return OK;
1339 int HttpCache::Transaction::DoDoomEntry() {
1340 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1341 cache_pending_ = true;
1342 if (first_cache_access_since_.is_null())
1343 first_cache_access_since_ = TimeTicks::Now();
1344 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1345 return cache_->DoomEntry(cache_key_, this);
1348 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1349 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1350 next_state_ = STATE_CREATE_ENTRY;
1351 cache_pending_ = false;
1352 if (result == ERR_CACHE_RACE)
1353 next_state_ = STATE_INIT_ENTRY;
1354 return OK;
1357 int HttpCache::Transaction::DoAddToEntry() {
1358 DCHECK(new_entry_);
1359 cache_pending_ = true;
1360 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1361 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1362 DCHECK(entry_lock_waiting_since_.is_null());
1363 entry_lock_waiting_since_ = TimeTicks::Now();
1364 int rv = cache_->AddTransactionToEntry(new_entry_, this);
1365 if (rv == ERR_IO_PENDING) {
1366 if (bypass_lock_for_test_) {
1367 OnAddToEntryTimeout(entry_lock_waiting_since_);
1368 } else {
1369 int timeout_milliseconds = 20 * 1000;
1370 if (partial_ && new_entry_->writer &&
1371 new_entry_->writer->range_requested_) {
1372 // Quickly timeout and bypass the cache if we're a range request and
1373 // we're blocked by the reader/writer lock. Doing so eliminates a long
1374 // running issue, http://crbug.com/31014, where two of the same media
1375 // resources could not be played back simultaneously due to one locking
1376 // the cache entry until the entire video was downloaded.
1378 // Bypassing the cache is not ideal, as we are now ignoring the cache
1379 // entirely for all range requests to a resource beyond the first. This
1380 // is however a much more succinct solution than the alternatives, which
1381 // would require somewhat significant changes to the http caching logic.
1383 // Allow some timeout slack for the entry addition to complete in case
1384 // the writer lock is imminently released; we want to avoid skipping
1385 // the cache if at all possible. See http://crbug.com/408765
1386 timeout_milliseconds = 25;
1388 base::MessageLoop::current()->PostDelayedTask(
1389 FROM_HERE,
1390 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout,
1391 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_),
1392 TimeDelta::FromMilliseconds(timeout_milliseconds));
1395 return rv;
1398 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1399 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1400 result);
1401 const TimeDelta entry_lock_wait =
1402 TimeTicks::Now() - entry_lock_waiting_since_;
1403 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1405 entry_lock_waiting_since_ = TimeTicks();
1406 DCHECK(new_entry_);
1407 cache_pending_ = false;
1409 if (result == OK)
1410 entry_ = new_entry_;
1412 // If there is a failure, the cache should have taken care of new_entry_.
1413 new_entry_ = NULL;
1415 if (result == ERR_CACHE_RACE) {
1416 next_state_ = STATE_INIT_ENTRY;
1417 return OK;
1420 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1421 // The cache is busy, bypass it for this transaction.
1422 mode_ = NONE;
1423 next_state_ = STATE_SEND_REQUEST;
1424 if (partial_) {
1425 partial_->RestoreHeaders(&custom_request_->extra_headers);
1426 partial_.reset();
1428 return OK;
1431 if (result != OK) {
1432 NOTREACHED();
1433 return result;
1436 if (mode_ == WRITE) {
1437 if (partial_.get())
1438 partial_->RestoreHeaders(&custom_request_->extra_headers);
1439 next_state_ = STATE_SEND_REQUEST;
1440 } else {
1441 // We have to read the headers from the cached entry.
1442 DCHECK(mode_ & READ_META);
1443 next_state_ = STATE_CACHE_READ_RESPONSE;
1445 return OK;
1448 // We may end up here multiple times for a given request.
1449 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1450 if (mode_ == NONE)
1451 return OK;
1453 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1454 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1457 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1458 if (!result) {
1459 // This is the end of the request.
1460 if (mode_ & WRITE) {
1461 DoneWritingToEntry(true);
1462 } else {
1463 cache_->DoneReadingFromEntry(entry_, this);
1464 entry_ = NULL;
1466 return result;
1469 if (result < 0)
1470 return result;
1472 partial_->PrepareCacheValidation(entry_->disk_entry,
1473 &custom_request_->extra_headers);
1475 if (reading_ && partial_->IsCurrentRangeCached()) {
1476 next_state_ = STATE_CACHE_READ_DATA;
1477 return OK;
1480 return BeginCacheValidation();
1483 // We received 304 or 206 and we want to update the cached response headers.
1484 int HttpCache::Transaction::DoUpdateCachedResponse() {
1485 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1486 int rv = OK;
1487 // Update the cached response based on the headers and properties of
1488 // new_response_.
1489 response_.headers->Update(*new_response_->headers.get());
1490 response_.response_time = new_response_->response_time;
1491 response_.request_time = new_response_->request_time;
1492 response_.network_accessed = new_response_->network_accessed;
1493 response_.unused_since_prefetch = new_response_->unused_since_prefetch;
1494 response_.ssl_info = new_response_->ssl_info;
1495 if (new_response_->vary_data.is_valid()) {
1496 response_.vary_data = new_response_->vary_data;
1497 } else if (response_.vary_data.is_valid()) {
1498 // There is a vary header in the stored response but not in the current one.
1499 // Update the data with the new request headers.
1500 HttpVaryData new_vary_data;
1501 new_vary_data.Init(*request_, *response_.headers.get());
1502 response_.vary_data = new_vary_data;
1505 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1506 if (!entry_->doomed) {
1507 int ret = cache_->DoomEntry(cache_key_, NULL);
1508 DCHECK_EQ(OK, ret);
1510 } else {
1511 // If we are already reading, we already updated the headers for this
1512 // request; doing it again will change Content-Length.
1513 if (!reading_) {
1514 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1515 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1516 rv = OK;
1519 return rv;
1522 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1523 if (mode_ == UPDATE) {
1524 DCHECK(!handling_206_);
1525 // We got a "not modified" response and already updated the corresponding
1526 // cache entry above.
1528 // By closing the cached entry now, we make sure that the 304 rather than
1529 // the cached 200 response, is what will be returned to the user.
1530 DoneWritingToEntry(true);
1531 } else if (entry_ && !handling_206_) {
1532 DCHECK_EQ(READ_WRITE, mode_);
1533 if (!partial_.get() || partial_->IsLastRange()) {
1534 cache_->ConvertWriterToReader(entry_);
1535 mode_ = READ;
1537 // We no longer need the network transaction, so destroy it.
1538 final_upload_progress_ = network_trans_->GetUploadProgress();
1539 ResetNetworkTransaction();
1540 } else if (entry_ && handling_206_ && truncated_ &&
1541 partial_->initial_validation()) {
1542 // We just finished the validation of a truncated entry, and the server
1543 // is willing to resume the operation. Now we go back and start serving
1544 // the first part to the user.
1545 ResetNetworkTransaction();
1546 new_response_ = NULL;
1547 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1548 partial_->SetRangeToStartDownload();
1549 return OK;
1551 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1552 return OK;
1555 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1556 if (mode_ & READ) {
1557 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1558 return OK;
1561 // We change the value of Content-Length for partial content.
1562 if (handling_206_ && partial_.get())
1563 partial_->FixContentLength(new_response_->headers.get());
1565 response_ = *new_response_;
1567 if (request_->method == "HEAD") {
1568 // This response is replacing the cached one.
1569 DoneWritingToEntry(false);
1570 mode_ = NONE;
1571 new_response_ = NULL;
1572 return OK;
1575 if (handling_206_ && !CanResume(false)) {
1576 // There is no point in storing this resource because it will never be used.
1577 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1578 DoneWritingToEntry(false);
1579 if (partial_.get())
1580 partial_->FixResponseHeaders(response_.headers.get(), true);
1581 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1582 return OK;
1585 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1586 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1587 STATE_CACHE_WRITE_RESPONSE;
1588 return OK;
1591 int HttpCache::Transaction::DoTruncateCachedData() {
1592 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1593 if (!entry_)
1594 return OK;
1595 if (net_log_.IsCapturing())
1596 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1597 // Truncate the stream.
1598 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1601 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1602 if (entry_) {
1603 if (net_log_.IsCapturing()) {
1604 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1605 result);
1609 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1610 return OK;
1613 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1614 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1615 if (!entry_)
1616 return OK;
1618 if (net_log_.IsCapturing())
1619 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1620 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1623 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1624 if (entry_) {
1625 if (net_log_.IsCapturing()) {
1626 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1627 result);
1631 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1632 return OK;
1635 int HttpCache::Transaction::DoPartialHeadersReceived() {
1636 new_response_ = NULL;
1637 if (entry_ && !partial_.get() &&
1638 entry_->disk_entry->GetDataSize(kMetadataIndex))
1639 next_state_ = STATE_CACHE_READ_METADATA;
1641 if (!partial_.get())
1642 return OK;
1644 if (reading_) {
1645 if (network_trans_.get()) {
1646 next_state_ = STATE_NETWORK_READ;
1647 } else {
1648 next_state_ = STATE_CACHE_READ_DATA;
1650 } else if (mode_ != NONE) {
1651 // We are about to return the headers for a byte-range request to the user,
1652 // so let's fix them.
1653 partial_->FixResponseHeaders(response_.headers.get(), true);
1655 return OK;
1658 int HttpCache::Transaction::DoCacheReadResponse() {
1659 DCHECK(entry_);
1660 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1662 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1663 read_buf_ = new IOBuffer(io_buf_len_);
1665 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1666 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1667 io_buf_len_, io_callback_);
1670 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1671 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1672 if (result != io_buf_len_ ||
1673 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1674 &response_, &truncated_)) {
1675 return OnCacheReadError(result, true);
1678 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1679 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1680 ReadCertChain();
1682 // Some resources may have slipped in as truncated when they're not.
1683 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1684 if (response_.headers->GetContentLength() == current_size)
1685 truncated_ = false;
1687 if ((response_.unused_since_prefetch &&
1688 !(request_->load_flags & LOAD_PREFETCH)) ||
1689 (!response_.unused_since_prefetch &&
1690 (request_->load_flags & LOAD_PREFETCH))) {
1691 // Either this is the first use of an entry since it was prefetched or
1692 // this is a prefetch. The value of response.unused_since_prefetch is valid
1693 // for this transaction but the bit needs to be flipped in storage.
1694 next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH;
1695 return OK;
1698 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1699 return OK;
1702 int HttpCache::Transaction::DoCacheDispatchValidation() {
1703 // We now have access to the cache entry.
1705 // o if we are a reader for the transaction, then we can start reading the
1706 // cache entry.
1708 // o if we can read or write, then we should check if the cache entry needs
1709 // to be validated and then issue a network request if needed or just read
1710 // from the cache if the cache entry is already valid.
1712 // o if we are set to UPDATE, then we are handling an externally
1713 // conditionalized request (if-modified-since / if-none-match). We check
1714 // if the request headers define a validation request.
1716 int result = ERR_FAILED;
1717 switch (mode_) {
1718 case READ:
1719 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1720 result = BeginCacheRead();
1721 break;
1722 case READ_WRITE:
1723 result = BeginPartialCacheValidation();
1724 break;
1725 case UPDATE:
1726 result = BeginExternallyConditionalizedRequest();
1727 break;
1728 case WRITE:
1729 default:
1730 NOTREACHED();
1732 return result;
1735 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1736 // Write back the toggled value for the next use of this entry.
1737 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1739 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1740 // transaction then metadata will be written to cache twice. If prefetching
1741 // becomes more common, consider combining the writes.
1742 target_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE;
1743 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1744 return OK;
1747 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1748 int result) {
1749 // Restore the original value for this transaction.
1750 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1751 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1752 return OK;
1755 int HttpCache::Transaction::DoCacheWriteResponse() {
1756 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1757 tracked_objects::ScopedTracker tracking_profile(
1758 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1759 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1761 if (entry_) {
1762 if (net_log_.IsCapturing())
1763 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1765 return WriteResponseInfoToEntry(false);
1768 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1769 if (entry_) {
1770 if (net_log_.IsCapturing())
1771 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1773 return WriteResponseInfoToEntry(true);
1776 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1777 next_state_ = target_state_;
1778 target_state_ = STATE_NONE;
1779 if (!entry_)
1780 return OK;
1781 if (net_log_.IsCapturing()) {
1782 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1783 result);
1786 // Balance the AddRef from WriteResponseInfoToEntry.
1787 if (result != io_buf_len_) {
1788 DLOG(ERROR) << "failed to write response info to cache";
1789 DoneWritingToEntry(false);
1791 return OK;
1794 int HttpCache::Transaction::DoCacheReadMetadata() {
1795 DCHECK(entry_);
1796 DCHECK(!response_.metadata.get());
1797 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1799 response_.metadata =
1800 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1802 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1803 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1804 response_.metadata.get(),
1805 response_.metadata->size(),
1806 io_callback_);
1809 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1810 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1811 if (result != response_.metadata->size())
1812 return OnCacheReadError(result, false);
1813 return OK;
1816 int HttpCache::Transaction::DoCacheQueryData() {
1817 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1818 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1821 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1822 DCHECK_EQ(OK, result);
1823 if (!cache_.get())
1824 return ERR_UNEXPECTED;
1826 return ValidateEntryHeadersAndContinue();
1829 int HttpCache::Transaction::DoCacheReadData() {
1830 DCHECK(entry_);
1831 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1833 if (net_log_.IsCapturing())
1834 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1835 if (partial_.get()) {
1836 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1837 io_callback_);
1840 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1841 read_buf_.get(), io_buf_len_,
1842 io_callback_);
1845 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1846 if (net_log_.IsCapturing()) {
1847 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1848 result);
1851 if (!cache_.get())
1852 return ERR_UNEXPECTED;
1854 if (partial_.get()) {
1855 // Partial requests are confusing to report in histograms because they may
1856 // have multiple underlying requests.
1857 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1858 return DoPartialCacheReadCompleted(result);
1861 if (result > 0) {
1862 read_offset_ += result;
1863 } else if (result == 0) { // End of file.
1864 RecordHistograms();
1865 cache_->DoneReadingFromEntry(entry_, this);
1866 entry_ = NULL;
1867 } else {
1868 return OnCacheReadError(result, false);
1870 return result;
1873 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1874 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1875 write_len_ = num_bytes;
1876 if (entry_) {
1877 if (net_log_.IsCapturing())
1878 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1881 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1884 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1885 if (entry_) {
1886 if (net_log_.IsCapturing()) {
1887 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1888 result);
1891 // Balance the AddRef from DoCacheWriteData.
1892 if (!cache_.get())
1893 return ERR_UNEXPECTED;
1895 if (result != write_len_) {
1896 DLOG(ERROR) << "failed to write response data to cache";
1897 DoneWritingToEntry(false);
1899 // We want to ignore errors writing to disk and just keep reading from
1900 // the network.
1901 result = write_len_;
1902 } else if (!done_reading_ && entry_) {
1903 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1904 int64 body_size = response_.headers->GetContentLength();
1905 if (body_size >= 0 && body_size <= current_size)
1906 done_reading_ = true;
1909 if (partial_.get()) {
1910 // This may be the last request.
1911 if (!(result == 0 && !truncated_ &&
1912 (partial_->IsLastRange() || mode_ == WRITE)))
1913 return DoPartialNetworkReadCompleted(result);
1916 if (result == 0) {
1917 // End of file. This may be the result of a connection problem so see if we
1918 // have to keep the entry around to be flagged as truncated later on.
1919 if (done_reading_ || !entry_ || partial_.get() ||
1920 response_.headers->GetContentLength() <= 0)
1921 DoneWritingToEntry(true);
1924 return result;
1927 //-----------------------------------------------------------------------------
1929 void HttpCache::Transaction::ReadCertChain() {
1930 std::string key =
1931 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
1932 const X509Certificate::OSCertHandles& intermediates =
1933 response_.ssl_info.cert->GetIntermediateCertificates();
1934 int dist_from_root = intermediates.size();
1936 scoped_refptr<SharedChainData> shared_chain_data(
1937 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1938 cache_->cert_cache()->GetCertificate(key,
1939 base::Bind(&OnCertReadIOComplete,
1940 dist_from_root,
1941 true /* is leaf */,
1942 shared_chain_data));
1944 for (X509Certificate::OSCertHandles::const_iterator it =
1945 intermediates.begin();
1946 it != intermediates.end();
1947 ++it) {
1948 --dist_from_root;
1949 key = GetCacheKeyForCert(*it);
1950 cache_->cert_cache()->GetCertificate(key,
1951 base::Bind(&OnCertReadIOComplete,
1952 dist_from_root,
1953 false /* is not leaf */,
1954 shared_chain_data));
1956 DCHECK_EQ(0, dist_from_root);
1959 void HttpCache::Transaction::WriteCertChain() {
1960 const X509Certificate::OSCertHandles& intermediates =
1961 response_.ssl_info.cert->GetIntermediateCertificates();
1962 int dist_from_root = intermediates.size();
1964 scoped_refptr<SharedChainData> shared_chain_data(
1965 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1966 cache_->cert_cache()->SetCertificate(
1967 response_.ssl_info.cert->os_cert_handle(),
1968 base::Bind(&OnCertWriteIOComplete,
1969 dist_from_root,
1970 true /* is leaf */,
1971 shared_chain_data));
1972 for (X509Certificate::OSCertHandles::const_iterator it =
1973 intermediates.begin();
1974 it != intermediates.end();
1975 ++it) {
1976 --dist_from_root;
1977 cache_->cert_cache()->SetCertificate(*it,
1978 base::Bind(&OnCertWriteIOComplete,
1979 dist_from_root,
1980 false /* is not leaf */,
1981 shared_chain_data));
1983 DCHECK_EQ(0, dist_from_root);
1986 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1987 const HttpRequestInfo* request) {
1988 net_log_ = net_log;
1989 request_ = request;
1990 effective_load_flags_ = request_->load_flags;
1992 if (cache_->mode() == DISABLE)
1993 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1995 // Some headers imply load flags. The order here is significant.
1997 // LOAD_DISABLE_CACHE : no cache read or write
1998 // LOAD_BYPASS_CACHE : no cache read
1999 // LOAD_VALIDATE_CACHE : no cache read unless validation
2001 // The former modes trump latter modes, so if we find a matching header we
2002 // can stop iterating kSpecialHeaders.
2004 static const struct {
2005 const HeaderNameAndValue* search;
2006 int load_flag;
2007 } kSpecialHeaders[] = {
2008 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
2009 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
2010 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
2013 bool range_found = false;
2014 bool external_validation_error = false;
2015 bool special_headers = false;
2017 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
2018 range_found = true;
2020 for (size_t i = 0; i < arraysize(kSpecialHeaders); ++i) {
2021 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
2022 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
2023 special_headers = true;
2024 break;
2028 // Check for conditionalization headers which may correspond with a
2029 // cache validation request.
2030 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
2031 const ValidationHeaderInfo& info = kValidationHeaders[i];
2032 std::string validation_value;
2033 if (request_->extra_headers.GetHeader(
2034 info.request_header_name, &validation_value)) {
2035 if (!external_validation_.values[i].empty() ||
2036 validation_value.empty()) {
2037 external_validation_error = true;
2039 external_validation_.values[i] = validation_value;
2040 external_validation_.initialized = true;
2044 if (range_found || special_headers || external_validation_.initialized) {
2045 // Log the headers before request_ is modified.
2046 std::string empty;
2047 net_log_.AddEvent(
2048 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS,
2049 base::Bind(&HttpRequestHeaders::NetLogCallback,
2050 base::Unretained(&request_->extra_headers), &empty));
2053 // We don't support ranges and validation headers.
2054 if (range_found && external_validation_.initialized) {
2055 LOG(WARNING) << "Byte ranges AND validation headers found.";
2056 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2059 // If there is more than one validation header, we can't treat this request as
2060 // a cache validation, since we don't know for sure which header the server
2061 // will give us a response for (and they could be contradictory).
2062 if (external_validation_error) {
2063 LOG(WARNING) << "Multiple or malformed validation headers found.";
2064 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2067 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2068 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2069 partial_.reset(new PartialData);
2070 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
2071 // We will be modifying the actual range requested to the server, so
2072 // let's remove the header here.
2073 custom_request_.reset(new HttpRequestInfo(*request_));
2074 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2075 request_ = custom_request_.get();
2076 partial_->SetHeaders(custom_request_->extra_headers);
2077 } else {
2078 // The range is invalid or we cannot handle it properly.
2079 VLOG(1) << "Invalid byte range found.";
2080 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2081 partial_.reset(NULL);
2086 bool HttpCache::Transaction::ShouldPassThrough() {
2087 // We may have a null disk_cache if there is an error we cannot recover from,
2088 // like not enough disk space, or sharing violations.
2089 if (!cache_->disk_cache_.get())
2090 return true;
2092 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2093 return true;
2095 if (request_->method == "GET" || request_->method == "HEAD")
2096 return false;
2098 if (request_->method == "POST" && request_->upload_data_stream &&
2099 request_->upload_data_stream->identifier()) {
2100 return false;
2103 if (request_->method == "PUT" && request_->upload_data_stream)
2104 return false;
2106 if (request_->method == "DELETE")
2107 return false;
2109 return true;
2112 int HttpCache::Transaction::BeginCacheRead() {
2113 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2114 if (response_.headers->response_code() == 206 || partial_.get()) {
2115 NOTREACHED();
2116 return ERR_CACHE_MISS;
2119 if (request_->method == "HEAD")
2120 FixHeadersForHead();
2122 // We don't have the whole resource.
2123 if (truncated_)
2124 return ERR_CACHE_MISS;
2126 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2127 next_state_ = STATE_CACHE_READ_METADATA;
2129 return OK;
2132 int HttpCache::Transaction::BeginCacheValidation() {
2133 DCHECK(mode_ == READ_WRITE);
2135 ValidationType required_validation = RequiresValidation();
2137 bool skip_validation = (required_validation == VALIDATION_NONE);
2139 if (required_validation == VALIDATION_ASYNCHRONOUS &&
2140 !(request_->method == "GET" && (truncated_ || partial_)) && cache_ &&
2141 cache_->use_stale_while_revalidate()) {
2142 TriggerAsyncValidation();
2143 skip_validation = true;
2146 if (request_->method == "HEAD" &&
2147 (truncated_ || response_.headers->response_code() == 206)) {
2148 DCHECK(!partial_);
2149 if (skip_validation)
2150 return SetupEntryForRead();
2152 // Bail out!
2153 next_state_ = STATE_SEND_REQUEST;
2154 mode_ = NONE;
2155 return OK;
2158 if (truncated_) {
2159 // Truncated entries can cause partial gets, so we shouldn't record this
2160 // load in histograms.
2161 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2162 skip_validation = !partial_->initial_validation();
2165 if (partial_.get() && (is_sparse_ || truncated_) &&
2166 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
2167 // Force revalidation for sparse or truncated entries. Note that we don't
2168 // want to ignore the regular validation logic just because a byte range was
2169 // part of the request.
2170 skip_validation = false;
2173 if (skip_validation) {
2174 // TODO(ricea): Is this pattern okay for asynchronous revalidations?
2175 UpdateTransactionPattern(PATTERN_ENTRY_USED);
2176 return SetupEntryForRead();
2177 } else {
2178 // Make the network request conditional, to see if we may reuse our cached
2179 // response. If we cannot do so, then we just resort to a normal fetch.
2180 // Our mode remains READ_WRITE for a conditional request. Even if the
2181 // conditionalization fails, we don't switch to WRITE mode until we
2182 // know we won't be falling back to using the cache entry in the
2183 // LOAD_FROM_CACHE_IF_OFFLINE case.
2184 if (!ConditionalizeRequest()) {
2185 couldnt_conditionalize_request_ = true;
2186 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
2187 if (partial_.get())
2188 return DoRestartPartialRequest();
2190 DCHECK_NE(206, response_.headers->response_code());
2192 next_state_ = STATE_SEND_REQUEST;
2194 return OK;
2197 int HttpCache::Transaction::BeginPartialCacheValidation() {
2198 DCHECK(mode_ == READ_WRITE);
2200 if (response_.headers->response_code() != 206 && !partial_.get() &&
2201 !truncated_) {
2202 return BeginCacheValidation();
2205 // Partial requests should not be recorded in histograms.
2206 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2207 if (range_requested_) {
2208 next_state_ = STATE_CACHE_QUERY_DATA;
2209 return OK;
2212 // The request is not for a range, but we have stored just ranges.
2214 if (request_->method == "HEAD")
2215 return BeginCacheValidation();
2217 partial_.reset(new PartialData());
2218 partial_->SetHeaders(request_->extra_headers);
2219 if (!custom_request_.get()) {
2220 custom_request_.reset(new HttpRequestInfo(*request_));
2221 request_ = custom_request_.get();
2224 return ValidateEntryHeadersAndContinue();
2227 // This should only be called once per request.
2228 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2229 DCHECK(mode_ == READ_WRITE);
2231 if (!partial_->UpdateFromStoredHeaders(
2232 response_.headers.get(), entry_->disk_entry, truncated_)) {
2233 return DoRestartPartialRequest();
2236 if (response_.headers->response_code() == 206)
2237 is_sparse_ = true;
2239 if (!partial_->IsRequestedRangeOK()) {
2240 // The stored data is fine, but the request may be invalid.
2241 invalid_range_ = true;
2244 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2245 return OK;
2248 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2249 DCHECK_EQ(UPDATE, mode_);
2250 DCHECK(external_validation_.initialized);
2252 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
2253 if (external_validation_.values[i].empty())
2254 continue;
2255 // Retrieve either the cached response's "etag" or "last-modified" header.
2256 std::string validator;
2257 response_.headers->EnumerateHeader(
2258 NULL,
2259 kValidationHeaders[i].related_response_header_name,
2260 &validator);
2262 if (response_.headers->response_code() != 200 || truncated_ ||
2263 validator.empty() || validator != external_validation_.values[i]) {
2264 // The externally conditionalized request is not a validation request
2265 // for our existing cache entry. Proceed with caching disabled.
2266 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2267 DoneWritingToEntry(true);
2271 // TODO(ricea): This calculation is expensive to perform just to collect
2272 // statistics. Either remove it or use the result, depending on the result of
2273 // the experiment.
2274 ExternallyConditionalizedType type =
2275 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE;
2276 if (mode_ == NONE)
2277 type = EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS;
2278 else if (RequiresValidation())
2279 type = EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION;
2281 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2282 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2283 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2284 type,
2285 EXTERNALLY_CONDITIONALIZED_MAX);
2287 next_state_ = STATE_SEND_REQUEST;
2288 return OK;
2291 int HttpCache::Transaction::RestartNetworkRequest() {
2292 DCHECK(mode_ & WRITE || mode_ == NONE);
2293 DCHECK(network_trans_.get());
2294 DCHECK_EQ(STATE_NONE, next_state_);
2296 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2297 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
2298 if (rv != ERR_IO_PENDING)
2299 return DoLoop(rv);
2300 return rv;
2303 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2304 X509Certificate* client_cert) {
2305 DCHECK(mode_ & WRITE || mode_ == NONE);
2306 DCHECK(network_trans_.get());
2307 DCHECK_EQ(STATE_NONE, next_state_);
2309 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2310 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
2311 if (rv != ERR_IO_PENDING)
2312 return DoLoop(rv);
2313 return rv;
2316 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2317 const AuthCredentials& credentials) {
2318 DCHECK(mode_ & WRITE || mode_ == NONE);
2319 DCHECK(network_trans_.get());
2320 DCHECK_EQ(STATE_NONE, next_state_);
2322 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2323 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
2324 if (rv != ERR_IO_PENDING)
2325 return DoLoop(rv);
2326 return rv;
2329 ValidationType HttpCache::Transaction::RequiresValidation() {
2330 // TODO(darin): need to do more work here:
2331 // - make sure we have a matching request method
2332 // - watch out for cached responses that depend on authentication
2334 if (response_.vary_data.is_valid() &&
2335 !response_.vary_data.MatchesRequest(*request_,
2336 *response_.headers.get())) {
2337 vary_mismatch_ = true;
2338 return VALIDATION_SYNCHRONOUS;
2341 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
2342 return VALIDATION_NONE;
2344 if (response_.unused_since_prefetch &&
2345 !(effective_load_flags_ & LOAD_PREFETCH) &&
2346 response_.headers->GetCurrentAge(
2347 response_.request_time, response_.response_time,
2348 cache_->clock_->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins)) {
2349 // The first use of a resource after prefetch within a short window skips
2350 // validation.
2351 return VALIDATION_NONE;
2354 if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION))
2355 return VALIDATION_SYNCHRONOUS;
2357 if (request_->method == "PUT" || request_->method == "DELETE")
2358 return VALIDATION_SYNCHRONOUS;
2360 ValidationType validation_required_by_headers =
2361 response_.headers->RequiresValidation(response_.request_time,
2362 response_.response_time,
2363 cache_->clock_->Now());
2365 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) {
2366 // Asynchronous revalidation is only supported for GET and HEAD methods.
2367 if (request_->method != "GET" && request_->method != "HEAD")
2368 return VALIDATION_SYNCHRONOUS;
2371 return validation_required_by_headers;
2374 bool HttpCache::Transaction::ConditionalizeRequest() {
2375 DCHECK(response_.headers.get());
2377 if (request_->method == "PUT" || request_->method == "DELETE")
2378 return false;
2380 // This only makes sense for cached 200 or 206 responses.
2381 if (response_.headers->response_code() != 200 &&
2382 response_.headers->response_code() != 206) {
2383 return false;
2386 if (fail_conditionalization_for_test_)
2387 return false;
2389 DCHECK(response_.headers->response_code() != 206 ||
2390 response_.headers->HasStrongValidators());
2392 // Just use the first available ETag and/or Last-Modified header value.
2393 // TODO(darin): Or should we use the last?
2395 std::string etag_value;
2396 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2397 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2399 std::string last_modified_value;
2400 if (!vary_mismatch_) {
2401 response_.headers->EnumerateHeader(NULL, "last-modified",
2402 &last_modified_value);
2405 if (etag_value.empty() && last_modified_value.empty())
2406 return false;
2408 if (!partial_.get()) {
2409 // Need to customize the request, so this forces us to allocate :(
2410 custom_request_.reset(new HttpRequestInfo(*request_));
2411 request_ = custom_request_.get();
2413 DCHECK(custom_request_.get());
2415 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2416 !invalid_range_;
2418 if (!use_if_range) {
2419 // stale-while-revalidate is not useful when we only have a partial response
2420 // cached, so don't set the header in that case.
2421 HttpResponseHeaders::FreshnessLifetimes lifetimes =
2422 response_.headers->GetFreshnessLifetimes(response_.response_time);
2423 if (lifetimes.staleness > TimeDelta()) {
2424 TimeDelta current_age = response_.headers->GetCurrentAge(
2425 response_.request_time, response_.response_time,
2426 cache_->clock_->Now());
2428 custom_request_->extra_headers.SetHeader(
2429 kFreshnessHeader,
2430 base::StringPrintf("max-age=%" PRId64
2431 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2432 lifetimes.freshness.InSeconds(),
2433 lifetimes.staleness.InSeconds(),
2434 current_age.InSeconds()));
2438 if (!etag_value.empty()) {
2439 if (use_if_range) {
2440 // We don't want to switch to WRITE mode if we don't have this block of a
2441 // byte-range request because we may have other parts cached.
2442 custom_request_->extra_headers.SetHeader(
2443 HttpRequestHeaders::kIfRange, etag_value);
2444 } else {
2445 custom_request_->extra_headers.SetHeader(
2446 HttpRequestHeaders::kIfNoneMatch, etag_value);
2448 // For byte-range requests, make sure that we use only one way to validate
2449 // the request.
2450 if (partial_.get() && !partial_->IsCurrentRangeCached())
2451 return true;
2454 if (!last_modified_value.empty()) {
2455 if (use_if_range) {
2456 custom_request_->extra_headers.SetHeader(
2457 HttpRequestHeaders::kIfRange, last_modified_value);
2458 } else {
2459 custom_request_->extra_headers.SetHeader(
2460 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2464 return true;
2467 // We just received some headers from the server. We may have asked for a range,
2468 // in which case partial_ has an object. This could be the first network request
2469 // we make to fulfill the original request, or we may be already reading (from
2470 // the net and / or the cache). If we are not expecting a certain response, we
2471 // just bypass the cache for this request (but again, maybe we are reading), and
2472 // delete partial_ (so we are not able to "fix" the headers that we return to
2473 // the user). This results in either a weird response for the caller (we don't
2474 // expect it after all), or maybe a range that was not exactly what it was asked
2475 // for.
2477 // If the server is simply telling us that the resource has changed, we delete
2478 // the cached entry and restart the request as the caller intended (by returning
2479 // false from this method). However, we may not be able to do that at any point,
2480 // for instance if we already returned the headers to the user.
2482 // WARNING: Whenever this code returns false, it has to make sure that the next
2483 // time it is called it will return true so that we don't keep retrying the
2484 // request.
2485 bool HttpCache::Transaction::ValidatePartialResponse() {
2486 const HttpResponseHeaders* headers = new_response_->headers.get();
2487 int response_code = headers->response_code();
2488 bool partial_response = (response_code == 206);
2489 handling_206_ = false;
2491 if (!entry_ || request_->method != "GET")
2492 return true;
2494 if (invalid_range_) {
2495 // We gave up trying to match this request with the stored data. If the
2496 // server is ok with the request, delete the entry, otherwise just ignore
2497 // this request
2498 DCHECK(!reading_);
2499 if (partial_response || response_code == 200) {
2500 DoomPartialEntry(true);
2501 mode_ = NONE;
2502 } else {
2503 if (response_code == 304)
2504 FailRangeRequest();
2505 IgnoreRangeRequest();
2507 return true;
2510 if (!partial_.get()) {
2511 // We are not expecting 206 but we may have one.
2512 if (partial_response)
2513 IgnoreRangeRequest();
2515 return true;
2518 // TODO(rvargas): Do we need to consider other results here?.
2519 bool failure = response_code == 200 || response_code == 416;
2521 if (partial_->IsCurrentRangeCached()) {
2522 // We asked for "If-None-Match: " so a 206 means a new object.
2523 if (partial_response)
2524 failure = true;
2526 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2527 return true;
2528 } else {
2529 // We asked for "If-Range: " so a 206 means just another range.
2530 if (partial_response) {
2531 if (partial_->ResponseHeadersOK(headers)) {
2532 handling_206_ = true;
2533 return true;
2534 } else {
2535 failure = true;
2539 if (!reading_ && !is_sparse_ && !partial_response) {
2540 // See if we can ignore the fact that we issued a byte range request.
2541 // If the server sends 200, just store it. If it sends an error, redirect
2542 // or something else, we may store the response as long as we didn't have
2543 // anything already stored.
2544 if (response_code == 200 ||
2545 (!truncated_ && response_code != 304 && response_code != 416)) {
2546 // The server is sending something else, and we can save it.
2547 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2548 partial_.reset();
2549 truncated_ = false;
2550 return true;
2554 // 304 is not expected here, but we'll spare the entry (unless it was
2555 // truncated).
2556 if (truncated_)
2557 failure = true;
2560 if (failure) {
2561 // We cannot truncate this entry, it has to be deleted.
2562 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2563 mode_ = NONE;
2564 if (is_sparse_ || truncated_) {
2565 // There was something cached to start with, either sparsed data (206), or
2566 // a truncated 200, which means that we probably modified the request,
2567 // adding a byte range or modifying the range requested by the caller.
2568 if (!reading_ && !partial_->IsLastRange()) {
2569 // We have not returned anything to the caller yet so it should be safe
2570 // to issue another network request, this time without us messing up the
2571 // headers.
2572 ResetPartialState(true);
2573 return false;
2575 LOG(WARNING) << "Failed to revalidate partial entry";
2577 DoomPartialEntry(true);
2578 return true;
2581 IgnoreRangeRequest();
2582 return true;
2585 void HttpCache::Transaction::IgnoreRangeRequest() {
2586 // We have a problem. We may or may not be reading already (in which case we
2587 // returned the headers), but we'll just pretend that this request is not
2588 // using the cache and see what happens. Most likely this is the first
2589 // response from the server (it's not changing its mind midway, right?).
2590 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2591 if (mode_ & WRITE)
2592 DoneWritingToEntry(mode_ != WRITE);
2593 else if (mode_ & READ && entry_)
2594 cache_->DoneReadingFromEntry(entry_, this);
2596 partial_.reset(NULL);
2597 entry_ = NULL;
2598 mode_ = NONE;
2601 void HttpCache::Transaction::FixHeadersForHead() {
2602 if (response_.headers->response_code() == 206) {
2603 response_.headers->RemoveHeader("Content-Range");
2604 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2608 void HttpCache::Transaction::TriggerAsyncValidation() {
2609 DCHECK(!request_->upload_data_stream);
2610 BoundNetLog async_revalidation_net_log(
2611 BoundNetLog::Make(net_log_.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION));
2612 net_log_.AddEvent(
2613 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC,
2614 async_revalidation_net_log.source().ToEventParametersCallback());
2615 async_revalidation_net_log.BeginEvent(
2616 NetLog::TYPE_ASYNC_REVALIDATION,
2617 base::Bind(
2618 &NetLogAsyncRevalidationInfoCallback, net_log_.source(), request_));
2619 base::MessageLoop::current()->PostTask(
2620 FROM_HERE,
2621 base::Bind(&HttpCache::PerformAsyncValidation,
2622 cache_, // cache_ is a weak pointer.
2623 *request_,
2624 async_revalidation_net_log));
2627 void HttpCache::Transaction::FailRangeRequest() {
2628 response_ = *new_response_;
2629 partial_->FixResponseHeaders(response_.headers.get(), false);
2632 int HttpCache::Transaction::SetupEntryForRead() {
2633 if (network_trans_)
2634 ResetNetworkTransaction();
2635 if (partial_.get()) {
2636 if (truncated_ || is_sparse_ || !invalid_range_) {
2637 // We are going to return the saved response headers to the caller, so
2638 // we may need to adjust them first.
2639 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2640 return OK;
2641 } else {
2642 partial_.reset();
2645 cache_->ConvertWriterToReader(entry_);
2646 mode_ = READ;
2648 if (request_->method == "HEAD")
2649 FixHeadersForHead();
2651 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2652 next_state_ = STATE_CACHE_READ_METADATA;
2653 return OK;
2657 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2658 read_buf_ = data;
2659 io_buf_len_ = data_len;
2660 next_state_ = STATE_NETWORK_READ;
2661 return DoLoop(OK);
2664 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2665 if (request_->method == "HEAD")
2666 return 0;
2668 read_buf_ = data;
2669 io_buf_len_ = data_len;
2670 next_state_ = STATE_CACHE_READ_DATA;
2671 return DoLoop(OK);
2674 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2675 IOBuffer* data, int data_len,
2676 const CompletionCallback& callback) {
2677 if (!entry_)
2678 return data_len;
2680 int rv = 0;
2681 if (!partial_.get() || !data_len) {
2682 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2683 true);
2684 } else {
2685 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2687 return rv;
2690 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2691 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2692 if (!entry_)
2693 return OK;
2695 // Do not cache no-store content. Do not cache content with cert errors
2696 // either. This is to prevent not reporting net errors when loading a
2697 // resource from the cache. When we load a page over HTTPS with a cert error
2698 // we show an SSL blocking page. If the user clicks proceed we reload the
2699 // resource ignoring the errors. The loaded resource is then cached. If that
2700 // resource is subsequently loaded from the cache, no net error is reported
2701 // (even though the cert status contains the actual errors) and no SSL
2702 // blocking page is shown. An alternative would be to reverse-map the cert
2703 // status to a net error and replay the net error.
2704 if ((response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2705 IsCertStatusError(response_.ssl_info.cert_status)) {
2706 DoneWritingToEntry(false);
2707 if (net_log_.IsCapturing())
2708 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2709 return OK;
2712 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2713 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2714 WriteCertChain();
2716 if (truncated)
2717 DCHECK_EQ(200, response_.headers->response_code());
2719 // When writing headers, we normally only write the non-transient headers.
2720 bool skip_transient_headers = true;
2721 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2722 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2723 data->Done();
2725 io_buf_len_ = data->pickle()->size();
2726 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2727 io_buf_len_, io_callback_, true);
2730 int HttpCache::Transaction::AppendResponseDataToEntry(
2731 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2732 if (!entry_ || !data_len)
2733 return data_len;
2735 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2736 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2737 callback);
2740 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2741 if (!entry_)
2742 return;
2744 RecordHistograms();
2746 cache_->DoneWritingToEntry(entry_, success);
2747 entry_ = NULL;
2748 mode_ = NONE; // switch to 'pass through' mode
2751 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2752 DLOG(ERROR) << "ReadData failed: " << result;
2753 const int result_for_histogram = std::max(0, -result);
2754 if (restart) {
2755 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2756 result_for_histogram);
2757 } else {
2758 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2759 result_for_histogram);
2762 // Avoid using this entry in the future.
2763 if (cache_.get())
2764 cache_->DoomActiveEntry(cache_key_);
2766 if (restart) {
2767 DCHECK(!reading_);
2768 DCHECK(!network_trans_.get());
2769 cache_->DoneWithEntry(entry_, this, false);
2770 entry_ = NULL;
2771 is_sparse_ = false;
2772 partial_.reset();
2773 next_state_ = STATE_GET_BACKEND;
2774 return OK;
2777 return ERR_CACHE_READ_FAILURE;
2780 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
2781 if (entry_lock_waiting_since_ != start_time)
2782 return;
2784 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
2786 if (!cache_)
2787 return;
2789 cache_->RemovePendingTransaction(this);
2790 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
2793 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2794 DVLOG(2) << "DoomPartialEntry";
2795 int rv = cache_->DoomEntry(cache_key_, NULL);
2796 DCHECK_EQ(OK, rv);
2797 cache_->DoneWithEntry(entry_, this, false);
2798 entry_ = NULL;
2799 is_sparse_ = false;
2800 truncated_ = false;
2801 if (delete_object)
2802 partial_.reset(NULL);
2805 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2806 partial_->OnNetworkReadCompleted(result);
2808 if (result == 0) {
2809 // We need to move on to the next range.
2810 ResetNetworkTransaction();
2811 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2813 return result;
2816 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2817 partial_->OnCacheReadCompleted(result);
2819 if (result == 0 && mode_ == READ_WRITE) {
2820 // We need to move on to the next range.
2821 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2822 } else if (result < 0) {
2823 return OnCacheReadError(result, false);
2825 return result;
2828 int HttpCache::Transaction::DoRestartPartialRequest() {
2829 // The stored data cannot be used. Get rid of it and restart this request.
2830 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST);
2832 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
2833 // to Doom the entry again).
2834 mode_ = WRITE;
2835 ResetPartialState(!range_requested_);
2836 next_state_ = STATE_CREATE_ENTRY;
2837 return OK;
2840 void HttpCache::Transaction::ResetPartialState(bool delete_object) {
2841 partial_->RestoreHeaders(&custom_request_->extra_headers);
2842 DoomPartialEntry(delete_object);
2844 if (!delete_object) {
2845 // The simplest way to re-initialize partial_ is to create a new object.
2846 partial_.reset(new PartialData());
2847 if (partial_->Init(request_->extra_headers))
2848 partial_->SetHeaders(custom_request_->extra_headers);
2849 else
2850 partial_.reset();
2854 void HttpCache::Transaction::ResetNetworkTransaction() {
2855 DCHECK(!old_network_trans_load_timing_);
2856 DCHECK(network_trans_);
2857 LoadTimingInfo load_timing;
2858 if (network_trans_->GetLoadTimingInfo(&load_timing))
2859 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2860 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2861 ConnectionAttempts attempts;
2862 network_trans_->GetConnectionAttempts(&attempts);
2863 for (const auto& attempt : attempts)
2864 old_connection_attempts_.push_back(attempt);
2865 network_trans_.reset();
2868 // Histogram data from the end of 2010 show the following distribution of
2869 // response headers:
2871 // Content-Length............... 87%
2872 // Date......................... 98%
2873 // Last-Modified................ 49%
2874 // Etag......................... 19%
2875 // Accept-Ranges: bytes......... 25%
2876 // Accept-Ranges: none.......... 0.4%
2877 // Strong Validator............. 50%
2878 // Strong Validator + ranges.... 24%
2879 // Strong Validator + CL........ 49%
2881 bool HttpCache::Transaction::CanResume(bool has_data) {
2882 // Double check that there is something worth keeping.
2883 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2884 return false;
2886 if (request_->method != "GET")
2887 return false;
2889 // Note that if this is a 206, content-length was already fixed after calling
2890 // PartialData::ResponseHeadersOK().
2891 if (response_.headers->GetContentLength() <= 0 ||
2892 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2893 !response_.headers->HasStrongValidators()) {
2894 return false;
2897 return true;
2900 void HttpCache::Transaction::UpdateTransactionPattern(
2901 TransactionPattern new_transaction_pattern) {
2902 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2903 return;
2904 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2905 new_transaction_pattern == PATTERN_NOT_COVERED);
2906 transaction_pattern_ = new_transaction_pattern;
2909 void HttpCache::Transaction::RecordHistograms() {
2910 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2911 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2912 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2913 cache_->mode() != NORMAL || request_->method != "GET") {
2914 return;
2916 UMA_HISTOGRAM_ENUMERATION(
2917 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2918 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2919 return;
2920 DCHECK(!range_requested_);
2921 DCHECK(!first_cache_access_since_.is_null());
2923 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2925 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2927 bool did_send_request = !send_request_since_.is_null();
2928 DCHECK(
2929 (did_send_request &&
2930 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2931 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2932 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2933 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2934 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2936 if (!did_send_request) {
2937 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2938 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2939 return;
2942 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2943 int64 before_send_percent = (total_time.ToInternalValue() == 0) ?
2944 0 : before_send_time * 100 / total_time;
2945 DCHECK_GE(before_send_percent, 0);
2946 DCHECK_LE(before_send_percent, 100);
2947 base::HistogramBase::Sample before_send_sample =
2948 static_cast<base::HistogramBase::Sample>(before_send_percent);
2950 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2951 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2952 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample);
2954 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2955 // below this comment after we have received initial data.
2956 switch (transaction_pattern_) {
2957 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2958 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2959 before_send_time);
2960 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2961 before_send_sample);
2962 break;
2964 case PATTERN_ENTRY_NOT_CACHED: {
2965 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2966 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2967 before_send_sample);
2968 break;
2970 case PATTERN_ENTRY_VALIDATED: {
2971 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2972 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2973 before_send_sample);
2974 break;
2976 case PATTERN_ENTRY_UPDATED: {
2977 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2978 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2979 before_send_sample);
2980 break;
2982 default:
2983 NOTREACHED();
2987 void HttpCache::Transaction::OnIOComplete(int result) {
2988 DoLoop(result);
2991 } // namespace net