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"
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"
53 using base::TimeDelta
;
54 using base::TimeTicks
;
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
70 class SharedChainData
: public base::RefCounted
<SharedChainData
> {
72 SharedChainData(int num_ops
, TimeTicks start
)
73 : num_pending_ops(num_ops
), start_time(start
) {}
79 friend class base::RefCounted
<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
88 std::string
GetCacheKeyForCert(X509Certificate::OSCertHandle cert_handle
) {
89 SHA1HashValue fingerprint
=
90 X509Certificate::CalculateFingerprint(cert_handle
);
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
101 void OnCertReadIOComplete(
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",
115 base::TimeDelta::FromMilliseconds(1),
116 base::TimeDelta::FromMinutes(10),
120 bool success
= (cert_handle
!= NULL
);
122 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success
);
125 UMA_HISTOGRAM_CUSTOM_COUNTS(
126 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root
, 0, 10, 7);
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
137 void OnCertWriteIOComplete(
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",
151 base::TimeDelta::FromMilliseconds(1),
152 base::TimeDelta::FromMinutes(10),
156 bool success
= !key
.empty();
158 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success
);
161 UMA_HISTOGRAM_CUSTOM_COUNTS(
162 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root
, 0, 10, 7);
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 // Error codes that will be considered indicative of a page being offline/
177 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
178 bool IsOfflineError(int error
) {
180 error
== ERR_NAME_NOT_RESOLVED
|| error
== ERR_INTERNET_DISCONNECTED
||
181 error
== ERR_ADDRESS_UNREACHABLE
|| error
== ERR_CONNECTION_TIMED_OUT
);
184 // Enum for UMA, indicating the status (with regard to offline mode) of
185 // a particular request.
186 enum RequestOfflineStatus
{
187 // A cache transaction hit in cache (data was present and not stale)
189 OFFLINE_STATUS_FRESH_CACHE
,
191 // A network request was required for a cache entry, and it succeeded.
192 OFFLINE_STATUS_NETWORK_SUCCEEDED
,
194 // A network request was required for a cache entry, and it failed with
195 // a non-offline error.
196 OFFLINE_STATUS_NETWORK_FAILED
,
198 // A network request was required for a cache entry, it failed with an
199 // offline error, and we could serve stale data if
200 // LOAD_FROM_CACHE_IF_OFFLINE was set.
201 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
,
203 // A network request was required for a cache entry, it failed with
204 // an offline error, and there was no servable data in cache (even
206 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
,
208 OFFLINE_STATUS_MAX_ENTRIES
211 void RecordOfflineStatus(int load_flags
, RequestOfflineStatus status
) {
212 // Restrict to main frame to keep statistics close to
213 // "would have shown them something useful if offline mode was enabled".
214 if (load_flags
& LOAD_MAIN_FRAME
) {
215 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status
,
216 OFFLINE_STATUS_MAX_ENTRIES
);
220 void RecordNoStoreHeaderHistogram(int load_flags
,
221 const HttpResponseInfo
* response
) {
222 if (load_flags
& LOAD_MAIN_FRAME
) {
223 UMA_HISTOGRAM_BOOLEAN(
224 "Net.MainFrameNoStore",
225 response
->headers
->HasHeaderValue("cache-control", "no-store"));
229 base::Value
* NetLogAsyncRevalidationInfoCallback(
230 const NetLog::Source
& source
,
231 const HttpRequestInfo
* request
,
232 NetLogCaptureMode capture_mode
) {
233 base::DictionaryValue
* dict
= new base::DictionaryValue();
234 source
.AddToEventParameters(dict
);
236 dict
->SetString("url", request
->url
.possibly_invalid_spec());
237 dict
->SetString("method", request
->method
);
241 enum ExternallyConditionalizedType
{
242 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION
,
243 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE
,
244 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS
,
245 EXTERNALLY_CONDITIONALIZED_MAX
250 struct HeaderNameAndValue
{
255 // If the request includes one of these request headers, then avoid caching
256 // to avoid getting confused.
257 static const HeaderNameAndValue kPassThroughHeaders
[] = {
258 { "if-unmodified-since", NULL
}, // causes unexpected 412s
259 { "if-match", NULL
}, // causes unexpected 412s
260 { "if-range", NULL
},
264 struct ValidationHeaderInfo
{
265 const char* request_header_name
;
266 const char* related_response_header_name
;
269 static const ValidationHeaderInfo kValidationHeaders
[] = {
270 { "if-modified-since", "last-modified" },
271 { "if-none-match", "etag" },
274 // If the request includes one of these request headers, then avoid reusing
275 // our cached copy if any.
276 static const HeaderNameAndValue kForceFetchHeaders
[] = {
277 { "cache-control", "no-cache" },
278 { "pragma", "no-cache" },
282 // If the request includes one of these request headers, then force our
283 // cached copy (if any) to be revalidated before reusing it.
284 static const HeaderNameAndValue kForceValidateHeaders
[] = {
285 { "cache-control", "max-age=0" },
289 static bool HeaderMatches(const HttpRequestHeaders
& headers
,
290 const HeaderNameAndValue
* search
) {
291 for (; search
->name
; ++search
) {
292 std::string header_value
;
293 if (!headers
.GetHeader(search
->name
, &header_value
))
299 HttpUtil::ValuesIterator
v(header_value
.begin(), header_value
.end(), ',');
300 while (v
.GetNext()) {
301 if (LowerCaseEqualsASCII(v
.value_begin(), v
.value_end(), search
->value
))
308 //-----------------------------------------------------------------------------
310 HttpCache::Transaction::Transaction(RequestPriority priority
, HttpCache
* cache
)
311 : next_state_(STATE_NONE
),
314 cache_(cache
->GetWeakPtr()),
319 target_state_(STATE_NONE
),
321 invalid_range_(false),
324 range_requested_(false),
325 handling_206_(false),
326 cache_pending_(false),
327 done_reading_(false),
328 vary_mismatch_(false),
329 couldnt_conditionalize_request_(false),
330 bypass_lock_for_test_(false),
331 fail_conditionalization_for_test_(false),
334 effective_load_flags_(0),
336 transaction_pattern_(PATTERN_UNDEFINED
),
337 total_received_bytes_(0),
338 websocket_handshake_stream_base_create_helper_(NULL
),
339 weak_factory_(this) {
340 static_assert(HttpCache::Transaction::kNumValidationHeaders
==
341 arraysize(kValidationHeaders
),
342 "invalid number of validation headers");
344 io_callback_
= base::Bind(&Transaction::OnIOComplete
,
345 weak_factory_
.GetWeakPtr());
348 HttpCache::Transaction::~Transaction() {
349 // We may have to issue another IO, but we should never invoke the callback_
355 bool cancel_request
= reading_
&& response_
.headers
.get();
356 if (cancel_request
) {
358 entry_
->disk_entry
->CancelSparseIO();
360 cancel_request
&= (response_
.headers
->response_code() == 200);
364 cache_
->DoneWithEntry(entry_
, this, cancel_request
);
365 } else if (cache_pending_
) {
366 cache_
->RemovePendingTransaction(this);
371 int HttpCache::Transaction::WriteMetadata(IOBuffer
* buf
, int buf_len
,
372 const CompletionCallback
& callback
) {
374 DCHECK_GT(buf_len
, 0);
375 DCHECK(!callback
.is_null());
376 if (!cache_
.get() || !entry_
)
377 return ERR_UNEXPECTED
;
379 // We don't need to track this operation for anything.
380 // It could be possible to check if there is something already written and
381 // avoid writing again (it should be the same, right?), but let's allow the
382 // caller to "update" the contents with something new.
383 return entry_
->disk_entry
->WriteData(kMetadataIndex
, 0, buf
, buf_len
,
387 bool HttpCache::Transaction::AddTruncatedFlag() {
388 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
390 // Don't set the flag for sparse entries.
391 if (partial_
.get() && !truncated_
)
394 if (!CanResume(true))
397 // We may have received the whole resource already.
402 target_state_
= STATE_NONE
;
403 next_state_
= STATE_CACHE_WRITE_TRUNCATED_RESPONSE
;
408 LoadState
HttpCache::Transaction::GetWriterLoadState() const {
409 if (network_trans_
.get())
410 return network_trans_
->GetLoadState();
411 if (entry_
|| !request_
)
412 return LOAD_STATE_IDLE
;
413 return LOAD_STATE_WAITING_FOR_CACHE
;
416 const BoundNetLog
& HttpCache::Transaction::net_log() const {
420 int HttpCache::Transaction::Start(const HttpRequestInfo
* request
,
421 const CompletionCallback
& callback
,
422 const BoundNetLog
& net_log
) {
424 DCHECK(!callback
.is_null());
426 // Ensure that we only have one asynchronous call at a time.
427 DCHECK(callback_
.is_null());
429 DCHECK(!network_trans_
.get());
433 return ERR_UNEXPECTED
;
435 SetRequest(net_log
, request
);
437 // We have to wait until the backend is initialized so we start the SM.
438 next_state_
= STATE_GET_BACKEND
;
441 // Setting this here allows us to check for the existence of a callback_ to
442 // determine if we are still inside Start.
443 if (rv
== ERR_IO_PENDING
)
444 callback_
= callback
;
449 int HttpCache::Transaction::RestartIgnoringLastError(
450 const CompletionCallback
& callback
) {
451 DCHECK(!callback
.is_null());
453 // Ensure that we only have one asynchronous call at a time.
454 DCHECK(callback_
.is_null());
457 return ERR_UNEXPECTED
;
459 int rv
= RestartNetworkRequest();
461 if (rv
== ERR_IO_PENDING
)
462 callback_
= callback
;
467 int HttpCache::Transaction::RestartWithCertificate(
468 X509Certificate
* client_cert
,
469 const CompletionCallback
& callback
) {
470 DCHECK(!callback
.is_null());
472 // Ensure that we only have one asynchronous call at a time.
473 DCHECK(callback_
.is_null());
476 return ERR_UNEXPECTED
;
478 int rv
= RestartNetworkRequestWithCertificate(client_cert
);
480 if (rv
== ERR_IO_PENDING
)
481 callback_
= callback
;
486 int HttpCache::Transaction::RestartWithAuth(
487 const AuthCredentials
& credentials
,
488 const CompletionCallback
& callback
) {
489 DCHECK(auth_response_
.headers
.get());
490 DCHECK(!callback
.is_null());
492 // Ensure that we only have one asynchronous call at a time.
493 DCHECK(callback_
.is_null());
496 return ERR_UNEXPECTED
;
498 // Clear the intermediate response since we are going to start over.
499 auth_response_
= HttpResponseInfo();
501 int rv
= RestartNetworkRequestWithAuth(credentials
);
503 if (rv
== ERR_IO_PENDING
)
504 callback_
= callback
;
509 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
510 if (!network_trans_
.get())
512 return network_trans_
->IsReadyToRestartForAuth();
515 int HttpCache::Transaction::Read(IOBuffer
* buf
, int buf_len
,
516 const CompletionCallback
& callback
) {
518 DCHECK_GT(buf_len
, 0);
519 DCHECK(!callback
.is_null());
521 DCHECK(callback_
.is_null());
524 return ERR_UNEXPECTED
;
526 // If we have an intermediate auth response at this point, then it means the
527 // user wishes to read the network response (the error page). If there is a
528 // previous response in the cache then we should leave it intact.
529 if (auth_response_
.headers
.get() && mode_
!= NONE
) {
530 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
531 DCHECK(mode_
& WRITE
);
532 DoneWritingToEntry(mode_
== READ_WRITE
);
541 DCHECK(partial_
.get());
542 if (!network_trans_
.get()) {
543 // We are just reading from the cache, but we may be writing later.
544 rv
= ReadFromEntry(buf
, buf_len
);
549 DCHECK(network_trans_
.get());
550 rv
= ReadFromNetwork(buf
, buf_len
);
553 rv
= ReadFromEntry(buf
, buf_len
);
560 if (rv
== ERR_IO_PENDING
) {
561 DCHECK(callback_
.is_null());
562 callback_
= callback
;
567 void HttpCache::Transaction::StopCaching() {
568 // We really don't know where we are now. Hopefully there is no operation in
569 // progress, but nothing really prevents this method to be called after we
570 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
571 // point because we need the state machine for that (and even if we are really
572 // free, that would be an asynchronous operation). In other words, keep the
573 // entry how it is (it will be marked as truncated at destruction), and let
574 // the next piece of code that executes know that we are now reading directly
576 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
577 // future request for the resource will be blocked on this one.
579 if (cache_
.get() && entry_
&& (mode_
& WRITE
) && network_trans_
.get() &&
580 !is_sparse_
&& !range_requested_
) {
585 bool HttpCache::Transaction::GetFullRequestHeaders(
586 HttpRequestHeaders
* headers
) const {
588 return network_trans_
->GetFullRequestHeaders(headers
);
590 // TODO(ttuttle): Read headers from cache.
594 int64
HttpCache::Transaction::GetTotalReceivedBytes() const {
595 int64 total_received_bytes
= total_received_bytes_
;
597 total_received_bytes
+= network_trans_
->GetTotalReceivedBytes();
598 return total_received_bytes
;
601 void HttpCache::Transaction::DoneReading() {
602 if (cache_
.get() && entry_
) {
603 DCHECK_NE(mode_
, UPDATE
);
605 DoneWritingToEntry(true);
606 } else if (mode_
& READ
) {
607 // It is necessary to check mode_ & READ because it is possible
608 // for mode_ to be NONE and entry_ non-NULL with a write entry
609 // if StopCaching was called.
610 cache_
->DoneReadingFromEntry(entry_
, this);
616 const HttpResponseInfo
* HttpCache::Transaction::GetResponseInfo() const {
617 // Null headers means we encountered an error or haven't a response yet
618 if (auth_response_
.headers
.get())
619 return &auth_response_
;
620 return (response_
.headers
.get() || response_
.ssl_info
.cert
.get() ||
621 response_
.cert_request_info
.get())
626 LoadState
HttpCache::Transaction::GetLoadState() const {
627 LoadState state
= GetWriterLoadState();
628 if (state
!= LOAD_STATE_WAITING_FOR_CACHE
)
632 return cache_
->GetLoadStateForPendingTransaction(this);
634 return LOAD_STATE_IDLE
;
637 UploadProgress
HttpCache::Transaction::GetUploadProgress() const {
638 if (network_trans_
.get())
639 return network_trans_
->GetUploadProgress();
640 return final_upload_progress_
;
643 void HttpCache::Transaction::SetQuicServerInfo(
644 QuicServerInfo
* quic_server_info
) {}
646 bool HttpCache::Transaction::GetLoadTimingInfo(
647 LoadTimingInfo
* load_timing_info
) const {
649 return network_trans_
->GetLoadTimingInfo(load_timing_info
);
651 if (old_network_trans_load_timing_
) {
652 *load_timing_info
= *old_network_trans_load_timing_
;
656 if (first_cache_access_since_
.is_null())
659 // If the cache entry was opened, return that time.
660 load_timing_info
->send_start
= first_cache_access_since_
;
661 // This time doesn't make much sense when reading from the cache, so just use
662 // the same time as send_start.
663 load_timing_info
->send_end
= first_cache_access_since_
;
667 void HttpCache::Transaction::SetPriority(RequestPriority priority
) {
668 priority_
= priority
;
670 network_trans_
->SetPriority(priority_
);
673 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
674 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
675 websocket_handshake_stream_base_create_helper_
= create_helper
;
677 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(create_helper
);
680 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
681 const BeforeNetworkStartCallback
& callback
) {
682 DCHECK(!network_trans_
);
683 before_network_start_callback_
= callback
;
686 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
687 const BeforeProxyHeadersSentCallback
& callback
) {
688 DCHECK(!network_trans_
);
689 before_proxy_headers_sent_callback_
= callback
;
692 int HttpCache::Transaction::ResumeNetworkStart() {
694 return network_trans_
->ResumeNetworkStart();
695 return ERR_UNEXPECTED
;
698 //-----------------------------------------------------------------------------
700 void HttpCache::Transaction::DoCallback(int rv
) {
701 DCHECK(rv
!= ERR_IO_PENDING
);
702 DCHECK(!callback_
.is_null());
704 read_buf_
= NULL
; // Release the buffer before invoking the callback.
706 // Since Run may result in Read being called, clear callback_ up front.
707 CompletionCallback c
= callback_
;
712 int HttpCache::Transaction::HandleResult(int rv
) {
713 DCHECK(rv
!= ERR_IO_PENDING
);
714 if (!callback_
.is_null())
720 // A few common patterns: (Foo* means Foo -> FooComplete)
722 // 1. Not-cached entry:
724 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
725 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
726 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
727 // PartialHeadersReceived
730 // NetworkRead* -> CacheWriteData*
732 // 2. Cached entry, no validation:
734 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
735 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
736 // BeginCacheValidation() -> SetupEntryForRead()
741 // 3. Cached entry, validation (304):
743 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
744 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
745 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
746 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
747 // -> OverwriteCachedResponse -> PartialHeadersReceived
752 // 4. Cached entry, validation and replace (200):
754 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
755 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
756 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
757 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
758 // TruncateCachedMetadata* -> PartialHeadersReceived
761 // NetworkRead* -> CacheWriteData*
763 // 5. Sparse entry, partially cached, byte range request:
765 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
766 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
767 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
768 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
769 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
770 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
771 // -> OverwriteCachedResponse -> PartialHeadersReceived
774 // NetworkRead* -> CacheWriteData*
777 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
778 // CompletePartialCacheValidation -> CacheReadData* ->
781 // CacheReadData* -> StartPartialCacheValidation ->
782 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
783 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
784 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
786 // 6. HEAD. Not-cached entry:
787 // Pass through. Don't save a HEAD by itself.
789 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
791 // 7. HEAD. Cached entry, no validation:
793 // The same flow as for a GET request (example #2)
796 // CacheReadData (returns 0)
798 // 8. HEAD. Cached entry, validation (304):
799 // The request updates the stored headers.
800 // Start(): Same as for a GET request (example #3)
803 // CacheReadData (returns 0)
805 // 9. HEAD. Cached entry, validation and replace (200):
806 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
809 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
810 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
811 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
812 // OverwriteCachedResponse
814 // 10. HEAD. Sparse entry, partially cached:
815 // Serve the request from the cache, as long as it doesn't require
816 // revalidation. Ignore missing ranges when deciding to revalidate. If the
817 // entry requires revalidation, ignore the whole request and go to full pass
818 // through (the result of the HEAD request will NOT update the entry).
820 // Start(): Basically the same as example 7, as we never create a partial_
821 // object for this request.
823 // 11. Prefetch, not-cached entry:
824 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
825 // UpdateCachedResponse.
827 // 12. Prefetch, cached entry:
828 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
829 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
832 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
833 // Skip validation, similar to example 2.
834 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
835 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
836 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
837 // SetupEntryForRead()
842 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
843 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
844 // CacheReadResponse* and CacheDispatchValidation.
845 int HttpCache::Transaction::DoLoop(int result
) {
846 DCHECK(next_state_
!= STATE_NONE
);
850 State state
= next_state_
;
851 next_state_
= STATE_NONE
;
853 case STATE_GET_BACKEND
:
857 case STATE_GET_BACKEND_COMPLETE
:
858 rv
= DoGetBackendComplete(rv
);
860 case STATE_SEND_REQUEST
:
862 rv
= DoSendRequest();
864 case STATE_SEND_REQUEST_COMPLETE
:
865 rv
= DoSendRequestComplete(rv
);
867 case STATE_SUCCESSFUL_SEND_REQUEST
:
869 rv
= DoSuccessfulSendRequest();
871 case STATE_NETWORK_READ
:
873 rv
= DoNetworkRead();
875 case STATE_NETWORK_READ_COMPLETE
:
876 rv
= DoNetworkReadComplete(rv
);
878 case STATE_INIT_ENTRY
:
882 case STATE_OPEN_ENTRY
:
886 case STATE_OPEN_ENTRY_COMPLETE
:
887 rv
= DoOpenEntryComplete(rv
);
889 case STATE_CREATE_ENTRY
:
891 rv
= DoCreateEntry();
893 case STATE_CREATE_ENTRY_COMPLETE
:
894 rv
= DoCreateEntryComplete(rv
);
896 case STATE_DOOM_ENTRY
:
900 case STATE_DOOM_ENTRY_COMPLETE
:
901 rv
= DoDoomEntryComplete(rv
);
903 case STATE_ADD_TO_ENTRY
:
907 case STATE_ADD_TO_ENTRY_COMPLETE
:
908 rv
= DoAddToEntryComplete(rv
);
910 case STATE_START_PARTIAL_CACHE_VALIDATION
:
912 rv
= DoStartPartialCacheValidation();
914 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
:
915 rv
= DoCompletePartialCacheValidation(rv
);
917 case STATE_UPDATE_CACHED_RESPONSE
:
919 rv
= DoUpdateCachedResponse();
921 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE
:
922 rv
= DoUpdateCachedResponseComplete(rv
);
924 case STATE_OVERWRITE_CACHED_RESPONSE
:
926 rv
= DoOverwriteCachedResponse();
928 case STATE_TRUNCATE_CACHED_DATA
:
930 rv
= DoTruncateCachedData();
932 case STATE_TRUNCATE_CACHED_DATA_COMPLETE
:
933 rv
= DoTruncateCachedDataComplete(rv
);
935 case STATE_TRUNCATE_CACHED_METADATA
:
937 rv
= DoTruncateCachedMetadata();
939 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE
:
940 rv
= DoTruncateCachedMetadataComplete(rv
);
942 case STATE_PARTIAL_HEADERS_RECEIVED
:
944 rv
= DoPartialHeadersReceived();
946 case STATE_CACHE_READ_RESPONSE
:
948 rv
= DoCacheReadResponse();
950 case STATE_CACHE_READ_RESPONSE_COMPLETE
:
951 rv
= DoCacheReadResponseComplete(rv
);
953 case STATE_CACHE_DISPATCH_VALIDATION
:
955 rv
= DoCacheDispatchValidation();
957 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH
:
959 rv
= DoCacheToggleUnusedSincePrefetch();
961 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE
:
962 rv
= DoCacheToggleUnusedSincePrefetchComplete(rv
);
964 case STATE_CACHE_WRITE_RESPONSE
:
966 rv
= DoCacheWriteResponse();
968 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
970 rv
= DoCacheWriteTruncatedResponse();
972 case STATE_CACHE_WRITE_RESPONSE_COMPLETE
:
973 rv
= DoCacheWriteResponseComplete(rv
);
975 case STATE_CACHE_READ_METADATA
:
977 rv
= DoCacheReadMetadata();
979 case STATE_CACHE_READ_METADATA_COMPLETE
:
980 rv
= DoCacheReadMetadataComplete(rv
);
982 case STATE_CACHE_QUERY_DATA
:
984 rv
= DoCacheQueryData();
986 case STATE_CACHE_QUERY_DATA_COMPLETE
:
987 rv
= DoCacheQueryDataComplete(rv
);
989 case STATE_CACHE_READ_DATA
:
991 rv
= DoCacheReadData();
993 case STATE_CACHE_READ_DATA_COMPLETE
:
994 rv
= DoCacheReadDataComplete(rv
);
996 case STATE_CACHE_WRITE_DATA
:
997 rv
= DoCacheWriteData(rv
);
999 case STATE_CACHE_WRITE_DATA_COMPLETE
:
1000 rv
= DoCacheWriteDataComplete(rv
);
1003 NOTREACHED() << "bad state";
1007 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
1009 if (rv
!= ERR_IO_PENDING
)
1015 int HttpCache::Transaction::DoGetBackend() {
1016 cache_pending_
= true;
1017 next_state_
= STATE_GET_BACKEND_COMPLETE
;
1018 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
);
1019 return cache_
->GetBackendForTransaction(this);
1022 int HttpCache::Transaction::DoGetBackendComplete(int result
) {
1023 DCHECK(result
== OK
|| result
== ERR_FAILED
);
1024 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
,
1026 cache_pending_
= false;
1028 if (!ShouldPassThrough()) {
1029 cache_key_
= cache_
->GenerateCacheKey(request_
);
1031 // Requested cache access mode.
1032 if (effective_load_flags_
& LOAD_ONLY_FROM_CACHE
) {
1034 } else if (effective_load_flags_
& LOAD_BYPASS_CACHE
) {
1040 // Downgrade to UPDATE if the request has been externally conditionalized.
1041 if (external_validation_
.initialized
) {
1042 if (mode_
& WRITE
) {
1043 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1044 // in case READ was off).
1052 // Use PUT and DELETE only to invalidate existing stored entries.
1053 if ((request_
->method
== "PUT" || request_
->method
== "DELETE") &&
1054 mode_
!= READ_WRITE
&& mode_
!= WRITE
) {
1058 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1059 // transaction behaves the same for GET and HEAD requests at this point: if it
1060 // was not modified, the entry is updated and a response is not returned from
1061 // the cache. If we receive 200, it doesn't matter if there was a validation
1063 if (request_
->method
== "HEAD" && mode_
== WRITE
)
1066 // If must use cache, then we must fail. This can happen for back/forward
1067 // navigations to a page generated via a form post.
1068 if (!(mode_
& READ
) && effective_load_flags_
& LOAD_ONLY_FROM_CACHE
)
1069 return ERR_CACHE_MISS
;
1071 if (mode_
== NONE
) {
1072 if (partial_
.get()) {
1073 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1076 next_state_
= STATE_SEND_REQUEST
;
1078 next_state_
= STATE_INIT_ENTRY
;
1081 // This is only set if we have something to do with the response.
1082 range_requested_
= (partial_
.get() != NULL
);
1087 int HttpCache::Transaction::DoSendRequest() {
1088 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1089 DCHECK(!network_trans_
.get());
1091 send_request_since_
= TimeTicks::Now();
1093 // Create a network transaction.
1094 int rv
= cache_
->network_layer_
->CreateTransaction(priority_
,
1098 network_trans_
->SetBeforeNetworkStartCallback(before_network_start_callback_
);
1099 network_trans_
->SetBeforeProxyHeadersSentCallback(
1100 before_proxy_headers_sent_callback_
);
1102 // Old load timing information, if any, is now obsolete.
1103 old_network_trans_load_timing_
.reset();
1105 if (websocket_handshake_stream_base_create_helper_
)
1106 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(
1107 websocket_handshake_stream_base_create_helper_
);
1109 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1110 rv
= network_trans_
->Start(request_
, io_callback_
, net_log_
);
1114 int HttpCache::Transaction::DoSendRequestComplete(int result
) {
1116 return ERR_UNEXPECTED
;
1118 // If requested, and we have a readable cache entry, and we have
1119 // an error indicating that we're offline as opposed to in contact
1120 // with a bad server, read from cache anyway.
1121 if (IsOfflineError(result
)) {
1122 if (mode_
== READ_WRITE
&& entry_
&& !partial_
) {
1123 RecordOfflineStatus(effective_load_flags_
,
1124 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
);
1125 if (effective_load_flags_
& LOAD_FROM_CACHE_IF_OFFLINE
) {
1126 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1127 response_
.server_data_unavailable
= true;
1128 return SetupEntryForRead();
1131 RecordOfflineStatus(effective_load_flags_
,
1132 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
);
1135 RecordOfflineStatus(effective_load_flags_
,
1136 (result
== OK
? OFFLINE_STATUS_NETWORK_SUCCEEDED
:
1137 OFFLINE_STATUS_NETWORK_FAILED
));
1140 // If we tried to conditionalize the request and failed, we know
1141 // we won't be reading from the cache after this point.
1142 if (couldnt_conditionalize_request_
)
1146 next_state_
= STATE_SUCCESSFUL_SEND_REQUEST
;
1150 // Do not record requests that have network errors or restarts.
1151 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1152 if (IsCertificateError(result
)) {
1153 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
1154 // If we get a certificate error, then there is a certificate in ssl_info,
1155 // so GetResponseInfo() should never return NULL here.
1157 response_
.ssl_info
= response
->ssl_info
;
1158 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
1159 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
1161 response_
.cert_request_info
= response
->cert_request_info
;
1162 } else if (response_
.was_cached
) {
1163 DoneWritingToEntry(true);
1168 // We received the response headers and there is no error.
1169 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1170 DCHECK(!new_response_
);
1171 const HttpResponseInfo
* new_response
= network_trans_
->GetResponseInfo();
1172 bool authentication_failure
= false;
1174 if (new_response
->headers
->response_code() == 401 ||
1175 new_response
->headers
->response_code() == 407) {
1176 auth_response_
= *new_response
;
1180 // We initiated a second request the caller doesn't know about. We should be
1181 // able to authenticate this request because we should have authenticated
1182 // this URL moments ago.
1183 if (IsReadyToRestartForAuth()) {
1184 DCHECK(!response_
.auth_challenge
.get());
1185 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1186 // In theory we should check to see if there are new cookies, but there
1187 // is no way to do that from here.
1188 return network_trans_
->RestartWithAuth(AuthCredentials(), io_callback_
);
1191 // We have to perform cleanup at this point so that at least the next
1192 // request can succeed.
1193 authentication_failure
= true;
1195 DoomPartialEntry(false);
1200 new_response_
= new_response
;
1201 if (authentication_failure
||
1202 (!ValidatePartialResponse() && !auth_response_
.headers
.get())) {
1203 // Something went wrong with this request and we have to restart it.
1204 // If we have an authentication response, we are exposed to weird things
1205 // hapenning if the user cancels the authentication before we receive
1206 // the new response.
1207 net_log_
.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST
);
1208 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1209 response_
= HttpResponseInfo();
1210 ResetNetworkTransaction();
1211 new_response_
= NULL
;
1212 next_state_
= STATE_SEND_REQUEST
;
1216 if (handling_206_
&& mode_
== READ_WRITE
&& !truncated_
&& !is_sparse_
) {
1217 // We have stored the full entry, but it changed and the server is
1218 // sending a range. We have to delete the old entry.
1219 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1220 DoneWritingToEntry(false);
1223 if (mode_
== WRITE
&&
1224 transaction_pattern_
!= PATTERN_ENTRY_CANT_CONDITIONALIZE
) {
1225 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED
);
1228 // Invalidate any cached GET with a successful PUT or DELETE.
1229 if (mode_
== WRITE
&&
1230 (request_
->method
== "PUT" || request_
->method
== "DELETE")) {
1231 if (NonErrorResponse(new_response
->headers
->response_code())) {
1232 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1235 cache_
->DoneWritingToEntry(entry_
, true);
1240 // Invalidate any cached GET with a successful POST.
1241 if (!(effective_load_flags_
& LOAD_DISABLE_CACHE
) &&
1242 request_
->method
== "POST" &&
1243 NonErrorResponse(new_response
->headers
->response_code())) {
1244 cache_
->DoomMainEntryForUrl(request_
->url
);
1247 RecordNoStoreHeaderHistogram(request_
->load_flags
, new_response
);
1249 if (new_response_
->headers
->response_code() == 416 &&
1250 (request_
->method
== "GET" || request_
->method
== "POST")) {
1251 // If there is an active entry it may be destroyed with this transaction.
1252 response_
= *new_response_
;
1256 // Are we expecting a response to a conditional query?
1257 if (mode_
== READ_WRITE
|| mode_
== UPDATE
) {
1258 if (new_response
->headers
->response_code() == 304 || handling_206_
) {
1259 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED
);
1260 next_state_
= STATE_UPDATE_CACHED_RESPONSE
;
1263 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED
);
1267 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1271 int HttpCache::Transaction::DoNetworkRead() {
1272 next_state_
= STATE_NETWORK_READ_COMPLETE
;
1273 return network_trans_
->Read(read_buf_
.get(), io_buf_len_
, io_callback_
);
1276 int HttpCache::Transaction::DoNetworkReadComplete(int result
) {
1277 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1280 return ERR_UNEXPECTED
;
1282 // If there is an error or we aren't saving the data, we are done; just wait
1283 // until the destructor runs to see if we can keep the data.
1284 if (mode_
== NONE
|| result
< 0)
1287 next_state_
= STATE_CACHE_WRITE_DATA
;
1291 int HttpCache::Transaction::DoInitEntry() {
1292 DCHECK(!new_entry_
);
1295 return ERR_UNEXPECTED
;
1297 if (mode_
== WRITE
) {
1298 next_state_
= STATE_DOOM_ENTRY
;
1302 next_state_
= STATE_OPEN_ENTRY
;
1306 int HttpCache::Transaction::DoOpenEntry() {
1307 DCHECK(!new_entry_
);
1308 next_state_
= STATE_OPEN_ENTRY_COMPLETE
;
1309 cache_pending_
= true;
1310 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
);
1311 first_cache_access_since_
= TimeTicks::Now();
1312 return cache_
->OpenEntry(cache_key_
, &new_entry_
, this);
1315 int HttpCache::Transaction::DoOpenEntryComplete(int result
) {
1316 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1317 // OK, otherwise the cache will end up with an active entry without any
1318 // transaction attached.
1319 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
, result
);
1320 cache_pending_
= false;
1322 next_state_
= STATE_ADD_TO_ENTRY
;
1326 if (result
== ERR_CACHE_RACE
) {
1327 next_state_
= STATE_INIT_ENTRY
;
1331 if (request_
->method
== "PUT" || request_
->method
== "DELETE" ||
1332 (request_
->method
== "HEAD" && mode_
== READ_WRITE
)) {
1333 DCHECK(mode_
== READ_WRITE
|| mode_
== WRITE
|| request_
->method
== "HEAD");
1335 next_state_
= STATE_SEND_REQUEST
;
1339 if (mode_
== READ_WRITE
) {
1341 next_state_
= STATE_CREATE_ENTRY
;
1344 if (mode_
== UPDATE
) {
1345 // There is no cache entry to update; proceed without caching.
1347 next_state_
= STATE_SEND_REQUEST
;
1351 // The entry does not exist, and we are not permitted to create a new entry,
1353 return ERR_CACHE_MISS
;
1356 int HttpCache::Transaction::DoCreateEntry() {
1357 DCHECK(!new_entry_
);
1358 next_state_
= STATE_CREATE_ENTRY_COMPLETE
;
1359 cache_pending_
= true;
1360 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
);
1361 return cache_
->CreateEntry(cache_key_
, &new_entry_
, this);
1364 int HttpCache::Transaction::DoCreateEntryComplete(int result
) {
1365 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1366 // OK, otherwise the cache will end up with an active entry without any
1367 // transaction attached.
1368 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
,
1370 cache_pending_
= false;
1371 next_state_
= STATE_ADD_TO_ENTRY
;
1373 if (result
== ERR_CACHE_RACE
) {
1374 next_state_
= STATE_INIT_ENTRY
;
1379 // We have a race here: Maybe we failed to open the entry and decided to
1380 // create one, but by the time we called create, another transaction already
1381 // created the entry. If we want to eliminate this issue, we need an atomic
1382 // OpenOrCreate() method exposed by the disk cache.
1383 DLOG(WARNING
) << "Unable to create cache entry";
1386 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1387 next_state_
= STATE_SEND_REQUEST
;
1392 int HttpCache::Transaction::DoDoomEntry() {
1393 next_state_
= STATE_DOOM_ENTRY_COMPLETE
;
1394 cache_pending_
= true;
1395 if (first_cache_access_since_
.is_null())
1396 first_cache_access_since_
= TimeTicks::Now();
1397 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
);
1398 return cache_
->DoomEntry(cache_key_
, this);
1401 int HttpCache::Transaction::DoDoomEntryComplete(int result
) {
1402 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
, result
);
1403 next_state_
= STATE_CREATE_ENTRY
;
1404 cache_pending_
= false;
1405 if (result
== ERR_CACHE_RACE
)
1406 next_state_
= STATE_INIT_ENTRY
;
1410 int HttpCache::Transaction::DoAddToEntry() {
1412 cache_pending_
= true;
1413 next_state_
= STATE_ADD_TO_ENTRY_COMPLETE
;
1414 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
);
1415 DCHECK(entry_lock_waiting_since_
.is_null());
1416 entry_lock_waiting_since_
= TimeTicks::Now();
1417 int rv
= cache_
->AddTransactionToEntry(new_entry_
, this);
1418 if (rv
== ERR_IO_PENDING
) {
1419 if (bypass_lock_for_test_
) {
1420 OnAddToEntryTimeout(entry_lock_waiting_since_
);
1422 int timeout_milliseconds
= 20 * 1000;
1423 if (partial_
&& new_entry_
->writer
&&
1424 new_entry_
->writer
->range_requested_
) {
1425 // Quickly timeout and bypass the cache if we're a range request and
1426 // we're blocked by the reader/writer lock. Doing so eliminates a long
1427 // running issue, http://crbug.com/31014, where two of the same media
1428 // resources could not be played back simultaneously due to one locking
1429 // the cache entry until the entire video was downloaded.
1431 // Bypassing the cache is not ideal, as we are now ignoring the cache
1432 // entirely for all range requests to a resource beyond the first. This
1433 // is however a much more succinct solution than the alternatives, which
1434 // would require somewhat significant changes to the http caching logic.
1436 // Allow some timeout slack for the entry addition to complete in case
1437 // the writer lock is imminently released; we want to avoid skipping
1438 // the cache if at all possible. See http://crbug.com/408765
1439 timeout_milliseconds
= 25;
1441 base::MessageLoop::current()->PostDelayedTask(
1443 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout
,
1444 weak_factory_
.GetWeakPtr(), entry_lock_waiting_since_
),
1445 TimeDelta::FromMilliseconds(timeout_milliseconds
));
1451 int HttpCache::Transaction::DoAddToEntryComplete(int result
) {
1452 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
,
1454 const TimeDelta entry_lock_wait
=
1455 TimeTicks::Now() - entry_lock_waiting_since_
;
1456 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait
);
1458 entry_lock_waiting_since_
= TimeTicks();
1460 cache_pending_
= false;
1463 entry_
= new_entry_
;
1465 // If there is a failure, the cache should have taken care of new_entry_.
1468 if (result
== ERR_CACHE_RACE
) {
1469 next_state_
= STATE_INIT_ENTRY
;
1473 if (result
== ERR_CACHE_LOCK_TIMEOUT
) {
1474 // The cache is busy, bypass it for this transaction.
1476 next_state_
= STATE_SEND_REQUEST
;
1478 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1489 if (mode_
== WRITE
) {
1491 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1492 next_state_
= STATE_SEND_REQUEST
;
1494 // We have to read the headers from the cached entry.
1495 DCHECK(mode_
& READ_META
);
1496 next_state_
= STATE_CACHE_READ_RESPONSE
;
1501 // We may end up here multiple times for a given request.
1502 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1506 next_state_
= STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
;
1507 return partial_
->ShouldValidateCache(entry_
->disk_entry
, io_callback_
);
1510 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result
) {
1512 // This is the end of the request.
1513 if (mode_
& WRITE
) {
1514 DoneWritingToEntry(true);
1516 cache_
->DoneReadingFromEntry(entry_
, this);
1525 partial_
->PrepareCacheValidation(entry_
->disk_entry
,
1526 &custom_request_
->extra_headers
);
1528 if (reading_
&& partial_
->IsCurrentRangeCached()) {
1529 next_state_
= STATE_CACHE_READ_DATA
;
1533 return BeginCacheValidation();
1536 // We received 304 or 206 and we want to update the cached response headers.
1537 int HttpCache::Transaction::DoUpdateCachedResponse() {
1538 next_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1540 // Update cached response based on headers in new_response.
1541 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1542 response_
.headers
->Update(*new_response_
->headers
.get());
1543 response_
.response_time
= new_response_
->response_time
;
1544 response_
.request_time
= new_response_
->request_time
;
1545 response_
.network_accessed
= new_response_
->network_accessed
;
1546 response_
.unused_since_prefetch
= new_response_
->unused_since_prefetch
;
1547 if (new_response_
->vary_data
.is_valid()) {
1548 response_
.vary_data
= new_response_
->vary_data
;
1549 } else if (response_
.vary_data
.is_valid()) {
1550 // There is a vary header in the stored response but not in the current one.
1551 // Update the data with the new request headers.
1552 HttpVaryData new_vary_data
;
1553 new_vary_data
.Init(*request_
, *response_
.headers
.get());
1554 response_
.vary_data
= new_vary_data
;
1557 if (response_
.headers
->HasHeaderValue("cache-control", "no-store")) {
1558 if (!entry_
->doomed
) {
1559 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1563 // If we are already reading, we already updated the headers for this
1564 // request; doing it again will change Content-Length.
1566 target_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1567 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1574 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result
) {
1575 if (mode_
== UPDATE
) {
1576 DCHECK(!handling_206_
);
1577 // We got a "not modified" response and already updated the corresponding
1578 // cache entry above.
1580 // By closing the cached entry now, we make sure that the 304 rather than
1581 // the cached 200 response, is what will be returned to the user.
1582 DoneWritingToEntry(true);
1583 } else if (entry_
&& !handling_206_
) {
1584 DCHECK_EQ(READ_WRITE
, mode_
);
1585 if (!partial_
.get() || partial_
->IsLastRange()) {
1586 cache_
->ConvertWriterToReader(entry_
);
1589 // We no longer need the network transaction, so destroy it.
1590 final_upload_progress_
= network_trans_
->GetUploadProgress();
1591 ResetNetworkTransaction();
1592 } else if (entry_
&& handling_206_
&& truncated_
&&
1593 partial_
->initial_validation()) {
1594 // We just finished the validation of a truncated entry, and the server
1595 // is willing to resume the operation. Now we go back and start serving
1596 // the first part to the user.
1597 ResetNetworkTransaction();
1598 new_response_
= NULL
;
1599 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
1600 partial_
->SetRangeToStartDownload();
1603 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1607 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1609 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1613 // We change the value of Content-Length for partial content.
1614 if (handling_206_
&& partial_
.get())
1615 partial_
->FixContentLength(new_response_
->headers
.get());
1617 response_
= *new_response_
;
1619 if (request_
->method
== "HEAD") {
1620 // This response is replacing the cached one.
1621 DoneWritingToEntry(false);
1623 new_response_
= NULL
;
1627 if (handling_206_
&& !CanResume(false)) {
1628 // There is no point in storing this resource because it will never be used.
1629 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1630 DoneWritingToEntry(false);
1632 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1633 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1637 target_state_
= STATE_TRUNCATE_CACHED_DATA
;
1638 next_state_
= truncated_
? STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
1639 STATE_CACHE_WRITE_RESPONSE
;
1643 int HttpCache::Transaction::DoTruncateCachedData() {
1644 next_state_
= STATE_TRUNCATE_CACHED_DATA_COMPLETE
;
1647 if (net_log_
.GetCaptureMode().enabled())
1648 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1649 // Truncate the stream.
1650 return WriteToEntry(kResponseContentIndex
, 0, NULL
, 0, io_callback_
);
1653 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result
) {
1655 if (net_log_
.GetCaptureMode().enabled()) {
1656 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1661 next_state_
= STATE_TRUNCATE_CACHED_METADATA
;
1665 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1666 next_state_
= STATE_TRUNCATE_CACHED_METADATA_COMPLETE
;
1670 if (net_log_
.GetCaptureMode().enabled())
1671 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1672 return WriteToEntry(kMetadataIndex
, 0, NULL
, 0, io_callback_
);
1675 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result
) {
1677 if (net_log_
.GetCaptureMode().enabled()) {
1678 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1683 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1687 int HttpCache::Transaction::DoPartialHeadersReceived() {
1688 new_response_
= NULL
;
1689 if (entry_
&& !partial_
.get() &&
1690 entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
1691 next_state_
= STATE_CACHE_READ_METADATA
;
1693 if (!partial_
.get())
1697 if (network_trans_
.get()) {
1698 next_state_
= STATE_NETWORK_READ
;
1700 next_state_
= STATE_CACHE_READ_DATA
;
1702 } else if (mode_
!= NONE
) {
1703 // We are about to return the headers for a byte-range request to the user,
1704 // so let's fix them.
1705 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1710 int HttpCache::Transaction::DoCacheReadResponse() {
1712 next_state_
= STATE_CACHE_READ_RESPONSE_COMPLETE
;
1714 io_buf_len_
= entry_
->disk_entry
->GetDataSize(kResponseInfoIndex
);
1715 read_buf_
= new IOBuffer(io_buf_len_
);
1717 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1718 return entry_
->disk_entry
->ReadData(kResponseInfoIndex
, 0, read_buf_
.get(),
1719 io_buf_len_
, io_callback_
);
1722 int HttpCache::Transaction::DoCacheReadResponseComplete(int result
) {
1723 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1724 if (result
!= io_buf_len_
||
1725 !HttpCache::ParseResponseInfo(read_buf_
->data(), io_buf_len_
,
1726 &response_
, &truncated_
)) {
1727 return OnCacheReadError(result
, true);
1730 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1731 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
1734 // Some resources may have slipped in as truncated when they're not.
1735 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1736 if (response_
.headers
->GetContentLength() == current_size
)
1739 if ((response_
.unused_since_prefetch
&&
1740 !(request_
->load_flags
& LOAD_PREFETCH
)) ||
1741 (!response_
.unused_since_prefetch
&&
1742 (request_
->load_flags
& LOAD_PREFETCH
))) {
1743 // Either this is the first use of an entry since it was prefetched or
1744 // this is a prefetch. The value of response.unused_since_prefetch is valid
1745 // for this transaction but the bit needs to be flipped in storage.
1746 next_state_
= STATE_TOGGLE_UNUSED_SINCE_PREFETCH
;
1750 next_state_
= STATE_CACHE_DISPATCH_VALIDATION
;
1754 int HttpCache::Transaction::DoCacheDispatchValidation() {
1755 // We now have access to the cache entry.
1757 // o if we are a reader for the transaction, then we can start reading the
1760 // o if we can read or write, then we should check if the cache entry needs
1761 // to be validated and then issue a network request if needed or just read
1762 // from the cache if the cache entry is already valid.
1764 // o if we are set to UPDATE, then we are handling an externally
1765 // conditionalized request (if-modified-since / if-none-match). We check
1766 // if the request headers define a validation request.
1768 int result
= ERR_FAILED
;
1771 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
1772 result
= BeginCacheRead();
1775 result
= BeginPartialCacheValidation();
1778 result
= BeginExternallyConditionalizedRequest();
1787 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1788 // Write back the toggled value for the next use of this entry.
1789 response_
.unused_since_prefetch
= !response_
.unused_since_prefetch
;
1791 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1792 // transaction then metadata will be written to cache twice. If prefetching
1793 // becomes more common, consider combining the writes.
1794 target_state_
= STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE
;
1795 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1799 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1801 // Restore the original value for this transaction.
1802 response_
.unused_since_prefetch
= !response_
.unused_since_prefetch
;
1803 next_state_
= STATE_CACHE_DISPATCH_VALIDATION
;
1807 int HttpCache::Transaction::DoCacheWriteResponse() {
1808 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1809 tracked_objects::ScopedTracker
tracking_profile(
1810 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1811 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1814 if (net_log_
.GetCaptureMode().enabled())
1815 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1817 return WriteResponseInfoToEntry(false);
1820 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1822 if (net_log_
.GetCaptureMode().enabled())
1823 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1825 return WriteResponseInfoToEntry(true);
1828 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result
) {
1829 next_state_
= target_state_
;
1830 target_state_
= STATE_NONE
;
1833 if (net_log_
.GetCaptureMode().enabled()) {
1834 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1838 // Balance the AddRef from WriteResponseInfoToEntry.
1839 if (result
!= io_buf_len_
) {
1840 DLOG(ERROR
) << "failed to write response info to cache";
1841 DoneWritingToEntry(false);
1846 int HttpCache::Transaction::DoCacheReadMetadata() {
1848 DCHECK(!response_
.metadata
.get());
1849 next_state_
= STATE_CACHE_READ_METADATA_COMPLETE
;
1851 response_
.metadata
=
1852 new IOBufferWithSize(entry_
->disk_entry
->GetDataSize(kMetadataIndex
));
1854 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1855 return entry_
->disk_entry
->ReadData(kMetadataIndex
, 0,
1856 response_
.metadata
.get(),
1857 response_
.metadata
->size(),
1861 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result
) {
1862 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1863 if (result
!= response_
.metadata
->size())
1864 return OnCacheReadError(result
, false);
1868 int HttpCache::Transaction::DoCacheQueryData() {
1869 next_state_
= STATE_CACHE_QUERY_DATA_COMPLETE
;
1870 return entry_
->disk_entry
->ReadyForSparseIO(io_callback_
);
1873 int HttpCache::Transaction::DoCacheQueryDataComplete(int result
) {
1874 DCHECK_EQ(OK
, result
);
1876 return ERR_UNEXPECTED
;
1878 return ValidateEntryHeadersAndContinue();
1881 int HttpCache::Transaction::DoCacheReadData() {
1883 next_state_
= STATE_CACHE_READ_DATA_COMPLETE
;
1885 if (net_log_
.GetCaptureMode().enabled())
1886 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA
);
1887 if (partial_
.get()) {
1888 return partial_
->CacheRead(entry_
->disk_entry
, read_buf_
.get(), io_buf_len_
,
1892 return entry_
->disk_entry
->ReadData(kResponseContentIndex
, read_offset_
,
1893 read_buf_
.get(), io_buf_len_
,
1897 int HttpCache::Transaction::DoCacheReadDataComplete(int result
) {
1898 if (net_log_
.GetCaptureMode().enabled()) {
1899 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA
,
1904 return ERR_UNEXPECTED
;
1906 if (partial_
.get()) {
1907 // Partial requests are confusing to report in histograms because they may
1908 // have multiple underlying requests.
1909 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1910 return DoPartialCacheReadCompleted(result
);
1914 read_offset_
+= result
;
1915 } else if (result
== 0) { // End of file.
1917 cache_
->DoneReadingFromEntry(entry_
, this);
1920 return OnCacheReadError(result
, false);
1925 int HttpCache::Transaction::DoCacheWriteData(int num_bytes
) {
1926 next_state_
= STATE_CACHE_WRITE_DATA_COMPLETE
;
1927 write_len_
= num_bytes
;
1929 if (net_log_
.GetCaptureMode().enabled())
1930 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1933 return AppendResponseDataToEntry(read_buf_
.get(), num_bytes
, io_callback_
);
1936 int HttpCache::Transaction::DoCacheWriteDataComplete(int result
) {
1938 if (net_log_
.GetCaptureMode().enabled()) {
1939 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1943 // Balance the AddRef from DoCacheWriteData.
1945 return ERR_UNEXPECTED
;
1947 if (result
!= write_len_
) {
1948 DLOG(ERROR
) << "failed to write response data to cache";
1949 DoneWritingToEntry(false);
1951 // We want to ignore errors writing to disk and just keep reading from
1953 result
= write_len_
;
1954 } else if (!done_reading_
&& entry_
) {
1955 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1956 int64 body_size
= response_
.headers
->GetContentLength();
1957 if (body_size
>= 0 && body_size
<= current_size
)
1958 done_reading_
= true;
1961 if (partial_
.get()) {
1962 // This may be the last request.
1963 if (!(result
== 0 && !truncated_
&&
1964 (partial_
->IsLastRange() || mode_
== WRITE
)))
1965 return DoPartialNetworkReadCompleted(result
);
1969 // End of file. This may be the result of a connection problem so see if we
1970 // have to keep the entry around to be flagged as truncated later on.
1971 if (done_reading_
|| !entry_
|| partial_
.get() ||
1972 response_
.headers
->GetContentLength() <= 0)
1973 DoneWritingToEntry(true);
1979 //-----------------------------------------------------------------------------
1981 void HttpCache::Transaction::ReadCertChain() {
1983 GetCacheKeyForCert(response_
.ssl_info
.cert
->os_cert_handle());
1984 const X509Certificate::OSCertHandles
& intermediates
=
1985 response_
.ssl_info
.cert
->GetIntermediateCertificates();
1986 int dist_from_root
= intermediates
.size();
1988 scoped_refptr
<SharedChainData
> shared_chain_data(
1989 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
1990 cache_
->cert_cache()->GetCertificate(key
,
1991 base::Bind(&OnCertReadIOComplete
,
1994 shared_chain_data
));
1996 for (X509Certificate::OSCertHandles::const_iterator it
=
1997 intermediates
.begin();
1998 it
!= intermediates
.end();
2001 key
= GetCacheKeyForCert(*it
);
2002 cache_
->cert_cache()->GetCertificate(key
,
2003 base::Bind(&OnCertReadIOComplete
,
2005 false /* is not leaf */,
2006 shared_chain_data
));
2008 DCHECK_EQ(0, dist_from_root
);
2011 void HttpCache::Transaction::WriteCertChain() {
2012 const X509Certificate::OSCertHandles
& intermediates
=
2013 response_
.ssl_info
.cert
->GetIntermediateCertificates();
2014 int dist_from_root
= intermediates
.size();
2016 scoped_refptr
<SharedChainData
> shared_chain_data(
2017 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
2018 cache_
->cert_cache()->SetCertificate(
2019 response_
.ssl_info
.cert
->os_cert_handle(),
2020 base::Bind(&OnCertWriteIOComplete
,
2023 shared_chain_data
));
2024 for (X509Certificate::OSCertHandles::const_iterator it
=
2025 intermediates
.begin();
2026 it
!= intermediates
.end();
2029 cache_
->cert_cache()->SetCertificate(*it
,
2030 base::Bind(&OnCertWriteIOComplete
,
2032 false /* is not leaf */,
2033 shared_chain_data
));
2035 DCHECK_EQ(0, dist_from_root
);
2038 void HttpCache::Transaction::SetRequest(const BoundNetLog
& net_log
,
2039 const HttpRequestInfo
* request
) {
2042 effective_load_flags_
= request_
->load_flags
;
2044 if (cache_
->mode() == DISABLE
)
2045 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2047 // Some headers imply load flags. The order here is significant.
2049 // LOAD_DISABLE_CACHE : no cache read or write
2050 // LOAD_BYPASS_CACHE : no cache read
2051 // LOAD_VALIDATE_CACHE : no cache read unless validation
2053 // The former modes trump latter modes, so if we find a matching header we
2054 // can stop iterating kSpecialHeaders.
2056 static const struct {
2057 const HeaderNameAndValue
* search
;
2059 } kSpecialHeaders
[] = {
2060 { kPassThroughHeaders
, LOAD_DISABLE_CACHE
},
2061 { kForceFetchHeaders
, LOAD_BYPASS_CACHE
},
2062 { kForceValidateHeaders
, LOAD_VALIDATE_CACHE
},
2065 bool range_found
= false;
2066 bool external_validation_error
= false;
2067 bool special_headers
= false;
2069 if (request_
->extra_headers
.HasHeader(HttpRequestHeaders::kRange
))
2072 for (size_t i
= 0; i
< arraysize(kSpecialHeaders
); ++i
) {
2073 if (HeaderMatches(request_
->extra_headers
, kSpecialHeaders
[i
].search
)) {
2074 effective_load_flags_
|= kSpecialHeaders
[i
].load_flag
;
2075 special_headers
= true;
2080 // Check for conditionalization headers which may correspond with a
2081 // cache validation request.
2082 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); ++i
) {
2083 const ValidationHeaderInfo
& info
= kValidationHeaders
[i
];
2084 std::string validation_value
;
2085 if (request_
->extra_headers
.GetHeader(
2086 info
.request_header_name
, &validation_value
)) {
2087 if (!external_validation_
.values
[i
].empty() ||
2088 validation_value
.empty()) {
2089 external_validation_error
= true;
2091 external_validation_
.values
[i
] = validation_value
;
2092 external_validation_
.initialized
= true;
2096 if (range_found
|| special_headers
|| external_validation_
.initialized
) {
2097 // Log the headers before request_ is modified.
2100 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS
,
2101 base::Bind(&HttpRequestHeaders::NetLogCallback
,
2102 base::Unretained(&request_
->extra_headers
), &empty
));
2105 // We don't support ranges and validation headers.
2106 if (range_found
&& external_validation_
.initialized
) {
2107 LOG(WARNING
) << "Byte ranges AND validation headers found.";
2108 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2111 // If there is more than one validation header, we can't treat this request as
2112 // a cache validation, since we don't know for sure which header the server
2113 // will give us a response for (and they could be contradictory).
2114 if (external_validation_error
) {
2115 LOG(WARNING
) << "Multiple or malformed validation headers found.";
2116 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2119 if (range_found
&& !(effective_load_flags_
& LOAD_DISABLE_CACHE
)) {
2120 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2121 partial_
.reset(new PartialData
);
2122 if (request_
->method
== "GET" && partial_
->Init(request_
->extra_headers
)) {
2123 // We will be modifying the actual range requested to the server, so
2124 // let's remove the header here.
2125 custom_request_
.reset(new HttpRequestInfo(*request_
));
2126 custom_request_
->extra_headers
.RemoveHeader(HttpRequestHeaders::kRange
);
2127 request_
= custom_request_
.get();
2128 partial_
->SetHeaders(custom_request_
->extra_headers
);
2130 // The range is invalid or we cannot handle it properly.
2131 VLOG(1) << "Invalid byte range found.";
2132 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2133 partial_
.reset(NULL
);
2138 bool HttpCache::Transaction::ShouldPassThrough() {
2139 // We may have a null disk_cache if there is an error we cannot recover from,
2140 // like not enough disk space, or sharing violations.
2141 if (!cache_
->disk_cache_
.get())
2144 if (effective_load_flags_
& LOAD_DISABLE_CACHE
)
2147 if (request_
->method
== "GET" || request_
->method
== "HEAD")
2150 if (request_
->method
== "POST" && request_
->upload_data_stream
&&
2151 request_
->upload_data_stream
->identifier()) {
2155 if (request_
->method
== "PUT" && request_
->upload_data_stream
)
2158 if (request_
->method
== "DELETE")
2164 int HttpCache::Transaction::BeginCacheRead() {
2165 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2166 if (response_
.headers
->response_code() == 206 || partial_
.get()) {
2168 return ERR_CACHE_MISS
;
2171 if (request_
->method
== "HEAD")
2172 FixHeadersForHead();
2174 // We don't have the whole resource.
2176 return ERR_CACHE_MISS
;
2178 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2179 next_state_
= STATE_CACHE_READ_METADATA
;
2184 int HttpCache::Transaction::BeginCacheValidation() {
2185 DCHECK(mode_
== READ_WRITE
);
2187 ValidationType required_validation
= RequiresValidation();
2189 bool skip_validation
= (required_validation
== VALIDATION_NONE
);
2191 if (required_validation
== VALIDATION_ASYNCHRONOUS
&&
2192 !(request_
->method
== "GET" && (truncated_
|| partial_
)) && cache_
&&
2193 cache_
->use_stale_while_revalidate()) {
2194 TriggerAsyncValidation();
2195 skip_validation
= true;
2198 if (request_
->method
== "HEAD" &&
2199 (truncated_
|| response_
.headers
->response_code() == 206)) {
2201 if (skip_validation
)
2202 return SetupEntryForRead();
2205 next_state_
= STATE_SEND_REQUEST
;
2211 // Truncated entries can cause partial gets, so we shouldn't record this
2212 // load in histograms.
2213 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2214 skip_validation
= !partial_
->initial_validation();
2217 if (partial_
.get() && (is_sparse_
|| truncated_
) &&
2218 (!partial_
->IsCurrentRangeCached() || invalid_range_
)) {
2219 // Force revalidation for sparse or truncated entries. Note that we don't
2220 // want to ignore the regular validation logic just because a byte range was
2221 // part of the request.
2222 skip_validation
= false;
2225 if (skip_validation
) {
2226 // TODO(ricea): Is this pattern okay for asynchronous revalidations?
2227 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
2228 RecordOfflineStatus(effective_load_flags_
, OFFLINE_STATUS_FRESH_CACHE
);
2229 return SetupEntryForRead();
2231 // Make the network request conditional, to see if we may reuse our cached
2232 // response. If we cannot do so, then we just resort to a normal fetch.
2233 // Our mode remains READ_WRITE for a conditional request. Even if the
2234 // conditionalization fails, we don't switch to WRITE mode until we
2235 // know we won't be falling back to using the cache entry in the
2236 // LOAD_FROM_CACHE_IF_OFFLINE case.
2237 if (!ConditionalizeRequest()) {
2238 couldnt_conditionalize_request_
= true;
2239 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE
);
2241 return DoRestartPartialRequest();
2243 DCHECK_NE(206, response_
.headers
->response_code());
2245 next_state_
= STATE_SEND_REQUEST
;
2250 int HttpCache::Transaction::BeginPartialCacheValidation() {
2251 DCHECK(mode_
== READ_WRITE
);
2253 if (response_
.headers
->response_code() != 206 && !partial_
.get() &&
2255 return BeginCacheValidation();
2258 // Partial requests should not be recorded in histograms.
2259 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2260 if (range_requested_
) {
2261 next_state_
= STATE_CACHE_QUERY_DATA
;
2265 // The request is not for a range, but we have stored just ranges.
2267 if (request_
->method
== "HEAD")
2268 return BeginCacheValidation();
2270 partial_
.reset(new PartialData());
2271 partial_
->SetHeaders(request_
->extra_headers
);
2272 if (!custom_request_
.get()) {
2273 custom_request_
.reset(new HttpRequestInfo(*request_
));
2274 request_
= custom_request_
.get();
2277 return ValidateEntryHeadersAndContinue();
2280 // This should only be called once per request.
2281 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2282 DCHECK(mode_
== READ_WRITE
);
2284 if (!partial_
->UpdateFromStoredHeaders(
2285 response_
.headers
.get(), entry_
->disk_entry
, truncated_
)) {
2286 return DoRestartPartialRequest();
2289 if (response_
.headers
->response_code() == 206)
2292 if (!partial_
->IsRequestedRangeOK()) {
2293 // The stored data is fine, but the request may be invalid.
2294 invalid_range_
= true;
2297 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2301 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2302 DCHECK_EQ(UPDATE
, mode_
);
2303 DCHECK(external_validation_
.initialized
);
2305 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); i
++) {
2306 if (external_validation_
.values
[i
].empty())
2308 // Retrieve either the cached response's "etag" or "last-modified" header.
2309 std::string validator
;
2310 response_
.headers
->EnumerateHeader(
2312 kValidationHeaders
[i
].related_response_header_name
,
2315 if (response_
.headers
->response_code() != 200 || truncated_
||
2316 validator
.empty() || validator
!= external_validation_
.values
[i
]) {
2317 // The externally conditionalized request is not a validation request
2318 // for our existing cache entry. Proceed with caching disabled.
2319 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2320 DoneWritingToEntry(true);
2324 // TODO(ricea): This calculation is expensive to perform just to collect
2325 // statistics. Either remove it or use the result, depending on the result of
2327 ExternallyConditionalizedType type
=
2328 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE
;
2330 type
= EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS
;
2331 else if (RequiresValidation())
2332 type
= EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION
;
2334 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2335 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2336 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2338 EXTERNALLY_CONDITIONALIZED_MAX
);
2340 next_state_
= STATE_SEND_REQUEST
;
2344 int HttpCache::Transaction::RestartNetworkRequest() {
2345 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2346 DCHECK(network_trans_
.get());
2347 DCHECK_EQ(STATE_NONE
, next_state_
);
2349 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2350 int rv
= network_trans_
->RestartIgnoringLastError(io_callback_
);
2351 if (rv
!= ERR_IO_PENDING
)
2356 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2357 X509Certificate
* client_cert
) {
2358 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2359 DCHECK(network_trans_
.get());
2360 DCHECK_EQ(STATE_NONE
, next_state_
);
2362 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2363 int rv
= network_trans_
->RestartWithCertificate(client_cert
, io_callback_
);
2364 if (rv
!= ERR_IO_PENDING
)
2369 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2370 const AuthCredentials
& credentials
) {
2371 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2372 DCHECK(network_trans_
.get());
2373 DCHECK_EQ(STATE_NONE
, next_state_
);
2375 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2376 int rv
= network_trans_
->RestartWithAuth(credentials
, io_callback_
);
2377 if (rv
!= ERR_IO_PENDING
)
2382 ValidationType
HttpCache::Transaction::RequiresValidation() {
2383 // TODO(darin): need to do more work here:
2384 // - make sure we have a matching request method
2385 // - watch out for cached responses that depend on authentication
2387 if (response_
.vary_data
.is_valid() &&
2388 !response_
.vary_data
.MatchesRequest(*request_
,
2389 *response_
.headers
.get())) {
2390 vary_mismatch_
= true;
2391 return VALIDATION_SYNCHRONOUS
;
2394 if (effective_load_flags_
& LOAD_PREFERRING_CACHE
)
2395 return VALIDATION_NONE
;
2397 if (response_
.unused_since_prefetch
&&
2398 !(effective_load_flags_
& LOAD_PREFETCH
) &&
2399 response_
.headers
->GetCurrentAge(
2400 response_
.request_time
, response_
.response_time
,
2401 cache_
->clock_
->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins
)) {
2402 // The first use of a resource after prefetch within a short window skips
2404 return VALIDATION_NONE
;
2407 if (effective_load_flags_
& (LOAD_VALIDATE_CACHE
| LOAD_ASYNC_REVALIDATION
))
2408 return VALIDATION_SYNCHRONOUS
;
2410 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2411 return VALIDATION_SYNCHRONOUS
;
2413 ValidationType validation_required_by_headers
=
2414 response_
.headers
->RequiresValidation(response_
.request_time
,
2415 response_
.response_time
,
2416 cache_
->clock_
->Now());
2418 if (validation_required_by_headers
== VALIDATION_ASYNCHRONOUS
) {
2419 // Asynchronous revalidation is only supported for GET and HEAD methods.
2420 if (request_
->method
!= "GET" && request_
->method
!= "HEAD")
2421 return VALIDATION_SYNCHRONOUS
;
2424 return validation_required_by_headers
;
2427 bool HttpCache::Transaction::ConditionalizeRequest() {
2428 DCHECK(response_
.headers
.get());
2430 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2433 // This only makes sense for cached 200 or 206 responses.
2434 if (response_
.headers
->response_code() != 200 &&
2435 response_
.headers
->response_code() != 206) {
2439 if (fail_conditionalization_for_test_
)
2442 DCHECK(response_
.headers
->response_code() != 206 ||
2443 response_
.headers
->HasStrongValidators());
2445 // Just use the first available ETag and/or Last-Modified header value.
2446 // TODO(darin): Or should we use the last?
2448 std::string etag_value
;
2449 if (response_
.headers
->GetHttpVersion() >= HttpVersion(1, 1))
2450 response_
.headers
->EnumerateHeader(NULL
, "etag", &etag_value
);
2452 std::string last_modified_value
;
2453 if (!vary_mismatch_
) {
2454 response_
.headers
->EnumerateHeader(NULL
, "last-modified",
2455 &last_modified_value
);
2458 if (etag_value
.empty() && last_modified_value
.empty())
2461 if (!partial_
.get()) {
2462 // Need to customize the request, so this forces us to allocate :(
2463 custom_request_
.reset(new HttpRequestInfo(*request_
));
2464 request_
= custom_request_
.get();
2466 DCHECK(custom_request_
.get());
2468 bool use_if_range
= partial_
.get() && !partial_
->IsCurrentRangeCached() &&
2471 if (!use_if_range
) {
2472 // stale-while-revalidate is not useful when we only have a partial response
2473 // cached, so don't set the header in that case.
2474 HttpResponseHeaders::FreshnessLifetimes lifetimes
=
2475 response_
.headers
->GetFreshnessLifetimes(response_
.response_time
);
2476 if (lifetimes
.staleness
> TimeDelta()) {
2477 TimeDelta current_age
= response_
.headers
->GetCurrentAge(
2478 response_
.request_time
, response_
.response_time
,
2479 cache_
->clock_
->Now());
2481 custom_request_
->extra_headers
.SetHeader(
2483 base::StringPrintf("max-age=%" PRId64
2484 ",stale-while-revalidate=%" PRId64
",age=%" PRId64
,
2485 lifetimes
.freshness
.InSeconds(),
2486 lifetimes
.staleness
.InSeconds(),
2487 current_age
.InSeconds()));
2491 if (!etag_value
.empty()) {
2493 // We don't want to switch to WRITE mode if we don't have this block of a
2494 // byte-range request because we may have other parts cached.
2495 custom_request_
->extra_headers
.SetHeader(
2496 HttpRequestHeaders::kIfRange
, etag_value
);
2498 custom_request_
->extra_headers
.SetHeader(
2499 HttpRequestHeaders::kIfNoneMatch
, etag_value
);
2501 // For byte-range requests, make sure that we use only one way to validate
2503 if (partial_
.get() && !partial_
->IsCurrentRangeCached())
2507 if (!last_modified_value
.empty()) {
2509 custom_request_
->extra_headers
.SetHeader(
2510 HttpRequestHeaders::kIfRange
, last_modified_value
);
2512 custom_request_
->extra_headers
.SetHeader(
2513 HttpRequestHeaders::kIfModifiedSince
, last_modified_value
);
2520 // We just received some headers from the server. We may have asked for a range,
2521 // in which case partial_ has an object. This could be the first network request
2522 // we make to fulfill the original request, or we may be already reading (from
2523 // the net and / or the cache). If we are not expecting a certain response, we
2524 // just bypass the cache for this request (but again, maybe we are reading), and
2525 // delete partial_ (so we are not able to "fix" the headers that we return to
2526 // the user). This results in either a weird response for the caller (we don't
2527 // expect it after all), or maybe a range that was not exactly what it was asked
2530 // If the server is simply telling us that the resource has changed, we delete
2531 // the cached entry and restart the request as the caller intended (by returning
2532 // false from this method). However, we may not be able to do that at any point,
2533 // for instance if we already returned the headers to the user.
2535 // WARNING: Whenever this code returns false, it has to make sure that the next
2536 // time it is called it will return true so that we don't keep retrying the
2538 bool HttpCache::Transaction::ValidatePartialResponse() {
2539 const HttpResponseHeaders
* headers
= new_response_
->headers
.get();
2540 int response_code
= headers
->response_code();
2541 bool partial_response
= (response_code
== 206);
2542 handling_206_
= false;
2544 if (!entry_
|| request_
->method
!= "GET")
2547 if (invalid_range_
) {
2548 // We gave up trying to match this request with the stored data. If the
2549 // server is ok with the request, delete the entry, otherwise just ignore
2552 if (partial_response
|| response_code
== 200) {
2553 DoomPartialEntry(true);
2556 if (response_code
== 304)
2558 IgnoreRangeRequest();
2563 if (!partial_
.get()) {
2564 // We are not expecting 206 but we may have one.
2565 if (partial_response
)
2566 IgnoreRangeRequest();
2571 // TODO(rvargas): Do we need to consider other results here?.
2572 bool failure
= response_code
== 200 || response_code
== 416;
2574 if (partial_
->IsCurrentRangeCached()) {
2575 // We asked for "If-None-Match: " so a 206 means a new object.
2576 if (partial_response
)
2579 if (response_code
== 304 && partial_
->ResponseHeadersOK(headers
))
2582 // We asked for "If-Range: " so a 206 means just another range.
2583 if (partial_response
) {
2584 if (partial_
->ResponseHeadersOK(headers
)) {
2585 handling_206_
= true;
2592 if (!reading_
&& !is_sparse_
&& !partial_response
) {
2593 // See if we can ignore the fact that we issued a byte range request.
2594 // If the server sends 200, just store it. If it sends an error, redirect
2595 // or something else, we may store the response as long as we didn't have
2596 // anything already stored.
2597 if (response_code
== 200 ||
2598 (!truncated_
&& response_code
!= 304 && response_code
!= 416)) {
2599 // The server is sending something else, and we can save it.
2600 DCHECK((truncated_
&& !partial_
->IsLastRange()) || range_requested_
);
2607 // 304 is not expected here, but we'll spare the entry (unless it was
2614 // We cannot truncate this entry, it has to be deleted.
2615 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2617 if (is_sparse_
|| truncated_
) {
2618 // There was something cached to start with, either sparsed data (206), or
2619 // a truncated 200, which means that we probably modified the request,
2620 // adding a byte range or modifying the range requested by the caller.
2621 if (!reading_
&& !partial_
->IsLastRange()) {
2622 // We have not returned anything to the caller yet so it should be safe
2623 // to issue another network request, this time without us messing up the
2625 ResetPartialState(true);
2628 LOG(WARNING
) << "Failed to revalidate partial entry";
2630 DoomPartialEntry(true);
2634 IgnoreRangeRequest();
2638 void HttpCache::Transaction::IgnoreRangeRequest() {
2639 // We have a problem. We may or may not be reading already (in which case we
2640 // returned the headers), but we'll just pretend that this request is not
2641 // using the cache and see what happens. Most likely this is the first
2642 // response from the server (it's not changing its mind midway, right?).
2643 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2645 DoneWritingToEntry(mode_
!= WRITE
);
2646 else if (mode_
& READ
&& entry_
)
2647 cache_
->DoneReadingFromEntry(entry_
, this);
2649 partial_
.reset(NULL
);
2654 void HttpCache::Transaction::FixHeadersForHead() {
2655 if (response_
.headers
->response_code() == 206) {
2656 response_
.headers
->RemoveHeader("Content-Range");
2657 response_
.headers
->ReplaceStatusLine("HTTP/1.1 200 OK");
2661 void HttpCache::Transaction::TriggerAsyncValidation() {
2662 DCHECK(!request_
->upload_data_stream
);
2663 BoundNetLog
async_revalidation_net_log(
2664 BoundNetLog::Make(net_log_
.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION
));
2666 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC
,
2667 async_revalidation_net_log
.source().ToEventParametersCallback());
2668 async_revalidation_net_log
.BeginEvent(
2669 NetLog::TYPE_ASYNC_REVALIDATION
,
2671 &NetLogAsyncRevalidationInfoCallback
, net_log_
.source(), request_
));
2672 base::MessageLoop::current()->PostTask(
2674 base::Bind(&HttpCache::PerformAsyncValidation
,
2675 cache_
, // cache_ is a weak pointer.
2677 async_revalidation_net_log
));
2680 void HttpCache::Transaction::FailRangeRequest() {
2681 response_
= *new_response_
;
2682 partial_
->FixResponseHeaders(response_
.headers
.get(), false);
2685 int HttpCache::Transaction::SetupEntryForRead() {
2687 ResetNetworkTransaction();
2688 if (partial_
.get()) {
2689 if (truncated_
|| is_sparse_
|| !invalid_range_
) {
2690 // We are going to return the saved response headers to the caller, so
2691 // we may need to adjust them first.
2692 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
2698 cache_
->ConvertWriterToReader(entry_
);
2701 if (request_
->method
== "HEAD")
2702 FixHeadersForHead();
2704 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2705 next_state_
= STATE_CACHE_READ_METADATA
;
2710 int HttpCache::Transaction::ReadFromNetwork(IOBuffer
* data
, int data_len
) {
2712 io_buf_len_
= data_len
;
2713 next_state_
= STATE_NETWORK_READ
;
2717 int HttpCache::Transaction::ReadFromEntry(IOBuffer
* data
, int data_len
) {
2718 if (request_
->method
== "HEAD")
2722 io_buf_len_
= data_len
;
2723 next_state_
= STATE_CACHE_READ_DATA
;
2727 int HttpCache::Transaction::WriteToEntry(int index
, int offset
,
2728 IOBuffer
* data
, int data_len
,
2729 const CompletionCallback
& callback
) {
2734 if (!partial_
.get() || !data_len
) {
2735 rv
= entry_
->disk_entry
->WriteData(index
, offset
, data
, data_len
, callback
,
2738 rv
= partial_
->CacheWrite(entry_
->disk_entry
, data
, data_len
, callback
);
2743 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated
) {
2744 next_state_
= STATE_CACHE_WRITE_RESPONSE_COMPLETE
;
2748 // Do not cache no-store content. Do not cache content with cert errors
2749 // either. This is to prevent not reporting net errors when loading a
2750 // resource from the cache. When we load a page over HTTPS with a cert error
2751 // we show an SSL blocking page. If the user clicks proceed we reload the
2752 // resource ignoring the errors. The loaded resource is then cached. If that
2753 // resource is subsequently loaded from the cache, no net error is reported
2754 // (even though the cert status contains the actual errors) and no SSL
2755 // blocking page is shown. An alternative would be to reverse-map the cert
2756 // status to a net error and replay the net error.
2757 if ((response_
.headers
->HasHeaderValue("cache-control", "no-store")) ||
2758 IsCertStatusError(response_
.ssl_info
.cert_status
)) {
2759 DoneWritingToEntry(false);
2760 if (net_log_
.GetCaptureMode().enabled())
2761 net_log_
.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
2765 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2766 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
2770 DCHECK_EQ(200, response_
.headers
->response_code());
2772 // When writing headers, we normally only write the non-transient headers.
2773 bool skip_transient_headers
= true;
2774 scoped_refptr
<PickledIOBuffer
> data(new PickledIOBuffer());
2775 response_
.Persist(data
->pickle(), skip_transient_headers
, truncated
);
2778 io_buf_len_
= data
->pickle()->size();
2779 return entry_
->disk_entry
->WriteData(kResponseInfoIndex
, 0, data
.get(),
2780 io_buf_len_
, io_callback_
, true);
2783 int HttpCache::Transaction::AppendResponseDataToEntry(
2784 IOBuffer
* data
, int data_len
, const CompletionCallback
& callback
) {
2785 if (!entry_
|| !data_len
)
2788 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
2789 return WriteToEntry(kResponseContentIndex
, current_size
, data
, data_len
,
2793 void HttpCache::Transaction::DoneWritingToEntry(bool success
) {
2799 cache_
->DoneWritingToEntry(entry_
, success
);
2801 mode_
= NONE
; // switch to 'pass through' mode
2804 int HttpCache::Transaction::OnCacheReadError(int result
, bool restart
) {
2805 DLOG(ERROR
) << "ReadData failed: " << result
;
2806 const int result_for_histogram
= std::max(0, -result
);
2808 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2809 result_for_histogram
);
2811 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2812 result_for_histogram
);
2815 // Avoid using this entry in the future.
2817 cache_
->DoomActiveEntry(cache_key_
);
2821 DCHECK(!network_trans_
.get());
2822 cache_
->DoneWithEntry(entry_
, this, false);
2826 next_state_
= STATE_GET_BACKEND
;
2830 return ERR_CACHE_READ_FAILURE
;
2833 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time
) {
2834 if (entry_lock_waiting_since_
!= start_time
)
2837 DCHECK_EQ(next_state_
, STATE_ADD_TO_ENTRY_COMPLETE
);
2842 cache_
->RemovePendingTransaction(this);
2843 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT
);
2846 void HttpCache::Transaction::DoomPartialEntry(bool delete_object
) {
2847 DVLOG(2) << "DoomPartialEntry";
2848 int rv
= cache_
->DoomEntry(cache_key_
, NULL
);
2850 cache_
->DoneWithEntry(entry_
, this, false);
2855 partial_
.reset(NULL
);
2858 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result
) {
2859 partial_
->OnNetworkReadCompleted(result
);
2862 // We need to move on to the next range.
2863 ResetNetworkTransaction();
2864 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2869 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result
) {
2870 partial_
->OnCacheReadCompleted(result
);
2872 if (result
== 0 && mode_
== READ_WRITE
) {
2873 // We need to move on to the next range.
2874 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2875 } else if (result
< 0) {
2876 return OnCacheReadError(result
, false);
2881 int HttpCache::Transaction::DoRestartPartialRequest() {
2882 // The stored data cannot be used. Get rid of it and restart this request.
2883 net_log_
.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST
);
2885 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
2886 // to Doom the entry again).
2888 ResetPartialState(!range_requested_
);
2889 next_state_
= STATE_CREATE_ENTRY
;
2893 void HttpCache::Transaction::ResetPartialState(bool delete_object
) {
2894 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
2895 DoomPartialEntry(delete_object
);
2897 if (!delete_object
) {
2898 // The simplest way to re-initialize partial_ is to create a new object.
2899 partial_
.reset(new PartialData());
2900 if (partial_
->Init(request_
->extra_headers
))
2901 partial_
->SetHeaders(custom_request_
->extra_headers
);
2907 void HttpCache::Transaction::ResetNetworkTransaction() {
2908 DCHECK(!old_network_trans_load_timing_
);
2909 DCHECK(network_trans_
);
2910 LoadTimingInfo load_timing
;
2911 if (network_trans_
->GetLoadTimingInfo(&load_timing
))
2912 old_network_trans_load_timing_
.reset(new LoadTimingInfo(load_timing
));
2913 total_received_bytes_
+= network_trans_
->GetTotalReceivedBytes();
2914 network_trans_
.reset();
2917 // Histogram data from the end of 2010 show the following distribution of
2918 // response headers:
2920 // Content-Length............... 87%
2921 // Date......................... 98%
2922 // Last-Modified................ 49%
2923 // Etag......................... 19%
2924 // Accept-Ranges: bytes......... 25%
2925 // Accept-Ranges: none.......... 0.4%
2926 // Strong Validator............. 50%
2927 // Strong Validator + ranges.... 24%
2928 // Strong Validator + CL........ 49%
2930 bool HttpCache::Transaction::CanResume(bool has_data
) {
2931 // Double check that there is something worth keeping.
2932 if (has_data
&& !entry_
->disk_entry
->GetDataSize(kResponseContentIndex
))
2935 if (request_
->method
!= "GET")
2938 // Note that if this is a 206, content-length was already fixed after calling
2939 // PartialData::ResponseHeadersOK().
2940 if (response_
.headers
->GetContentLength() <= 0 ||
2941 response_
.headers
->HasHeaderValue("Accept-Ranges", "none") ||
2942 !response_
.headers
->HasStrongValidators()) {
2949 void HttpCache::Transaction::UpdateTransactionPattern(
2950 TransactionPattern new_transaction_pattern
) {
2951 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2953 DCHECK(transaction_pattern_
== PATTERN_UNDEFINED
||
2954 new_transaction_pattern
== PATTERN_NOT_COVERED
);
2955 transaction_pattern_
= new_transaction_pattern
;
2958 void HttpCache::Transaction::RecordHistograms() {
2959 DCHECK_NE(PATTERN_UNDEFINED
, transaction_pattern_
);
2960 if (!cache_
.get() || !cache_
->GetCurrentBackend() ||
2961 cache_
->GetCurrentBackend()->GetCacheType() != DISK_CACHE
||
2962 cache_
->mode() != NORMAL
|| request_
->method
!= "GET") {
2965 UMA_HISTOGRAM_ENUMERATION(
2966 "HttpCache.Pattern", transaction_pattern_
, PATTERN_MAX
);
2967 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2969 DCHECK(!range_requested_
);
2970 DCHECK(!first_cache_access_since_
.is_null());
2972 TimeDelta total_time
= base::TimeTicks::Now() - first_cache_access_since_
;
2974 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time
);
2976 bool did_send_request
= !send_request_since_
.is_null();
2978 (did_send_request
&&
2979 (transaction_pattern_
== PATTERN_ENTRY_NOT_CACHED
||
2980 transaction_pattern_
== PATTERN_ENTRY_VALIDATED
||
2981 transaction_pattern_
== PATTERN_ENTRY_UPDATED
||
2982 transaction_pattern_
== PATTERN_ENTRY_CANT_CONDITIONALIZE
)) ||
2983 (!did_send_request
&& transaction_pattern_
== PATTERN_ENTRY_USED
));
2985 if (!did_send_request
) {
2986 DCHECK(transaction_pattern_
== PATTERN_ENTRY_USED
);
2987 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time
);
2991 TimeDelta before_send_time
= send_request_since_
- first_cache_access_since_
;
2992 int64 before_send_percent
= (total_time
.ToInternalValue() == 0) ?
2993 0 : before_send_time
* 100 / total_time
;
2994 DCHECK_GE(before_send_percent
, 0);
2995 DCHECK_LE(before_send_percent
, 100);
2996 base::HistogramBase::Sample before_send_sample
=
2997 static_cast<base::HistogramBase::Sample
>(before_send_percent
);
2999 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time
);
3000 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time
);
3001 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample
);
3003 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
3004 // below this comment after we have received initial data.
3005 switch (transaction_pattern_
) {
3006 case PATTERN_ENTRY_CANT_CONDITIONALIZE
: {
3007 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
3009 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
3010 before_send_sample
);
3013 case PATTERN_ENTRY_NOT_CACHED
: {
3014 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time
);
3015 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
3016 before_send_sample
);
3019 case PATTERN_ENTRY_VALIDATED
: {
3020 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time
);
3021 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
3022 before_send_sample
);
3025 case PATTERN_ENTRY_UPDATED
: {
3026 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time
);
3027 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
3028 before_send_sample
);
3036 void HttpCache::Transaction::OnIOComplete(int result
) {