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" // For OS_POSIX
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/format_macros.h"
19 #include "base/location.h"
20 #include "base/metrics/histogram_macros.h"
21 #include "base/metrics/sparse_histogram.h"
22 #include "base/profiler/scoped_tracker.h"
23 #include "base/single_thread_task_runner.h"
24 #include "base/strings/string_number_conversions.h" // For HexEncode.
25 #include "base/strings/string_piece.h"
26 #include "base/strings/string_util.h" // For LowerCaseEqualsASCII.
27 #include "base/strings/stringprintf.h"
28 #include "base/thread_task_runner_handle.h"
29 #include "base/time/clock.h"
30 #include "base/values.h"
31 #include "net/base/auth.h"
32 #include "net/base/load_flags.h"
33 #include "net/base/load_timing_info.h"
34 #include "net/base/net_errors.h"
35 #include "net/base/upload_data_stream.h"
36 #include "net/cert/cert_status_flags.h"
37 #include "net/cert/x509_certificate.h"
38 #include "net/disk_cache/disk_cache.h"
39 #include "net/http/disk_based_cert_cache.h"
40 #include "net/http/http_network_session.h"
41 #include "net/http/http_request_info.h"
42 #include "net/http/http_util.h"
43 #include "net/ssl/ssl_cert_request_info.h"
44 #include "net/ssl/ssl_config_service.h"
47 using base::TimeDelta
;
48 using base::TimeTicks
;
54 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
55 static const char kFreshnessHeader
[] = "Resource-Freshness";
57 // Stores data relevant to the statistics of writing and reading entire
58 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
59 // of certificates in the chain that have pending operations in the
60 // DiskBasedCertCache. |start_time| is the time that the read and write
61 // commands began being issued to the DiskBasedCertCache.
62 // TODO(brandonsalmon): Remove this when it is no longer necessary to
64 class SharedChainData
: public base::RefCounted
<SharedChainData
> {
66 SharedChainData(int num_ops
, TimeTicks start
)
67 : num_pending_ops(num_ops
), start_time(start
) {}
73 friend class base::RefCounted
<SharedChainData
>;
75 DISALLOW_COPY_AND_ASSIGN(SharedChainData
);
78 // Used to obtain a cache entry key for an OSCertHandle.
79 // TODO(brandonsalmon): Remove this when cache keys are stored
80 // and no longer have to be recomputed to retrieve the OSCertHandle
82 std::string
GetCacheKeyForCert(X509Certificate::OSCertHandle cert_handle
) {
83 SHA1HashValue fingerprint
=
84 X509Certificate::CalculateFingerprint(cert_handle
);
87 base::HexEncode(fingerprint
.data
, arraysize(fingerprint
.data
));
90 // |dist_from_root| indicates the position of the read certificate in the
91 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
92 // whether or not the read certificate was the leaf of the chain.
93 // |shared_chain_data| contains data shared by each certificate in
95 void OnCertReadIOComplete(
98 const scoped_refptr
<SharedChainData
>& shared_chain_data
,
99 X509Certificate::OSCertHandle cert_handle
) {
100 // If |num_pending_ops| is one, this was the last pending read operation
101 // for this chain of certificates. The total time used to read the chain
102 // can be calculated by subtracting the starting time from Now().
103 shared_chain_data
->num_pending_ops
--;
104 if (!shared_chain_data
->num_pending_ops
) {
105 const TimeDelta read_chain_wait
=
106 TimeTicks::Now() - shared_chain_data
->start_time
;
107 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
109 base::TimeDelta::FromMilliseconds(1),
110 base::TimeDelta::FromMinutes(10),
114 bool success
= (cert_handle
!= NULL
);
116 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success
);
119 UMA_HISTOGRAM_CUSTOM_COUNTS(
120 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root
, 0, 10, 7);
122 UMA_HISTOGRAM_CUSTOM_COUNTS(
123 "DiskBasedCertCache.CertIoReadFailure", dist_from_root
, 0, 10, 7);
126 // |dist_from_root| indicates the position of the written certificate in the
127 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
128 // whether or not the written certificate was the leaf of the chain.
129 // |shared_chain_data| contains data shared by each certificate in
131 void OnCertWriteIOComplete(
134 const scoped_refptr
<SharedChainData
>& shared_chain_data
,
135 const std::string
& key
) {
136 // If |num_pending_ops| is one, this was the last pending write operation
137 // for this chain of certificates. The total time used to write the chain
138 // can be calculated by subtracting the starting time from Now().
139 shared_chain_data
->num_pending_ops
--;
140 if (!shared_chain_data
->num_pending_ops
) {
141 const TimeDelta write_chain_wait
=
142 TimeTicks::Now() - shared_chain_data
->start_time
;
143 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
145 base::TimeDelta::FromMilliseconds(1),
146 base::TimeDelta::FromMinutes(10),
150 bool success
= !key
.empty();
152 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success
);
155 UMA_HISTOGRAM_CUSTOM_COUNTS(
156 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root
, 0, 10, 7);
158 UMA_HISTOGRAM_CUSTOM_COUNTS(
159 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root
, 0, 10, 7);
162 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
163 // a "non-error response" is one with a 2xx (Successful) or 3xx
164 // (Redirection) status code.
165 bool NonErrorResponse(int status_code
) {
166 int status_code_range
= status_code
/ 100;
167 return status_code_range
== 2 || status_code_range
== 3;
170 void RecordNoStoreHeaderHistogram(int load_flags
,
171 const HttpResponseInfo
* response
) {
172 if (load_flags
& LOAD_MAIN_FRAME
) {
173 UMA_HISTOGRAM_BOOLEAN(
174 "Net.MainFrameNoStore",
175 response
->headers
->HasHeaderValue("cache-control", "no-store"));
179 enum ExternallyConditionalizedType
{
180 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION
,
181 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE
,
182 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS
,
183 EXTERNALLY_CONDITIONALIZED_MAX
188 struct HeaderNameAndValue
{
193 // If the request includes one of these request headers, then avoid caching
194 // to avoid getting confused.
195 static const HeaderNameAndValue kPassThroughHeaders
[] = {
196 { "if-unmodified-since", NULL
}, // causes unexpected 412s
197 { "if-match", NULL
}, // causes unexpected 412s
198 { "if-range", NULL
},
202 struct ValidationHeaderInfo
{
203 const char* request_header_name
;
204 const char* related_response_header_name
;
207 static const ValidationHeaderInfo kValidationHeaders
[] = {
208 { "if-modified-since", "last-modified" },
209 { "if-none-match", "etag" },
212 // If the request includes one of these request headers, then avoid reusing
213 // our cached copy if any.
214 static const HeaderNameAndValue kForceFetchHeaders
[] = {
215 { "cache-control", "no-cache" },
216 { "pragma", "no-cache" },
220 // If the request includes one of these request headers, then force our
221 // cached copy (if any) to be revalidated before reusing it.
222 static const HeaderNameAndValue kForceValidateHeaders
[] = {
223 { "cache-control", "max-age=0" },
227 static bool HeaderMatches(const HttpRequestHeaders
& headers
,
228 const HeaderNameAndValue
* search
) {
229 for (; search
->name
; ++search
) {
230 std::string header_value
;
231 if (!headers
.GetHeader(search
->name
, &header_value
))
237 HttpUtil::ValuesIterator
v(header_value
.begin(), header_value
.end(), ',');
238 while (v
.GetNext()) {
239 if (base::LowerCaseEqualsASCII(v
.value_begin(), v
.value_end(),
247 //-----------------------------------------------------------------------------
249 HttpCache::Transaction::Transaction(RequestPriority priority
, HttpCache
* cache
)
250 : next_state_(STATE_NONE
),
253 cache_(cache
->GetWeakPtr()),
258 target_state_(STATE_NONE
),
260 invalid_range_(false),
263 range_requested_(false),
264 handling_206_(false),
265 cache_pending_(false),
266 done_reading_(false),
267 vary_mismatch_(false),
268 couldnt_conditionalize_request_(false),
269 bypass_lock_for_test_(false),
270 fail_conditionalization_for_test_(false),
273 effective_load_flags_(0),
275 transaction_pattern_(PATTERN_UNDEFINED
),
276 total_received_bytes_(0),
277 websocket_handshake_stream_base_create_helper_(NULL
),
278 weak_factory_(this) {
279 static_assert(HttpCache::Transaction::kNumValidationHeaders
==
280 arraysize(kValidationHeaders
),
281 "invalid number of validation headers");
283 io_callback_
= base::Bind(&Transaction::OnIOComplete
,
284 weak_factory_
.GetWeakPtr());
287 HttpCache::Transaction::~Transaction() {
288 // We may have to issue another IO, but we should never invoke the callback_
294 bool cancel_request
= reading_
&& response_
.headers
.get();
295 if (cancel_request
) {
297 entry_
->disk_entry
->CancelSparseIO();
299 cancel_request
&= (response_
.headers
->response_code() == 200);
303 cache_
->DoneWithEntry(entry_
, this, cancel_request
);
304 } else if (cache_pending_
) {
305 cache_
->RemovePendingTransaction(this);
310 int HttpCache::Transaction::WriteMetadata(IOBuffer
* buf
, int buf_len
,
311 const CompletionCallback
& callback
) {
313 DCHECK_GT(buf_len
, 0);
314 DCHECK(!callback
.is_null());
315 if (!cache_
.get() || !entry_
)
316 return ERR_UNEXPECTED
;
318 // We don't need to track this operation for anything.
319 // It could be possible to check if there is something already written and
320 // avoid writing again (it should be the same, right?), but let's allow the
321 // caller to "update" the contents with something new.
322 return entry_
->disk_entry
->WriteData(kMetadataIndex
, 0, buf
, buf_len
,
326 bool HttpCache::Transaction::AddTruncatedFlag() {
327 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
329 // Don't set the flag for sparse entries.
330 if (partial_
.get() && !truncated_
)
333 if (!CanResume(true))
336 // We may have received the whole resource already.
341 target_state_
= STATE_NONE
;
342 next_state_
= STATE_CACHE_WRITE_TRUNCATED_RESPONSE
;
347 LoadState
HttpCache::Transaction::GetWriterLoadState() const {
348 if (network_trans_
.get())
349 return network_trans_
->GetLoadState();
350 if (entry_
|| !request_
)
351 return LOAD_STATE_IDLE
;
352 return LOAD_STATE_WAITING_FOR_CACHE
;
355 const BoundNetLog
& HttpCache::Transaction::net_log() const {
359 int HttpCache::Transaction::Start(const HttpRequestInfo
* request
,
360 const CompletionCallback
& callback
,
361 const BoundNetLog
& net_log
) {
363 DCHECK(!callback
.is_null());
365 // Ensure that we only have one asynchronous call at a time.
366 DCHECK(callback_
.is_null());
368 DCHECK(!network_trans_
.get());
372 return ERR_UNEXPECTED
;
374 SetRequest(net_log
, request
);
376 // We have to wait until the backend is initialized so we start the SM.
377 next_state_
= STATE_GET_BACKEND
;
380 // Setting this here allows us to check for the existence of a callback_ to
381 // determine if we are still inside Start.
382 if (rv
== ERR_IO_PENDING
)
383 callback_
= callback
;
388 int HttpCache::Transaction::RestartIgnoringLastError(
389 const CompletionCallback
& callback
) {
390 DCHECK(!callback
.is_null());
392 // Ensure that we only have one asynchronous call at a time.
393 DCHECK(callback_
.is_null());
396 return ERR_UNEXPECTED
;
398 int rv
= RestartNetworkRequest();
400 if (rv
== ERR_IO_PENDING
)
401 callback_
= callback
;
406 int HttpCache::Transaction::RestartWithCertificate(
407 X509Certificate
* client_cert
,
408 const CompletionCallback
& callback
) {
409 DCHECK(!callback
.is_null());
411 // Ensure that we only have one asynchronous call at a time.
412 DCHECK(callback_
.is_null());
415 return ERR_UNEXPECTED
;
417 int rv
= RestartNetworkRequestWithCertificate(client_cert
);
419 if (rv
== ERR_IO_PENDING
)
420 callback_
= callback
;
425 int HttpCache::Transaction::RestartWithAuth(
426 const AuthCredentials
& credentials
,
427 const CompletionCallback
& callback
) {
428 DCHECK(auth_response_
.headers
.get());
429 DCHECK(!callback
.is_null());
431 // Ensure that we only have one asynchronous call at a time.
432 DCHECK(callback_
.is_null());
435 return ERR_UNEXPECTED
;
437 // Clear the intermediate response since we are going to start over.
438 auth_response_
= HttpResponseInfo();
440 int rv
= RestartNetworkRequestWithAuth(credentials
);
442 if (rv
== ERR_IO_PENDING
)
443 callback_
= callback
;
448 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
449 if (!network_trans_
.get())
451 return network_trans_
->IsReadyToRestartForAuth();
454 int HttpCache::Transaction::Read(IOBuffer
* buf
, int buf_len
,
455 const CompletionCallback
& callback
) {
457 DCHECK_GT(buf_len
, 0);
458 DCHECK(!callback
.is_null());
460 DCHECK(callback_
.is_null());
463 return ERR_UNEXPECTED
;
465 // If we have an intermediate auth response at this point, then it means the
466 // user wishes to read the network response (the error page). If there is a
467 // previous response in the cache then we should leave it intact.
468 if (auth_response_
.headers
.get() && mode_
!= NONE
) {
469 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
470 DCHECK(mode_
& WRITE
);
471 DoneWritingToEntry(mode_
== READ_WRITE
);
480 DCHECK(partial_
.get());
481 if (!network_trans_
.get()) {
482 // We are just reading from the cache, but we may be writing later.
483 rv
= ReadFromEntry(buf
, buf_len
);
488 DCHECK(network_trans_
.get());
489 rv
= ReadFromNetwork(buf
, buf_len
);
492 rv
= ReadFromEntry(buf
, buf_len
);
499 if (rv
== ERR_IO_PENDING
) {
500 DCHECK(callback_
.is_null());
501 callback_
= callback
;
506 void HttpCache::Transaction::StopCaching() {
507 // We really don't know where we are now. Hopefully there is no operation in
508 // progress, but nothing really prevents this method to be called after we
509 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
510 // point because we need the state machine for that (and even if we are really
511 // free, that would be an asynchronous operation). In other words, keep the
512 // entry how it is (it will be marked as truncated at destruction), and let
513 // the next piece of code that executes know that we are now reading directly
515 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
516 // future request for the resource will be blocked on this one.
518 if (cache_
.get() && entry_
&& (mode_
& WRITE
) && network_trans_
.get() &&
519 !is_sparse_
&& !range_requested_
) {
524 bool HttpCache::Transaction::GetFullRequestHeaders(
525 HttpRequestHeaders
* headers
) const {
527 return network_trans_
->GetFullRequestHeaders(headers
);
529 // TODO(ttuttle): Read headers from cache.
533 int64
HttpCache::Transaction::GetTotalReceivedBytes() const {
534 int64 total_received_bytes
= total_received_bytes_
;
536 total_received_bytes
+= network_trans_
->GetTotalReceivedBytes();
537 return total_received_bytes
;
540 void HttpCache::Transaction::DoneReading() {
541 if (cache_
.get() && entry_
) {
542 DCHECK_NE(mode_
, UPDATE
);
544 DoneWritingToEntry(true);
545 } else if (mode_
& READ
) {
546 // It is necessary to check mode_ & READ because it is possible
547 // for mode_ to be NONE and entry_ non-NULL with a write entry
548 // if StopCaching was called.
549 cache_
->DoneReadingFromEntry(entry_
, this);
555 const HttpResponseInfo
* HttpCache::Transaction::GetResponseInfo() const {
556 // Null headers means we encountered an error or haven't a response yet
557 if (auth_response_
.headers
.get())
558 return &auth_response_
;
562 LoadState
HttpCache::Transaction::GetLoadState() const {
563 LoadState state
= GetWriterLoadState();
564 if (state
!= LOAD_STATE_WAITING_FOR_CACHE
)
568 return cache_
->GetLoadStateForPendingTransaction(this);
570 return LOAD_STATE_IDLE
;
573 UploadProgress
HttpCache::Transaction::GetUploadProgress() const {
574 if (network_trans_
.get())
575 return network_trans_
->GetUploadProgress();
576 return final_upload_progress_
;
579 void HttpCache::Transaction::SetQuicServerInfo(
580 QuicServerInfo
* quic_server_info
) {}
582 bool HttpCache::Transaction::GetLoadTimingInfo(
583 LoadTimingInfo
* load_timing_info
) const {
585 return network_trans_
->GetLoadTimingInfo(load_timing_info
);
587 if (old_network_trans_load_timing_
) {
588 *load_timing_info
= *old_network_trans_load_timing_
;
592 if (first_cache_access_since_
.is_null())
595 // If the cache entry was opened, return that time.
596 load_timing_info
->send_start
= first_cache_access_since_
;
597 // This time doesn't make much sense when reading from the cache, so just use
598 // the same time as send_start.
599 load_timing_info
->send_end
= first_cache_access_since_
;
603 void HttpCache::Transaction::SetPriority(RequestPriority priority
) {
604 priority_
= priority
;
606 network_trans_
->SetPriority(priority_
);
609 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
610 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
611 websocket_handshake_stream_base_create_helper_
= create_helper
;
613 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(create_helper
);
616 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
617 const BeforeNetworkStartCallback
& callback
) {
618 DCHECK(!network_trans_
);
619 before_network_start_callback_
= callback
;
622 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
623 const BeforeProxyHeadersSentCallback
& callback
) {
624 DCHECK(!network_trans_
);
625 before_proxy_headers_sent_callback_
= callback
;
628 int HttpCache::Transaction::ResumeNetworkStart() {
630 return network_trans_
->ResumeNetworkStart();
631 return ERR_UNEXPECTED
;
634 void HttpCache::Transaction::GetConnectionAttempts(
635 ConnectionAttempts
* out
) const {
636 ConnectionAttempts new_connection_attempts
;
638 network_trans_
->GetConnectionAttempts(&new_connection_attempts
);
640 out
->swap(new_connection_attempts
);
641 out
->insert(out
->begin(), old_connection_attempts_
.begin(),
642 old_connection_attempts_
.end());
645 //-----------------------------------------------------------------------------
647 void HttpCache::Transaction::DoCallback(int rv
) {
648 DCHECK(rv
!= ERR_IO_PENDING
);
649 DCHECK(!callback_
.is_null());
651 read_buf_
= NULL
; // Release the buffer before invoking the callback.
653 // Since Run may result in Read being called, clear callback_ up front.
654 CompletionCallback c
= callback_
;
659 int HttpCache::Transaction::HandleResult(int rv
) {
660 DCHECK(rv
!= ERR_IO_PENDING
);
661 if (!callback_
.is_null())
667 // A few common patterns: (Foo* means Foo -> FooComplete)
669 // 1. Not-cached entry:
671 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
672 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
673 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
674 // PartialHeadersReceived
677 // NetworkRead* -> CacheWriteData*
679 // 2. Cached entry, no validation:
681 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
682 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
683 // BeginCacheValidation() -> SetupEntryForRead()
688 // 3. Cached entry, validation (304):
690 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
691 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
692 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
693 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
694 // -> OverwriteCachedResponse -> PartialHeadersReceived
699 // 4. Cached entry, validation and replace (200):
701 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
702 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
703 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
704 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
705 // TruncateCachedMetadata* -> PartialHeadersReceived
708 // NetworkRead* -> CacheWriteData*
710 // 5. Sparse entry, partially cached, byte range request:
712 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
713 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
714 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
715 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
716 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
717 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
718 // -> OverwriteCachedResponse -> PartialHeadersReceived
721 // NetworkRead* -> CacheWriteData*
724 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
725 // CompletePartialCacheValidation -> CacheReadData* ->
728 // CacheReadData* -> StartPartialCacheValidation ->
729 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
730 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
731 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
733 // 6. HEAD. Not-cached entry:
734 // Pass through. Don't save a HEAD by itself.
736 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
738 // 7. HEAD. Cached entry, no validation:
740 // The same flow as for a GET request (example #2)
743 // CacheReadData (returns 0)
745 // 8. HEAD. Cached entry, validation (304):
746 // The request updates the stored headers.
747 // Start(): Same as for a GET request (example #3)
750 // CacheReadData (returns 0)
752 // 9. HEAD. Cached entry, validation and replace (200):
753 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
756 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
757 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
758 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
759 // OverwriteCachedResponse
761 // 10. HEAD. Sparse entry, partially cached:
762 // Serve the request from the cache, as long as it doesn't require
763 // revalidation. Ignore missing ranges when deciding to revalidate. If the
764 // entry requires revalidation, ignore the whole request and go to full pass
765 // through (the result of the HEAD request will NOT update the entry).
767 // Start(): Basically the same as example 7, as we never create a partial_
768 // object for this request.
770 // 11. Prefetch, not-cached entry:
771 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
772 // UpdateCachedResponse.
774 // 12. Prefetch, cached entry:
775 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
776 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
779 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
780 // Skip validation, similar to example 2.
781 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
782 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
783 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
784 // SetupEntryForRead()
789 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
790 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
791 // CacheReadResponse* and CacheDispatchValidation.
792 int HttpCache::Transaction::DoLoop(int result
) {
793 DCHECK(next_state_
!= STATE_NONE
);
797 State state
= next_state_
;
798 next_state_
= STATE_NONE
;
800 case STATE_GET_BACKEND
:
804 case STATE_GET_BACKEND_COMPLETE
:
805 rv
= DoGetBackendComplete(rv
);
807 case STATE_SEND_REQUEST
:
809 rv
= DoSendRequest();
811 case STATE_SEND_REQUEST_COMPLETE
:
812 rv
= DoSendRequestComplete(rv
);
814 case STATE_SUCCESSFUL_SEND_REQUEST
:
816 rv
= DoSuccessfulSendRequest();
818 case STATE_NETWORK_READ
:
820 rv
= DoNetworkRead();
822 case STATE_NETWORK_READ_COMPLETE
:
823 rv
= DoNetworkReadComplete(rv
);
825 case STATE_INIT_ENTRY
:
829 case STATE_OPEN_ENTRY
:
833 case STATE_OPEN_ENTRY_COMPLETE
:
834 rv
= DoOpenEntryComplete(rv
);
836 case STATE_CREATE_ENTRY
:
838 rv
= DoCreateEntry();
840 case STATE_CREATE_ENTRY_COMPLETE
:
841 rv
= DoCreateEntryComplete(rv
);
843 case STATE_DOOM_ENTRY
:
847 case STATE_DOOM_ENTRY_COMPLETE
:
848 rv
= DoDoomEntryComplete(rv
);
850 case STATE_ADD_TO_ENTRY
:
854 case STATE_ADD_TO_ENTRY_COMPLETE
:
855 rv
= DoAddToEntryComplete(rv
);
857 case STATE_START_PARTIAL_CACHE_VALIDATION
:
859 rv
= DoStartPartialCacheValidation();
861 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
:
862 rv
= DoCompletePartialCacheValidation(rv
);
864 case STATE_UPDATE_CACHED_RESPONSE
:
866 rv
= DoUpdateCachedResponse();
868 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE
:
869 rv
= DoUpdateCachedResponseComplete(rv
);
871 case STATE_OVERWRITE_CACHED_RESPONSE
:
873 rv
= DoOverwriteCachedResponse();
875 case STATE_TRUNCATE_CACHED_DATA
:
877 rv
= DoTruncateCachedData();
879 case STATE_TRUNCATE_CACHED_DATA_COMPLETE
:
880 rv
= DoTruncateCachedDataComplete(rv
);
882 case STATE_TRUNCATE_CACHED_METADATA
:
884 rv
= DoTruncateCachedMetadata();
886 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE
:
887 rv
= DoTruncateCachedMetadataComplete(rv
);
889 case STATE_PARTIAL_HEADERS_RECEIVED
:
891 rv
= DoPartialHeadersReceived();
893 case STATE_CACHE_READ_RESPONSE
:
895 rv
= DoCacheReadResponse();
897 case STATE_CACHE_READ_RESPONSE_COMPLETE
:
898 rv
= DoCacheReadResponseComplete(rv
);
900 case STATE_CACHE_DISPATCH_VALIDATION
:
902 rv
= DoCacheDispatchValidation();
904 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH
:
906 rv
= DoCacheToggleUnusedSincePrefetch();
908 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE
:
909 rv
= DoCacheToggleUnusedSincePrefetchComplete(rv
);
911 case STATE_CACHE_WRITE_RESPONSE
:
913 rv
= DoCacheWriteResponse();
915 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
917 rv
= DoCacheWriteTruncatedResponse();
919 case STATE_CACHE_WRITE_RESPONSE_COMPLETE
:
920 rv
= DoCacheWriteResponseComplete(rv
);
922 case STATE_CACHE_READ_METADATA
:
924 rv
= DoCacheReadMetadata();
926 case STATE_CACHE_READ_METADATA_COMPLETE
:
927 rv
= DoCacheReadMetadataComplete(rv
);
929 case STATE_CACHE_QUERY_DATA
:
931 rv
= DoCacheQueryData();
933 case STATE_CACHE_QUERY_DATA_COMPLETE
:
934 rv
= DoCacheQueryDataComplete(rv
);
936 case STATE_CACHE_READ_DATA
:
938 rv
= DoCacheReadData();
940 case STATE_CACHE_READ_DATA_COMPLETE
:
941 rv
= DoCacheReadDataComplete(rv
);
943 case STATE_CACHE_WRITE_DATA
:
944 rv
= DoCacheWriteData(rv
);
946 case STATE_CACHE_WRITE_DATA_COMPLETE
:
947 rv
= DoCacheWriteDataComplete(rv
);
950 NOTREACHED() << "bad state";
954 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
956 if (rv
!= ERR_IO_PENDING
)
962 int HttpCache::Transaction::DoGetBackend() {
963 cache_pending_
= true;
964 next_state_
= STATE_GET_BACKEND_COMPLETE
;
965 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
);
966 return cache_
->GetBackendForTransaction(this);
969 int HttpCache::Transaction::DoGetBackendComplete(int result
) {
970 DCHECK(result
== OK
|| result
== ERR_FAILED
);
971 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
,
973 cache_pending_
= false;
975 if (!ShouldPassThrough()) {
976 cache_key_
= cache_
->GenerateCacheKey(request_
);
978 // Requested cache access mode.
979 if (effective_load_flags_
& LOAD_ONLY_FROM_CACHE
) {
981 } else if (effective_load_flags_
& LOAD_BYPASS_CACHE
) {
987 // Downgrade to UPDATE if the request has been externally conditionalized.
988 if (external_validation_
.initialized
) {
990 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
991 // in case READ was off).
999 // Use PUT and DELETE only to invalidate existing stored entries.
1000 if ((request_
->method
== "PUT" || request_
->method
== "DELETE") &&
1001 mode_
!= READ_WRITE
&& mode_
!= WRITE
) {
1005 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1006 // transaction behaves the same for GET and HEAD requests at this point: if it
1007 // was not modified, the entry is updated and a response is not returned from
1008 // the cache. If we receive 200, it doesn't matter if there was a validation
1010 if (request_
->method
== "HEAD" && mode_
== WRITE
)
1013 // If must use cache, then we must fail. This can happen for back/forward
1014 // navigations to a page generated via a form post.
1015 if (!(mode_
& READ
) && effective_load_flags_
& LOAD_ONLY_FROM_CACHE
)
1016 return ERR_CACHE_MISS
;
1018 if (mode_
== NONE
) {
1019 if (partial_
.get()) {
1020 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1023 next_state_
= STATE_SEND_REQUEST
;
1025 next_state_
= STATE_INIT_ENTRY
;
1028 // This is only set if we have something to do with the response.
1029 range_requested_
= (partial_
.get() != NULL
);
1034 int HttpCache::Transaction::DoSendRequest() {
1035 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1036 DCHECK(!network_trans_
.get());
1038 send_request_since_
= TimeTicks::Now();
1040 // Create a network transaction.
1041 int rv
= cache_
->network_layer_
->CreateTransaction(priority_
,
1045 network_trans_
->SetBeforeNetworkStartCallback(before_network_start_callback_
);
1046 network_trans_
->SetBeforeProxyHeadersSentCallback(
1047 before_proxy_headers_sent_callback_
);
1049 // Old load timing information, if any, is now obsolete.
1050 old_network_trans_load_timing_
.reset();
1052 if (websocket_handshake_stream_base_create_helper_
)
1053 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(
1054 websocket_handshake_stream_base_create_helper_
);
1056 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1057 rv
= network_trans_
->Start(request_
, io_callback_
, net_log_
);
1061 int HttpCache::Transaction::DoSendRequestComplete(int result
) {
1063 return ERR_UNEXPECTED
;
1065 // If we tried to conditionalize the request and failed, we know
1066 // we won't be reading from the cache after this point.
1067 if (couldnt_conditionalize_request_
)
1071 next_state_
= STATE_SUCCESSFUL_SEND_REQUEST
;
1075 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
1076 response_
.network_accessed
= response
->network_accessed
;
1078 // Do not record requests that have network errors or restarts.
1079 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1080 if (IsCertificateError(result
)) {
1081 // If we get a certificate error, then there is a certificate in ssl_info,
1082 // so GetResponseInfo() should never return NULL here.
1084 response_
.ssl_info
= response
->ssl_info
;
1085 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
1087 response_
.cert_request_info
= response
->cert_request_info
;
1088 } else if (response_
.was_cached
) {
1089 DoneWritingToEntry(true);
1095 // We received the response headers and there is no error.
1096 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1097 DCHECK(!new_response_
);
1098 const HttpResponseInfo
* new_response
= network_trans_
->GetResponseInfo();
1100 if (new_response
->headers
->response_code() == 401 ||
1101 new_response
->headers
->response_code() == 407) {
1102 auth_response_
= *new_response
;
1106 // We initiated a second request the caller doesn't know about. We should be
1107 // able to authenticate this request because we should have authenticated
1108 // this URL moments ago.
1109 if (IsReadyToRestartForAuth()) {
1110 DCHECK(!response_
.auth_challenge
.get());
1111 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1112 // In theory we should check to see if there are new cookies, but there
1113 // is no way to do that from here.
1114 return network_trans_
->RestartWithAuth(AuthCredentials(), io_callback_
);
1117 // We have to perform cleanup at this point so that at least the next
1118 // request can succeed. We do not retry at this point, because data
1119 // has been read and we have no way to gather credentials. We would
1120 // fail again, and potentially loop. This can happen if the credentials
1121 // expire while chrome is suspended.
1123 DoomPartialEntry(false);
1126 ResetNetworkTransaction();
1127 return ERR_CACHE_AUTH_FAILURE_AFTER_READ
;
1130 new_response_
= new_response
;
1131 if (!ValidatePartialResponse() && !auth_response_
.headers
.get()) {
1132 // Something went wrong with this request and we have to restart it.
1133 // If we have an authentication response, we are exposed to weird things
1134 // hapenning if the user cancels the authentication before we receive
1135 // the new response.
1136 net_log_
.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST
);
1137 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1138 response_
= HttpResponseInfo();
1139 ResetNetworkTransaction();
1140 new_response_
= NULL
;
1141 next_state_
= STATE_SEND_REQUEST
;
1145 if (handling_206_
&& mode_
== READ_WRITE
&& !truncated_
&& !is_sparse_
) {
1146 // We have stored the full entry, but it changed and the server is
1147 // sending a range. We have to delete the old entry.
1148 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1149 DoneWritingToEntry(false);
1152 if (mode_
== WRITE
&&
1153 transaction_pattern_
!= PATTERN_ENTRY_CANT_CONDITIONALIZE
) {
1154 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED
);
1157 // Invalidate any cached GET with a successful PUT or DELETE.
1158 if (mode_
== WRITE
&&
1159 (request_
->method
== "PUT" || request_
->method
== "DELETE")) {
1160 if (NonErrorResponse(new_response
->headers
->response_code())) {
1161 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1164 cache_
->DoneWritingToEntry(entry_
, true);
1169 // Invalidate any cached GET with a successful POST.
1170 if (!(effective_load_flags_
& LOAD_DISABLE_CACHE
) &&
1171 request_
->method
== "POST" &&
1172 NonErrorResponse(new_response
->headers
->response_code())) {
1173 cache_
->DoomMainEntryForUrl(request_
->url
);
1176 RecordNoStoreHeaderHistogram(request_
->load_flags
, new_response
);
1178 if (new_response_
->headers
->response_code() == 416 &&
1179 (request_
->method
== "GET" || request_
->method
== "POST")) {
1180 // If there is an active entry it may be destroyed with this transaction.
1181 response_
= *new_response_
;
1185 // Are we expecting a response to a conditional query?
1186 if (mode_
== READ_WRITE
|| mode_
== UPDATE
) {
1187 if (new_response
->headers
->response_code() == 304 || handling_206_
) {
1188 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED
);
1189 next_state_
= STATE_UPDATE_CACHED_RESPONSE
;
1192 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED
);
1196 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1200 int HttpCache::Transaction::DoNetworkRead() {
1201 next_state_
= STATE_NETWORK_READ_COMPLETE
;
1202 return network_trans_
->Read(read_buf_
.get(), io_buf_len_
, io_callback_
);
1205 int HttpCache::Transaction::DoNetworkReadComplete(int result
) {
1206 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1209 return ERR_UNEXPECTED
;
1211 // If there is an error or we aren't saving the data, we are done; just wait
1212 // until the destructor runs to see if we can keep the data.
1213 if (mode_
== NONE
|| result
< 0)
1216 next_state_
= STATE_CACHE_WRITE_DATA
;
1220 int HttpCache::Transaction::DoInitEntry() {
1221 DCHECK(!new_entry_
);
1224 return ERR_UNEXPECTED
;
1226 if (mode_
== WRITE
) {
1227 next_state_
= STATE_DOOM_ENTRY
;
1231 next_state_
= STATE_OPEN_ENTRY
;
1235 int HttpCache::Transaction::DoOpenEntry() {
1236 DCHECK(!new_entry_
);
1237 next_state_
= STATE_OPEN_ENTRY_COMPLETE
;
1238 cache_pending_
= true;
1239 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
);
1240 first_cache_access_since_
= TimeTicks::Now();
1241 return cache_
->OpenEntry(cache_key_
, &new_entry_
, this);
1244 int HttpCache::Transaction::DoOpenEntryComplete(int result
) {
1245 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1246 // OK, otherwise the cache will end up with an active entry without any
1247 // transaction attached.
1248 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
, result
);
1249 cache_pending_
= false;
1251 next_state_
= STATE_ADD_TO_ENTRY
;
1255 if (result
== ERR_CACHE_RACE
) {
1256 next_state_
= STATE_INIT_ENTRY
;
1260 if (request_
->method
== "PUT" || request_
->method
== "DELETE" ||
1261 (request_
->method
== "HEAD" && mode_
== READ_WRITE
)) {
1262 DCHECK(mode_
== READ_WRITE
|| mode_
== WRITE
|| request_
->method
== "HEAD");
1264 next_state_
= STATE_SEND_REQUEST
;
1268 if (mode_
== READ_WRITE
) {
1270 next_state_
= STATE_CREATE_ENTRY
;
1273 if (mode_
== UPDATE
) {
1274 // There is no cache entry to update; proceed without caching.
1276 next_state_
= STATE_SEND_REQUEST
;
1280 // The entry does not exist, and we are not permitted to create a new entry,
1282 return ERR_CACHE_MISS
;
1285 int HttpCache::Transaction::DoCreateEntry() {
1286 DCHECK(!new_entry_
);
1287 next_state_
= STATE_CREATE_ENTRY_COMPLETE
;
1288 cache_pending_
= true;
1289 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
);
1290 return cache_
->CreateEntry(cache_key_
, &new_entry_
, this);
1293 int HttpCache::Transaction::DoCreateEntryComplete(int result
) {
1294 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1295 // OK, otherwise the cache will end up with an active entry without any
1296 // transaction attached.
1297 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
,
1299 cache_pending_
= false;
1300 next_state_
= STATE_ADD_TO_ENTRY
;
1302 if (result
== ERR_CACHE_RACE
) {
1303 next_state_
= STATE_INIT_ENTRY
;
1308 // We have a race here: Maybe we failed to open the entry and decided to
1309 // create one, but by the time we called create, another transaction already
1310 // created the entry. If we want to eliminate this issue, we need an atomic
1311 // OpenOrCreate() method exposed by the disk cache.
1312 DLOG(WARNING
) << "Unable to create cache entry";
1315 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1316 next_state_
= STATE_SEND_REQUEST
;
1321 int HttpCache::Transaction::DoDoomEntry() {
1322 next_state_
= STATE_DOOM_ENTRY_COMPLETE
;
1323 cache_pending_
= true;
1324 if (first_cache_access_since_
.is_null())
1325 first_cache_access_since_
= TimeTicks::Now();
1326 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
);
1327 return cache_
->DoomEntry(cache_key_
, this);
1330 int HttpCache::Transaction::DoDoomEntryComplete(int result
) {
1331 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
, result
);
1332 next_state_
= STATE_CREATE_ENTRY
;
1333 cache_pending_
= false;
1334 if (result
== ERR_CACHE_RACE
)
1335 next_state_
= STATE_INIT_ENTRY
;
1339 int HttpCache::Transaction::DoAddToEntry() {
1341 cache_pending_
= true;
1342 next_state_
= STATE_ADD_TO_ENTRY_COMPLETE
;
1343 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
);
1344 DCHECK(entry_lock_waiting_since_
.is_null());
1345 entry_lock_waiting_since_
= TimeTicks::Now();
1346 int rv
= cache_
->AddTransactionToEntry(new_entry_
, this);
1347 if (rv
== ERR_IO_PENDING
) {
1348 if (bypass_lock_for_test_
) {
1349 OnAddToEntryTimeout(entry_lock_waiting_since_
);
1351 int timeout_milliseconds
= 20 * 1000;
1352 if (partial_
&& new_entry_
->writer
&&
1353 new_entry_
->writer
->range_requested_
) {
1354 // Quickly timeout and bypass the cache if we're a range request and
1355 // we're blocked by the reader/writer lock. Doing so eliminates a long
1356 // running issue, http://crbug.com/31014, where two of the same media
1357 // resources could not be played back simultaneously due to one locking
1358 // the cache entry until the entire video was downloaded.
1360 // Bypassing the cache is not ideal, as we are now ignoring the cache
1361 // entirely for all range requests to a resource beyond the first. This
1362 // is however a much more succinct solution than the alternatives, which
1363 // would require somewhat significant changes to the http caching logic.
1365 // Allow some timeout slack for the entry addition to complete in case
1366 // the writer lock is imminently released; we want to avoid skipping
1367 // the cache if at all possible. See http://crbug.com/408765
1368 timeout_milliseconds
= 25;
1370 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
1372 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout
,
1373 weak_factory_
.GetWeakPtr(), entry_lock_waiting_since_
),
1374 TimeDelta::FromMilliseconds(timeout_milliseconds
));
1380 int HttpCache::Transaction::DoAddToEntryComplete(int result
) {
1381 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
,
1383 const TimeDelta entry_lock_wait
=
1384 TimeTicks::Now() - entry_lock_waiting_since_
;
1385 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait
);
1387 entry_lock_waiting_since_
= TimeTicks();
1389 cache_pending_
= false;
1392 entry_
= new_entry_
;
1394 // If there is a failure, the cache should have taken care of new_entry_.
1397 if (result
== ERR_CACHE_RACE
) {
1398 next_state_
= STATE_INIT_ENTRY
;
1402 if (result
== ERR_CACHE_LOCK_TIMEOUT
) {
1403 // The cache is busy, bypass it for this transaction.
1405 next_state_
= STATE_SEND_REQUEST
;
1407 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1418 if (mode_
== WRITE
) {
1420 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1421 next_state_
= STATE_SEND_REQUEST
;
1423 // We have to read the headers from the cached entry.
1424 DCHECK(mode_
& READ_META
);
1425 next_state_
= STATE_CACHE_READ_RESPONSE
;
1430 // We may end up here multiple times for a given request.
1431 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1435 next_state_
= STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
;
1436 return partial_
->ShouldValidateCache(entry_
->disk_entry
, io_callback_
);
1439 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result
) {
1441 // This is the end of the request.
1442 if (mode_
& WRITE
) {
1443 DoneWritingToEntry(true);
1445 cache_
->DoneReadingFromEntry(entry_
, this);
1454 partial_
->PrepareCacheValidation(entry_
->disk_entry
,
1455 &custom_request_
->extra_headers
);
1457 if (reading_
&& partial_
->IsCurrentRangeCached()) {
1458 next_state_
= STATE_CACHE_READ_DATA
;
1462 return BeginCacheValidation();
1465 // We received 304 or 206 and we want to update the cached response headers.
1466 int HttpCache::Transaction::DoUpdateCachedResponse() {
1467 next_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1469 // Update the cached response based on the headers and properties of
1471 response_
.headers
->Update(*new_response_
->headers
.get());
1472 response_
.response_time
= new_response_
->response_time
;
1473 response_
.request_time
= new_response_
->request_time
;
1474 response_
.network_accessed
= new_response_
->network_accessed
;
1475 response_
.unused_since_prefetch
= new_response_
->unused_since_prefetch
;
1476 response_
.ssl_info
= new_response_
->ssl_info
;
1477 if (new_response_
->vary_data
.is_valid()) {
1478 response_
.vary_data
= new_response_
->vary_data
;
1479 } else if (response_
.vary_data
.is_valid()) {
1480 // There is a vary header in the stored response but not in the current one.
1481 // Update the data with the new request headers.
1482 HttpVaryData new_vary_data
;
1483 new_vary_data
.Init(*request_
, *response_
.headers
.get());
1484 response_
.vary_data
= new_vary_data
;
1487 if (response_
.headers
->HasHeaderValue("cache-control", "no-store")) {
1488 if (!entry_
->doomed
) {
1489 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1493 // If we are already reading, we already updated the headers for this
1494 // request; doing it again will change Content-Length.
1496 target_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1497 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1504 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result
) {
1505 if (mode_
== UPDATE
) {
1506 DCHECK(!handling_206_
);
1507 // We got a "not modified" response and already updated the corresponding
1508 // cache entry above.
1510 // By closing the cached entry now, we make sure that the 304 rather than
1511 // the cached 200 response, is what will be returned to the user.
1512 DoneWritingToEntry(true);
1513 } else if (entry_
&& !handling_206_
) {
1514 DCHECK_EQ(READ_WRITE
, mode_
);
1515 if (!partial_
.get() || partial_
->IsLastRange()) {
1516 cache_
->ConvertWriterToReader(entry_
);
1519 // We no longer need the network transaction, so destroy it.
1520 final_upload_progress_
= network_trans_
->GetUploadProgress();
1521 ResetNetworkTransaction();
1522 } else if (entry_
&& handling_206_
&& truncated_
&&
1523 partial_
->initial_validation()) {
1524 // We just finished the validation of a truncated entry, and the server
1525 // is willing to resume the operation. Now we go back and start serving
1526 // the first part to the user.
1527 ResetNetworkTransaction();
1528 new_response_
= NULL
;
1529 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
1530 partial_
->SetRangeToStartDownload();
1533 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1537 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1539 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1543 // We change the value of Content-Length for partial content.
1544 if (handling_206_
&& partial_
.get())
1545 partial_
->FixContentLength(new_response_
->headers
.get());
1547 response_
= *new_response_
;
1549 if (request_
->method
== "HEAD") {
1550 // This response is replacing the cached one.
1551 DoneWritingToEntry(false);
1553 new_response_
= NULL
;
1557 if (handling_206_
&& !CanResume(false)) {
1558 // There is no point in storing this resource because it will never be used.
1559 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1560 DoneWritingToEntry(false);
1562 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1563 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1567 target_state_
= STATE_TRUNCATE_CACHED_DATA
;
1568 next_state_
= truncated_
? STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
1569 STATE_CACHE_WRITE_RESPONSE
;
1573 int HttpCache::Transaction::DoTruncateCachedData() {
1574 next_state_
= STATE_TRUNCATE_CACHED_DATA_COMPLETE
;
1577 if (net_log_
.IsCapturing())
1578 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1579 // Truncate the stream.
1580 return WriteToEntry(kResponseContentIndex
, 0, NULL
, 0, io_callback_
);
1583 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result
) {
1585 if (net_log_
.IsCapturing()) {
1586 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1591 next_state_
= STATE_TRUNCATE_CACHED_METADATA
;
1595 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1596 next_state_
= STATE_TRUNCATE_CACHED_METADATA_COMPLETE
;
1600 if (net_log_
.IsCapturing())
1601 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1602 return WriteToEntry(kMetadataIndex
, 0, NULL
, 0, io_callback_
);
1605 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result
) {
1607 if (net_log_
.IsCapturing()) {
1608 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1613 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1617 int HttpCache::Transaction::DoPartialHeadersReceived() {
1618 new_response_
= NULL
;
1619 if (entry_
&& !partial_
.get() &&
1620 entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
1621 next_state_
= STATE_CACHE_READ_METADATA
;
1623 if (!partial_
.get())
1627 if (network_trans_
.get()) {
1628 next_state_
= STATE_NETWORK_READ
;
1630 next_state_
= STATE_CACHE_READ_DATA
;
1632 } else if (mode_
!= NONE
) {
1633 // We are about to return the headers for a byte-range request to the user,
1634 // so let's fix them.
1635 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1640 int HttpCache::Transaction::DoCacheReadResponse() {
1642 next_state_
= STATE_CACHE_READ_RESPONSE_COMPLETE
;
1644 io_buf_len_
= entry_
->disk_entry
->GetDataSize(kResponseInfoIndex
);
1645 read_buf_
= new IOBuffer(io_buf_len_
);
1647 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1648 return entry_
->disk_entry
->ReadData(kResponseInfoIndex
, 0, read_buf_
.get(),
1649 io_buf_len_
, io_callback_
);
1652 int HttpCache::Transaction::DoCacheReadResponseComplete(int result
) {
1653 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1654 if (result
!= io_buf_len_
||
1655 !HttpCache::ParseResponseInfo(read_buf_
->data(), io_buf_len_
,
1656 &response_
, &truncated_
)) {
1657 return OnCacheReadError(result
, true);
1660 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1661 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
1664 // Some resources may have slipped in as truncated when they're not.
1665 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1666 if (response_
.headers
->GetContentLength() == current_size
)
1669 if ((response_
.unused_since_prefetch
&&
1670 !(request_
->load_flags
& LOAD_PREFETCH
)) ||
1671 (!response_
.unused_since_prefetch
&&
1672 (request_
->load_flags
& LOAD_PREFETCH
))) {
1673 // Either this is the first use of an entry since it was prefetched or
1674 // this is a prefetch. The value of response.unused_since_prefetch is valid
1675 // for this transaction but the bit needs to be flipped in storage.
1676 next_state_
= STATE_TOGGLE_UNUSED_SINCE_PREFETCH
;
1680 next_state_
= STATE_CACHE_DISPATCH_VALIDATION
;
1684 int HttpCache::Transaction::DoCacheDispatchValidation() {
1685 // We now have access to the cache entry.
1687 // o if we are a reader for the transaction, then we can start reading the
1690 // o if we can read or write, then we should check if the cache entry needs
1691 // to be validated and then issue a network request if needed or just read
1692 // from the cache if the cache entry is already valid.
1694 // o if we are set to UPDATE, then we are handling an externally
1695 // conditionalized request (if-modified-since / if-none-match). We check
1696 // if the request headers define a validation request.
1698 int result
= ERR_FAILED
;
1701 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
1702 result
= BeginCacheRead();
1705 result
= BeginPartialCacheValidation();
1708 result
= BeginExternallyConditionalizedRequest();
1717 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1718 // Write back the toggled value for the next use of this entry.
1719 response_
.unused_since_prefetch
= !response_
.unused_since_prefetch
;
1721 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1722 // transaction then metadata will be written to cache twice. If prefetching
1723 // becomes more common, consider combining the writes.
1724 target_state_
= STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE
;
1725 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1729 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1731 // Restore the original value for this transaction.
1732 response_
.unused_since_prefetch
= !response_
.unused_since_prefetch
;
1733 next_state_
= STATE_CACHE_DISPATCH_VALIDATION
;
1737 int HttpCache::Transaction::DoCacheWriteResponse() {
1738 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1739 tracked_objects::ScopedTracker
tracking_profile(
1740 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1741 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1744 if (net_log_
.IsCapturing())
1745 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1747 return WriteResponseInfoToEntry(false);
1750 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1752 if (net_log_
.IsCapturing())
1753 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1755 return WriteResponseInfoToEntry(true);
1758 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result
) {
1759 next_state_
= target_state_
;
1760 target_state_
= STATE_NONE
;
1763 if (net_log_
.IsCapturing()) {
1764 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1768 // Balance the AddRef from WriteResponseInfoToEntry.
1769 if (result
!= io_buf_len_
) {
1770 DLOG(ERROR
) << "failed to write response info to cache";
1771 DoneWritingToEntry(false);
1776 int HttpCache::Transaction::DoCacheReadMetadata() {
1778 DCHECK(!response_
.metadata
.get());
1779 next_state_
= STATE_CACHE_READ_METADATA_COMPLETE
;
1781 response_
.metadata
=
1782 new IOBufferWithSize(entry_
->disk_entry
->GetDataSize(kMetadataIndex
));
1784 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1785 return entry_
->disk_entry
->ReadData(kMetadataIndex
, 0,
1786 response_
.metadata
.get(),
1787 response_
.metadata
->size(),
1791 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result
) {
1792 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1793 if (result
!= response_
.metadata
->size())
1794 return OnCacheReadError(result
, false);
1798 int HttpCache::Transaction::DoCacheQueryData() {
1799 next_state_
= STATE_CACHE_QUERY_DATA_COMPLETE
;
1800 return entry_
->disk_entry
->ReadyForSparseIO(io_callback_
);
1803 int HttpCache::Transaction::DoCacheQueryDataComplete(int result
) {
1804 DCHECK_EQ(OK
, result
);
1806 return ERR_UNEXPECTED
;
1808 return ValidateEntryHeadersAndContinue();
1811 int HttpCache::Transaction::DoCacheReadData() {
1813 next_state_
= STATE_CACHE_READ_DATA_COMPLETE
;
1815 if (net_log_
.IsCapturing())
1816 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA
);
1817 if (partial_
.get()) {
1818 return partial_
->CacheRead(entry_
->disk_entry
, read_buf_
.get(), io_buf_len_
,
1822 return entry_
->disk_entry
->ReadData(kResponseContentIndex
, read_offset_
,
1823 read_buf_
.get(), io_buf_len_
,
1827 int HttpCache::Transaction::DoCacheReadDataComplete(int result
) {
1828 if (net_log_
.IsCapturing()) {
1829 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA
,
1834 return ERR_UNEXPECTED
;
1836 if (partial_
.get()) {
1837 // Partial requests are confusing to report in histograms because they may
1838 // have multiple underlying requests.
1839 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1840 return DoPartialCacheReadCompleted(result
);
1844 read_offset_
+= result
;
1845 } else if (result
== 0) { // End of file.
1847 cache_
->DoneReadingFromEntry(entry_
, this);
1850 return OnCacheReadError(result
, false);
1855 int HttpCache::Transaction::DoCacheWriteData(int num_bytes
) {
1856 next_state_
= STATE_CACHE_WRITE_DATA_COMPLETE
;
1857 write_len_
= num_bytes
;
1859 if (net_log_
.IsCapturing())
1860 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1863 return AppendResponseDataToEntry(read_buf_
.get(), num_bytes
, io_callback_
);
1866 int HttpCache::Transaction::DoCacheWriteDataComplete(int result
) {
1868 if (net_log_
.IsCapturing()) {
1869 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1873 // Balance the AddRef from DoCacheWriteData.
1875 return ERR_UNEXPECTED
;
1877 if (result
!= write_len_
) {
1878 DLOG(ERROR
) << "failed to write response data to cache";
1879 DoneWritingToEntry(false);
1881 // We want to ignore errors writing to disk and just keep reading from
1883 result
= write_len_
;
1884 } else if (!done_reading_
&& entry_
) {
1885 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1886 int64 body_size
= response_
.headers
->GetContentLength();
1887 if (body_size
>= 0 && body_size
<= current_size
)
1888 done_reading_
= true;
1891 if (partial_
.get()) {
1892 // This may be the last request.
1893 if (!(result
== 0 && !truncated_
&&
1894 (partial_
->IsLastRange() || mode_
== WRITE
)))
1895 return DoPartialNetworkReadCompleted(result
);
1899 // End of file. This may be the result of a connection problem so see if we
1900 // have to keep the entry around to be flagged as truncated later on.
1901 if (done_reading_
|| !entry_
|| partial_
.get() ||
1902 response_
.headers
->GetContentLength() <= 0)
1903 DoneWritingToEntry(true);
1909 //-----------------------------------------------------------------------------
1911 void HttpCache::Transaction::ReadCertChain() {
1913 GetCacheKeyForCert(response_
.ssl_info
.cert
->os_cert_handle());
1914 const X509Certificate::OSCertHandles
& intermediates
=
1915 response_
.ssl_info
.cert
->GetIntermediateCertificates();
1916 int dist_from_root
= intermediates
.size();
1918 scoped_refptr
<SharedChainData
> shared_chain_data(
1919 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
1920 cache_
->cert_cache()->GetCertificate(key
,
1921 base::Bind(&OnCertReadIOComplete
,
1924 shared_chain_data
));
1926 for (X509Certificate::OSCertHandles::const_iterator it
=
1927 intermediates
.begin();
1928 it
!= intermediates
.end();
1931 key
= GetCacheKeyForCert(*it
);
1932 cache_
->cert_cache()->GetCertificate(key
,
1933 base::Bind(&OnCertReadIOComplete
,
1935 false /* is not leaf */,
1936 shared_chain_data
));
1938 DCHECK_EQ(0, dist_from_root
);
1941 void HttpCache::Transaction::WriteCertChain() {
1942 const X509Certificate::OSCertHandles
& intermediates
=
1943 response_
.ssl_info
.cert
->GetIntermediateCertificates();
1944 int dist_from_root
= intermediates
.size();
1946 scoped_refptr
<SharedChainData
> shared_chain_data(
1947 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
1948 cache_
->cert_cache()->SetCertificate(
1949 response_
.ssl_info
.cert
->os_cert_handle(),
1950 base::Bind(&OnCertWriteIOComplete
,
1953 shared_chain_data
));
1954 for (X509Certificate::OSCertHandles::const_iterator it
=
1955 intermediates
.begin();
1956 it
!= intermediates
.end();
1959 cache_
->cert_cache()->SetCertificate(*it
,
1960 base::Bind(&OnCertWriteIOComplete
,
1962 false /* is not leaf */,
1963 shared_chain_data
));
1965 DCHECK_EQ(0, dist_from_root
);
1968 void HttpCache::Transaction::SetRequest(const BoundNetLog
& net_log
,
1969 const HttpRequestInfo
* request
) {
1972 effective_load_flags_
= request_
->load_flags
;
1974 if (cache_
->mode() == DISABLE
)
1975 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
1977 // Some headers imply load flags. The order here is significant.
1979 // LOAD_DISABLE_CACHE : no cache read or write
1980 // LOAD_BYPASS_CACHE : no cache read
1981 // LOAD_VALIDATE_CACHE : no cache read unless validation
1983 // The former modes trump latter modes, so if we find a matching header we
1984 // can stop iterating kSpecialHeaders.
1986 static const struct {
1987 const HeaderNameAndValue
* search
;
1989 } kSpecialHeaders
[] = {
1990 { kPassThroughHeaders
, LOAD_DISABLE_CACHE
},
1991 { kForceFetchHeaders
, LOAD_BYPASS_CACHE
},
1992 { kForceValidateHeaders
, LOAD_VALIDATE_CACHE
},
1995 bool range_found
= false;
1996 bool external_validation_error
= false;
1997 bool special_headers
= false;
1999 if (request_
->extra_headers
.HasHeader(HttpRequestHeaders::kRange
))
2002 for (size_t i
= 0; i
< arraysize(kSpecialHeaders
); ++i
) {
2003 if (HeaderMatches(request_
->extra_headers
, kSpecialHeaders
[i
].search
)) {
2004 effective_load_flags_
|= kSpecialHeaders
[i
].load_flag
;
2005 special_headers
= true;
2010 // Check for conditionalization headers which may correspond with a
2011 // cache validation request.
2012 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); ++i
) {
2013 const ValidationHeaderInfo
& info
= kValidationHeaders
[i
];
2014 std::string validation_value
;
2015 if (request_
->extra_headers
.GetHeader(
2016 info
.request_header_name
, &validation_value
)) {
2017 if (!external_validation_
.values
[i
].empty() ||
2018 validation_value
.empty()) {
2019 external_validation_error
= true;
2021 external_validation_
.values
[i
] = validation_value
;
2022 external_validation_
.initialized
= true;
2026 if (range_found
|| special_headers
|| external_validation_
.initialized
) {
2027 // Log the headers before request_ is modified.
2030 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS
,
2031 base::Bind(&HttpRequestHeaders::NetLogCallback
,
2032 base::Unretained(&request_
->extra_headers
), &empty
));
2035 // We don't support ranges and validation headers.
2036 if (range_found
&& external_validation_
.initialized
) {
2037 LOG(WARNING
) << "Byte ranges AND validation headers found.";
2038 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2041 // If there is more than one validation header, we can't treat this request as
2042 // a cache validation, since we don't know for sure which header the server
2043 // will give us a response for (and they could be contradictory).
2044 if (external_validation_error
) {
2045 LOG(WARNING
) << "Multiple or malformed validation headers found.";
2046 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2049 if (range_found
&& !(effective_load_flags_
& LOAD_DISABLE_CACHE
)) {
2050 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2051 partial_
.reset(new PartialData
);
2052 if (request_
->method
== "GET" && partial_
->Init(request_
->extra_headers
)) {
2053 // We will be modifying the actual range requested to the server, so
2054 // let's remove the header here.
2055 custom_request_
.reset(new HttpRequestInfo(*request_
));
2056 custom_request_
->extra_headers
.RemoveHeader(HttpRequestHeaders::kRange
);
2057 request_
= custom_request_
.get();
2058 partial_
->SetHeaders(custom_request_
->extra_headers
);
2060 // The range is invalid or we cannot handle it properly.
2061 VLOG(1) << "Invalid byte range found.";
2062 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2063 partial_
.reset(NULL
);
2068 bool HttpCache::Transaction::ShouldPassThrough() {
2069 // We may have a null disk_cache if there is an error we cannot recover from,
2070 // like not enough disk space, or sharing violations.
2071 if (!cache_
->disk_cache_
.get())
2074 if (effective_load_flags_
& LOAD_DISABLE_CACHE
)
2077 if (request_
->method
== "GET" || request_
->method
== "HEAD")
2080 if (request_
->method
== "POST" && request_
->upload_data_stream
&&
2081 request_
->upload_data_stream
->identifier()) {
2085 if (request_
->method
== "PUT" && request_
->upload_data_stream
)
2088 if (request_
->method
== "DELETE")
2094 int HttpCache::Transaction::BeginCacheRead() {
2095 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2096 if (response_
.headers
->response_code() == 206 || partial_
.get()) {
2098 return ERR_CACHE_MISS
;
2101 if (request_
->method
== "HEAD")
2102 FixHeadersForHead();
2104 // We don't have the whole resource.
2106 return ERR_CACHE_MISS
;
2108 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2109 next_state_
= STATE_CACHE_READ_METADATA
;
2114 int HttpCache::Transaction::BeginCacheValidation() {
2115 DCHECK(mode_
== READ_WRITE
);
2117 ValidationType required_validation
= RequiresValidation();
2119 bool skip_validation
= (required_validation
== VALIDATION_NONE
);
2121 if (request_
->method
== "HEAD" &&
2122 (truncated_
|| response_
.headers
->response_code() == 206)) {
2124 if (skip_validation
)
2125 return SetupEntryForRead();
2128 next_state_
= STATE_SEND_REQUEST
;
2134 // Truncated entries can cause partial gets, so we shouldn't record this
2135 // load in histograms.
2136 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2137 skip_validation
= !partial_
->initial_validation();
2140 if (partial_
.get() && (is_sparse_
|| truncated_
) &&
2141 (!partial_
->IsCurrentRangeCached() || invalid_range_
)) {
2142 // Force revalidation for sparse or truncated entries. Note that we don't
2143 // want to ignore the regular validation logic just because a byte range was
2144 // part of the request.
2145 skip_validation
= false;
2148 if (skip_validation
) {
2149 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
2150 return SetupEntryForRead();
2152 // Make the network request conditional, to see if we may reuse our cached
2153 // response. If we cannot do so, then we just resort to a normal fetch.
2154 // Our mode remains READ_WRITE for a conditional request. Even if the
2155 // conditionalization fails, we don't switch to WRITE mode until we
2156 // know we won't be falling back to using the cache entry in the
2157 // LOAD_FROM_CACHE_IF_OFFLINE case.
2158 if (!ConditionalizeRequest()) {
2159 couldnt_conditionalize_request_
= true;
2160 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE
);
2162 return DoRestartPartialRequest();
2164 DCHECK_NE(206, response_
.headers
->response_code());
2166 next_state_
= STATE_SEND_REQUEST
;
2171 int HttpCache::Transaction::BeginPartialCacheValidation() {
2172 DCHECK(mode_
== READ_WRITE
);
2174 if (response_
.headers
->response_code() != 206 && !partial_
.get() &&
2176 return BeginCacheValidation();
2179 // Partial requests should not be recorded in histograms.
2180 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2181 if (range_requested_
) {
2182 next_state_
= STATE_CACHE_QUERY_DATA
;
2186 // The request is not for a range, but we have stored just ranges.
2188 if (request_
->method
== "HEAD")
2189 return BeginCacheValidation();
2191 partial_
.reset(new PartialData());
2192 partial_
->SetHeaders(request_
->extra_headers
);
2193 if (!custom_request_
.get()) {
2194 custom_request_
.reset(new HttpRequestInfo(*request_
));
2195 request_
= custom_request_
.get();
2198 return ValidateEntryHeadersAndContinue();
2201 // This should only be called once per request.
2202 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2203 DCHECK(mode_
== READ_WRITE
);
2205 if (!partial_
->UpdateFromStoredHeaders(
2206 response_
.headers
.get(), entry_
->disk_entry
, truncated_
)) {
2207 return DoRestartPartialRequest();
2210 if (response_
.headers
->response_code() == 206)
2213 if (!partial_
->IsRequestedRangeOK()) {
2214 // The stored data is fine, but the request may be invalid.
2215 invalid_range_
= true;
2218 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2222 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2223 DCHECK_EQ(UPDATE
, mode_
);
2224 DCHECK(external_validation_
.initialized
);
2226 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); i
++) {
2227 if (external_validation_
.values
[i
].empty())
2229 // Retrieve either the cached response's "etag" or "last-modified" header.
2230 std::string validator
;
2231 response_
.headers
->EnumerateHeader(
2233 kValidationHeaders
[i
].related_response_header_name
,
2236 if (response_
.headers
->response_code() != 200 || truncated_
||
2237 validator
.empty() || validator
!= external_validation_
.values
[i
]) {
2238 // The externally conditionalized request is not a validation request
2239 // for our existing cache entry. Proceed with caching disabled.
2240 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2241 DoneWritingToEntry(true);
2245 // TODO(ricea): This calculation is expensive to perform just to collect
2246 // statistics. Either remove it or use the result, depending on the result of
2248 ExternallyConditionalizedType type
=
2249 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE
;
2251 type
= EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS
;
2252 else if (RequiresValidation())
2253 type
= EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION
;
2255 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2256 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2257 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2259 EXTERNALLY_CONDITIONALIZED_MAX
);
2261 next_state_
= STATE_SEND_REQUEST
;
2265 int HttpCache::Transaction::RestartNetworkRequest() {
2266 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2267 DCHECK(network_trans_
.get());
2268 DCHECK_EQ(STATE_NONE
, next_state_
);
2270 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2271 int rv
= network_trans_
->RestartIgnoringLastError(io_callback_
);
2272 if (rv
!= ERR_IO_PENDING
)
2277 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2278 X509Certificate
* client_cert
) {
2279 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2280 DCHECK(network_trans_
.get());
2281 DCHECK_EQ(STATE_NONE
, next_state_
);
2283 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2284 int rv
= network_trans_
->RestartWithCertificate(client_cert
, io_callback_
);
2285 if (rv
!= ERR_IO_PENDING
)
2290 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2291 const AuthCredentials
& credentials
) {
2292 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2293 DCHECK(network_trans_
.get());
2294 DCHECK_EQ(STATE_NONE
, next_state_
);
2296 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2297 int rv
= network_trans_
->RestartWithAuth(credentials
, io_callback_
);
2298 if (rv
!= ERR_IO_PENDING
)
2303 ValidationType
HttpCache::Transaction::RequiresValidation() {
2304 // TODO(darin): need to do more work here:
2305 // - make sure we have a matching request method
2306 // - watch out for cached responses that depend on authentication
2308 if (response_
.vary_data
.is_valid() &&
2309 !response_
.vary_data
.MatchesRequest(*request_
,
2310 *response_
.headers
.get())) {
2311 vary_mismatch_
= true;
2312 return VALIDATION_SYNCHRONOUS
;
2315 if (effective_load_flags_
& LOAD_PREFERRING_CACHE
)
2316 return VALIDATION_NONE
;
2318 if (response_
.unused_since_prefetch
&&
2319 !(effective_load_flags_
& LOAD_PREFETCH
) &&
2320 response_
.headers
->GetCurrentAge(
2321 response_
.request_time
, response_
.response_time
,
2322 cache_
->clock_
->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins
)) {
2323 // The first use of a resource after prefetch within a short window skips
2325 return VALIDATION_NONE
;
2328 if (effective_load_flags_
& LOAD_VALIDATE_CACHE
)
2329 return VALIDATION_SYNCHRONOUS
;
2331 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2332 return VALIDATION_SYNCHRONOUS
;
2334 ValidationType validation_required_by_headers
=
2335 response_
.headers
->RequiresValidation(response_
.request_time
,
2336 response_
.response_time
,
2337 cache_
->clock_
->Now());
2339 if (validation_required_by_headers
== VALIDATION_ASYNCHRONOUS
) {
2340 // Asynchronous revalidation is only supported for GET and HEAD methods.
2341 if (request_
->method
!= "GET" && request_
->method
!= "HEAD")
2342 return VALIDATION_SYNCHRONOUS
;
2345 return validation_required_by_headers
;
2348 bool HttpCache::Transaction::ConditionalizeRequest() {
2349 DCHECK(response_
.headers
.get());
2351 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2354 // This only makes sense for cached 200 or 206 responses.
2355 if (response_
.headers
->response_code() != 200 &&
2356 response_
.headers
->response_code() != 206) {
2360 if (fail_conditionalization_for_test_
)
2363 DCHECK(response_
.headers
->response_code() != 206 ||
2364 response_
.headers
->HasStrongValidators());
2366 // Just use the first available ETag and/or Last-Modified header value.
2367 // TODO(darin): Or should we use the last?
2369 std::string etag_value
;
2370 if (response_
.headers
->GetHttpVersion() >= HttpVersion(1, 1))
2371 response_
.headers
->EnumerateHeader(NULL
, "etag", &etag_value
);
2373 std::string last_modified_value
;
2374 if (!vary_mismatch_
) {
2375 response_
.headers
->EnumerateHeader(NULL
, "last-modified",
2376 &last_modified_value
);
2379 if (etag_value
.empty() && last_modified_value
.empty())
2382 if (!partial_
.get()) {
2383 // Need to customize the request, so this forces us to allocate :(
2384 custom_request_
.reset(new HttpRequestInfo(*request_
));
2385 request_
= custom_request_
.get();
2387 DCHECK(custom_request_
.get());
2389 bool use_if_range
= partial_
.get() && !partial_
->IsCurrentRangeCached() &&
2392 if (!use_if_range
) {
2393 // stale-while-revalidate is not useful when we only have a partial response
2394 // cached, so don't set the header in that case.
2395 HttpResponseHeaders::FreshnessLifetimes lifetimes
=
2396 response_
.headers
->GetFreshnessLifetimes(response_
.response_time
);
2397 if (lifetimes
.staleness
> TimeDelta()) {
2398 TimeDelta current_age
= response_
.headers
->GetCurrentAge(
2399 response_
.request_time
, response_
.response_time
,
2400 cache_
->clock_
->Now());
2402 custom_request_
->extra_headers
.SetHeader(
2404 base::StringPrintf("max-age=%" PRId64
2405 ",stale-while-revalidate=%" PRId64
",age=%" PRId64
,
2406 lifetimes
.freshness
.InSeconds(),
2407 lifetimes
.staleness
.InSeconds(),
2408 current_age
.InSeconds()));
2412 if (!etag_value
.empty()) {
2414 // We don't want to switch to WRITE mode if we don't have this block of a
2415 // byte-range request because we may have other parts cached.
2416 custom_request_
->extra_headers
.SetHeader(
2417 HttpRequestHeaders::kIfRange
, etag_value
);
2419 custom_request_
->extra_headers
.SetHeader(
2420 HttpRequestHeaders::kIfNoneMatch
, etag_value
);
2422 // For byte-range requests, make sure that we use only one way to validate
2424 if (partial_
.get() && !partial_
->IsCurrentRangeCached())
2428 if (!last_modified_value
.empty()) {
2430 custom_request_
->extra_headers
.SetHeader(
2431 HttpRequestHeaders::kIfRange
, last_modified_value
);
2433 custom_request_
->extra_headers
.SetHeader(
2434 HttpRequestHeaders::kIfModifiedSince
, last_modified_value
);
2441 // We just received some headers from the server. We may have asked for a range,
2442 // in which case partial_ has an object. This could be the first network request
2443 // we make to fulfill the original request, or we may be already reading (from
2444 // the net and / or the cache). If we are not expecting a certain response, we
2445 // just bypass the cache for this request (but again, maybe we are reading), and
2446 // delete partial_ (so we are not able to "fix" the headers that we return to
2447 // the user). This results in either a weird response for the caller (we don't
2448 // expect it after all), or maybe a range that was not exactly what it was asked
2451 // If the server is simply telling us that the resource has changed, we delete
2452 // the cached entry and restart the request as the caller intended (by returning
2453 // false from this method). However, we may not be able to do that at any point,
2454 // for instance if we already returned the headers to the user.
2456 // WARNING: Whenever this code returns false, it has to make sure that the next
2457 // time it is called it will return true so that we don't keep retrying the
2459 bool HttpCache::Transaction::ValidatePartialResponse() {
2460 const HttpResponseHeaders
* headers
= new_response_
->headers
.get();
2461 int response_code
= headers
->response_code();
2462 bool partial_response
= (response_code
== 206);
2463 handling_206_
= false;
2465 if (!entry_
|| request_
->method
!= "GET")
2468 if (invalid_range_
) {
2469 // We gave up trying to match this request with the stored data. If the
2470 // server is ok with the request, delete the entry, otherwise just ignore
2473 if (partial_response
|| response_code
== 200) {
2474 DoomPartialEntry(true);
2477 if (response_code
== 304)
2479 IgnoreRangeRequest();
2484 if (!partial_
.get()) {
2485 // We are not expecting 206 but we may have one.
2486 if (partial_response
)
2487 IgnoreRangeRequest();
2492 // TODO(rvargas): Do we need to consider other results here?.
2493 bool failure
= response_code
== 200 || response_code
== 416;
2495 if (partial_
->IsCurrentRangeCached()) {
2496 // We asked for "If-None-Match: " so a 206 means a new object.
2497 if (partial_response
)
2500 if (response_code
== 304 && partial_
->ResponseHeadersOK(headers
))
2503 // We asked for "If-Range: " so a 206 means just another range.
2504 if (partial_response
) {
2505 if (partial_
->ResponseHeadersOK(headers
)) {
2506 handling_206_
= true;
2513 if (!reading_
&& !is_sparse_
&& !partial_response
) {
2514 // See if we can ignore the fact that we issued a byte range request.
2515 // If the server sends 200, just store it. If it sends an error, redirect
2516 // or something else, we may store the response as long as we didn't have
2517 // anything already stored.
2518 if (response_code
== 200 ||
2519 (!truncated_
&& response_code
!= 304 && response_code
!= 416)) {
2520 // The server is sending something else, and we can save it.
2521 DCHECK((truncated_
&& !partial_
->IsLastRange()) || range_requested_
);
2528 // 304 is not expected here, but we'll spare the entry (unless it was
2535 // We cannot truncate this entry, it has to be deleted.
2536 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2538 if (is_sparse_
|| truncated_
) {
2539 // There was something cached to start with, either sparsed data (206), or
2540 // a truncated 200, which means that we probably modified the request,
2541 // adding a byte range or modifying the range requested by the caller.
2542 if (!reading_
&& !partial_
->IsLastRange()) {
2543 // We have not returned anything to the caller yet so it should be safe
2544 // to issue another network request, this time without us messing up the
2546 ResetPartialState(true);
2549 LOG(WARNING
) << "Failed to revalidate partial entry";
2551 DoomPartialEntry(true);
2555 IgnoreRangeRequest();
2559 void HttpCache::Transaction::IgnoreRangeRequest() {
2560 // We have a problem. We may or may not be reading already (in which case we
2561 // returned the headers), but we'll just pretend that this request is not
2562 // using the cache and see what happens. Most likely this is the first
2563 // response from the server (it's not changing its mind midway, right?).
2564 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2566 DoneWritingToEntry(mode_
!= WRITE
);
2567 else if (mode_
& READ
&& entry_
)
2568 cache_
->DoneReadingFromEntry(entry_
, this);
2570 partial_
.reset(NULL
);
2575 void HttpCache::Transaction::FixHeadersForHead() {
2576 if (response_
.headers
->response_code() == 206) {
2577 response_
.headers
->RemoveHeader("Content-Range");
2578 response_
.headers
->ReplaceStatusLine("HTTP/1.1 200 OK");
2582 void HttpCache::Transaction::FailRangeRequest() {
2583 response_
= *new_response_
;
2584 partial_
->FixResponseHeaders(response_
.headers
.get(), false);
2587 int HttpCache::Transaction::SetupEntryForRead() {
2589 ResetNetworkTransaction();
2590 if (partial_
.get()) {
2591 if (truncated_
|| is_sparse_
|| !invalid_range_
) {
2592 // We are going to return the saved response headers to the caller, so
2593 // we may need to adjust them first.
2594 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
2600 cache_
->ConvertWriterToReader(entry_
);
2603 if (request_
->method
== "HEAD")
2604 FixHeadersForHead();
2606 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2607 next_state_
= STATE_CACHE_READ_METADATA
;
2612 int HttpCache::Transaction::ReadFromNetwork(IOBuffer
* data
, int data_len
) {
2614 io_buf_len_
= data_len
;
2615 next_state_
= STATE_NETWORK_READ
;
2619 int HttpCache::Transaction::ReadFromEntry(IOBuffer
* data
, int data_len
) {
2620 if (request_
->method
== "HEAD")
2624 io_buf_len_
= data_len
;
2625 next_state_
= STATE_CACHE_READ_DATA
;
2629 int HttpCache::Transaction::WriteToEntry(int index
, int offset
,
2630 IOBuffer
* data
, int data_len
,
2631 const CompletionCallback
& callback
) {
2636 if (!partial_
.get() || !data_len
) {
2637 rv
= entry_
->disk_entry
->WriteData(index
, offset
, data
, data_len
, callback
,
2640 rv
= partial_
->CacheWrite(entry_
->disk_entry
, data
, data_len
, callback
);
2645 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated
) {
2646 next_state_
= STATE_CACHE_WRITE_RESPONSE_COMPLETE
;
2650 // Do not cache no-store content. Do not cache content with cert errors
2651 // either. This is to prevent not reporting net errors when loading a
2652 // resource from the cache. When we load a page over HTTPS with a cert error
2653 // we show an SSL blocking page. If the user clicks proceed we reload the
2654 // resource ignoring the errors. The loaded resource is then cached. If that
2655 // resource is subsequently loaded from the cache, no net error is reported
2656 // (even though the cert status contains the actual errors) and no SSL
2657 // blocking page is shown. An alternative would be to reverse-map the cert
2658 // status to a net error and replay the net error.
2659 if ((response_
.headers
->HasHeaderValue("cache-control", "no-store")) ||
2660 IsCertStatusError(response_
.ssl_info
.cert_status
)) {
2661 DoneWritingToEntry(false);
2662 if (net_log_
.IsCapturing())
2663 net_log_
.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
2667 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2668 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
2672 DCHECK_EQ(200, response_
.headers
->response_code());
2674 // When writing headers, we normally only write the non-transient headers.
2675 bool skip_transient_headers
= true;
2676 scoped_refptr
<PickledIOBuffer
> data(new PickledIOBuffer());
2677 response_
.Persist(data
->pickle(), skip_transient_headers
, truncated
);
2680 io_buf_len_
= data
->pickle()->size();
2681 return entry_
->disk_entry
->WriteData(kResponseInfoIndex
, 0, data
.get(),
2682 io_buf_len_
, io_callback_
, true);
2685 int HttpCache::Transaction::AppendResponseDataToEntry(
2686 IOBuffer
* data
, int data_len
, const CompletionCallback
& callback
) {
2687 if (!entry_
|| !data_len
)
2690 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
2691 return WriteToEntry(kResponseContentIndex
, current_size
, data
, data_len
,
2695 void HttpCache::Transaction::DoneWritingToEntry(bool success
) {
2701 cache_
->DoneWritingToEntry(entry_
, success
);
2703 mode_
= NONE
; // switch to 'pass through' mode
2706 int HttpCache::Transaction::OnCacheReadError(int result
, bool restart
) {
2707 DLOG(ERROR
) << "ReadData failed: " << result
;
2708 const int result_for_histogram
= std::max(0, -result
);
2710 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2711 result_for_histogram
);
2713 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2714 result_for_histogram
);
2717 // Avoid using this entry in the future.
2719 cache_
->DoomActiveEntry(cache_key_
);
2723 DCHECK(!network_trans_
.get());
2724 cache_
->DoneWithEntry(entry_
, this, false);
2728 next_state_
= STATE_GET_BACKEND
;
2732 return ERR_CACHE_READ_FAILURE
;
2735 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time
) {
2736 if (entry_lock_waiting_since_
!= start_time
)
2739 DCHECK_EQ(next_state_
, STATE_ADD_TO_ENTRY_COMPLETE
);
2744 cache_
->RemovePendingTransaction(this);
2745 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT
);
2748 void HttpCache::Transaction::DoomPartialEntry(bool delete_object
) {
2749 DVLOG(2) << "DoomPartialEntry";
2750 int rv
= cache_
->DoomEntry(cache_key_
, NULL
);
2752 cache_
->DoneWithEntry(entry_
, this, false);
2757 partial_
.reset(NULL
);
2760 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result
) {
2761 partial_
->OnNetworkReadCompleted(result
);
2764 // We need to move on to the next range.
2765 ResetNetworkTransaction();
2766 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2771 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result
) {
2772 partial_
->OnCacheReadCompleted(result
);
2774 if (result
== 0 && mode_
== READ_WRITE
) {
2775 // We need to move on to the next range.
2776 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2777 } else if (result
< 0) {
2778 return OnCacheReadError(result
, false);
2783 int HttpCache::Transaction::DoRestartPartialRequest() {
2784 // The stored data cannot be used. Get rid of it and restart this request.
2785 net_log_
.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST
);
2787 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
2788 // to Doom the entry again).
2790 ResetPartialState(!range_requested_
);
2791 next_state_
= STATE_CREATE_ENTRY
;
2795 void HttpCache::Transaction::ResetPartialState(bool delete_object
) {
2796 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
2797 DoomPartialEntry(delete_object
);
2799 if (!delete_object
) {
2800 // The simplest way to re-initialize partial_ is to create a new object.
2801 partial_
.reset(new PartialData());
2802 if (partial_
->Init(request_
->extra_headers
))
2803 partial_
->SetHeaders(custom_request_
->extra_headers
);
2809 void HttpCache::Transaction::ResetNetworkTransaction() {
2810 DCHECK(!old_network_trans_load_timing_
);
2811 DCHECK(network_trans_
);
2812 LoadTimingInfo load_timing
;
2813 if (network_trans_
->GetLoadTimingInfo(&load_timing
))
2814 old_network_trans_load_timing_
.reset(new LoadTimingInfo(load_timing
));
2815 total_received_bytes_
+= network_trans_
->GetTotalReceivedBytes();
2816 ConnectionAttempts attempts
;
2817 network_trans_
->GetConnectionAttempts(&attempts
);
2818 for (const auto& attempt
: attempts
)
2819 old_connection_attempts_
.push_back(attempt
);
2820 network_trans_
.reset();
2823 // Histogram data from the end of 2010 show the following distribution of
2824 // response headers:
2826 // Content-Length............... 87%
2827 // Date......................... 98%
2828 // Last-Modified................ 49%
2829 // Etag......................... 19%
2830 // Accept-Ranges: bytes......... 25%
2831 // Accept-Ranges: none.......... 0.4%
2832 // Strong Validator............. 50%
2833 // Strong Validator + ranges.... 24%
2834 // Strong Validator + CL........ 49%
2836 bool HttpCache::Transaction::CanResume(bool has_data
) {
2837 // Double check that there is something worth keeping.
2838 if (has_data
&& !entry_
->disk_entry
->GetDataSize(kResponseContentIndex
))
2841 if (request_
->method
!= "GET")
2844 // Note that if this is a 206, content-length was already fixed after calling
2845 // PartialData::ResponseHeadersOK().
2846 if (response_
.headers
->GetContentLength() <= 0 ||
2847 response_
.headers
->HasHeaderValue("Accept-Ranges", "none") ||
2848 !response_
.headers
->HasStrongValidators()) {
2855 void HttpCache::Transaction::UpdateTransactionPattern(
2856 TransactionPattern new_transaction_pattern
) {
2857 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2859 DCHECK(transaction_pattern_
== PATTERN_UNDEFINED
||
2860 new_transaction_pattern
== PATTERN_NOT_COVERED
);
2861 transaction_pattern_
= new_transaction_pattern
;
2864 void HttpCache::Transaction::RecordHistograms() {
2865 DCHECK_NE(PATTERN_UNDEFINED
, transaction_pattern_
);
2866 if (!cache_
.get() || !cache_
->GetCurrentBackend() ||
2867 cache_
->GetCurrentBackend()->GetCacheType() != DISK_CACHE
||
2868 cache_
->mode() != NORMAL
|| request_
->method
!= "GET") {
2871 UMA_HISTOGRAM_ENUMERATION(
2872 "HttpCache.Pattern", transaction_pattern_
, PATTERN_MAX
);
2873 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2875 DCHECK(!range_requested_
);
2876 DCHECK(!first_cache_access_since_
.is_null());
2878 TimeDelta total_time
= base::TimeTicks::Now() - first_cache_access_since_
;
2880 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time
);
2882 bool did_send_request
= !send_request_since_
.is_null();
2884 (did_send_request
&&
2885 (transaction_pattern_
== PATTERN_ENTRY_NOT_CACHED
||
2886 transaction_pattern_
== PATTERN_ENTRY_VALIDATED
||
2887 transaction_pattern_
== PATTERN_ENTRY_UPDATED
||
2888 transaction_pattern_
== PATTERN_ENTRY_CANT_CONDITIONALIZE
)) ||
2889 (!did_send_request
&& transaction_pattern_
== PATTERN_ENTRY_USED
));
2891 if (!did_send_request
) {
2892 DCHECK(transaction_pattern_
== PATTERN_ENTRY_USED
);
2893 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time
);
2897 TimeDelta before_send_time
= send_request_since_
- first_cache_access_since_
;
2898 int64 before_send_percent
= (total_time
.ToInternalValue() == 0) ?
2899 0 : before_send_time
* 100 / total_time
;
2900 DCHECK_GE(before_send_percent
, 0);
2901 DCHECK_LE(before_send_percent
, 100);
2902 base::HistogramBase::Sample before_send_sample
=
2903 static_cast<base::HistogramBase::Sample
>(before_send_percent
);
2905 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time
);
2906 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time
);
2907 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample
);
2909 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2910 // below this comment after we have received initial data.
2911 switch (transaction_pattern_
) {
2912 case PATTERN_ENTRY_CANT_CONDITIONALIZE
: {
2913 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2915 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2916 before_send_sample
);
2919 case PATTERN_ENTRY_NOT_CACHED
: {
2920 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time
);
2921 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2922 before_send_sample
);
2925 case PATTERN_ENTRY_VALIDATED
: {
2926 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time
);
2927 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2928 before_send_sample
);
2931 case PATTERN_ENTRY_UPDATED
: {
2932 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time
);
2933 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2934 before_send_sample
);
2942 void HttpCache::Transaction::OnIOComplete(int result
) {