Update minimum NDK used for building Chrome.
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blob1819fd2b12482e25a2781bd561e3d05e55196a9d
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_cache_transaction.h"
7 #include "build/build_config.h"
9 #if defined(OS_POSIX)
10 #include <unistd.h>
11 #endif
13 #include <algorithm>
14 #include <string>
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/format_macros.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/metrics/field_trial.h"
22 #include "base/metrics/histogram.h"
23 #include "base/metrics/sparse_histogram.h"
24 #include "base/profiler/scoped_tracker.h"
25 #include "base/rand_util.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_piece.h"
28 #include "base/strings/string_util.h"
29 #include "base/strings/stringprintf.h"
30 #include "base/time/clock.h"
31 #include "base/time/time.h"
32 #include "base/values.h"
33 #include "net/base/completion_callback.h"
34 #include "net/base/io_buffer.h"
35 #include "net/base/load_flags.h"
36 #include "net/base/load_timing_info.h"
37 #include "net/base/net_errors.h"
38 #include "net/base/upload_data_stream.h"
39 #include "net/cert/cert_status_flags.h"
40 #include "net/disk_cache/disk_cache.h"
41 #include "net/http/disk_based_cert_cache.h"
42 #include "net/http/http_network_session.h"
43 #include "net/http/http_request_info.h"
44 #include "net/http/http_response_headers.h"
45 #include "net/http/http_transaction.h"
46 #include "net/http/http_util.h"
47 #include "net/http/partial_data.h"
48 #include "net/log/net_log.h"
49 #include "net/ssl/ssl_cert_request_info.h"
50 #include "net/ssl/ssl_config_service.h"
52 using base::Time;
53 using base::TimeDelta;
54 using base::TimeTicks;
56 namespace net {
58 namespace {
60 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
61 static const char kFreshnessHeader[] = "Resource-Freshness";
63 // Stores data relevant to the statistics of writing and reading entire
64 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
65 // of certificates in the chain that have pending operations in the
66 // DiskBasedCertCache. |start_time| is the time that the read and write
67 // commands began being issued to the DiskBasedCertCache.
68 // TODO(brandonsalmon): Remove this when it is no longer necessary to
69 // collect data.
70 class SharedChainData : public base::RefCounted<SharedChainData> {
71 public:
72 SharedChainData(int num_ops, TimeTicks start)
73 : num_pending_ops(num_ops), start_time(start) {}
75 int num_pending_ops;
76 TimeTicks start_time;
78 private:
79 friend class base::RefCounted<SharedChainData>;
80 ~SharedChainData() {}
81 DISALLOW_COPY_AND_ASSIGN(SharedChainData);
84 // Used to obtain a cache entry key for an OSCertHandle.
85 // TODO(brandonsalmon): Remove this when cache keys are stored
86 // and no longer have to be recomputed to retrieve the OSCertHandle
87 // from the disk.
88 std::string GetCacheKeyForCert(X509Certificate::OSCertHandle cert_handle) {
89 SHA1HashValue fingerprint =
90 X509Certificate::CalculateFingerprint(cert_handle);
92 return "cert:" +
93 base::HexEncode(fingerprint.data, arraysize(fingerprint.data));
96 // |dist_from_root| indicates the position of the read certificate in the
97 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
98 // whether or not the read certificate was the leaf of the chain.
99 // |shared_chain_data| contains data shared by each certificate in
100 // the chain.
101 void OnCertReadIOComplete(
102 int dist_from_root,
103 bool is_leaf,
104 const scoped_refptr<SharedChainData>& shared_chain_data,
105 X509Certificate::OSCertHandle cert_handle) {
106 // If |num_pending_ops| is one, this was the last pending read operation
107 // for this chain of certificates. The total time used to read the chain
108 // can be calculated by subtracting the starting time from Now().
109 shared_chain_data->num_pending_ops--;
110 if (!shared_chain_data->num_pending_ops) {
111 const TimeDelta read_chain_wait =
112 TimeTicks::Now() - shared_chain_data->start_time;
113 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
114 read_chain_wait,
115 base::TimeDelta::FromMilliseconds(1),
116 base::TimeDelta::FromMinutes(10),
117 50);
120 bool success = (cert_handle != NULL);
121 if (is_leaf)
122 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success);
124 if (success)
125 UMA_HISTOGRAM_CUSTOM_COUNTS(
126 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root, 0, 10, 7);
127 else
128 UMA_HISTOGRAM_CUSTOM_COUNTS(
129 "DiskBasedCertCache.CertIoReadFailure", dist_from_root, 0, 10, 7);
132 // |dist_from_root| indicates the position of the written certificate in the
133 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
134 // whether or not the written certificate was the leaf of the chain.
135 // |shared_chain_data| contains data shared by each certificate in
136 // the chain.
137 void OnCertWriteIOComplete(
138 int dist_from_root,
139 bool is_leaf,
140 const scoped_refptr<SharedChainData>& shared_chain_data,
141 const std::string& key) {
142 // If |num_pending_ops| is one, this was the last pending write operation
143 // for this chain of certificates. The total time used to write the chain
144 // can be calculated by subtracting the starting time from Now().
145 shared_chain_data->num_pending_ops--;
146 if (!shared_chain_data->num_pending_ops) {
147 const TimeDelta write_chain_wait =
148 TimeTicks::Now() - shared_chain_data->start_time;
149 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
150 write_chain_wait,
151 base::TimeDelta::FromMilliseconds(1),
152 base::TimeDelta::FromMinutes(10),
153 50);
156 bool success = !key.empty();
157 if (is_leaf)
158 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success);
160 if (success)
161 UMA_HISTOGRAM_CUSTOM_COUNTS(
162 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root, 0, 10, 7);
163 else
164 UMA_HISTOGRAM_CUSTOM_COUNTS(
165 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root, 0, 10, 7);
168 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
169 // a "non-error response" is one with a 2xx (Successful) or 3xx
170 // (Redirection) status code.
171 bool NonErrorResponse(int status_code) {
172 int status_code_range = status_code / 100;
173 return status_code_range == 2 || status_code_range == 3;
176 void RecordNoStoreHeaderHistogram(int load_flags,
177 const HttpResponseInfo* response) {
178 if (load_flags & LOAD_MAIN_FRAME) {
179 UMA_HISTOGRAM_BOOLEAN(
180 "Net.MainFrameNoStore",
181 response->headers->HasHeaderValue("cache-control", "no-store"));
185 scoped_ptr<base::Value> NetLogAsyncRevalidationInfoCallback(
186 const NetLog::Source& source,
187 const HttpRequestInfo* request,
188 NetLogCaptureMode capture_mode) {
189 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
190 source.AddToEventParameters(dict.get());
192 dict->SetString("url", request->url.possibly_invalid_spec());
193 dict->SetString("method", request->method);
194 return dict.Pass();
197 enum ExternallyConditionalizedType {
198 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION,
199 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE,
200 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS,
201 EXTERNALLY_CONDITIONALIZED_MAX
204 } // namespace
206 struct HeaderNameAndValue {
207 const char* name;
208 const char* value;
211 // If the request includes one of these request headers, then avoid caching
212 // to avoid getting confused.
213 static const HeaderNameAndValue kPassThroughHeaders[] = {
214 { "if-unmodified-since", NULL }, // causes unexpected 412s
215 { "if-match", NULL }, // causes unexpected 412s
216 { "if-range", NULL },
217 { NULL, NULL }
220 struct ValidationHeaderInfo {
221 const char* request_header_name;
222 const char* related_response_header_name;
225 static const ValidationHeaderInfo kValidationHeaders[] = {
226 { "if-modified-since", "last-modified" },
227 { "if-none-match", "etag" },
230 // If the request includes one of these request headers, then avoid reusing
231 // our cached copy if any.
232 static const HeaderNameAndValue kForceFetchHeaders[] = {
233 { "cache-control", "no-cache" },
234 { "pragma", "no-cache" },
235 { NULL, NULL }
238 // If the request includes one of these request headers, then force our
239 // cached copy (if any) to be revalidated before reusing it.
240 static const HeaderNameAndValue kForceValidateHeaders[] = {
241 { "cache-control", "max-age=0" },
242 { NULL, NULL }
245 static bool HeaderMatches(const HttpRequestHeaders& headers,
246 const HeaderNameAndValue* search) {
247 for (; search->name; ++search) {
248 std::string header_value;
249 if (!headers.GetHeader(search->name, &header_value))
250 continue;
252 if (!search->value)
253 return true;
255 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
256 while (v.GetNext()) {
257 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
258 return true;
261 return false;
264 //-----------------------------------------------------------------------------
266 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
267 : next_state_(STATE_NONE),
268 request_(NULL),
269 priority_(priority),
270 cache_(cache->GetWeakPtr()),
271 entry_(NULL),
272 new_entry_(NULL),
273 new_response_(NULL),
274 mode_(NONE),
275 target_state_(STATE_NONE),
276 reading_(false),
277 invalid_range_(false),
278 truncated_(false),
279 is_sparse_(false),
280 range_requested_(false),
281 handling_206_(false),
282 cache_pending_(false),
283 done_reading_(false),
284 vary_mismatch_(false),
285 couldnt_conditionalize_request_(false),
286 bypass_lock_for_test_(false),
287 fail_conditionalization_for_test_(false),
288 io_buf_len_(0),
289 read_offset_(0),
290 effective_load_flags_(0),
291 write_len_(0),
292 transaction_pattern_(PATTERN_UNDEFINED),
293 total_received_bytes_(0),
294 websocket_handshake_stream_base_create_helper_(NULL),
295 weak_factory_(this) {
296 static_assert(HttpCache::Transaction::kNumValidationHeaders ==
297 arraysize(kValidationHeaders),
298 "invalid number of validation headers");
300 io_callback_ = base::Bind(&Transaction::OnIOComplete,
301 weak_factory_.GetWeakPtr());
304 HttpCache::Transaction::~Transaction() {
305 // We may have to issue another IO, but we should never invoke the callback_
306 // after this point.
307 callback_.Reset();
309 if (cache_) {
310 if (entry_) {
311 bool cancel_request = reading_ && response_.headers.get();
312 if (cancel_request) {
313 if (partial_) {
314 entry_->disk_entry->CancelSparseIO();
315 } else {
316 cancel_request &= (response_.headers->response_code() == 200);
320 cache_->DoneWithEntry(entry_, this, cancel_request);
321 } else if (cache_pending_) {
322 cache_->RemovePendingTransaction(this);
327 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
328 const CompletionCallback& callback) {
329 DCHECK(buf);
330 DCHECK_GT(buf_len, 0);
331 DCHECK(!callback.is_null());
332 if (!cache_.get() || !entry_)
333 return ERR_UNEXPECTED;
335 // We don't need to track this operation for anything.
336 // It could be possible to check if there is something already written and
337 // avoid writing again (it should be the same, right?), but let's allow the
338 // caller to "update" the contents with something new.
339 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
340 callback, true);
343 bool HttpCache::Transaction::AddTruncatedFlag() {
344 DCHECK(mode_ & WRITE || mode_ == NONE);
346 // Don't set the flag for sparse entries.
347 if (partial_.get() && !truncated_)
348 return true;
350 if (!CanResume(true))
351 return false;
353 // We may have received the whole resource already.
354 if (done_reading_)
355 return true;
357 truncated_ = true;
358 target_state_ = STATE_NONE;
359 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
360 DoLoop(OK);
361 return true;
364 LoadState HttpCache::Transaction::GetWriterLoadState() const {
365 if (network_trans_.get())
366 return network_trans_->GetLoadState();
367 if (entry_ || !request_)
368 return LOAD_STATE_IDLE;
369 return LOAD_STATE_WAITING_FOR_CACHE;
372 const BoundNetLog& HttpCache::Transaction::net_log() const {
373 return net_log_;
376 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
377 const CompletionCallback& callback,
378 const BoundNetLog& net_log) {
379 DCHECK(request);
380 DCHECK(!callback.is_null());
382 // Ensure that we only have one asynchronous call at a time.
383 DCHECK(callback_.is_null());
384 DCHECK(!reading_);
385 DCHECK(!network_trans_.get());
386 DCHECK(!entry_);
388 if (!cache_.get())
389 return ERR_UNEXPECTED;
391 SetRequest(net_log, request);
393 // We have to wait until the backend is initialized so we start the SM.
394 next_state_ = STATE_GET_BACKEND;
395 int rv = DoLoop(OK);
397 // Setting this here allows us to check for the existence of a callback_ to
398 // determine if we are still inside Start.
399 if (rv == ERR_IO_PENDING)
400 callback_ = callback;
402 return rv;
405 int HttpCache::Transaction::RestartIgnoringLastError(
406 const CompletionCallback& callback) {
407 DCHECK(!callback.is_null());
409 // Ensure that we only have one asynchronous call at a time.
410 DCHECK(callback_.is_null());
412 if (!cache_.get())
413 return ERR_UNEXPECTED;
415 int rv = RestartNetworkRequest();
417 if (rv == ERR_IO_PENDING)
418 callback_ = callback;
420 return rv;
423 int HttpCache::Transaction::RestartWithCertificate(
424 X509Certificate* client_cert,
425 const CompletionCallback& callback) {
426 DCHECK(!callback.is_null());
428 // Ensure that we only have one asynchronous call at a time.
429 DCHECK(callback_.is_null());
431 if (!cache_.get())
432 return ERR_UNEXPECTED;
434 int rv = RestartNetworkRequestWithCertificate(client_cert);
436 if (rv == ERR_IO_PENDING)
437 callback_ = callback;
439 return rv;
442 int HttpCache::Transaction::RestartWithAuth(
443 const AuthCredentials& credentials,
444 const CompletionCallback& callback) {
445 DCHECK(auth_response_.headers.get());
446 DCHECK(!callback.is_null());
448 // Ensure that we only have one asynchronous call at a time.
449 DCHECK(callback_.is_null());
451 if (!cache_.get())
452 return ERR_UNEXPECTED;
454 // Clear the intermediate response since we are going to start over.
455 auth_response_ = HttpResponseInfo();
457 int rv = RestartNetworkRequestWithAuth(credentials);
459 if (rv == ERR_IO_PENDING)
460 callback_ = callback;
462 return rv;
465 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
466 if (!network_trans_.get())
467 return false;
468 return network_trans_->IsReadyToRestartForAuth();
471 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
472 const CompletionCallback& callback) {
473 DCHECK(buf);
474 DCHECK_GT(buf_len, 0);
475 DCHECK(!callback.is_null());
477 DCHECK(callback_.is_null());
479 if (!cache_.get())
480 return ERR_UNEXPECTED;
482 // If we have an intermediate auth response at this point, then it means the
483 // user wishes to read the network response (the error page). If there is a
484 // previous response in the cache then we should leave it intact.
485 if (auth_response_.headers.get() && mode_ != NONE) {
486 UpdateTransactionPattern(PATTERN_NOT_COVERED);
487 DCHECK(mode_ & WRITE);
488 DoneWritingToEntry(mode_ == READ_WRITE);
489 mode_ = NONE;
492 reading_ = true;
493 int rv;
495 switch (mode_) {
496 case READ_WRITE:
497 DCHECK(partial_.get());
498 if (!network_trans_.get()) {
499 // We are just reading from the cache, but we may be writing later.
500 rv = ReadFromEntry(buf, buf_len);
501 break;
503 case NONE:
504 case WRITE:
505 DCHECK(network_trans_.get());
506 rv = ReadFromNetwork(buf, buf_len);
507 break;
508 case READ:
509 rv = ReadFromEntry(buf, buf_len);
510 break;
511 default:
512 NOTREACHED();
513 rv = ERR_FAILED;
516 if (rv == ERR_IO_PENDING) {
517 DCHECK(callback_.is_null());
518 callback_ = callback;
520 return rv;
523 void HttpCache::Transaction::StopCaching() {
524 // We really don't know where we are now. Hopefully there is no operation in
525 // progress, but nothing really prevents this method to be called after we
526 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
527 // point because we need the state machine for that (and even if we are really
528 // free, that would be an asynchronous operation). In other words, keep the
529 // entry how it is (it will be marked as truncated at destruction), and let
530 // the next piece of code that executes know that we are now reading directly
531 // from the net.
532 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
533 // future request for the resource will be blocked on this one.
534 // Fix this.
535 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
536 !is_sparse_ && !range_requested_) {
537 mode_ = NONE;
541 bool HttpCache::Transaction::GetFullRequestHeaders(
542 HttpRequestHeaders* headers) const {
543 if (network_trans_)
544 return network_trans_->GetFullRequestHeaders(headers);
546 // TODO(ttuttle): Read headers from cache.
547 return false;
550 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
551 int64 total_received_bytes = total_received_bytes_;
552 if (network_trans_)
553 total_received_bytes += network_trans_->GetTotalReceivedBytes();
554 return total_received_bytes;
557 void HttpCache::Transaction::DoneReading() {
558 if (cache_.get() && entry_) {
559 DCHECK_NE(mode_, UPDATE);
560 if (mode_ & WRITE) {
561 DoneWritingToEntry(true);
562 } else if (mode_ & READ) {
563 // It is necessary to check mode_ & READ because it is possible
564 // for mode_ to be NONE and entry_ non-NULL with a write entry
565 // if StopCaching was called.
566 cache_->DoneReadingFromEntry(entry_, this);
567 entry_ = NULL;
572 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
573 // Null headers means we encountered an error or haven't a response yet
574 if (auth_response_.headers.get())
575 return &auth_response_;
576 return &response_;
579 LoadState HttpCache::Transaction::GetLoadState() const {
580 LoadState state = GetWriterLoadState();
581 if (state != LOAD_STATE_WAITING_FOR_CACHE)
582 return state;
584 if (cache_.get())
585 return cache_->GetLoadStateForPendingTransaction(this);
587 return LOAD_STATE_IDLE;
590 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
591 if (network_trans_.get())
592 return network_trans_->GetUploadProgress();
593 return final_upload_progress_;
596 void HttpCache::Transaction::SetQuicServerInfo(
597 QuicServerInfo* quic_server_info) {}
599 bool HttpCache::Transaction::GetLoadTimingInfo(
600 LoadTimingInfo* load_timing_info) const {
601 if (network_trans_)
602 return network_trans_->GetLoadTimingInfo(load_timing_info);
604 if (old_network_trans_load_timing_) {
605 *load_timing_info = *old_network_trans_load_timing_;
606 return true;
609 if (first_cache_access_since_.is_null())
610 return false;
612 // If the cache entry was opened, return that time.
613 load_timing_info->send_start = first_cache_access_since_;
614 // This time doesn't make much sense when reading from the cache, so just use
615 // the same time as send_start.
616 load_timing_info->send_end = first_cache_access_since_;
617 return true;
620 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
621 priority_ = priority;
622 if (network_trans_)
623 network_trans_->SetPriority(priority_);
626 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
627 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
628 websocket_handshake_stream_base_create_helper_ = create_helper;
629 if (network_trans_)
630 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
633 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
634 const BeforeNetworkStartCallback& callback) {
635 DCHECK(!network_trans_);
636 before_network_start_callback_ = callback;
639 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
640 const BeforeProxyHeadersSentCallback& callback) {
641 DCHECK(!network_trans_);
642 before_proxy_headers_sent_callback_ = callback;
645 int HttpCache::Transaction::ResumeNetworkStart() {
646 if (network_trans_)
647 return network_trans_->ResumeNetworkStart();
648 return ERR_UNEXPECTED;
651 void HttpCache::Transaction::GetConnectionAttempts(
652 ConnectionAttempts* out) const {
653 ConnectionAttempts new_connection_attempts;
654 if (network_trans_)
655 network_trans_->GetConnectionAttempts(&new_connection_attempts);
657 out->swap(new_connection_attempts);
658 out->insert(out->begin(), old_connection_attempts_.begin(),
659 old_connection_attempts_.end());
662 //-----------------------------------------------------------------------------
664 void HttpCache::Transaction::DoCallback(int rv) {
665 DCHECK(rv != ERR_IO_PENDING);
666 DCHECK(!callback_.is_null());
668 read_buf_ = NULL; // Release the buffer before invoking the callback.
670 // Since Run may result in Read being called, clear callback_ up front.
671 CompletionCallback c = callback_;
672 callback_.Reset();
673 c.Run(rv);
676 int HttpCache::Transaction::HandleResult(int rv) {
677 DCHECK(rv != ERR_IO_PENDING);
678 if (!callback_.is_null())
679 DoCallback(rv);
681 return rv;
684 // A few common patterns: (Foo* means Foo -> FooComplete)
686 // 1. Not-cached entry:
687 // Start():
688 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
689 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
690 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
691 // PartialHeadersReceived
693 // Read():
694 // NetworkRead* -> CacheWriteData*
696 // 2. Cached entry, no validation:
697 // Start():
698 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
699 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
700 // BeginCacheValidation() -> SetupEntryForRead()
702 // Read():
703 // CacheReadData*
705 // 3. Cached entry, validation (304):
706 // Start():
707 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
708 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
709 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
710 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
711 // -> OverwriteCachedResponse -> PartialHeadersReceived
713 // Read():
714 // CacheReadData*
716 // 4. Cached entry, validation and replace (200):
717 // Start():
718 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
719 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
720 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
721 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
722 // TruncateCachedMetadata* -> PartialHeadersReceived
724 // Read():
725 // NetworkRead* -> CacheWriteData*
727 // 5. Sparse entry, partially cached, byte range request:
728 // Start():
729 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
730 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
731 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
732 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
733 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
734 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
735 // -> OverwriteCachedResponse -> PartialHeadersReceived
737 // Read() 1:
738 // NetworkRead* -> CacheWriteData*
740 // Read() 2:
741 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
742 // CompletePartialCacheValidation -> CacheReadData* ->
744 // Read() 3:
745 // CacheReadData* -> StartPartialCacheValidation ->
746 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
747 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
748 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
750 // 6. HEAD. Not-cached entry:
751 // Pass through. Don't save a HEAD by itself.
752 // Start():
753 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
755 // 7. HEAD. Cached entry, no validation:
756 // Start():
757 // The same flow as for a GET request (example #2)
759 // Read():
760 // CacheReadData (returns 0)
762 // 8. HEAD. Cached entry, validation (304):
763 // The request updates the stored headers.
764 // Start(): Same as for a GET request (example #3)
766 // Read():
767 // CacheReadData (returns 0)
769 // 9. HEAD. Cached entry, validation and replace (200):
770 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
771 // itself.
772 // Start():
773 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
774 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
775 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
776 // OverwriteCachedResponse
778 // 10. HEAD. Sparse entry, partially cached:
779 // Serve the request from the cache, as long as it doesn't require
780 // revalidation. Ignore missing ranges when deciding to revalidate. If the
781 // entry requires revalidation, ignore the whole request and go to full pass
782 // through (the result of the HEAD request will NOT update the entry).
784 // Start(): Basically the same as example 7, as we never create a partial_
785 // object for this request.
787 // 11. Prefetch, not-cached entry:
788 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
789 // UpdateCachedResponse.
791 // 12. Prefetch, cached entry:
792 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
793 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
794 // bit is unset.
796 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
797 // Skip validation, similar to example 2.
798 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
799 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
800 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
801 // SetupEntryForRead()
803 // Read():
804 // CacheReadData*
806 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
807 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
808 // CacheReadResponse* and CacheDispatchValidation.
809 int HttpCache::Transaction::DoLoop(int result) {
810 DCHECK(next_state_ != STATE_NONE);
812 int rv = result;
813 do {
814 State state = next_state_;
815 next_state_ = STATE_NONE;
816 switch (state) {
817 case STATE_GET_BACKEND:
818 DCHECK_EQ(OK, rv);
819 rv = DoGetBackend();
820 break;
821 case STATE_GET_BACKEND_COMPLETE:
822 rv = DoGetBackendComplete(rv);
823 break;
824 case STATE_SEND_REQUEST:
825 DCHECK_EQ(OK, rv);
826 rv = DoSendRequest();
827 break;
828 case STATE_SEND_REQUEST_COMPLETE:
829 rv = DoSendRequestComplete(rv);
830 break;
831 case STATE_SUCCESSFUL_SEND_REQUEST:
832 DCHECK_EQ(OK, rv);
833 rv = DoSuccessfulSendRequest();
834 break;
835 case STATE_NETWORK_READ:
836 DCHECK_EQ(OK, rv);
837 rv = DoNetworkRead();
838 break;
839 case STATE_NETWORK_READ_COMPLETE:
840 rv = DoNetworkReadComplete(rv);
841 break;
842 case STATE_INIT_ENTRY:
843 DCHECK_EQ(OK, rv);
844 rv = DoInitEntry();
845 break;
846 case STATE_OPEN_ENTRY:
847 DCHECK_EQ(OK, rv);
848 rv = DoOpenEntry();
849 break;
850 case STATE_OPEN_ENTRY_COMPLETE:
851 rv = DoOpenEntryComplete(rv);
852 break;
853 case STATE_CREATE_ENTRY:
854 DCHECK_EQ(OK, rv);
855 rv = DoCreateEntry();
856 break;
857 case STATE_CREATE_ENTRY_COMPLETE:
858 rv = DoCreateEntryComplete(rv);
859 break;
860 case STATE_DOOM_ENTRY:
861 DCHECK_EQ(OK, rv);
862 rv = DoDoomEntry();
863 break;
864 case STATE_DOOM_ENTRY_COMPLETE:
865 rv = DoDoomEntryComplete(rv);
866 break;
867 case STATE_ADD_TO_ENTRY:
868 DCHECK_EQ(OK, rv);
869 rv = DoAddToEntry();
870 break;
871 case STATE_ADD_TO_ENTRY_COMPLETE:
872 rv = DoAddToEntryComplete(rv);
873 break;
874 case STATE_START_PARTIAL_CACHE_VALIDATION:
875 DCHECK_EQ(OK, rv);
876 rv = DoStartPartialCacheValidation();
877 break;
878 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
879 rv = DoCompletePartialCacheValidation(rv);
880 break;
881 case STATE_UPDATE_CACHED_RESPONSE:
882 DCHECK_EQ(OK, rv);
883 rv = DoUpdateCachedResponse();
884 break;
885 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
886 rv = DoUpdateCachedResponseComplete(rv);
887 break;
888 case STATE_OVERWRITE_CACHED_RESPONSE:
889 DCHECK_EQ(OK, rv);
890 rv = DoOverwriteCachedResponse();
891 break;
892 case STATE_TRUNCATE_CACHED_DATA:
893 DCHECK_EQ(OK, rv);
894 rv = DoTruncateCachedData();
895 break;
896 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
897 rv = DoTruncateCachedDataComplete(rv);
898 break;
899 case STATE_TRUNCATE_CACHED_METADATA:
900 DCHECK_EQ(OK, rv);
901 rv = DoTruncateCachedMetadata();
902 break;
903 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
904 rv = DoTruncateCachedMetadataComplete(rv);
905 break;
906 case STATE_PARTIAL_HEADERS_RECEIVED:
907 DCHECK_EQ(OK, rv);
908 rv = DoPartialHeadersReceived();
909 break;
910 case STATE_CACHE_READ_RESPONSE:
911 DCHECK_EQ(OK, rv);
912 rv = DoCacheReadResponse();
913 break;
914 case STATE_CACHE_READ_RESPONSE_COMPLETE:
915 rv = DoCacheReadResponseComplete(rv);
916 break;
917 case STATE_CACHE_DISPATCH_VALIDATION:
918 DCHECK_EQ(OK, rv);
919 rv = DoCacheDispatchValidation();
920 break;
921 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH:
922 DCHECK_EQ(OK, rv);
923 rv = DoCacheToggleUnusedSincePrefetch();
924 break;
925 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE:
926 rv = DoCacheToggleUnusedSincePrefetchComplete(rv);
927 break;
928 case STATE_CACHE_WRITE_RESPONSE:
929 DCHECK_EQ(OK, rv);
930 rv = DoCacheWriteResponse();
931 break;
932 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
933 DCHECK_EQ(OK, rv);
934 rv = DoCacheWriteTruncatedResponse();
935 break;
936 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
937 rv = DoCacheWriteResponseComplete(rv);
938 break;
939 case STATE_CACHE_READ_METADATA:
940 DCHECK_EQ(OK, rv);
941 rv = DoCacheReadMetadata();
942 break;
943 case STATE_CACHE_READ_METADATA_COMPLETE:
944 rv = DoCacheReadMetadataComplete(rv);
945 break;
946 case STATE_CACHE_QUERY_DATA:
947 DCHECK_EQ(OK, rv);
948 rv = DoCacheQueryData();
949 break;
950 case STATE_CACHE_QUERY_DATA_COMPLETE:
951 rv = DoCacheQueryDataComplete(rv);
952 break;
953 case STATE_CACHE_READ_DATA:
954 DCHECK_EQ(OK, rv);
955 rv = DoCacheReadData();
956 break;
957 case STATE_CACHE_READ_DATA_COMPLETE:
958 rv = DoCacheReadDataComplete(rv);
959 break;
960 case STATE_CACHE_WRITE_DATA:
961 rv = DoCacheWriteData(rv);
962 break;
963 case STATE_CACHE_WRITE_DATA_COMPLETE:
964 rv = DoCacheWriteDataComplete(rv);
965 break;
966 default:
967 NOTREACHED() << "bad state";
968 rv = ERR_FAILED;
969 break;
971 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
973 if (rv != ERR_IO_PENDING)
974 HandleResult(rv);
976 return rv;
979 int HttpCache::Transaction::DoGetBackend() {
980 cache_pending_ = true;
981 next_state_ = STATE_GET_BACKEND_COMPLETE;
982 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
983 return cache_->GetBackendForTransaction(this);
986 int HttpCache::Transaction::DoGetBackendComplete(int result) {
987 DCHECK(result == OK || result == ERR_FAILED);
988 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
989 result);
990 cache_pending_ = false;
992 if (!ShouldPassThrough()) {
993 cache_key_ = cache_->GenerateCacheKey(request_);
995 // Requested cache access mode.
996 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
997 mode_ = READ;
998 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
999 mode_ = WRITE;
1000 } else {
1001 mode_ = READ_WRITE;
1004 // Downgrade to UPDATE if the request has been externally conditionalized.
1005 if (external_validation_.initialized) {
1006 if (mode_ & WRITE) {
1007 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1008 // in case READ was off).
1009 mode_ = UPDATE;
1010 } else {
1011 mode_ = NONE;
1016 // Use PUT and DELETE only to invalidate existing stored entries.
1017 if ((request_->method == "PUT" || request_->method == "DELETE") &&
1018 mode_ != READ_WRITE && mode_ != WRITE) {
1019 mode_ = NONE;
1022 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1023 // transaction behaves the same for GET and HEAD requests at this point: if it
1024 // was not modified, the entry is updated and a response is not returned from
1025 // the cache. If we receive 200, it doesn't matter if there was a validation
1026 // header or not.
1027 if (request_->method == "HEAD" && mode_ == WRITE)
1028 mode_ = NONE;
1030 // If must use cache, then we must fail. This can happen for back/forward
1031 // navigations to a page generated via a form post.
1032 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
1033 return ERR_CACHE_MISS;
1035 if (mode_ == NONE) {
1036 if (partial_.get()) {
1037 partial_->RestoreHeaders(&custom_request_->extra_headers);
1038 partial_.reset();
1040 next_state_ = STATE_SEND_REQUEST;
1041 } else {
1042 next_state_ = STATE_INIT_ENTRY;
1045 // This is only set if we have something to do with the response.
1046 range_requested_ = (partial_.get() != NULL);
1048 return OK;
1051 int HttpCache::Transaction::DoSendRequest() {
1052 DCHECK(mode_ & WRITE || mode_ == NONE);
1053 DCHECK(!network_trans_.get());
1055 send_request_since_ = TimeTicks::Now();
1057 // Create a network transaction.
1058 int rv = cache_->network_layer_->CreateTransaction(priority_,
1059 &network_trans_);
1060 if (rv != OK)
1061 return rv;
1062 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1063 network_trans_->SetBeforeProxyHeadersSentCallback(
1064 before_proxy_headers_sent_callback_);
1066 // Old load timing information, if any, is now obsolete.
1067 old_network_trans_load_timing_.reset();
1069 if (websocket_handshake_stream_base_create_helper_)
1070 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1071 websocket_handshake_stream_base_create_helper_);
1073 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1074 rv = network_trans_->Start(request_, io_callback_, net_log_);
1075 return rv;
1078 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1079 if (!cache_.get())
1080 return ERR_UNEXPECTED;
1082 // If we tried to conditionalize the request and failed, we know
1083 // we won't be reading from the cache after this point.
1084 if (couldnt_conditionalize_request_)
1085 mode_ = WRITE;
1087 if (result == OK) {
1088 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
1089 return OK;
1092 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1093 response_.network_accessed = response->network_accessed;
1095 // Do not record requests that have network errors or restarts.
1096 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1097 if (IsCertificateError(result)) {
1098 // If we get a certificate error, then there is a certificate in ssl_info,
1099 // so GetResponseInfo() should never return NULL here.
1100 DCHECK(response);
1101 response_.ssl_info = response->ssl_info;
1102 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1103 DCHECK(response);
1104 response_.cert_request_info = response->cert_request_info;
1105 } else if (response_.was_cached) {
1106 DoneWritingToEntry(true);
1109 return result;
1112 // We received the response headers and there is no error.
1113 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1114 DCHECK(!new_response_);
1115 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1117 if (new_response->headers->response_code() == 401 ||
1118 new_response->headers->response_code() == 407) {
1119 auth_response_ = *new_response;
1120 if (!reading_)
1121 return OK;
1123 // We initiated a second request the caller doesn't know about. We should be
1124 // able to authenticate this request because we should have authenticated
1125 // this URL moments ago.
1126 if (IsReadyToRestartForAuth()) {
1127 DCHECK(!response_.auth_challenge.get());
1128 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1129 // In theory we should check to see if there are new cookies, but there
1130 // is no way to do that from here.
1131 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1134 // We have to perform cleanup at this point so that at least the next
1135 // request can succeed. We do not retry at this point, because data
1136 // has been read and we have no way to gather credentials. We would
1137 // fail again, and potentially loop. This can happen if the credentials
1138 // expire while chrome is suspended.
1139 if (entry_)
1140 DoomPartialEntry(false);
1141 mode_ = NONE;
1142 partial_.reset();
1143 ResetNetworkTransaction();
1144 return ERR_CACHE_AUTH_FAILURE_AFTER_READ;
1147 new_response_ = new_response;
1148 if (!ValidatePartialResponse() && !auth_response_.headers.get()) {
1149 // Something went wrong with this request and we have to restart it.
1150 // If we have an authentication response, we are exposed to weird things
1151 // hapenning if the user cancels the authentication before we receive
1152 // the new response.
1153 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST);
1154 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1155 response_ = HttpResponseInfo();
1156 ResetNetworkTransaction();
1157 new_response_ = NULL;
1158 next_state_ = STATE_SEND_REQUEST;
1159 return OK;
1162 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1163 // We have stored the full entry, but it changed and the server is
1164 // sending a range. We have to delete the old entry.
1165 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1166 DoneWritingToEntry(false);
1169 if (mode_ == WRITE &&
1170 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1171 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1174 // Invalidate any cached GET with a successful PUT or DELETE.
1175 if (mode_ == WRITE &&
1176 (request_->method == "PUT" || request_->method == "DELETE")) {
1177 if (NonErrorResponse(new_response->headers->response_code())) {
1178 int ret = cache_->DoomEntry(cache_key_, NULL);
1179 DCHECK_EQ(OK, ret);
1181 cache_->DoneWritingToEntry(entry_, true);
1182 entry_ = NULL;
1183 mode_ = NONE;
1186 // Invalidate any cached GET with a successful POST.
1187 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1188 request_->method == "POST" &&
1189 NonErrorResponse(new_response->headers->response_code())) {
1190 cache_->DoomMainEntryForUrl(request_->url);
1193 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1195 if (new_response_->headers->response_code() == 416 &&
1196 (request_->method == "GET" || request_->method == "POST")) {
1197 // If there is an active entry it may be destroyed with this transaction.
1198 response_ = *new_response_;
1199 return OK;
1202 // Are we expecting a response to a conditional query?
1203 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1204 if (new_response->headers->response_code() == 304 || handling_206_) {
1205 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1206 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1207 return OK;
1209 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1210 mode_ = WRITE;
1213 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1214 return OK;
1217 int HttpCache::Transaction::DoNetworkRead() {
1218 next_state_ = STATE_NETWORK_READ_COMPLETE;
1219 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1222 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1223 DCHECK(mode_ & WRITE || mode_ == NONE);
1225 if (!cache_.get())
1226 return ERR_UNEXPECTED;
1228 // If there is an error or we aren't saving the data, we are done; just wait
1229 // until the destructor runs to see if we can keep the data.
1230 if (mode_ == NONE || result < 0)
1231 return result;
1233 next_state_ = STATE_CACHE_WRITE_DATA;
1234 return result;
1237 int HttpCache::Transaction::DoInitEntry() {
1238 DCHECK(!new_entry_);
1240 if (!cache_.get())
1241 return ERR_UNEXPECTED;
1243 if (mode_ == WRITE) {
1244 next_state_ = STATE_DOOM_ENTRY;
1245 return OK;
1248 next_state_ = STATE_OPEN_ENTRY;
1249 return OK;
1252 int HttpCache::Transaction::DoOpenEntry() {
1253 DCHECK(!new_entry_);
1254 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1255 cache_pending_ = true;
1256 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1257 first_cache_access_since_ = TimeTicks::Now();
1258 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1261 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1262 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1263 // OK, otherwise the cache will end up with an active entry without any
1264 // transaction attached.
1265 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1266 cache_pending_ = false;
1267 if (result == OK) {
1268 next_state_ = STATE_ADD_TO_ENTRY;
1269 return OK;
1272 if (result == ERR_CACHE_RACE) {
1273 next_state_ = STATE_INIT_ENTRY;
1274 return OK;
1277 if (request_->method == "PUT" || request_->method == "DELETE" ||
1278 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1279 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1280 mode_ = NONE;
1281 next_state_ = STATE_SEND_REQUEST;
1282 return OK;
1285 if (mode_ == READ_WRITE) {
1286 mode_ = WRITE;
1287 next_state_ = STATE_CREATE_ENTRY;
1288 return OK;
1290 if (mode_ == UPDATE) {
1291 // There is no cache entry to update; proceed without caching.
1292 mode_ = NONE;
1293 next_state_ = STATE_SEND_REQUEST;
1294 return OK;
1297 // The entry does not exist, and we are not permitted to create a new entry,
1298 // so we must fail.
1299 return ERR_CACHE_MISS;
1302 int HttpCache::Transaction::DoCreateEntry() {
1303 DCHECK(!new_entry_);
1304 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1305 cache_pending_ = true;
1306 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1307 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1310 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1311 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1312 // OK, otherwise the cache will end up with an active entry without any
1313 // transaction attached.
1314 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1315 result);
1316 cache_pending_ = false;
1317 next_state_ = STATE_ADD_TO_ENTRY;
1319 if (result == ERR_CACHE_RACE) {
1320 next_state_ = STATE_INIT_ENTRY;
1321 return OK;
1324 if (result != OK) {
1325 // We have a race here: Maybe we failed to open the entry and decided to
1326 // create one, but by the time we called create, another transaction already
1327 // created the entry. If we want to eliminate this issue, we need an atomic
1328 // OpenOrCreate() method exposed by the disk cache.
1329 DLOG(WARNING) << "Unable to create cache entry";
1330 mode_ = NONE;
1331 if (partial_.get())
1332 partial_->RestoreHeaders(&custom_request_->extra_headers);
1333 next_state_ = STATE_SEND_REQUEST;
1335 return OK;
1338 int HttpCache::Transaction::DoDoomEntry() {
1339 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1340 cache_pending_ = true;
1341 if (first_cache_access_since_.is_null())
1342 first_cache_access_since_ = TimeTicks::Now();
1343 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1344 return cache_->DoomEntry(cache_key_, this);
1347 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1348 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1349 next_state_ = STATE_CREATE_ENTRY;
1350 cache_pending_ = false;
1351 if (result == ERR_CACHE_RACE)
1352 next_state_ = STATE_INIT_ENTRY;
1353 return OK;
1356 int HttpCache::Transaction::DoAddToEntry() {
1357 DCHECK(new_entry_);
1358 cache_pending_ = true;
1359 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1360 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1361 DCHECK(entry_lock_waiting_since_.is_null());
1362 entry_lock_waiting_since_ = TimeTicks::Now();
1363 int rv = cache_->AddTransactionToEntry(new_entry_, this);
1364 if (rv == ERR_IO_PENDING) {
1365 if (bypass_lock_for_test_) {
1366 OnAddToEntryTimeout(entry_lock_waiting_since_);
1367 } else {
1368 int timeout_milliseconds = 20 * 1000;
1369 if (partial_ && new_entry_->writer &&
1370 new_entry_->writer->range_requested_) {
1371 // Quickly timeout and bypass the cache if we're a range request and
1372 // we're blocked by the reader/writer lock. Doing so eliminates a long
1373 // running issue, http://crbug.com/31014, where two of the same media
1374 // resources could not be played back simultaneously due to one locking
1375 // the cache entry until the entire video was downloaded.
1377 // Bypassing the cache is not ideal, as we are now ignoring the cache
1378 // entirely for all range requests to a resource beyond the first. This
1379 // is however a much more succinct solution than the alternatives, which
1380 // would require somewhat significant changes to the http caching logic.
1382 // Allow some timeout slack for the entry addition to complete in case
1383 // the writer lock is imminently released; we want to avoid skipping
1384 // the cache if at all possible. See http://crbug.com/408765
1385 timeout_milliseconds = 25;
1387 base::MessageLoop::current()->PostDelayedTask(
1388 FROM_HERE,
1389 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout,
1390 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_),
1391 TimeDelta::FromMilliseconds(timeout_milliseconds));
1394 return rv;
1397 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1398 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1399 result);
1400 const TimeDelta entry_lock_wait =
1401 TimeTicks::Now() - entry_lock_waiting_since_;
1402 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1404 entry_lock_waiting_since_ = TimeTicks();
1405 DCHECK(new_entry_);
1406 cache_pending_ = false;
1408 if (result == OK)
1409 entry_ = new_entry_;
1411 // If there is a failure, the cache should have taken care of new_entry_.
1412 new_entry_ = NULL;
1414 if (result == ERR_CACHE_RACE) {
1415 next_state_ = STATE_INIT_ENTRY;
1416 return OK;
1419 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1420 // The cache is busy, bypass it for this transaction.
1421 mode_ = NONE;
1422 next_state_ = STATE_SEND_REQUEST;
1423 if (partial_) {
1424 partial_->RestoreHeaders(&custom_request_->extra_headers);
1425 partial_.reset();
1427 return OK;
1430 if (result != OK) {
1431 NOTREACHED();
1432 return result;
1435 if (mode_ == WRITE) {
1436 if (partial_.get())
1437 partial_->RestoreHeaders(&custom_request_->extra_headers);
1438 next_state_ = STATE_SEND_REQUEST;
1439 } else {
1440 // We have to read the headers from the cached entry.
1441 DCHECK(mode_ & READ_META);
1442 next_state_ = STATE_CACHE_READ_RESPONSE;
1444 return OK;
1447 // We may end up here multiple times for a given request.
1448 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1449 if (mode_ == NONE)
1450 return OK;
1452 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1453 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1456 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1457 if (!result) {
1458 // This is the end of the request.
1459 if (mode_ & WRITE) {
1460 DoneWritingToEntry(true);
1461 } else {
1462 cache_->DoneReadingFromEntry(entry_, this);
1463 entry_ = NULL;
1465 return result;
1468 if (result < 0)
1469 return result;
1471 partial_->PrepareCacheValidation(entry_->disk_entry,
1472 &custom_request_->extra_headers);
1474 if (reading_ && partial_->IsCurrentRangeCached()) {
1475 next_state_ = STATE_CACHE_READ_DATA;
1476 return OK;
1479 return BeginCacheValidation();
1482 // We received 304 or 206 and we want to update the cached response headers.
1483 int HttpCache::Transaction::DoUpdateCachedResponse() {
1484 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1485 int rv = OK;
1486 // Update cached response based on headers in new_response.
1487 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1488 response_.headers->Update(*new_response_->headers.get());
1489 response_.response_time = new_response_->response_time;
1490 response_.request_time = new_response_->request_time;
1491 response_.network_accessed = new_response_->network_accessed;
1492 response_.unused_since_prefetch = new_response_->unused_since_prefetch;
1493 if (new_response_->vary_data.is_valid()) {
1494 response_.vary_data = new_response_->vary_data;
1495 } else if (response_.vary_data.is_valid()) {
1496 // There is a vary header in the stored response but not in the current one.
1497 // Update the data with the new request headers.
1498 HttpVaryData new_vary_data;
1499 new_vary_data.Init(*request_, *response_.headers.get());
1500 response_.vary_data = new_vary_data;
1503 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1504 if (!entry_->doomed) {
1505 int ret = cache_->DoomEntry(cache_key_, NULL);
1506 DCHECK_EQ(OK, ret);
1508 } else {
1509 // If we are already reading, we already updated the headers for this
1510 // request; doing it again will change Content-Length.
1511 if (!reading_) {
1512 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1513 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1514 rv = OK;
1517 return rv;
1520 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1521 if (mode_ == UPDATE) {
1522 DCHECK(!handling_206_);
1523 // We got a "not modified" response and already updated the corresponding
1524 // cache entry above.
1526 // By closing the cached entry now, we make sure that the 304 rather than
1527 // the cached 200 response, is what will be returned to the user.
1528 DoneWritingToEntry(true);
1529 } else if (entry_ && !handling_206_) {
1530 DCHECK_EQ(READ_WRITE, mode_);
1531 if (!partial_.get() || partial_->IsLastRange()) {
1532 cache_->ConvertWriterToReader(entry_);
1533 mode_ = READ;
1535 // We no longer need the network transaction, so destroy it.
1536 final_upload_progress_ = network_trans_->GetUploadProgress();
1537 ResetNetworkTransaction();
1538 } else if (entry_ && handling_206_ && truncated_ &&
1539 partial_->initial_validation()) {
1540 // We just finished the validation of a truncated entry, and the server
1541 // is willing to resume the operation. Now we go back and start serving
1542 // the first part to the user.
1543 ResetNetworkTransaction();
1544 new_response_ = NULL;
1545 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1546 partial_->SetRangeToStartDownload();
1547 return OK;
1549 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1550 return OK;
1553 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1554 if (mode_ & READ) {
1555 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1556 return OK;
1559 // We change the value of Content-Length for partial content.
1560 if (handling_206_ && partial_.get())
1561 partial_->FixContentLength(new_response_->headers.get());
1563 response_ = *new_response_;
1565 if (request_->method == "HEAD") {
1566 // This response is replacing the cached one.
1567 DoneWritingToEntry(false);
1568 mode_ = NONE;
1569 new_response_ = NULL;
1570 return OK;
1573 if (handling_206_ && !CanResume(false)) {
1574 // There is no point in storing this resource because it will never be used.
1575 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1576 DoneWritingToEntry(false);
1577 if (partial_.get())
1578 partial_->FixResponseHeaders(response_.headers.get(), true);
1579 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1580 return OK;
1583 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1584 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1585 STATE_CACHE_WRITE_RESPONSE;
1586 return OK;
1589 int HttpCache::Transaction::DoTruncateCachedData() {
1590 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1591 if (!entry_)
1592 return OK;
1593 if (net_log_.IsCapturing())
1594 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1595 // Truncate the stream.
1596 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1599 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1600 if (entry_) {
1601 if (net_log_.IsCapturing()) {
1602 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1603 result);
1607 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1608 return OK;
1611 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1612 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1613 if (!entry_)
1614 return OK;
1616 if (net_log_.IsCapturing())
1617 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1618 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1621 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1622 if (entry_) {
1623 if (net_log_.IsCapturing()) {
1624 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1625 result);
1629 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1630 return OK;
1633 int HttpCache::Transaction::DoPartialHeadersReceived() {
1634 new_response_ = NULL;
1635 if (entry_ && !partial_.get() &&
1636 entry_->disk_entry->GetDataSize(kMetadataIndex))
1637 next_state_ = STATE_CACHE_READ_METADATA;
1639 if (!partial_.get())
1640 return OK;
1642 if (reading_) {
1643 if (network_trans_.get()) {
1644 next_state_ = STATE_NETWORK_READ;
1645 } else {
1646 next_state_ = STATE_CACHE_READ_DATA;
1648 } else if (mode_ != NONE) {
1649 // We are about to return the headers for a byte-range request to the user,
1650 // so let's fix them.
1651 partial_->FixResponseHeaders(response_.headers.get(), true);
1653 return OK;
1656 int HttpCache::Transaction::DoCacheReadResponse() {
1657 DCHECK(entry_);
1658 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1660 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1661 read_buf_ = new IOBuffer(io_buf_len_);
1663 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1664 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1665 io_buf_len_, io_callback_);
1668 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1669 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1670 if (result != io_buf_len_ ||
1671 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1672 &response_, &truncated_)) {
1673 return OnCacheReadError(result, true);
1676 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1677 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1678 ReadCertChain();
1680 // Some resources may have slipped in as truncated when they're not.
1681 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1682 if (response_.headers->GetContentLength() == current_size)
1683 truncated_ = false;
1685 if ((response_.unused_since_prefetch &&
1686 !(request_->load_flags & LOAD_PREFETCH)) ||
1687 (!response_.unused_since_prefetch &&
1688 (request_->load_flags & LOAD_PREFETCH))) {
1689 // Either this is the first use of an entry since it was prefetched or
1690 // this is a prefetch. The value of response.unused_since_prefetch is valid
1691 // for this transaction but the bit needs to be flipped in storage.
1692 next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH;
1693 return OK;
1696 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1697 return OK;
1700 int HttpCache::Transaction::DoCacheDispatchValidation() {
1701 // We now have access to the cache entry.
1703 // o if we are a reader for the transaction, then we can start reading the
1704 // cache entry.
1706 // o if we can read or write, then we should check if the cache entry needs
1707 // to be validated and then issue a network request if needed or just read
1708 // from the cache if the cache entry is already valid.
1710 // o if we are set to UPDATE, then we are handling an externally
1711 // conditionalized request (if-modified-since / if-none-match). We check
1712 // if the request headers define a validation request.
1714 int result = ERR_FAILED;
1715 switch (mode_) {
1716 case READ:
1717 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1718 result = BeginCacheRead();
1719 break;
1720 case READ_WRITE:
1721 result = BeginPartialCacheValidation();
1722 break;
1723 case UPDATE:
1724 result = BeginExternallyConditionalizedRequest();
1725 break;
1726 case WRITE:
1727 default:
1728 NOTREACHED();
1730 return result;
1733 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1734 // Write back the toggled value for the next use of this entry.
1735 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1737 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1738 // transaction then metadata will be written to cache twice. If prefetching
1739 // becomes more common, consider combining the writes.
1740 target_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE;
1741 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1742 return OK;
1745 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1746 int result) {
1747 // Restore the original value for this transaction.
1748 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1749 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1750 return OK;
1753 int HttpCache::Transaction::DoCacheWriteResponse() {
1754 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1755 tracked_objects::ScopedTracker tracking_profile(
1756 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1757 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1759 if (entry_) {
1760 if (net_log_.IsCapturing())
1761 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1763 return WriteResponseInfoToEntry(false);
1766 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1767 if (entry_) {
1768 if (net_log_.IsCapturing())
1769 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1771 return WriteResponseInfoToEntry(true);
1774 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1775 next_state_ = target_state_;
1776 target_state_ = STATE_NONE;
1777 if (!entry_)
1778 return OK;
1779 if (net_log_.IsCapturing()) {
1780 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1781 result);
1784 // Balance the AddRef from WriteResponseInfoToEntry.
1785 if (result != io_buf_len_) {
1786 DLOG(ERROR) << "failed to write response info to cache";
1787 DoneWritingToEntry(false);
1789 return OK;
1792 int HttpCache::Transaction::DoCacheReadMetadata() {
1793 DCHECK(entry_);
1794 DCHECK(!response_.metadata.get());
1795 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1797 response_.metadata =
1798 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1800 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1801 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1802 response_.metadata.get(),
1803 response_.metadata->size(),
1804 io_callback_);
1807 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1808 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1809 if (result != response_.metadata->size())
1810 return OnCacheReadError(result, false);
1811 return OK;
1814 int HttpCache::Transaction::DoCacheQueryData() {
1815 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1816 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1819 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1820 DCHECK_EQ(OK, result);
1821 if (!cache_.get())
1822 return ERR_UNEXPECTED;
1824 return ValidateEntryHeadersAndContinue();
1827 int HttpCache::Transaction::DoCacheReadData() {
1828 DCHECK(entry_);
1829 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1831 if (net_log_.IsCapturing())
1832 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1833 if (partial_.get()) {
1834 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1835 io_callback_);
1838 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1839 read_buf_.get(), io_buf_len_,
1840 io_callback_);
1843 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1844 if (net_log_.IsCapturing()) {
1845 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1846 result);
1849 if (!cache_.get())
1850 return ERR_UNEXPECTED;
1852 if (partial_.get()) {
1853 // Partial requests are confusing to report in histograms because they may
1854 // have multiple underlying requests.
1855 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1856 return DoPartialCacheReadCompleted(result);
1859 if (result > 0) {
1860 read_offset_ += result;
1861 } else if (result == 0) { // End of file.
1862 RecordHistograms();
1863 cache_->DoneReadingFromEntry(entry_, this);
1864 entry_ = NULL;
1865 } else {
1866 return OnCacheReadError(result, false);
1868 return result;
1871 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1872 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1873 write_len_ = num_bytes;
1874 if (entry_) {
1875 if (net_log_.IsCapturing())
1876 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1879 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1882 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1883 if (entry_) {
1884 if (net_log_.IsCapturing()) {
1885 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1886 result);
1889 // Balance the AddRef from DoCacheWriteData.
1890 if (!cache_.get())
1891 return ERR_UNEXPECTED;
1893 if (result != write_len_) {
1894 DLOG(ERROR) << "failed to write response data to cache";
1895 DoneWritingToEntry(false);
1897 // We want to ignore errors writing to disk and just keep reading from
1898 // the network.
1899 result = write_len_;
1900 } else if (!done_reading_ && entry_) {
1901 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1902 int64 body_size = response_.headers->GetContentLength();
1903 if (body_size >= 0 && body_size <= current_size)
1904 done_reading_ = true;
1907 if (partial_.get()) {
1908 // This may be the last request.
1909 if (!(result == 0 && !truncated_ &&
1910 (partial_->IsLastRange() || mode_ == WRITE)))
1911 return DoPartialNetworkReadCompleted(result);
1914 if (result == 0) {
1915 // End of file. This may be the result of a connection problem so see if we
1916 // have to keep the entry around to be flagged as truncated later on.
1917 if (done_reading_ || !entry_ || partial_.get() ||
1918 response_.headers->GetContentLength() <= 0)
1919 DoneWritingToEntry(true);
1922 return result;
1925 //-----------------------------------------------------------------------------
1927 void HttpCache::Transaction::ReadCertChain() {
1928 std::string key =
1929 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
1930 const X509Certificate::OSCertHandles& intermediates =
1931 response_.ssl_info.cert->GetIntermediateCertificates();
1932 int dist_from_root = intermediates.size();
1934 scoped_refptr<SharedChainData> shared_chain_data(
1935 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1936 cache_->cert_cache()->GetCertificate(key,
1937 base::Bind(&OnCertReadIOComplete,
1938 dist_from_root,
1939 true /* is leaf */,
1940 shared_chain_data));
1942 for (X509Certificate::OSCertHandles::const_iterator it =
1943 intermediates.begin();
1944 it != intermediates.end();
1945 ++it) {
1946 --dist_from_root;
1947 key = GetCacheKeyForCert(*it);
1948 cache_->cert_cache()->GetCertificate(key,
1949 base::Bind(&OnCertReadIOComplete,
1950 dist_from_root,
1951 false /* is not leaf */,
1952 shared_chain_data));
1954 DCHECK_EQ(0, dist_from_root);
1957 void HttpCache::Transaction::WriteCertChain() {
1958 const X509Certificate::OSCertHandles& intermediates =
1959 response_.ssl_info.cert->GetIntermediateCertificates();
1960 int dist_from_root = intermediates.size();
1962 scoped_refptr<SharedChainData> shared_chain_data(
1963 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1964 cache_->cert_cache()->SetCertificate(
1965 response_.ssl_info.cert->os_cert_handle(),
1966 base::Bind(&OnCertWriteIOComplete,
1967 dist_from_root,
1968 true /* is leaf */,
1969 shared_chain_data));
1970 for (X509Certificate::OSCertHandles::const_iterator it =
1971 intermediates.begin();
1972 it != intermediates.end();
1973 ++it) {
1974 --dist_from_root;
1975 cache_->cert_cache()->SetCertificate(*it,
1976 base::Bind(&OnCertWriteIOComplete,
1977 dist_from_root,
1978 false /* is not leaf */,
1979 shared_chain_data));
1981 DCHECK_EQ(0, dist_from_root);
1984 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1985 const HttpRequestInfo* request) {
1986 net_log_ = net_log;
1987 request_ = request;
1988 effective_load_flags_ = request_->load_flags;
1990 if (cache_->mode() == DISABLE)
1991 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1993 // Some headers imply load flags. The order here is significant.
1995 // LOAD_DISABLE_CACHE : no cache read or write
1996 // LOAD_BYPASS_CACHE : no cache read
1997 // LOAD_VALIDATE_CACHE : no cache read unless validation
1999 // The former modes trump latter modes, so if we find a matching header we
2000 // can stop iterating kSpecialHeaders.
2002 static const struct {
2003 const HeaderNameAndValue* search;
2004 int load_flag;
2005 } kSpecialHeaders[] = {
2006 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
2007 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
2008 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
2011 bool range_found = false;
2012 bool external_validation_error = false;
2013 bool special_headers = false;
2015 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
2016 range_found = true;
2018 for (size_t i = 0; i < arraysize(kSpecialHeaders); ++i) {
2019 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
2020 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
2021 special_headers = true;
2022 break;
2026 // Check for conditionalization headers which may correspond with a
2027 // cache validation request.
2028 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
2029 const ValidationHeaderInfo& info = kValidationHeaders[i];
2030 std::string validation_value;
2031 if (request_->extra_headers.GetHeader(
2032 info.request_header_name, &validation_value)) {
2033 if (!external_validation_.values[i].empty() ||
2034 validation_value.empty()) {
2035 external_validation_error = true;
2037 external_validation_.values[i] = validation_value;
2038 external_validation_.initialized = true;
2042 if (range_found || special_headers || external_validation_.initialized) {
2043 // Log the headers before request_ is modified.
2044 std::string empty;
2045 net_log_.AddEvent(
2046 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS,
2047 base::Bind(&HttpRequestHeaders::NetLogCallback,
2048 base::Unretained(&request_->extra_headers), &empty));
2051 // We don't support ranges and validation headers.
2052 if (range_found && external_validation_.initialized) {
2053 LOG(WARNING) << "Byte ranges AND validation headers found.";
2054 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2057 // If there is more than one validation header, we can't treat this request as
2058 // a cache validation, since we don't know for sure which header the server
2059 // will give us a response for (and they could be contradictory).
2060 if (external_validation_error) {
2061 LOG(WARNING) << "Multiple or malformed validation headers found.";
2062 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2065 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2066 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2067 partial_.reset(new PartialData);
2068 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
2069 // We will be modifying the actual range requested to the server, so
2070 // let's remove the header here.
2071 custom_request_.reset(new HttpRequestInfo(*request_));
2072 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2073 request_ = custom_request_.get();
2074 partial_->SetHeaders(custom_request_->extra_headers);
2075 } else {
2076 // The range is invalid or we cannot handle it properly.
2077 VLOG(1) << "Invalid byte range found.";
2078 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2079 partial_.reset(NULL);
2084 bool HttpCache::Transaction::ShouldPassThrough() {
2085 // We may have a null disk_cache if there is an error we cannot recover from,
2086 // like not enough disk space, or sharing violations.
2087 if (!cache_->disk_cache_.get())
2088 return true;
2090 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2091 return true;
2093 if (request_->method == "GET" || request_->method == "HEAD")
2094 return false;
2096 if (request_->method == "POST" && request_->upload_data_stream &&
2097 request_->upload_data_stream->identifier()) {
2098 return false;
2101 if (request_->method == "PUT" && request_->upload_data_stream)
2102 return false;
2104 if (request_->method == "DELETE")
2105 return false;
2107 return true;
2110 int HttpCache::Transaction::BeginCacheRead() {
2111 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2112 if (response_.headers->response_code() == 206 || partial_.get()) {
2113 NOTREACHED();
2114 return ERR_CACHE_MISS;
2117 if (request_->method == "HEAD")
2118 FixHeadersForHead();
2120 // We don't have the whole resource.
2121 if (truncated_)
2122 return ERR_CACHE_MISS;
2124 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2125 next_state_ = STATE_CACHE_READ_METADATA;
2127 return OK;
2130 int HttpCache::Transaction::BeginCacheValidation() {
2131 DCHECK(mode_ == READ_WRITE);
2133 ValidationType required_validation = RequiresValidation();
2135 bool skip_validation = (required_validation == VALIDATION_NONE);
2137 if (required_validation == VALIDATION_ASYNCHRONOUS &&
2138 !(request_->method == "GET" && (truncated_ || partial_)) && cache_ &&
2139 cache_->use_stale_while_revalidate()) {
2140 TriggerAsyncValidation();
2141 skip_validation = true;
2144 if (request_->method == "HEAD" &&
2145 (truncated_ || response_.headers->response_code() == 206)) {
2146 DCHECK(!partial_);
2147 if (skip_validation)
2148 return SetupEntryForRead();
2150 // Bail out!
2151 next_state_ = STATE_SEND_REQUEST;
2152 mode_ = NONE;
2153 return OK;
2156 if (truncated_) {
2157 // Truncated entries can cause partial gets, so we shouldn't record this
2158 // load in histograms.
2159 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2160 skip_validation = !partial_->initial_validation();
2163 if (partial_.get() && (is_sparse_ || truncated_) &&
2164 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
2165 // Force revalidation for sparse or truncated entries. Note that we don't
2166 // want to ignore the regular validation logic just because a byte range was
2167 // part of the request.
2168 skip_validation = false;
2171 if (skip_validation) {
2172 // TODO(ricea): Is this pattern okay for asynchronous revalidations?
2173 UpdateTransactionPattern(PATTERN_ENTRY_USED);
2174 return SetupEntryForRead();
2175 } else {
2176 // Make the network request conditional, to see if we may reuse our cached
2177 // response. If we cannot do so, then we just resort to a normal fetch.
2178 // Our mode remains READ_WRITE for a conditional request. Even if the
2179 // conditionalization fails, we don't switch to WRITE mode until we
2180 // know we won't be falling back to using the cache entry in the
2181 // LOAD_FROM_CACHE_IF_OFFLINE case.
2182 if (!ConditionalizeRequest()) {
2183 couldnt_conditionalize_request_ = true;
2184 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
2185 if (partial_.get())
2186 return DoRestartPartialRequest();
2188 DCHECK_NE(206, response_.headers->response_code());
2190 next_state_ = STATE_SEND_REQUEST;
2192 return OK;
2195 int HttpCache::Transaction::BeginPartialCacheValidation() {
2196 DCHECK(mode_ == READ_WRITE);
2198 if (response_.headers->response_code() != 206 && !partial_.get() &&
2199 !truncated_) {
2200 return BeginCacheValidation();
2203 // Partial requests should not be recorded in histograms.
2204 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2205 if (range_requested_) {
2206 next_state_ = STATE_CACHE_QUERY_DATA;
2207 return OK;
2210 // The request is not for a range, but we have stored just ranges.
2212 if (request_->method == "HEAD")
2213 return BeginCacheValidation();
2215 partial_.reset(new PartialData());
2216 partial_->SetHeaders(request_->extra_headers);
2217 if (!custom_request_.get()) {
2218 custom_request_.reset(new HttpRequestInfo(*request_));
2219 request_ = custom_request_.get();
2222 return ValidateEntryHeadersAndContinue();
2225 // This should only be called once per request.
2226 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2227 DCHECK(mode_ == READ_WRITE);
2229 if (!partial_->UpdateFromStoredHeaders(
2230 response_.headers.get(), entry_->disk_entry, truncated_)) {
2231 return DoRestartPartialRequest();
2234 if (response_.headers->response_code() == 206)
2235 is_sparse_ = true;
2237 if (!partial_->IsRequestedRangeOK()) {
2238 // The stored data is fine, but the request may be invalid.
2239 invalid_range_ = true;
2242 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2243 return OK;
2246 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2247 DCHECK_EQ(UPDATE, mode_);
2248 DCHECK(external_validation_.initialized);
2250 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
2251 if (external_validation_.values[i].empty())
2252 continue;
2253 // Retrieve either the cached response's "etag" or "last-modified" header.
2254 std::string validator;
2255 response_.headers->EnumerateHeader(
2256 NULL,
2257 kValidationHeaders[i].related_response_header_name,
2258 &validator);
2260 if (response_.headers->response_code() != 200 || truncated_ ||
2261 validator.empty() || validator != external_validation_.values[i]) {
2262 // The externally conditionalized request is not a validation request
2263 // for our existing cache entry. Proceed with caching disabled.
2264 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2265 DoneWritingToEntry(true);
2269 // TODO(ricea): This calculation is expensive to perform just to collect
2270 // statistics. Either remove it or use the result, depending on the result of
2271 // the experiment.
2272 ExternallyConditionalizedType type =
2273 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE;
2274 if (mode_ == NONE)
2275 type = EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS;
2276 else if (RequiresValidation())
2277 type = EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION;
2279 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2280 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2281 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2282 type,
2283 EXTERNALLY_CONDITIONALIZED_MAX);
2285 next_state_ = STATE_SEND_REQUEST;
2286 return OK;
2289 int HttpCache::Transaction::RestartNetworkRequest() {
2290 DCHECK(mode_ & WRITE || mode_ == NONE);
2291 DCHECK(network_trans_.get());
2292 DCHECK_EQ(STATE_NONE, next_state_);
2294 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2295 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
2296 if (rv != ERR_IO_PENDING)
2297 return DoLoop(rv);
2298 return rv;
2301 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2302 X509Certificate* client_cert) {
2303 DCHECK(mode_ & WRITE || mode_ == NONE);
2304 DCHECK(network_trans_.get());
2305 DCHECK_EQ(STATE_NONE, next_state_);
2307 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2308 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
2309 if (rv != ERR_IO_PENDING)
2310 return DoLoop(rv);
2311 return rv;
2314 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2315 const AuthCredentials& credentials) {
2316 DCHECK(mode_ & WRITE || mode_ == NONE);
2317 DCHECK(network_trans_.get());
2318 DCHECK_EQ(STATE_NONE, next_state_);
2320 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2321 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
2322 if (rv != ERR_IO_PENDING)
2323 return DoLoop(rv);
2324 return rv;
2327 ValidationType HttpCache::Transaction::RequiresValidation() {
2328 // TODO(darin): need to do more work here:
2329 // - make sure we have a matching request method
2330 // - watch out for cached responses that depend on authentication
2332 if (response_.vary_data.is_valid() &&
2333 !response_.vary_data.MatchesRequest(*request_,
2334 *response_.headers.get())) {
2335 vary_mismatch_ = true;
2336 return VALIDATION_SYNCHRONOUS;
2339 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
2340 return VALIDATION_NONE;
2342 if (response_.unused_since_prefetch &&
2343 !(effective_load_flags_ & LOAD_PREFETCH) &&
2344 response_.headers->GetCurrentAge(
2345 response_.request_time, response_.response_time,
2346 cache_->clock_->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins)) {
2347 // The first use of a resource after prefetch within a short window skips
2348 // validation.
2349 return VALIDATION_NONE;
2352 if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION))
2353 return VALIDATION_SYNCHRONOUS;
2355 if (request_->method == "PUT" || request_->method == "DELETE")
2356 return VALIDATION_SYNCHRONOUS;
2358 ValidationType validation_required_by_headers =
2359 response_.headers->RequiresValidation(response_.request_time,
2360 response_.response_time,
2361 cache_->clock_->Now());
2363 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) {
2364 // Asynchronous revalidation is only supported for GET and HEAD methods.
2365 if (request_->method != "GET" && request_->method != "HEAD")
2366 return VALIDATION_SYNCHRONOUS;
2369 return validation_required_by_headers;
2372 bool HttpCache::Transaction::ConditionalizeRequest() {
2373 DCHECK(response_.headers.get());
2375 if (request_->method == "PUT" || request_->method == "DELETE")
2376 return false;
2378 // This only makes sense for cached 200 or 206 responses.
2379 if (response_.headers->response_code() != 200 &&
2380 response_.headers->response_code() != 206) {
2381 return false;
2384 if (fail_conditionalization_for_test_)
2385 return false;
2387 DCHECK(response_.headers->response_code() != 206 ||
2388 response_.headers->HasStrongValidators());
2390 // Just use the first available ETag and/or Last-Modified header value.
2391 // TODO(darin): Or should we use the last?
2393 std::string etag_value;
2394 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2395 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2397 std::string last_modified_value;
2398 if (!vary_mismatch_) {
2399 response_.headers->EnumerateHeader(NULL, "last-modified",
2400 &last_modified_value);
2403 if (etag_value.empty() && last_modified_value.empty())
2404 return false;
2406 if (!partial_.get()) {
2407 // Need to customize the request, so this forces us to allocate :(
2408 custom_request_.reset(new HttpRequestInfo(*request_));
2409 request_ = custom_request_.get();
2411 DCHECK(custom_request_.get());
2413 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2414 !invalid_range_;
2416 if (!use_if_range) {
2417 // stale-while-revalidate is not useful when we only have a partial response
2418 // cached, so don't set the header in that case.
2419 HttpResponseHeaders::FreshnessLifetimes lifetimes =
2420 response_.headers->GetFreshnessLifetimes(response_.response_time);
2421 if (lifetimes.staleness > TimeDelta()) {
2422 TimeDelta current_age = response_.headers->GetCurrentAge(
2423 response_.request_time, response_.response_time,
2424 cache_->clock_->Now());
2426 custom_request_->extra_headers.SetHeader(
2427 kFreshnessHeader,
2428 base::StringPrintf("max-age=%" PRId64
2429 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2430 lifetimes.freshness.InSeconds(),
2431 lifetimes.staleness.InSeconds(),
2432 current_age.InSeconds()));
2436 if (!etag_value.empty()) {
2437 if (use_if_range) {
2438 // We don't want to switch to WRITE mode if we don't have this block of a
2439 // byte-range request because we may have other parts cached.
2440 custom_request_->extra_headers.SetHeader(
2441 HttpRequestHeaders::kIfRange, etag_value);
2442 } else {
2443 custom_request_->extra_headers.SetHeader(
2444 HttpRequestHeaders::kIfNoneMatch, etag_value);
2446 // For byte-range requests, make sure that we use only one way to validate
2447 // the request.
2448 if (partial_.get() && !partial_->IsCurrentRangeCached())
2449 return true;
2452 if (!last_modified_value.empty()) {
2453 if (use_if_range) {
2454 custom_request_->extra_headers.SetHeader(
2455 HttpRequestHeaders::kIfRange, last_modified_value);
2456 } else {
2457 custom_request_->extra_headers.SetHeader(
2458 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2462 return true;
2465 // We just received some headers from the server. We may have asked for a range,
2466 // in which case partial_ has an object. This could be the first network request
2467 // we make to fulfill the original request, or we may be already reading (from
2468 // the net and / or the cache). If we are not expecting a certain response, we
2469 // just bypass the cache for this request (but again, maybe we are reading), and
2470 // delete partial_ (so we are not able to "fix" the headers that we return to
2471 // the user). This results in either a weird response for the caller (we don't
2472 // expect it after all), or maybe a range that was not exactly what it was asked
2473 // for.
2475 // If the server is simply telling us that the resource has changed, we delete
2476 // the cached entry and restart the request as the caller intended (by returning
2477 // false from this method). However, we may not be able to do that at any point,
2478 // for instance if we already returned the headers to the user.
2480 // WARNING: Whenever this code returns false, it has to make sure that the next
2481 // time it is called it will return true so that we don't keep retrying the
2482 // request.
2483 bool HttpCache::Transaction::ValidatePartialResponse() {
2484 const HttpResponseHeaders* headers = new_response_->headers.get();
2485 int response_code = headers->response_code();
2486 bool partial_response = (response_code == 206);
2487 handling_206_ = false;
2489 if (!entry_ || request_->method != "GET")
2490 return true;
2492 if (invalid_range_) {
2493 // We gave up trying to match this request with the stored data. If the
2494 // server is ok with the request, delete the entry, otherwise just ignore
2495 // this request
2496 DCHECK(!reading_);
2497 if (partial_response || response_code == 200) {
2498 DoomPartialEntry(true);
2499 mode_ = NONE;
2500 } else {
2501 if (response_code == 304)
2502 FailRangeRequest();
2503 IgnoreRangeRequest();
2505 return true;
2508 if (!partial_.get()) {
2509 // We are not expecting 206 but we may have one.
2510 if (partial_response)
2511 IgnoreRangeRequest();
2513 return true;
2516 // TODO(rvargas): Do we need to consider other results here?.
2517 bool failure = response_code == 200 || response_code == 416;
2519 if (partial_->IsCurrentRangeCached()) {
2520 // We asked for "If-None-Match: " so a 206 means a new object.
2521 if (partial_response)
2522 failure = true;
2524 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2525 return true;
2526 } else {
2527 // We asked for "If-Range: " so a 206 means just another range.
2528 if (partial_response) {
2529 if (partial_->ResponseHeadersOK(headers)) {
2530 handling_206_ = true;
2531 return true;
2532 } else {
2533 failure = true;
2537 if (!reading_ && !is_sparse_ && !partial_response) {
2538 // See if we can ignore the fact that we issued a byte range request.
2539 // If the server sends 200, just store it. If it sends an error, redirect
2540 // or something else, we may store the response as long as we didn't have
2541 // anything already stored.
2542 if (response_code == 200 ||
2543 (!truncated_ && response_code != 304 && response_code != 416)) {
2544 // The server is sending something else, and we can save it.
2545 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2546 partial_.reset();
2547 truncated_ = false;
2548 return true;
2552 // 304 is not expected here, but we'll spare the entry (unless it was
2553 // truncated).
2554 if (truncated_)
2555 failure = true;
2558 if (failure) {
2559 // We cannot truncate this entry, it has to be deleted.
2560 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2561 mode_ = NONE;
2562 if (is_sparse_ || truncated_) {
2563 // There was something cached to start with, either sparsed data (206), or
2564 // a truncated 200, which means that we probably modified the request,
2565 // adding a byte range or modifying the range requested by the caller.
2566 if (!reading_ && !partial_->IsLastRange()) {
2567 // We have not returned anything to the caller yet so it should be safe
2568 // to issue another network request, this time without us messing up the
2569 // headers.
2570 ResetPartialState(true);
2571 return false;
2573 LOG(WARNING) << "Failed to revalidate partial entry";
2575 DoomPartialEntry(true);
2576 return true;
2579 IgnoreRangeRequest();
2580 return true;
2583 void HttpCache::Transaction::IgnoreRangeRequest() {
2584 // We have a problem. We may or may not be reading already (in which case we
2585 // returned the headers), but we'll just pretend that this request is not
2586 // using the cache and see what happens. Most likely this is the first
2587 // response from the server (it's not changing its mind midway, right?).
2588 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2589 if (mode_ & WRITE)
2590 DoneWritingToEntry(mode_ != WRITE);
2591 else if (mode_ & READ && entry_)
2592 cache_->DoneReadingFromEntry(entry_, this);
2594 partial_.reset(NULL);
2595 entry_ = NULL;
2596 mode_ = NONE;
2599 void HttpCache::Transaction::FixHeadersForHead() {
2600 if (response_.headers->response_code() == 206) {
2601 response_.headers->RemoveHeader("Content-Range");
2602 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2606 void HttpCache::Transaction::TriggerAsyncValidation() {
2607 DCHECK(!request_->upload_data_stream);
2608 BoundNetLog async_revalidation_net_log(
2609 BoundNetLog::Make(net_log_.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION));
2610 net_log_.AddEvent(
2611 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC,
2612 async_revalidation_net_log.source().ToEventParametersCallback());
2613 async_revalidation_net_log.BeginEvent(
2614 NetLog::TYPE_ASYNC_REVALIDATION,
2615 base::Bind(
2616 &NetLogAsyncRevalidationInfoCallback, net_log_.source(), request_));
2617 base::MessageLoop::current()->PostTask(
2618 FROM_HERE,
2619 base::Bind(&HttpCache::PerformAsyncValidation,
2620 cache_, // cache_ is a weak pointer.
2621 *request_,
2622 async_revalidation_net_log));
2625 void HttpCache::Transaction::FailRangeRequest() {
2626 response_ = *new_response_;
2627 partial_->FixResponseHeaders(response_.headers.get(), false);
2630 int HttpCache::Transaction::SetupEntryForRead() {
2631 if (network_trans_)
2632 ResetNetworkTransaction();
2633 if (partial_.get()) {
2634 if (truncated_ || is_sparse_ || !invalid_range_) {
2635 // We are going to return the saved response headers to the caller, so
2636 // we may need to adjust them first.
2637 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2638 return OK;
2639 } else {
2640 partial_.reset();
2643 cache_->ConvertWriterToReader(entry_);
2644 mode_ = READ;
2646 if (request_->method == "HEAD")
2647 FixHeadersForHead();
2649 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2650 next_state_ = STATE_CACHE_READ_METADATA;
2651 return OK;
2655 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2656 read_buf_ = data;
2657 io_buf_len_ = data_len;
2658 next_state_ = STATE_NETWORK_READ;
2659 return DoLoop(OK);
2662 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2663 if (request_->method == "HEAD")
2664 return 0;
2666 read_buf_ = data;
2667 io_buf_len_ = data_len;
2668 next_state_ = STATE_CACHE_READ_DATA;
2669 return DoLoop(OK);
2672 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2673 IOBuffer* data, int data_len,
2674 const CompletionCallback& callback) {
2675 if (!entry_)
2676 return data_len;
2678 int rv = 0;
2679 if (!partial_.get() || !data_len) {
2680 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2681 true);
2682 } else {
2683 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2685 return rv;
2688 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2689 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2690 if (!entry_)
2691 return OK;
2693 // Do not cache no-store content. Do not cache content with cert errors
2694 // either. This is to prevent not reporting net errors when loading a
2695 // resource from the cache. When we load a page over HTTPS with a cert error
2696 // we show an SSL blocking page. If the user clicks proceed we reload the
2697 // resource ignoring the errors. The loaded resource is then cached. If that
2698 // resource is subsequently loaded from the cache, no net error is reported
2699 // (even though the cert status contains the actual errors) and no SSL
2700 // blocking page is shown. An alternative would be to reverse-map the cert
2701 // status to a net error and replay the net error.
2702 if ((response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2703 IsCertStatusError(response_.ssl_info.cert_status)) {
2704 DoneWritingToEntry(false);
2705 if (net_log_.IsCapturing())
2706 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2707 return OK;
2710 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2711 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2712 WriteCertChain();
2714 if (truncated)
2715 DCHECK_EQ(200, response_.headers->response_code());
2717 // When writing headers, we normally only write the non-transient headers.
2718 bool skip_transient_headers = true;
2719 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2720 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2721 data->Done();
2723 io_buf_len_ = data->pickle()->size();
2724 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2725 io_buf_len_, io_callback_, true);
2728 int HttpCache::Transaction::AppendResponseDataToEntry(
2729 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2730 if (!entry_ || !data_len)
2731 return data_len;
2733 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2734 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2735 callback);
2738 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2739 if (!entry_)
2740 return;
2742 RecordHistograms();
2744 cache_->DoneWritingToEntry(entry_, success);
2745 entry_ = NULL;
2746 mode_ = NONE; // switch to 'pass through' mode
2749 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2750 DLOG(ERROR) << "ReadData failed: " << result;
2751 const int result_for_histogram = std::max(0, -result);
2752 if (restart) {
2753 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2754 result_for_histogram);
2755 } else {
2756 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2757 result_for_histogram);
2760 // Avoid using this entry in the future.
2761 if (cache_.get())
2762 cache_->DoomActiveEntry(cache_key_);
2764 if (restart) {
2765 DCHECK(!reading_);
2766 DCHECK(!network_trans_.get());
2767 cache_->DoneWithEntry(entry_, this, false);
2768 entry_ = NULL;
2769 is_sparse_ = false;
2770 partial_.reset();
2771 next_state_ = STATE_GET_BACKEND;
2772 return OK;
2775 return ERR_CACHE_READ_FAILURE;
2778 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
2779 if (entry_lock_waiting_since_ != start_time)
2780 return;
2782 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
2784 if (!cache_)
2785 return;
2787 cache_->RemovePendingTransaction(this);
2788 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
2791 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2792 DVLOG(2) << "DoomPartialEntry";
2793 int rv = cache_->DoomEntry(cache_key_, NULL);
2794 DCHECK_EQ(OK, rv);
2795 cache_->DoneWithEntry(entry_, this, false);
2796 entry_ = NULL;
2797 is_sparse_ = false;
2798 truncated_ = false;
2799 if (delete_object)
2800 partial_.reset(NULL);
2803 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2804 partial_->OnNetworkReadCompleted(result);
2806 if (result == 0) {
2807 // We need to move on to the next range.
2808 ResetNetworkTransaction();
2809 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2811 return result;
2814 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2815 partial_->OnCacheReadCompleted(result);
2817 if (result == 0 && mode_ == READ_WRITE) {
2818 // We need to move on to the next range.
2819 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2820 } else if (result < 0) {
2821 return OnCacheReadError(result, false);
2823 return result;
2826 int HttpCache::Transaction::DoRestartPartialRequest() {
2827 // The stored data cannot be used. Get rid of it and restart this request.
2828 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST);
2830 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
2831 // to Doom the entry again).
2832 mode_ = WRITE;
2833 ResetPartialState(!range_requested_);
2834 next_state_ = STATE_CREATE_ENTRY;
2835 return OK;
2838 void HttpCache::Transaction::ResetPartialState(bool delete_object) {
2839 partial_->RestoreHeaders(&custom_request_->extra_headers);
2840 DoomPartialEntry(delete_object);
2842 if (!delete_object) {
2843 // The simplest way to re-initialize partial_ is to create a new object.
2844 partial_.reset(new PartialData());
2845 if (partial_->Init(request_->extra_headers))
2846 partial_->SetHeaders(custom_request_->extra_headers);
2847 else
2848 partial_.reset();
2852 void HttpCache::Transaction::ResetNetworkTransaction() {
2853 DCHECK(!old_network_trans_load_timing_);
2854 DCHECK(network_trans_);
2855 LoadTimingInfo load_timing;
2856 if (network_trans_->GetLoadTimingInfo(&load_timing))
2857 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2858 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2859 ConnectionAttempts attempts;
2860 network_trans_->GetConnectionAttempts(&attempts);
2861 for (const auto& attempt : attempts)
2862 old_connection_attempts_.push_back(attempt);
2863 network_trans_.reset();
2866 // Histogram data from the end of 2010 show the following distribution of
2867 // response headers:
2869 // Content-Length............... 87%
2870 // Date......................... 98%
2871 // Last-Modified................ 49%
2872 // Etag......................... 19%
2873 // Accept-Ranges: bytes......... 25%
2874 // Accept-Ranges: none.......... 0.4%
2875 // Strong Validator............. 50%
2876 // Strong Validator + ranges.... 24%
2877 // Strong Validator + CL........ 49%
2879 bool HttpCache::Transaction::CanResume(bool has_data) {
2880 // Double check that there is something worth keeping.
2881 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2882 return false;
2884 if (request_->method != "GET")
2885 return false;
2887 // Note that if this is a 206, content-length was already fixed after calling
2888 // PartialData::ResponseHeadersOK().
2889 if (response_.headers->GetContentLength() <= 0 ||
2890 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2891 !response_.headers->HasStrongValidators()) {
2892 return false;
2895 return true;
2898 void HttpCache::Transaction::UpdateTransactionPattern(
2899 TransactionPattern new_transaction_pattern) {
2900 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2901 return;
2902 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2903 new_transaction_pattern == PATTERN_NOT_COVERED);
2904 transaction_pattern_ = new_transaction_pattern;
2907 void HttpCache::Transaction::RecordHistograms() {
2908 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2909 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2910 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2911 cache_->mode() != NORMAL || request_->method != "GET") {
2912 return;
2914 UMA_HISTOGRAM_ENUMERATION(
2915 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2916 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2917 return;
2918 DCHECK(!range_requested_);
2919 DCHECK(!first_cache_access_since_.is_null());
2921 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2923 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2925 bool did_send_request = !send_request_since_.is_null();
2926 DCHECK(
2927 (did_send_request &&
2928 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2929 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2930 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2931 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2932 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2934 if (!did_send_request) {
2935 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2936 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2937 return;
2940 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2941 int64 before_send_percent = (total_time.ToInternalValue() == 0) ?
2942 0 : before_send_time * 100 / total_time;
2943 DCHECK_GE(before_send_percent, 0);
2944 DCHECK_LE(before_send_percent, 100);
2945 base::HistogramBase::Sample before_send_sample =
2946 static_cast<base::HistogramBase::Sample>(before_send_percent);
2948 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2949 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2950 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample);
2952 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2953 // below this comment after we have received initial data.
2954 switch (transaction_pattern_) {
2955 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2956 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2957 before_send_time);
2958 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2959 before_send_sample);
2960 break;
2962 case PATTERN_ENTRY_NOT_CACHED: {
2963 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2964 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2965 before_send_sample);
2966 break;
2968 case PATTERN_ENTRY_VALIDATED: {
2969 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2970 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2971 before_send_sample);
2972 break;
2974 case PATTERN_ENTRY_UPDATED: {
2975 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2976 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2977 before_send_sample);
2978 break;
2980 default:
2981 NOTREACHED();
2985 void HttpCache::Transaction::OnIOComplete(int result) {
2986 DoLoop(result);
2989 } // namespace net