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/net_log.h"
39 #include "net/base/upload_data_stream.h"
40 #include "net/cert/cert_status_flags.h"
41 #include "net/disk_cache/disk_cache.h"
42 #include "net/http/disk_based_cert_cache.h"
43 #include "net/http/http_network_session.h"
44 #include "net/http/http_request_info.h"
45 #include "net/http/http_response_headers.h"
46 #include "net/http/http_transaction.h"
47 #include "net/http/http_util.h"
48 #include "net/http/partial_data.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
;
58 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
59 static const char kFreshnessHeader
[] = "Resource-Freshness";
61 // Stores data relevant to the statistics of writing and reading entire
62 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
63 // of certificates in the chain that have pending operations in the
64 // DiskBasedCertCache. |start_time| is the time that the read and write
65 // commands began being issued to the DiskBasedCertCache.
66 // TODO(brandonsalmon): Remove this when it is no longer necessary to
68 class SharedChainData
: public base::RefCounted
<SharedChainData
> {
70 SharedChainData(int num_ops
, TimeTicks start
)
71 : num_pending_ops(num_ops
), start_time(start
) {}
77 friend class base::RefCounted
<SharedChainData
>;
79 DISALLOW_COPY_AND_ASSIGN(SharedChainData
);
82 // Used to obtain a cache entry key for an OSCertHandle.
83 // TODO(brandonsalmon): Remove this when cache keys are stored
84 // and no longer have to be recomputed to retrieve the OSCertHandle
86 std::string
GetCacheKeyForCert(net::X509Certificate::OSCertHandle cert_handle
) {
87 net::SHA1HashValue fingerprint
=
88 net::X509Certificate::CalculateFingerprint(cert_handle
);
91 base::HexEncode(fingerprint
.data
, arraysize(fingerprint
.data
));
94 // |dist_from_root| indicates the position of the read certificate in the
95 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
96 // whether or not the read certificate was the leaf of the chain.
97 // |shared_chain_data| contains data shared by each certificate in
99 void OnCertReadIOComplete(
102 const scoped_refptr
<SharedChainData
>& shared_chain_data
,
103 net::X509Certificate::OSCertHandle cert_handle
) {
104 // If |num_pending_ops| is one, this was the last pending read operation
105 // for this chain of certificates. The total time used to read the chain
106 // can be calculated by subtracting the starting time from Now().
107 shared_chain_data
->num_pending_ops
--;
108 if (!shared_chain_data
->num_pending_ops
) {
109 const TimeDelta read_chain_wait
=
110 TimeTicks::Now() - shared_chain_data
->start_time
;
111 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
113 base::TimeDelta::FromMilliseconds(1),
114 base::TimeDelta::FromMinutes(10),
118 bool success
= (cert_handle
!= NULL
);
120 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success
);
123 UMA_HISTOGRAM_CUSTOM_COUNTS(
124 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root
, 0, 10, 7);
126 UMA_HISTOGRAM_CUSTOM_COUNTS(
127 "DiskBasedCertCache.CertIoReadFailure", dist_from_root
, 0, 10, 7);
130 // |dist_from_root| indicates the position of the written certificate in the
131 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
132 // whether or not the written certificate was the leaf of the chain.
133 // |shared_chain_data| contains data shared by each certificate in
135 void OnCertWriteIOComplete(
138 const scoped_refptr
<SharedChainData
>& shared_chain_data
,
139 const std::string
& key
) {
140 // If |num_pending_ops| is one, this was the last pending write operation
141 // for this chain of certificates. The total time used to write the chain
142 // can be calculated by subtracting the starting time from Now().
143 shared_chain_data
->num_pending_ops
--;
144 if (!shared_chain_data
->num_pending_ops
) {
145 const TimeDelta write_chain_wait
=
146 TimeTicks::Now() - shared_chain_data
->start_time
;
147 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
149 base::TimeDelta::FromMilliseconds(1),
150 base::TimeDelta::FromMinutes(10),
154 bool success
= !key
.empty();
156 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success
);
159 UMA_HISTOGRAM_CUSTOM_COUNTS(
160 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root
, 0, 10, 7);
162 UMA_HISTOGRAM_CUSTOM_COUNTS(
163 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root
, 0, 10, 7);
166 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
167 // a "non-error response" is one with a 2xx (Successful) or 3xx
168 // (Redirection) status code.
169 bool NonErrorResponse(int status_code
) {
170 int status_code_range
= status_code
/ 100;
171 return status_code_range
== 2 || status_code_range
== 3;
174 // Error codes that will be considered indicative of a page being offline/
175 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
176 bool IsOfflineError(int error
) {
177 return (error
== net::ERR_NAME_NOT_RESOLVED
||
178 error
== net::ERR_INTERNET_DISCONNECTED
||
179 error
== net::ERR_ADDRESS_UNREACHABLE
||
180 error
== net::ERR_CONNECTION_TIMED_OUT
);
183 // Enum for UMA, indicating the status (with regard to offline mode) of
184 // a particular request.
185 enum RequestOfflineStatus
{
186 // A cache transaction hit in cache (data was present and not stale)
188 OFFLINE_STATUS_FRESH_CACHE
,
190 // A network request was required for a cache entry, and it succeeded.
191 OFFLINE_STATUS_NETWORK_SUCCEEDED
,
193 // A network request was required for a cache entry, and it failed with
194 // a non-offline error.
195 OFFLINE_STATUS_NETWORK_FAILED
,
197 // A network request was required for a cache entry, it failed with an
198 // offline error, and we could serve stale data if
199 // LOAD_FROM_CACHE_IF_OFFLINE was set.
200 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
,
202 // A network request was required for a cache entry, it failed with
203 // an offline error, and there was no servable data in cache (even
205 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
,
207 OFFLINE_STATUS_MAX_ENTRIES
210 void RecordOfflineStatus(int load_flags
, RequestOfflineStatus status
) {
211 // Restrict to main frame to keep statistics close to
212 // "would have shown them something useful if offline mode was enabled".
213 if (load_flags
& net::LOAD_MAIN_FRAME
) {
214 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status
,
215 OFFLINE_STATUS_MAX_ENTRIES
);
219 void RecordNoStoreHeaderHistogram(int load_flags
,
220 const net::HttpResponseInfo
* response
) {
221 if (load_flags
& net::LOAD_MAIN_FRAME
) {
222 UMA_HISTOGRAM_BOOLEAN(
223 "Net.MainFrameNoStore",
224 response
->headers
->HasHeaderValue("cache-control", "no-store"));
228 base::Value
* NetLogAsyncRevalidationInfoCallback(
229 const net::NetLog::Source
& source
,
230 const net::HttpRequestInfo
* request
,
231 net::NetLog::LogLevel log_level
) {
232 base::DictionaryValue
* dict
= new base::DictionaryValue();
233 source
.AddToEventParameters(dict
);
235 dict
->SetString("url", request
->url
.possibly_invalid_spec());
236 dict
->SetString("method", request
->method
);
240 enum ExternallyConditionalizedType
{
241 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION
,
242 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE
,
243 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS
,
244 EXTERNALLY_CONDITIONALIZED_MAX
251 struct HeaderNameAndValue
{
256 // If the request includes one of these request headers, then avoid caching
257 // to avoid getting confused.
258 static const HeaderNameAndValue kPassThroughHeaders
[] = {
259 { "if-unmodified-since", NULL
}, // causes unexpected 412s
260 { "if-match", NULL
}, // causes unexpected 412s
261 { "if-range", NULL
},
265 struct ValidationHeaderInfo
{
266 const char* request_header_name
;
267 const char* related_response_header_name
;
270 static const ValidationHeaderInfo kValidationHeaders
[] = {
271 { "if-modified-since", "last-modified" },
272 { "if-none-match", "etag" },
275 // If the request includes one of these request headers, then avoid reusing
276 // our cached copy if any.
277 static const HeaderNameAndValue kForceFetchHeaders
[] = {
278 { "cache-control", "no-cache" },
279 { "pragma", "no-cache" },
283 // If the request includes one of these request headers, then force our
284 // cached copy (if any) to be revalidated before reusing it.
285 static const HeaderNameAndValue kForceValidateHeaders
[] = {
286 { "cache-control", "max-age=0" },
290 static bool HeaderMatches(const HttpRequestHeaders
& headers
,
291 const HeaderNameAndValue
* search
) {
292 for (; search
->name
; ++search
) {
293 std::string header_value
;
294 if (!headers
.GetHeader(search
->name
, &header_value
))
300 HttpUtil::ValuesIterator
v(header_value
.begin(), header_value
.end(), ',');
301 while (v
.GetNext()) {
302 if (LowerCaseEqualsASCII(v
.value_begin(), v
.value_end(), search
->value
))
309 //-----------------------------------------------------------------------------
311 HttpCache::Transaction::Transaction(RequestPriority priority
, HttpCache
* cache
)
312 : next_state_(STATE_NONE
),
315 cache_(cache
->GetWeakPtr()),
320 target_state_(STATE_NONE
),
322 invalid_range_(false),
325 range_requested_(false),
326 handling_206_(false),
327 cache_pending_(false),
328 done_reading_(false),
329 vary_mismatch_(false),
330 couldnt_conditionalize_request_(false),
331 bypass_lock_for_test_(false),
332 fail_conditionalization_for_test_(false),
335 effective_load_flags_(0),
337 transaction_pattern_(PATTERN_UNDEFINED
),
338 total_received_bytes_(0),
339 websocket_handshake_stream_base_create_helper_(NULL
),
340 weak_factory_(this) {
341 static_assert(HttpCache::Transaction::kNumValidationHeaders
==
342 arraysize(kValidationHeaders
),
343 "invalid number of validation headers");
345 io_callback_
= base::Bind(&Transaction::OnIOComplete
,
346 weak_factory_
.GetWeakPtr());
349 HttpCache::Transaction::~Transaction() {
350 // We may have to issue another IO, but we should never invoke the callback_
356 bool cancel_request
= reading_
&& response_
.headers
.get();
357 if (cancel_request
) {
359 entry_
->disk_entry
->CancelSparseIO();
361 cancel_request
&= (response_
.headers
->response_code() == 200);
365 cache_
->DoneWithEntry(entry_
, this, cancel_request
);
366 } else if (cache_pending_
) {
367 cache_
->RemovePendingTransaction(this);
372 int HttpCache::Transaction::WriteMetadata(IOBuffer
* buf
, int buf_len
,
373 const CompletionCallback
& callback
) {
375 DCHECK_GT(buf_len
, 0);
376 DCHECK(!callback
.is_null());
377 if (!cache_
.get() || !entry_
)
378 return ERR_UNEXPECTED
;
380 // We don't need to track this operation for anything.
381 // It could be possible to check if there is something already written and
382 // avoid writing again (it should be the same, right?), but let's allow the
383 // caller to "update" the contents with something new.
384 return entry_
->disk_entry
->WriteData(kMetadataIndex
, 0, buf
, buf_len
,
388 bool HttpCache::Transaction::AddTruncatedFlag() {
389 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
391 // Don't set the flag for sparse entries.
392 if (partial_
.get() && !truncated_
)
395 if (!CanResume(true))
398 // We may have received the whole resource already.
403 target_state_
= STATE_NONE
;
404 next_state_
= STATE_CACHE_WRITE_TRUNCATED_RESPONSE
;
409 LoadState
HttpCache::Transaction::GetWriterLoadState() const {
410 if (network_trans_
.get())
411 return network_trans_
->GetLoadState();
412 if (entry_
|| !request_
)
413 return LOAD_STATE_IDLE
;
414 return LOAD_STATE_WAITING_FOR_CACHE
;
417 const BoundNetLog
& HttpCache::Transaction::net_log() const {
421 int HttpCache::Transaction::Start(const HttpRequestInfo
* request
,
422 const CompletionCallback
& callback
,
423 const BoundNetLog
& net_log
) {
425 DCHECK(!callback
.is_null());
427 // Ensure that we only have one asynchronous call at a time.
428 DCHECK(callback_
.is_null());
430 DCHECK(!network_trans_
.get());
434 return ERR_UNEXPECTED
;
436 SetRequest(net_log
, request
);
438 // We have to wait until the backend is initialized so we start the SM.
439 next_state_
= STATE_GET_BACKEND
;
442 // Setting this here allows us to check for the existence of a callback_ to
443 // determine if we are still inside Start.
444 if (rv
== ERR_IO_PENDING
) {
445 callback_
= tracked_objects::ScopedTracker::TrackCallback(
446 FROM_HERE_WITH_EXPLICIT_FUNCTION(
447 "422516 HttpCache::Transaction::Start"),
454 int HttpCache::Transaction::RestartIgnoringLastError(
455 const CompletionCallback
& callback
) {
456 DCHECK(!callback
.is_null());
458 // Ensure that we only have one asynchronous call at a time.
459 DCHECK(callback_
.is_null());
462 return ERR_UNEXPECTED
;
464 int rv
= RestartNetworkRequest();
466 if (rv
== ERR_IO_PENDING
) {
467 callback_
= tracked_objects::ScopedTracker::TrackCallback(
468 FROM_HERE_WITH_EXPLICIT_FUNCTION(
469 "422516 HttpCache::Transaction::RestartIgnoringLastError"),
476 int HttpCache::Transaction::RestartWithCertificate(
477 X509Certificate
* client_cert
,
478 const CompletionCallback
& callback
) {
479 DCHECK(!callback
.is_null());
481 // Ensure that we only have one asynchronous call at a time.
482 DCHECK(callback_
.is_null());
485 return ERR_UNEXPECTED
;
487 int rv
= RestartNetworkRequestWithCertificate(client_cert
);
489 if (rv
== ERR_IO_PENDING
) {
490 callback_
= tracked_objects::ScopedTracker::TrackCallback(
491 FROM_HERE_WITH_EXPLICIT_FUNCTION(
492 "422516 HttpCache::Transaction::RestartWithCertificate"),
499 int HttpCache::Transaction::RestartWithAuth(
500 const AuthCredentials
& credentials
,
501 const CompletionCallback
& callback
) {
502 DCHECK(auth_response_
.headers
.get());
503 DCHECK(!callback
.is_null());
505 // Ensure that we only have one asynchronous call at a time.
506 DCHECK(callback_
.is_null());
509 return ERR_UNEXPECTED
;
511 // Clear the intermediate response since we are going to start over.
512 auth_response_
= HttpResponseInfo();
514 int rv
= RestartNetworkRequestWithAuth(credentials
);
516 if (rv
== ERR_IO_PENDING
) {
517 callback_
= tracked_objects::ScopedTracker::TrackCallback(
518 FROM_HERE_WITH_EXPLICIT_FUNCTION(
519 "422516 HttpCache::Transaction::RestartWithAuth"),
526 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
527 if (!network_trans_
.get())
529 return network_trans_
->IsReadyToRestartForAuth();
532 int HttpCache::Transaction::Read(IOBuffer
* buf
, int buf_len
,
533 const CompletionCallback
& callback
) {
535 DCHECK_GT(buf_len
, 0);
536 DCHECK(!callback
.is_null());
538 DCHECK(callback_
.is_null());
541 return ERR_UNEXPECTED
;
543 // If we have an intermediate auth response at this point, then it means the
544 // user wishes to read the network response (the error page). If there is a
545 // previous response in the cache then we should leave it intact.
546 if (auth_response_
.headers
.get() && mode_
!= NONE
) {
547 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
548 DCHECK(mode_
& WRITE
);
549 DoneWritingToEntry(mode_
== READ_WRITE
);
558 DCHECK(partial_
.get());
559 if (!network_trans_
.get()) {
560 // We are just reading from the cache, but we may be writing later.
561 rv
= ReadFromEntry(buf
, buf_len
);
566 DCHECK(network_trans_
.get());
567 rv
= ReadFromNetwork(buf
, buf_len
);
570 rv
= ReadFromEntry(buf
, buf_len
);
577 if (rv
== ERR_IO_PENDING
) {
578 DCHECK(callback_
.is_null());
579 callback_
= tracked_objects::ScopedTracker::TrackCallback(
580 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 HttpCache::Transaction::Read"),
586 void HttpCache::Transaction::StopCaching() {
587 // We really don't know where we are now. Hopefully there is no operation in
588 // progress, but nothing really prevents this method to be called after we
589 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
590 // point because we need the state machine for that (and even if we are really
591 // free, that would be an asynchronous operation). In other words, keep the
592 // entry how it is (it will be marked as truncated at destruction), and let
593 // the next piece of code that executes know that we are now reading directly
595 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
596 // future request for the resource will be blocked on this one.
598 if (cache_
.get() && entry_
&& (mode_
& WRITE
) && network_trans_
.get() &&
599 !is_sparse_
&& !range_requested_
) {
604 bool HttpCache::Transaction::GetFullRequestHeaders(
605 HttpRequestHeaders
* headers
) const {
607 return network_trans_
->GetFullRequestHeaders(headers
);
609 // TODO(ttuttle): Read headers from cache.
613 int64
HttpCache::Transaction::GetTotalReceivedBytes() const {
614 int64 total_received_bytes
= total_received_bytes_
;
616 total_received_bytes
+= network_trans_
->GetTotalReceivedBytes();
617 return total_received_bytes
;
620 void HttpCache::Transaction::DoneReading() {
621 if (cache_
.get() && entry_
) {
622 DCHECK_NE(mode_
, UPDATE
);
624 DoneWritingToEntry(true);
625 } else if (mode_
& READ
) {
626 // It is necessary to check mode_ & READ because it is possible
627 // for mode_ to be NONE and entry_ non-NULL with a write entry
628 // if StopCaching was called.
629 cache_
->DoneReadingFromEntry(entry_
, this);
635 const HttpResponseInfo
* HttpCache::Transaction::GetResponseInfo() const {
636 // Null headers means we encountered an error or haven't a response yet
637 if (auth_response_
.headers
.get())
638 return &auth_response_
;
639 return (response_
.headers
.get() || response_
.ssl_info
.cert
.get() ||
640 response_
.cert_request_info
.get())
645 LoadState
HttpCache::Transaction::GetLoadState() const {
646 LoadState state
= GetWriterLoadState();
647 if (state
!= LOAD_STATE_WAITING_FOR_CACHE
)
651 return cache_
->GetLoadStateForPendingTransaction(this);
653 return LOAD_STATE_IDLE
;
656 UploadProgress
HttpCache::Transaction::GetUploadProgress() const {
657 if (network_trans_
.get())
658 return network_trans_
->GetUploadProgress();
659 return final_upload_progress_
;
662 void HttpCache::Transaction::SetQuicServerInfo(
663 QuicServerInfo
* quic_server_info
) {}
665 bool HttpCache::Transaction::GetLoadTimingInfo(
666 LoadTimingInfo
* load_timing_info
) const {
668 return network_trans_
->GetLoadTimingInfo(load_timing_info
);
670 if (old_network_trans_load_timing_
) {
671 *load_timing_info
= *old_network_trans_load_timing_
;
675 if (first_cache_access_since_
.is_null())
678 // If the cache entry was opened, return that time.
679 load_timing_info
->send_start
= first_cache_access_since_
;
680 // This time doesn't make much sense when reading from the cache, so just use
681 // the same time as send_start.
682 load_timing_info
->send_end
= first_cache_access_since_
;
686 void HttpCache::Transaction::SetPriority(RequestPriority priority
) {
687 priority_
= priority
;
689 network_trans_
->SetPriority(priority_
);
692 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
693 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
694 websocket_handshake_stream_base_create_helper_
= create_helper
;
696 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(create_helper
);
699 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
700 const BeforeNetworkStartCallback
& callback
) {
701 DCHECK(!network_trans_
);
702 before_network_start_callback_
= callback
;
705 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
706 const BeforeProxyHeadersSentCallback
& callback
) {
707 DCHECK(!network_trans_
);
708 before_proxy_headers_sent_callback_
= callback
;
711 int HttpCache::Transaction::ResumeNetworkStart() {
713 return network_trans_
->ResumeNetworkStart();
714 return ERR_UNEXPECTED
;
717 //-----------------------------------------------------------------------------
719 void HttpCache::Transaction::DoCallback(int rv
) {
720 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
721 tracked_objects::ScopedTracker
tracking_profile(
722 FROM_HERE_WITH_EXPLICIT_FUNCTION(
723 "422516 HttpCache::Transaction::DoCallback"));
725 DCHECK(rv
!= ERR_IO_PENDING
);
726 DCHECK(!callback_
.is_null());
728 read_buf_
= NULL
; // Release the buffer before invoking the callback.
730 // Since Run may result in Read being called, clear callback_ up front.
731 CompletionCallback c
= callback_
;
736 int HttpCache::Transaction::HandleResult(int rv
) {
737 DCHECK(rv
!= ERR_IO_PENDING
);
738 if (!callback_
.is_null())
744 // A few common patterns: (Foo* means Foo -> FooComplete)
746 // 1. Not-cached entry:
748 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
749 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
750 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
751 // PartialHeadersReceived
754 // NetworkRead* -> CacheWriteData*
756 // 2. Cached entry, no validation:
758 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
759 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
760 // BeginCacheValidation() -> SetupEntryForRead()
765 // 3. Cached entry, validation (304):
767 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
768 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
769 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
770 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
771 // -> OverwriteCachedResponse -> PartialHeadersReceived
776 // 4. Cached entry, validation and replace (200):
778 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
779 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
780 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
781 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
782 // TruncateCachedMetadata* -> PartialHeadersReceived
785 // NetworkRead* -> CacheWriteData*
787 // 5. Sparse entry, partially cached, byte range request:
789 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
790 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
791 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
792 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
793 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
794 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
795 // -> OverwriteCachedResponse -> PartialHeadersReceived
798 // NetworkRead* -> CacheWriteData*
801 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
802 // CompletePartialCacheValidation -> CacheReadData* ->
805 // CacheReadData* -> StartPartialCacheValidation ->
806 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
807 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
808 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
810 // 6. HEAD. Not-cached entry:
811 // Pass through. Don't save a HEAD by itself.
813 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
815 // 7. HEAD. Cached entry, no validation:
817 // The same flow as for a GET request (example #2)
820 // CacheReadData (returns 0)
822 // 8. HEAD. Cached entry, validation (304):
823 // The request updates the stored headers.
824 // Start(): Same as for a GET request (example #3)
827 // CacheReadData (returns 0)
829 // 9. HEAD. Cached entry, validation and replace (200):
830 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
833 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
834 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
835 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
836 // OverwriteCachedResponse
838 // 10. HEAD. Sparse entry, partially cached:
839 // Serve the request from the cache, as long as it doesn't require
840 // revalidation. Ignore missing ranges when deciding to revalidate. If the
841 // entry requires revalidation, ignore the whole request and go to full pass
842 // through (the result of the HEAD request will NOT update the entry).
844 // Start(): Basically the same as example 7, as we never create a partial_
845 // object for this request.
847 // 11. Prefetch, not-cached entry:
848 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
849 // UpdateCachedResponse.
851 // 12. Prefetch, cached entry:
852 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
853 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
856 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
857 // Skip validation, similar to example 2.
858 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
859 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
860 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
861 // SetupEntryForRead()
866 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
867 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
868 // CacheReadResponse* and CacheDispatchValidation.
869 int HttpCache::Transaction::DoLoop(int result
) {
870 DCHECK(next_state_
!= STATE_NONE
);
874 State state
= next_state_
;
875 next_state_
= STATE_NONE
;
877 case STATE_GET_BACKEND
:
881 case STATE_GET_BACKEND_COMPLETE
:
882 rv
= DoGetBackendComplete(rv
);
884 case STATE_SEND_REQUEST
:
886 rv
= DoSendRequest();
888 case STATE_SEND_REQUEST_COMPLETE
:
889 rv
= DoSendRequestComplete(rv
);
891 case STATE_SUCCESSFUL_SEND_REQUEST
:
893 rv
= DoSuccessfulSendRequest();
895 case STATE_NETWORK_READ
:
897 rv
= DoNetworkRead();
899 case STATE_NETWORK_READ_COMPLETE
:
900 rv
= DoNetworkReadComplete(rv
);
902 case STATE_INIT_ENTRY
:
906 case STATE_OPEN_ENTRY
:
910 case STATE_OPEN_ENTRY_COMPLETE
:
911 rv
= DoOpenEntryComplete(rv
);
913 case STATE_CREATE_ENTRY
:
915 rv
= DoCreateEntry();
917 case STATE_CREATE_ENTRY_COMPLETE
:
918 rv
= DoCreateEntryComplete(rv
);
920 case STATE_DOOM_ENTRY
:
924 case STATE_DOOM_ENTRY_COMPLETE
:
925 rv
= DoDoomEntryComplete(rv
);
927 case STATE_ADD_TO_ENTRY
:
931 case STATE_ADD_TO_ENTRY_COMPLETE
:
932 rv
= DoAddToEntryComplete(rv
);
934 case STATE_START_PARTIAL_CACHE_VALIDATION
:
936 rv
= DoStartPartialCacheValidation();
938 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
:
939 rv
= DoCompletePartialCacheValidation(rv
);
941 case STATE_UPDATE_CACHED_RESPONSE
:
943 rv
= DoUpdateCachedResponse();
945 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE
:
946 rv
= DoUpdateCachedResponseComplete(rv
);
948 case STATE_OVERWRITE_CACHED_RESPONSE
:
950 rv
= DoOverwriteCachedResponse();
952 case STATE_TRUNCATE_CACHED_DATA
:
954 rv
= DoTruncateCachedData();
956 case STATE_TRUNCATE_CACHED_DATA_COMPLETE
:
957 rv
= DoTruncateCachedDataComplete(rv
);
959 case STATE_TRUNCATE_CACHED_METADATA
:
961 rv
= DoTruncateCachedMetadata();
963 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE
:
964 rv
= DoTruncateCachedMetadataComplete(rv
);
966 case STATE_PARTIAL_HEADERS_RECEIVED
:
968 rv
= DoPartialHeadersReceived();
970 case STATE_CACHE_READ_RESPONSE
:
972 rv
= DoCacheReadResponse();
974 case STATE_CACHE_READ_RESPONSE_COMPLETE
:
975 rv
= DoCacheReadResponseComplete(rv
);
977 case STATE_CACHE_DISPATCH_VALIDATION
:
979 rv
= DoCacheDispatchValidation();
981 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH
:
983 rv
= DoCacheToggleUnusedSincePrefetch();
985 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE
:
986 rv
= DoCacheToggleUnusedSincePrefetchComplete(rv
);
988 case STATE_CACHE_WRITE_RESPONSE
:
990 rv
= DoCacheWriteResponse();
992 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
994 rv
= DoCacheWriteTruncatedResponse();
996 case STATE_CACHE_WRITE_RESPONSE_COMPLETE
:
997 rv
= DoCacheWriteResponseComplete(rv
);
999 case STATE_CACHE_READ_METADATA
:
1001 rv
= DoCacheReadMetadata();
1003 case STATE_CACHE_READ_METADATA_COMPLETE
:
1004 rv
= DoCacheReadMetadataComplete(rv
);
1006 case STATE_CACHE_QUERY_DATA
:
1008 rv
= DoCacheQueryData();
1010 case STATE_CACHE_QUERY_DATA_COMPLETE
:
1011 rv
= DoCacheQueryDataComplete(rv
);
1013 case STATE_CACHE_READ_DATA
:
1015 rv
= DoCacheReadData();
1017 case STATE_CACHE_READ_DATA_COMPLETE
:
1018 rv
= DoCacheReadDataComplete(rv
);
1020 case STATE_CACHE_WRITE_DATA
:
1021 rv
= DoCacheWriteData(rv
);
1023 case STATE_CACHE_WRITE_DATA_COMPLETE
:
1024 rv
= DoCacheWriteDataComplete(rv
);
1027 NOTREACHED() << "bad state";
1031 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
1033 if (rv
!= ERR_IO_PENDING
)
1039 int HttpCache::Transaction::DoGetBackend() {
1040 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1041 tracked_objects::ScopedTracker
tracking_profile(
1042 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1043 "422516 HttpCache::Transaction::DoGetBackend"));
1045 cache_pending_
= true;
1046 next_state_
= STATE_GET_BACKEND_COMPLETE
;
1047 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
);
1048 return cache_
->GetBackendForTransaction(this);
1051 int HttpCache::Transaction::DoGetBackendComplete(int result
) {
1052 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1053 tracked_objects::ScopedTracker
tracking_profile(
1054 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1055 "422516 HttpCache::Transaction::DoGetBackendComplete"));
1057 DCHECK(result
== OK
|| result
== ERR_FAILED
);
1058 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
,
1060 cache_pending_
= false;
1062 if (!ShouldPassThrough()) {
1063 cache_key_
= cache_
->GenerateCacheKey(request_
);
1065 // Requested cache access mode.
1066 if (effective_load_flags_
& LOAD_ONLY_FROM_CACHE
) {
1068 } else if (effective_load_flags_
& LOAD_BYPASS_CACHE
) {
1074 // Downgrade to UPDATE if the request has been externally conditionalized.
1075 if (external_validation_
.initialized
) {
1076 if (mode_
& WRITE
) {
1077 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1078 // in case READ was off).
1086 // Use PUT and DELETE only to invalidate existing stored entries.
1087 if ((request_
->method
== "PUT" || request_
->method
== "DELETE") &&
1088 mode_
!= READ_WRITE
&& mode_
!= WRITE
) {
1092 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1093 // transaction behaves the same for GET and HEAD requests at this point: if it
1094 // was not modified, the entry is updated and a response is not returned from
1095 // the cache. If we receive 200, it doesn't matter if there was a validation
1097 if (request_
->method
== "HEAD" && mode_
== WRITE
)
1100 // If must use cache, then we must fail. This can happen for back/forward
1101 // navigations to a page generated via a form post.
1102 if (!(mode_
& READ
) && effective_load_flags_
& LOAD_ONLY_FROM_CACHE
)
1103 return ERR_CACHE_MISS
;
1105 if (mode_
== NONE
) {
1106 if (partial_
.get()) {
1107 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1110 next_state_
= STATE_SEND_REQUEST
;
1112 next_state_
= STATE_INIT_ENTRY
;
1115 // This is only set if we have something to do with the response.
1116 range_requested_
= (partial_
.get() != NULL
);
1121 int HttpCache::Transaction::DoSendRequest() {
1122 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1123 tracked_objects::ScopedTracker
tracking_profile(
1124 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1125 "422516 HttpCache::Transaction::DoSendRequest"));
1127 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1128 DCHECK(!network_trans_
.get());
1130 send_request_since_
= TimeTicks::Now();
1132 // Create a network transaction.
1133 int rv
= cache_
->network_layer_
->CreateTransaction(priority_
,
1137 network_trans_
->SetBeforeNetworkStartCallback(before_network_start_callback_
);
1138 network_trans_
->SetBeforeProxyHeadersSentCallback(
1139 before_proxy_headers_sent_callback_
);
1141 // Old load timing information, if any, is now obsolete.
1142 old_network_trans_load_timing_
.reset();
1144 if (websocket_handshake_stream_base_create_helper_
)
1145 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(
1146 websocket_handshake_stream_base_create_helper_
);
1148 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1149 rv
= network_trans_
->Start(request_
, io_callback_
, net_log_
);
1153 int HttpCache::Transaction::DoSendRequestComplete(int result
) {
1154 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1155 tracked_objects::ScopedTracker
tracking_profile(
1156 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1157 "422516 HttpCache::Transaction::DoSendRequestComplete"));
1160 return ERR_UNEXPECTED
;
1162 // If requested, and we have a readable cache entry, and we have
1163 // an error indicating that we're offline as opposed to in contact
1164 // with a bad server, read from cache anyway.
1165 if (IsOfflineError(result
)) {
1166 if (mode_
== READ_WRITE
&& entry_
&& !partial_
) {
1167 RecordOfflineStatus(effective_load_flags_
,
1168 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
);
1169 if (effective_load_flags_
& LOAD_FROM_CACHE_IF_OFFLINE
) {
1170 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1171 response_
.server_data_unavailable
= true;
1172 return SetupEntryForRead();
1175 RecordOfflineStatus(effective_load_flags_
,
1176 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
);
1179 RecordOfflineStatus(effective_load_flags_
,
1180 (result
== OK
? OFFLINE_STATUS_NETWORK_SUCCEEDED
:
1181 OFFLINE_STATUS_NETWORK_FAILED
));
1184 // If we tried to conditionalize the request and failed, we know
1185 // we won't be reading from the cache after this point.
1186 if (couldnt_conditionalize_request_
)
1190 next_state_
= STATE_SUCCESSFUL_SEND_REQUEST
;
1194 // Do not record requests that have network errors or restarts.
1195 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1196 if (IsCertificateError(result
)) {
1197 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
1198 // If we get a certificate error, then there is a certificate in ssl_info,
1199 // so GetResponseInfo() should never return NULL here.
1201 response_
.ssl_info
= response
->ssl_info
;
1202 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
1203 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
1205 response_
.cert_request_info
= response
->cert_request_info
;
1206 } else if (response_
.was_cached
) {
1207 DoneWritingToEntry(true);
1212 // We received the response headers and there is no error.
1213 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1214 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1215 tracked_objects::ScopedTracker
tracking_profile(
1216 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1217 "422516 HttpCache::Transaction::DoSuccessfulSendRequest"));
1219 DCHECK(!new_response_
);
1220 const HttpResponseInfo
* new_response
= network_trans_
->GetResponseInfo();
1221 bool authentication_failure
= false;
1223 if (new_response
->headers
->response_code() == 401 ||
1224 new_response
->headers
->response_code() == 407) {
1225 auth_response_
= *new_response
;
1229 // We initiated a second request the caller doesn't know about. We should be
1230 // able to authenticate this request because we should have authenticated
1231 // this URL moments ago.
1232 if (IsReadyToRestartForAuth()) {
1233 DCHECK(!response_
.auth_challenge
.get());
1234 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1235 // In theory we should check to see if there are new cookies, but there
1236 // is no way to do that from here.
1237 return network_trans_
->RestartWithAuth(AuthCredentials(), io_callback_
);
1240 // We have to perform cleanup at this point so that at least the next
1241 // request can succeed.
1242 authentication_failure
= true;
1244 DoomPartialEntry(false);
1249 new_response_
= new_response
;
1250 if (authentication_failure
||
1251 (!ValidatePartialResponse() && !auth_response_
.headers
.get())) {
1252 // Something went wrong with this request and we have to restart it.
1253 // If we have an authentication response, we are exposed to weird things
1254 // hapenning if the user cancels the authentication before we receive
1255 // the new response.
1256 net_log_
.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST
);
1257 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1258 response_
= HttpResponseInfo();
1259 ResetNetworkTransaction();
1260 new_response_
= NULL
;
1261 next_state_
= STATE_SEND_REQUEST
;
1265 if (handling_206_
&& mode_
== READ_WRITE
&& !truncated_
&& !is_sparse_
) {
1266 // We have stored the full entry, but it changed and the server is
1267 // sending a range. We have to delete the old entry.
1268 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1269 DoneWritingToEntry(false);
1272 if (mode_
== WRITE
&&
1273 transaction_pattern_
!= PATTERN_ENTRY_CANT_CONDITIONALIZE
) {
1274 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED
);
1277 // Invalidate any cached GET with a successful PUT or DELETE.
1278 if (mode_
== WRITE
&&
1279 (request_
->method
== "PUT" || request_
->method
== "DELETE")) {
1280 if (NonErrorResponse(new_response
->headers
->response_code())) {
1281 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1284 cache_
->DoneWritingToEntry(entry_
, true);
1289 // Invalidate any cached GET with a successful POST.
1290 if (!(effective_load_flags_
& LOAD_DISABLE_CACHE
) &&
1291 request_
->method
== "POST" &&
1292 NonErrorResponse(new_response
->headers
->response_code())) {
1293 cache_
->DoomMainEntryForUrl(request_
->url
);
1296 RecordNoStoreHeaderHistogram(request_
->load_flags
, new_response
);
1298 if (new_response_
->headers
->response_code() == 416 &&
1299 (request_
->method
== "GET" || request_
->method
== "POST")) {
1300 // If there is an active entry it may be destroyed with this transaction.
1301 response_
= *new_response_
;
1305 // Are we expecting a response to a conditional query?
1306 if (mode_
== READ_WRITE
|| mode_
== UPDATE
) {
1307 if (new_response
->headers
->response_code() == 304 || handling_206_
) {
1308 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED
);
1309 next_state_
= STATE_UPDATE_CACHED_RESPONSE
;
1312 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED
);
1316 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1320 int HttpCache::Transaction::DoNetworkRead() {
1321 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1322 tracked_objects::ScopedTracker
tracking_profile(
1323 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1324 "422516 HttpCache::Transaction::DoNetworkRead"));
1326 next_state_
= STATE_NETWORK_READ_COMPLETE
;
1327 return network_trans_
->Read(read_buf_
.get(), io_buf_len_
, io_callback_
);
1330 int HttpCache::Transaction::DoNetworkReadComplete(int result
) {
1331 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1332 tracked_objects::ScopedTracker
tracking_profile(
1333 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1334 "422516 HttpCache::Transaction::DoNetworkReadComplete"));
1336 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1339 return ERR_UNEXPECTED
;
1341 // If there is an error or we aren't saving the data, we are done; just wait
1342 // until the destructor runs to see if we can keep the data.
1343 if (mode_
== NONE
|| result
< 0)
1346 next_state_
= STATE_CACHE_WRITE_DATA
;
1350 int HttpCache::Transaction::DoInitEntry() {
1351 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1352 tracked_objects::ScopedTracker
tracking_profile(
1353 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1354 "422516 HttpCache::Transaction::DoInitEntry"));
1356 DCHECK(!new_entry_
);
1359 return ERR_UNEXPECTED
;
1361 if (mode_
== WRITE
) {
1362 next_state_
= STATE_DOOM_ENTRY
;
1366 next_state_
= STATE_OPEN_ENTRY
;
1370 int HttpCache::Transaction::DoOpenEntry() {
1371 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1372 tracked_objects::ScopedTracker
tracking_profile(
1373 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1374 "422516 HttpCache::Transaction::DoOpenEntry"));
1376 DCHECK(!new_entry_
);
1377 next_state_
= STATE_OPEN_ENTRY_COMPLETE
;
1378 cache_pending_
= true;
1379 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
);
1380 first_cache_access_since_
= TimeTicks::Now();
1381 return cache_
->OpenEntry(cache_key_
, &new_entry_
, this);
1384 int HttpCache::Transaction::DoOpenEntryComplete(int result
) {
1385 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1386 tracked_objects::ScopedTracker
tracking_profile(
1387 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1388 "422516 HttpCache::Transaction::DoOpenEntryComplete"));
1390 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1391 // OK, otherwise the cache will end up with an active entry without any
1392 // transaction attached.
1393 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
, result
);
1394 cache_pending_
= false;
1396 next_state_
= STATE_ADD_TO_ENTRY
;
1400 if (result
== ERR_CACHE_RACE
) {
1401 next_state_
= STATE_INIT_ENTRY
;
1405 if (request_
->method
== "PUT" || request_
->method
== "DELETE" ||
1406 (request_
->method
== "HEAD" && mode_
== READ_WRITE
)) {
1407 DCHECK(mode_
== READ_WRITE
|| mode_
== WRITE
|| request_
->method
== "HEAD");
1409 next_state_
= STATE_SEND_REQUEST
;
1413 if (mode_
== READ_WRITE
) {
1415 next_state_
= STATE_CREATE_ENTRY
;
1418 if (mode_
== UPDATE
) {
1419 // There is no cache entry to update; proceed without caching.
1421 next_state_
= STATE_SEND_REQUEST
;
1425 // The entry does not exist, and we are not permitted to create a new entry,
1427 return ERR_CACHE_MISS
;
1430 int HttpCache::Transaction::DoCreateEntry() {
1431 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1432 tracked_objects::ScopedTracker
tracking_profile(
1433 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1434 "422516 HttpCache::Transaction::DoCreateEntry"));
1436 DCHECK(!new_entry_
);
1437 next_state_
= STATE_CREATE_ENTRY_COMPLETE
;
1438 cache_pending_
= true;
1439 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
);
1440 return cache_
->CreateEntry(cache_key_
, &new_entry_
, this);
1443 int HttpCache::Transaction::DoCreateEntryComplete(int result
) {
1444 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1445 tracked_objects::ScopedTracker
tracking_profile(
1446 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1447 "422516 HttpCache::Transaction::DoCreateEntryComplete"));
1449 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1450 // OK, otherwise the cache will end up with an active entry without any
1451 // transaction attached.
1452 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
,
1454 cache_pending_
= false;
1455 next_state_
= STATE_ADD_TO_ENTRY
;
1457 if (result
== ERR_CACHE_RACE
) {
1458 next_state_
= STATE_INIT_ENTRY
;
1463 // We have a race here: Maybe we failed to open the entry and decided to
1464 // create one, but by the time we called create, another transaction already
1465 // created the entry. If we want to eliminate this issue, we need an atomic
1466 // OpenOrCreate() method exposed by the disk cache.
1467 DLOG(WARNING
) << "Unable to create cache entry";
1470 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1471 next_state_
= STATE_SEND_REQUEST
;
1476 int HttpCache::Transaction::DoDoomEntry() {
1477 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1478 tracked_objects::ScopedTracker
tracking_profile(
1479 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1480 "422516 HttpCache::Transaction::DoDoomEntry"));
1482 next_state_
= STATE_DOOM_ENTRY_COMPLETE
;
1483 cache_pending_
= true;
1484 if (first_cache_access_since_
.is_null())
1485 first_cache_access_since_
= TimeTicks::Now();
1486 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
);
1487 return cache_
->DoomEntry(cache_key_
, this);
1490 int HttpCache::Transaction::DoDoomEntryComplete(int result
) {
1491 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1492 tracked_objects::ScopedTracker
tracking_profile(
1493 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1494 "422516 HttpCache::Transaction::DoDoomEntryComplete"));
1496 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
, result
);
1497 next_state_
= STATE_CREATE_ENTRY
;
1498 cache_pending_
= false;
1499 if (result
== ERR_CACHE_RACE
)
1500 next_state_
= STATE_INIT_ENTRY
;
1504 int HttpCache::Transaction::DoAddToEntry() {
1505 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1506 tracked_objects::ScopedTracker
tracking_profile(
1507 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1508 "422516 HttpCache::Transaction::DoAddToEntry"));
1511 cache_pending_
= true;
1512 next_state_
= STATE_ADD_TO_ENTRY_COMPLETE
;
1513 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
);
1514 DCHECK(entry_lock_waiting_since_
.is_null());
1515 entry_lock_waiting_since_
= TimeTicks::Now();
1516 int rv
= cache_
->AddTransactionToEntry(new_entry_
, this);
1517 if (rv
== ERR_IO_PENDING
) {
1518 if (bypass_lock_for_test_
) {
1519 OnAddToEntryTimeout(entry_lock_waiting_since_
);
1521 int timeout_milliseconds
= 20 * 1000;
1522 if (partial_
&& new_entry_
->writer
&&
1523 new_entry_
->writer
->range_requested_
) {
1524 // Quickly timeout and bypass the cache if we're a range request and
1525 // we're blocked by the reader/writer lock. Doing so eliminates a long
1526 // running issue, http://crbug.com/31014, where two of the same media
1527 // resources could not be played back simultaneously due to one locking
1528 // the cache entry until the entire video was downloaded.
1530 // Bypassing the cache is not ideal, as we are now ignoring the cache
1531 // entirely for all range requests to a resource beyond the first. This
1532 // is however a much more succinct solution than the alternatives, which
1533 // would require somewhat significant changes to the http caching logic.
1535 // Allow some timeout slack for the entry addition to complete in case
1536 // the writer lock is imminently released; we want to avoid skipping
1537 // the cache if at all possible. See http://crbug.com/408765
1538 timeout_milliseconds
= 25;
1540 base::MessageLoop::current()->PostDelayedTask(
1542 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout
,
1543 weak_factory_
.GetWeakPtr(), entry_lock_waiting_since_
),
1544 TimeDelta::FromMilliseconds(timeout_milliseconds
));
1550 int HttpCache::Transaction::DoAddToEntryComplete(int result
) {
1551 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1552 tracked_objects::ScopedTracker
tracking_profile(
1553 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1554 "422516 HttpCache::Transaction::DoAddToEntryComplete"));
1556 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
,
1558 const TimeDelta entry_lock_wait
=
1559 TimeTicks::Now() - entry_lock_waiting_since_
;
1560 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait
);
1562 entry_lock_waiting_since_
= TimeTicks();
1564 cache_pending_
= false;
1567 entry_
= new_entry_
;
1569 // If there is a failure, the cache should have taken care of new_entry_.
1572 if (result
== ERR_CACHE_RACE
) {
1573 next_state_
= STATE_INIT_ENTRY
;
1577 if (result
== ERR_CACHE_LOCK_TIMEOUT
) {
1578 // The cache is busy, bypass it for this transaction.
1580 next_state_
= STATE_SEND_REQUEST
;
1582 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1593 if (mode_
== WRITE
) {
1595 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1596 next_state_
= STATE_SEND_REQUEST
;
1598 // We have to read the headers from the cached entry.
1599 DCHECK(mode_
& READ_META
);
1600 next_state_
= STATE_CACHE_READ_RESPONSE
;
1605 // We may end up here multiple times for a given request.
1606 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1607 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1608 tracked_objects::ScopedTracker
tracking_profile(
1609 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1610 "422516 HttpCache::Transaction::DoStartPartialCacheValidation"));
1615 next_state_
= STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
;
1616 return partial_
->ShouldValidateCache(entry_
->disk_entry
, io_callback_
);
1619 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result
) {
1620 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1621 tracked_objects::ScopedTracker
tracking_profile(
1622 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1623 "422516 HttpCache::Transaction::DoCompletePartialCacheValidation"));
1626 // This is the end of the request.
1627 if (mode_
& WRITE
) {
1628 DoneWritingToEntry(true);
1630 cache_
->DoneReadingFromEntry(entry_
, this);
1639 partial_
->PrepareCacheValidation(entry_
->disk_entry
,
1640 &custom_request_
->extra_headers
);
1642 if (reading_
&& partial_
->IsCurrentRangeCached()) {
1643 next_state_
= STATE_CACHE_READ_DATA
;
1647 return BeginCacheValidation();
1650 // We received 304 or 206 and we want to update the cached response headers.
1651 int HttpCache::Transaction::DoUpdateCachedResponse() {
1652 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1653 tracked_objects::ScopedTracker
tracking_profile(
1654 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1655 "422516 HttpCache::Transaction::DoUpdateCachedResponse"));
1657 next_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1659 // Update cached response based on headers in new_response.
1660 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1661 response_
.headers
->Update(*new_response_
->headers
.get());
1662 response_
.response_time
= new_response_
->response_time
;
1663 response_
.request_time
= new_response_
->request_time
;
1664 response_
.network_accessed
= new_response_
->network_accessed
;
1665 response_
.unused_since_prefetch
= new_response_
->unused_since_prefetch
;
1666 if (new_response_
->vary_data
.is_valid()) {
1667 response_
.vary_data
= new_response_
->vary_data
;
1668 } else if (response_
.vary_data
.is_valid()) {
1669 // There is a vary header in the stored response but not in the current one.
1670 // Update the data with the new request headers.
1671 HttpVaryData new_vary_data
;
1672 new_vary_data
.Init(*request_
, *response_
.headers
.get());
1673 response_
.vary_data
= new_vary_data
;
1676 if (response_
.headers
->HasHeaderValue("cache-control", "no-store")) {
1677 if (!entry_
->doomed
) {
1678 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1682 // If we are already reading, we already updated the headers for this
1683 // request; doing it again will change Content-Length.
1685 target_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1686 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1693 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result
) {
1694 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1695 tracked_objects::ScopedTracker
tracking_profile(
1696 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1697 "422516 HttpCache::Transaction::DoUpdateCachedResponseComplete"));
1699 if (mode_
== UPDATE
) {
1700 DCHECK(!handling_206_
);
1701 // We got a "not modified" response and already updated the corresponding
1702 // cache entry above.
1704 // By closing the cached entry now, we make sure that the 304 rather than
1705 // the cached 200 response, is what will be returned to the user.
1706 DoneWritingToEntry(true);
1707 } else if (entry_
&& !handling_206_
) {
1708 DCHECK_EQ(READ_WRITE
, mode_
);
1709 if (!partial_
.get() || partial_
->IsLastRange()) {
1710 cache_
->ConvertWriterToReader(entry_
);
1713 // We no longer need the network transaction, so destroy it.
1714 final_upload_progress_
= network_trans_
->GetUploadProgress();
1715 ResetNetworkTransaction();
1716 } else if (entry_
&& handling_206_
&& truncated_
&&
1717 partial_
->initial_validation()) {
1718 // We just finished the validation of a truncated entry, and the server
1719 // is willing to resume the operation. Now we go back and start serving
1720 // the first part to the user.
1721 ResetNetworkTransaction();
1722 new_response_
= NULL
;
1723 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
1724 partial_
->SetRangeToStartDownload();
1727 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1731 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1732 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1733 tracked_objects::ScopedTracker
tracking_profile(
1734 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1735 "422516 HttpCache::Transaction::DoOverwriteCachedResponse"));
1738 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1742 // We change the value of Content-Length for partial content.
1743 if (handling_206_
&& partial_
.get())
1744 partial_
->FixContentLength(new_response_
->headers
.get());
1746 response_
= *new_response_
;
1748 if (request_
->method
== "HEAD") {
1749 // This response is replacing the cached one.
1750 DoneWritingToEntry(false);
1752 new_response_
= NULL
;
1756 if (handling_206_
&& !CanResume(false)) {
1757 // There is no point in storing this resource because it will never be used.
1758 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1759 DoneWritingToEntry(false);
1761 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1762 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1766 target_state_
= STATE_TRUNCATE_CACHED_DATA
;
1767 next_state_
= truncated_
? STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
1768 STATE_CACHE_WRITE_RESPONSE
;
1772 int HttpCache::Transaction::DoTruncateCachedData() {
1773 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1774 tracked_objects::ScopedTracker
tracking_profile(
1775 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1776 "422516 HttpCache::Transaction::DoTruncateCachedData"));
1778 next_state_
= STATE_TRUNCATE_CACHED_DATA_COMPLETE
;
1781 if (net_log_
.IsLogging())
1782 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1783 // Truncate the stream.
1784 return WriteToEntry(kResponseContentIndex
, 0, NULL
, 0, io_callback_
);
1787 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result
) {
1788 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1789 tracked_objects::ScopedTracker
tracking_profile(
1790 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1791 "422516 HttpCache::Transaction::DoTruncateCachedDataComplete"));
1794 if (net_log_
.IsLogging()) {
1795 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1800 next_state_
= STATE_TRUNCATE_CACHED_METADATA
;
1804 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1805 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1806 tracked_objects::ScopedTracker
tracking_profile(
1807 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1808 "422516 HttpCache::Transaction::DoTruncateCachedMetadata"));
1810 next_state_
= STATE_TRUNCATE_CACHED_METADATA_COMPLETE
;
1814 if (net_log_
.IsLogging())
1815 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1816 return WriteToEntry(kMetadataIndex
, 0, NULL
, 0, io_callback_
);
1819 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result
) {
1820 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1821 tracked_objects::ScopedTracker
tracking_profile(
1822 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1823 "422516 HttpCache::Transaction::DoTruncateCachedMetadataComplete"));
1826 if (net_log_
.IsLogging()) {
1827 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1832 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1836 int HttpCache::Transaction::DoPartialHeadersReceived() {
1837 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1838 tracked_objects::ScopedTracker
tracking_profile(
1839 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1840 "422516 HttpCache::Transaction::DoPartialHeadersReceived"));
1842 new_response_
= NULL
;
1843 if (entry_
&& !partial_
.get() &&
1844 entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
1845 next_state_
= STATE_CACHE_READ_METADATA
;
1847 if (!partial_
.get())
1851 if (network_trans_
.get()) {
1852 next_state_
= STATE_NETWORK_READ
;
1854 next_state_
= STATE_CACHE_READ_DATA
;
1856 } else if (mode_
!= NONE
) {
1857 // We are about to return the headers for a byte-range request to the user,
1858 // so let's fix them.
1859 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1864 int HttpCache::Transaction::DoCacheReadResponse() {
1865 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1866 tracked_objects::ScopedTracker
tracking_profile(
1867 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1868 "422516 HttpCache::Transaction::DoCacheReadResponse"));
1871 next_state_
= STATE_CACHE_READ_RESPONSE_COMPLETE
;
1873 io_buf_len_
= entry_
->disk_entry
->GetDataSize(kResponseInfoIndex
);
1874 read_buf_
= new IOBuffer(io_buf_len_
);
1876 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1877 return entry_
->disk_entry
->ReadData(kResponseInfoIndex
, 0, read_buf_
.get(),
1878 io_buf_len_
, io_callback_
);
1881 int HttpCache::Transaction::DoCacheReadResponseComplete(int result
) {
1882 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1883 tracked_objects::ScopedTracker
tracking_profile(
1884 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1885 "422516 HttpCache::Transaction::DoCacheReadResponseComplete"));
1887 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1888 if (result
!= io_buf_len_
||
1889 !HttpCache::ParseResponseInfo(read_buf_
->data(), io_buf_len_
,
1890 &response_
, &truncated_
)) {
1891 return OnCacheReadError(result
, true);
1894 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1895 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
1898 // Some resources may have slipped in as truncated when they're not.
1899 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1900 if (response_
.headers
->GetContentLength() == current_size
)
1903 if ((response_
.unused_since_prefetch
&&
1904 !(request_
->load_flags
& LOAD_PREFETCH
)) ||
1905 (!response_
.unused_since_prefetch
&&
1906 (request_
->load_flags
& LOAD_PREFETCH
))) {
1907 // Either this is the first use of an entry since it was prefetched or
1908 // this is a prefetch. The value of response.unused_since_prefetch is valid
1909 // for this transaction but the bit needs to be flipped in storage.
1910 next_state_
= STATE_TOGGLE_UNUSED_SINCE_PREFETCH
;
1914 next_state_
= STATE_CACHE_DISPATCH_VALIDATION
;
1918 int HttpCache::Transaction::DoCacheDispatchValidation() {
1919 // We now have access to the cache entry.
1921 // o if we are a reader for the transaction, then we can start reading the
1924 // o if we can read or write, then we should check if the cache entry needs
1925 // to be validated and then issue a network request if needed or just read
1926 // from the cache if the cache entry is already valid.
1928 // o if we are set to UPDATE, then we are handling an externally
1929 // conditionalized request (if-modified-since / if-none-match). We check
1930 // if the request headers define a validation request.
1932 int result
= ERR_FAILED
;
1935 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
1936 result
= BeginCacheRead();
1939 result
= BeginPartialCacheValidation();
1942 result
= BeginExternallyConditionalizedRequest();
1951 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1952 // Write back the toggled value for the next use of this entry.
1953 response_
.unused_since_prefetch
= !response_
.unused_since_prefetch
;
1955 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1956 // transaction then metadata will be written to cache twice. If prefetching
1957 // becomes more common, consider combining the writes.
1958 target_state_
= STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE
;
1959 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1963 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1965 // Restore the original value for this transaction.
1966 response_
.unused_since_prefetch
= !response_
.unused_since_prefetch
;
1967 next_state_
= STATE_CACHE_DISPATCH_VALIDATION
;
1971 int HttpCache::Transaction::DoCacheWriteResponse() {
1972 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1973 tracked_objects::ScopedTracker
tracking_profile(
1974 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1975 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1978 if (net_log_
.IsLogging())
1979 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1981 return WriteResponseInfoToEntry(false);
1984 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1985 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1986 tracked_objects::ScopedTracker
tracking_profile(
1987 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1988 "422516 HttpCache::Transaction::DoCacheWriteTruncatedResponse"));
1991 if (net_log_
.IsLogging())
1992 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1994 return WriteResponseInfoToEntry(true);
1997 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result
) {
1998 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1999 tracked_objects::ScopedTracker
tracking_profile(
2000 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2001 "422516 HttpCache::Transaction::DoCacheWriteResponseComplete"));
2003 next_state_
= target_state_
;
2004 target_state_
= STATE_NONE
;
2007 if (net_log_
.IsLogging()) {
2008 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
2012 // Balance the AddRef from WriteResponseInfoToEntry.
2013 if (result
!= io_buf_len_
) {
2014 DLOG(ERROR
) << "failed to write response info to cache";
2015 DoneWritingToEntry(false);
2020 int HttpCache::Transaction::DoCacheReadMetadata() {
2021 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2022 tracked_objects::ScopedTracker
tracking_profile(
2023 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2024 "422516 HttpCache::Transaction::DoCacheReadMetadata"));
2027 DCHECK(!response_
.metadata
.get());
2028 next_state_
= STATE_CACHE_READ_METADATA_COMPLETE
;
2030 response_
.metadata
=
2031 new IOBufferWithSize(entry_
->disk_entry
->GetDataSize(kMetadataIndex
));
2033 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
2034 return entry_
->disk_entry
->ReadData(kMetadataIndex
, 0,
2035 response_
.metadata
.get(),
2036 response_
.metadata
->size(),
2040 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result
) {
2041 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2042 tracked_objects::ScopedTracker
tracking_profile(
2043 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2044 "422516 HttpCache::Transaction::DoCacheReadMetadataComplete"));
2046 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
2047 if (result
!= response_
.metadata
->size())
2048 return OnCacheReadError(result
, false);
2052 int HttpCache::Transaction::DoCacheQueryData() {
2053 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2054 tracked_objects::ScopedTracker
tracking_profile(
2055 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2056 "422516 HttpCache::Transaction::DoCacheQueryData"));
2058 next_state_
= STATE_CACHE_QUERY_DATA_COMPLETE
;
2059 return entry_
->disk_entry
->ReadyForSparseIO(io_callback_
);
2062 int HttpCache::Transaction::DoCacheQueryDataComplete(int result
) {
2063 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2064 tracked_objects::ScopedTracker
tracking_profile(
2065 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2066 "422516 HttpCache::Transaction::DoCacheQueryDataComplete"));
2068 DCHECK_EQ(OK
, result
);
2070 return ERR_UNEXPECTED
;
2072 return ValidateEntryHeadersAndContinue();
2075 int HttpCache::Transaction::DoCacheReadData() {
2076 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2077 tracked_objects::ScopedTracker
tracking_profile(
2078 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2079 "422516 HttpCache::Transaction::DoCacheReadData"));
2082 next_state_
= STATE_CACHE_READ_DATA_COMPLETE
;
2084 if (net_log_
.IsLogging())
2085 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA
);
2086 if (partial_
.get()) {
2087 return partial_
->CacheRead(entry_
->disk_entry
, read_buf_
.get(), io_buf_len_
,
2091 return entry_
->disk_entry
->ReadData(kResponseContentIndex
, read_offset_
,
2092 read_buf_
.get(), io_buf_len_
,
2096 int HttpCache::Transaction::DoCacheReadDataComplete(int result
) {
2097 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2098 tracked_objects::ScopedTracker
tracking_profile(
2099 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2100 "422516 HttpCache::Transaction::DoCacheReadDataComplete"));
2102 if (net_log_
.IsLogging()) {
2103 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA
,
2108 return ERR_UNEXPECTED
;
2110 if (partial_
.get()) {
2111 // Partial requests are confusing to report in histograms because they may
2112 // have multiple underlying requests.
2113 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2114 return DoPartialCacheReadCompleted(result
);
2118 read_offset_
+= result
;
2119 } else if (result
== 0) { // End of file.
2121 cache_
->DoneReadingFromEntry(entry_
, this);
2124 return OnCacheReadError(result
, false);
2129 int HttpCache::Transaction::DoCacheWriteData(int num_bytes
) {
2130 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2131 tracked_objects::ScopedTracker
tracking_profile(
2132 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2133 "422516 HttpCache::Transaction::DoCacheWriteData"));
2135 next_state_
= STATE_CACHE_WRITE_DATA_COMPLETE
;
2136 write_len_
= num_bytes
;
2138 if (net_log_
.IsLogging())
2139 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
2142 return AppendResponseDataToEntry(read_buf_
.get(), num_bytes
, io_callback_
);
2145 int HttpCache::Transaction::DoCacheWriteDataComplete(int result
) {
2146 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2147 tracked_objects::ScopedTracker
tracking_profile(
2148 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2149 "422516 HttpCache::Transaction::DoCacheWriteDataComplete"));
2152 if (net_log_
.IsLogging()) {
2153 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
2157 // Balance the AddRef from DoCacheWriteData.
2159 return ERR_UNEXPECTED
;
2161 if (result
!= write_len_
) {
2162 DLOG(ERROR
) << "failed to write response data to cache";
2163 DoneWritingToEntry(false);
2165 // We want to ignore errors writing to disk and just keep reading from
2167 result
= write_len_
;
2168 } else if (!done_reading_
&& entry_
) {
2169 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
2170 int64 body_size
= response_
.headers
->GetContentLength();
2171 if (body_size
>= 0 && body_size
<= current_size
)
2172 done_reading_
= true;
2175 if (partial_
.get()) {
2176 // This may be the last request.
2177 if (!(result
== 0 && !truncated_
&&
2178 (partial_
->IsLastRange() || mode_
== WRITE
)))
2179 return DoPartialNetworkReadCompleted(result
);
2183 // End of file. This may be the result of a connection problem so see if we
2184 // have to keep the entry around to be flagged as truncated later on.
2185 if (done_reading_
|| !entry_
|| partial_
.get() ||
2186 response_
.headers
->GetContentLength() <= 0)
2187 DoneWritingToEntry(true);
2193 //-----------------------------------------------------------------------------
2195 void HttpCache::Transaction::ReadCertChain() {
2197 GetCacheKeyForCert(response_
.ssl_info
.cert
->os_cert_handle());
2198 const X509Certificate::OSCertHandles
& intermediates
=
2199 response_
.ssl_info
.cert
->GetIntermediateCertificates();
2200 int dist_from_root
= intermediates
.size();
2202 scoped_refptr
<SharedChainData
> shared_chain_data(
2203 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
2204 cache_
->cert_cache()->GetCertificate(key
,
2205 base::Bind(&OnCertReadIOComplete
,
2208 shared_chain_data
));
2210 for (X509Certificate::OSCertHandles::const_iterator it
=
2211 intermediates
.begin();
2212 it
!= intermediates
.end();
2215 key
= GetCacheKeyForCert(*it
);
2216 cache_
->cert_cache()->GetCertificate(key
,
2217 base::Bind(&OnCertReadIOComplete
,
2219 false /* is not leaf */,
2220 shared_chain_data
));
2222 DCHECK_EQ(0, dist_from_root
);
2225 void HttpCache::Transaction::WriteCertChain() {
2226 const X509Certificate::OSCertHandles
& intermediates
=
2227 response_
.ssl_info
.cert
->GetIntermediateCertificates();
2228 int dist_from_root
= intermediates
.size();
2230 scoped_refptr
<SharedChainData
> shared_chain_data(
2231 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
2232 cache_
->cert_cache()->SetCertificate(
2233 response_
.ssl_info
.cert
->os_cert_handle(),
2234 base::Bind(&OnCertWriteIOComplete
,
2237 shared_chain_data
));
2238 for (X509Certificate::OSCertHandles::const_iterator it
=
2239 intermediates
.begin();
2240 it
!= intermediates
.end();
2243 cache_
->cert_cache()->SetCertificate(*it
,
2244 base::Bind(&OnCertWriteIOComplete
,
2246 false /* is not leaf */,
2247 shared_chain_data
));
2249 DCHECK_EQ(0, dist_from_root
);
2252 void HttpCache::Transaction::SetRequest(const BoundNetLog
& net_log
,
2253 const HttpRequestInfo
* request
) {
2256 effective_load_flags_
= request_
->load_flags
;
2258 if (cache_
->mode() == DISABLE
)
2259 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2261 // Some headers imply load flags. The order here is significant.
2263 // LOAD_DISABLE_CACHE : no cache read or write
2264 // LOAD_BYPASS_CACHE : no cache read
2265 // LOAD_VALIDATE_CACHE : no cache read unless validation
2267 // The former modes trump latter modes, so if we find a matching header we
2268 // can stop iterating kSpecialHeaders.
2270 static const struct {
2271 const HeaderNameAndValue
* search
;
2273 } kSpecialHeaders
[] = {
2274 { kPassThroughHeaders
, LOAD_DISABLE_CACHE
},
2275 { kForceFetchHeaders
, LOAD_BYPASS_CACHE
},
2276 { kForceValidateHeaders
, LOAD_VALIDATE_CACHE
},
2279 bool range_found
= false;
2280 bool external_validation_error
= false;
2281 bool special_headers
= false;
2283 if (request_
->extra_headers
.HasHeader(HttpRequestHeaders::kRange
))
2286 for (size_t i
= 0; i
< arraysize(kSpecialHeaders
); ++i
) {
2287 if (HeaderMatches(request_
->extra_headers
, kSpecialHeaders
[i
].search
)) {
2288 effective_load_flags_
|= kSpecialHeaders
[i
].load_flag
;
2289 special_headers
= true;
2294 // Check for conditionalization headers which may correspond with a
2295 // cache validation request.
2296 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); ++i
) {
2297 const ValidationHeaderInfo
& info
= kValidationHeaders
[i
];
2298 std::string validation_value
;
2299 if (request_
->extra_headers
.GetHeader(
2300 info
.request_header_name
, &validation_value
)) {
2301 if (!external_validation_
.values
[i
].empty() ||
2302 validation_value
.empty()) {
2303 external_validation_error
= true;
2305 external_validation_
.values
[i
] = validation_value
;
2306 external_validation_
.initialized
= true;
2310 if (range_found
|| special_headers
|| external_validation_
.initialized
) {
2311 // Log the headers before request_ is modified.
2314 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS
,
2315 base::Bind(&HttpRequestHeaders::NetLogCallback
,
2316 base::Unretained(&request_
->extra_headers
), &empty
));
2319 // We don't support ranges and validation headers.
2320 if (range_found
&& external_validation_
.initialized
) {
2321 LOG(WARNING
) << "Byte ranges AND validation headers found.";
2322 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2325 // If there is more than one validation header, we can't treat this request as
2326 // a cache validation, since we don't know for sure which header the server
2327 // will give us a response for (and they could be contradictory).
2328 if (external_validation_error
) {
2329 LOG(WARNING
) << "Multiple or malformed validation headers found.";
2330 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2333 if (range_found
&& !(effective_load_flags_
& LOAD_DISABLE_CACHE
)) {
2334 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2335 partial_
.reset(new PartialData
);
2336 if (request_
->method
== "GET" && partial_
->Init(request_
->extra_headers
)) {
2337 // We will be modifying the actual range requested to the server, so
2338 // let's remove the header here.
2339 custom_request_
.reset(new HttpRequestInfo(*request_
));
2340 custom_request_
->extra_headers
.RemoveHeader(HttpRequestHeaders::kRange
);
2341 request_
= custom_request_
.get();
2342 partial_
->SetHeaders(custom_request_
->extra_headers
);
2344 // The range is invalid or we cannot handle it properly.
2345 VLOG(1) << "Invalid byte range found.";
2346 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2347 partial_
.reset(NULL
);
2352 bool HttpCache::Transaction::ShouldPassThrough() {
2353 // We may have a null disk_cache if there is an error we cannot recover from,
2354 // like not enough disk space, or sharing violations.
2355 if (!cache_
->disk_cache_
.get())
2358 if (effective_load_flags_
& LOAD_DISABLE_CACHE
)
2361 if (request_
->method
== "GET" || request_
->method
== "HEAD")
2364 if (request_
->method
== "POST" && request_
->upload_data_stream
&&
2365 request_
->upload_data_stream
->identifier()) {
2369 if (request_
->method
== "PUT" && request_
->upload_data_stream
)
2372 if (request_
->method
== "DELETE")
2378 int HttpCache::Transaction::BeginCacheRead() {
2379 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2380 if (response_
.headers
->response_code() == 206 || partial_
.get()) {
2382 return ERR_CACHE_MISS
;
2385 if (request_
->method
== "HEAD")
2386 FixHeadersForHead();
2388 // We don't have the whole resource.
2390 return ERR_CACHE_MISS
;
2392 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2393 next_state_
= STATE_CACHE_READ_METADATA
;
2398 int HttpCache::Transaction::BeginCacheValidation() {
2399 DCHECK(mode_
== READ_WRITE
);
2401 ValidationType required_validation
= RequiresValidation();
2403 bool skip_validation
= (required_validation
== VALIDATION_NONE
);
2405 if (required_validation
== VALIDATION_ASYNCHRONOUS
&&
2406 !(request_
->method
== "GET" && (truncated_
|| partial_
)) && cache_
&&
2407 cache_
->use_stale_while_revalidate()) {
2408 TriggerAsyncValidation();
2409 skip_validation
= true;
2412 if (request_
->method
== "HEAD" &&
2413 (truncated_
|| response_
.headers
->response_code() == 206)) {
2415 if (skip_validation
)
2416 return SetupEntryForRead();
2419 next_state_
= STATE_SEND_REQUEST
;
2425 // Truncated entries can cause partial gets, so we shouldn't record this
2426 // load in histograms.
2427 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2428 skip_validation
= !partial_
->initial_validation();
2431 if (partial_
.get() && (is_sparse_
|| truncated_
) &&
2432 (!partial_
->IsCurrentRangeCached() || invalid_range_
)) {
2433 // Force revalidation for sparse or truncated entries. Note that we don't
2434 // want to ignore the regular validation logic just because a byte range was
2435 // part of the request.
2436 skip_validation
= false;
2439 if (skip_validation
) {
2440 // TODO(ricea): Is this pattern okay for asynchronous revalidations?
2441 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
2442 RecordOfflineStatus(effective_load_flags_
, OFFLINE_STATUS_FRESH_CACHE
);
2443 return SetupEntryForRead();
2445 // Make the network request conditional, to see if we may reuse our cached
2446 // response. If we cannot do so, then we just resort to a normal fetch.
2447 // Our mode remains READ_WRITE for a conditional request. Even if the
2448 // conditionalization fails, we don't switch to WRITE mode until we
2449 // know we won't be falling back to using the cache entry in the
2450 // LOAD_FROM_CACHE_IF_OFFLINE case.
2451 if (!ConditionalizeRequest()) {
2452 couldnt_conditionalize_request_
= true;
2453 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE
);
2455 return DoRestartPartialRequest();
2457 DCHECK_NE(206, response_
.headers
->response_code());
2459 next_state_
= STATE_SEND_REQUEST
;
2464 int HttpCache::Transaction::BeginPartialCacheValidation() {
2465 DCHECK(mode_
== READ_WRITE
);
2467 if (response_
.headers
->response_code() != 206 && !partial_
.get() &&
2469 return BeginCacheValidation();
2472 // Partial requests should not be recorded in histograms.
2473 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2474 if (range_requested_
) {
2475 next_state_
= STATE_CACHE_QUERY_DATA
;
2479 // The request is not for a range, but we have stored just ranges.
2481 if (request_
->method
== "HEAD")
2482 return BeginCacheValidation();
2484 partial_
.reset(new PartialData());
2485 partial_
->SetHeaders(request_
->extra_headers
);
2486 if (!custom_request_
.get()) {
2487 custom_request_
.reset(new HttpRequestInfo(*request_
));
2488 request_
= custom_request_
.get();
2491 return ValidateEntryHeadersAndContinue();
2494 // This should only be called once per request.
2495 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2496 DCHECK(mode_
== READ_WRITE
);
2498 if (!partial_
->UpdateFromStoredHeaders(
2499 response_
.headers
.get(), entry_
->disk_entry
, truncated_
)) {
2500 return DoRestartPartialRequest();
2503 if (response_
.headers
->response_code() == 206)
2506 if (!partial_
->IsRequestedRangeOK()) {
2507 // The stored data is fine, but the request may be invalid.
2508 invalid_range_
= true;
2511 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2515 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2516 DCHECK_EQ(UPDATE
, mode_
);
2517 DCHECK(external_validation_
.initialized
);
2519 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); i
++) {
2520 if (external_validation_
.values
[i
].empty())
2522 // Retrieve either the cached response's "etag" or "last-modified" header.
2523 std::string validator
;
2524 response_
.headers
->EnumerateHeader(
2526 kValidationHeaders
[i
].related_response_header_name
,
2529 if (response_
.headers
->response_code() != 200 || truncated_
||
2530 validator
.empty() || validator
!= external_validation_
.values
[i
]) {
2531 // The externally conditionalized request is not a validation request
2532 // for our existing cache entry. Proceed with caching disabled.
2533 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2534 DoneWritingToEntry(true);
2538 // TODO(ricea): This calculation is expensive to perform just to collect
2539 // statistics. Either remove it or use the result, depending on the result of
2541 ExternallyConditionalizedType type
=
2542 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE
;
2544 type
= EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS
;
2545 else if (RequiresValidation())
2546 type
= EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION
;
2548 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2549 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2550 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2552 EXTERNALLY_CONDITIONALIZED_MAX
);
2554 next_state_
= STATE_SEND_REQUEST
;
2558 int HttpCache::Transaction::RestartNetworkRequest() {
2559 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2560 DCHECK(network_trans_
.get());
2561 DCHECK_EQ(STATE_NONE
, next_state_
);
2563 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2564 int rv
= network_trans_
->RestartIgnoringLastError(io_callback_
);
2565 if (rv
!= ERR_IO_PENDING
)
2570 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2571 X509Certificate
* client_cert
) {
2572 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2573 DCHECK(network_trans_
.get());
2574 DCHECK_EQ(STATE_NONE
, next_state_
);
2576 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2577 int rv
= network_trans_
->RestartWithCertificate(client_cert
, io_callback_
);
2578 if (rv
!= ERR_IO_PENDING
)
2583 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2584 const AuthCredentials
& credentials
) {
2585 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2586 DCHECK(network_trans_
.get());
2587 DCHECK_EQ(STATE_NONE
, next_state_
);
2589 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2590 int rv
= network_trans_
->RestartWithAuth(credentials
, io_callback_
);
2591 if (rv
!= ERR_IO_PENDING
)
2596 ValidationType
HttpCache::Transaction::RequiresValidation() {
2597 // TODO(darin): need to do more work here:
2598 // - make sure we have a matching request method
2599 // - watch out for cached responses that depend on authentication
2601 if (response_
.vary_data
.is_valid() &&
2602 !response_
.vary_data
.MatchesRequest(*request_
,
2603 *response_
.headers
.get())) {
2604 vary_mismatch_
= true;
2605 return VALIDATION_SYNCHRONOUS
;
2608 if (effective_load_flags_
& LOAD_PREFERRING_CACHE
)
2609 return VALIDATION_NONE
;
2611 if (response_
.unused_since_prefetch
&&
2612 !(effective_load_flags_
& LOAD_PREFETCH
) &&
2613 response_
.headers
->GetCurrentAge(
2614 response_
.request_time
, response_
.response_time
,
2615 cache_
->clock_
->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins
)) {
2616 // The first use of a resource after prefetch within a short window skips
2618 return VALIDATION_NONE
;
2621 if (effective_load_flags_
& (LOAD_VALIDATE_CACHE
| LOAD_ASYNC_REVALIDATION
))
2622 return VALIDATION_SYNCHRONOUS
;
2624 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2625 return VALIDATION_SYNCHRONOUS
;
2627 ValidationType validation_required_by_headers
=
2628 response_
.headers
->RequiresValidation(response_
.request_time
,
2629 response_
.response_time
,
2630 cache_
->clock_
->Now());
2632 if (validation_required_by_headers
== VALIDATION_ASYNCHRONOUS
) {
2633 // Asynchronous revalidation is only supported for GET and HEAD methods.
2634 if (request_
->method
!= "GET" && request_
->method
!= "HEAD")
2635 return VALIDATION_SYNCHRONOUS
;
2638 return validation_required_by_headers
;
2641 bool HttpCache::Transaction::ConditionalizeRequest() {
2642 DCHECK(response_
.headers
.get());
2644 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2647 // This only makes sense for cached 200 or 206 responses.
2648 if (response_
.headers
->response_code() != 200 &&
2649 response_
.headers
->response_code() != 206) {
2653 if (fail_conditionalization_for_test_
)
2656 DCHECK(response_
.headers
->response_code() != 206 ||
2657 response_
.headers
->HasStrongValidators());
2659 // Just use the first available ETag and/or Last-Modified header value.
2660 // TODO(darin): Or should we use the last?
2662 std::string etag_value
;
2663 if (response_
.headers
->GetHttpVersion() >= HttpVersion(1, 1))
2664 response_
.headers
->EnumerateHeader(NULL
, "etag", &etag_value
);
2666 std::string last_modified_value
;
2667 if (!vary_mismatch_
) {
2668 response_
.headers
->EnumerateHeader(NULL
, "last-modified",
2669 &last_modified_value
);
2672 if (etag_value
.empty() && last_modified_value
.empty())
2675 if (!partial_
.get()) {
2676 // Need to customize the request, so this forces us to allocate :(
2677 custom_request_
.reset(new HttpRequestInfo(*request_
));
2678 request_
= custom_request_
.get();
2680 DCHECK(custom_request_
.get());
2682 bool use_if_range
= partial_
.get() && !partial_
->IsCurrentRangeCached() &&
2685 if (!use_if_range
) {
2686 // stale-while-revalidate is not useful when we only have a partial response
2687 // cached, so don't set the header in that case.
2688 HttpResponseHeaders::FreshnessLifetimes lifetimes
=
2689 response_
.headers
->GetFreshnessLifetimes(response_
.response_time
);
2690 if (lifetimes
.staleness
> TimeDelta()) {
2691 TimeDelta current_age
= response_
.headers
->GetCurrentAge(
2692 response_
.request_time
, response_
.response_time
,
2693 cache_
->clock_
->Now());
2695 custom_request_
->extra_headers
.SetHeader(
2697 base::StringPrintf("max-age=%" PRId64
2698 ",stale-while-revalidate=%" PRId64
",age=%" PRId64
,
2699 lifetimes
.freshness
.InSeconds(),
2700 lifetimes
.staleness
.InSeconds(),
2701 current_age
.InSeconds()));
2705 if (!etag_value
.empty()) {
2707 // We don't want to switch to WRITE mode if we don't have this block of a
2708 // byte-range request because we may have other parts cached.
2709 custom_request_
->extra_headers
.SetHeader(
2710 HttpRequestHeaders::kIfRange
, etag_value
);
2712 custom_request_
->extra_headers
.SetHeader(
2713 HttpRequestHeaders::kIfNoneMatch
, etag_value
);
2715 // For byte-range requests, make sure that we use only one way to validate
2717 if (partial_
.get() && !partial_
->IsCurrentRangeCached())
2721 if (!last_modified_value
.empty()) {
2723 custom_request_
->extra_headers
.SetHeader(
2724 HttpRequestHeaders::kIfRange
, last_modified_value
);
2726 custom_request_
->extra_headers
.SetHeader(
2727 HttpRequestHeaders::kIfModifiedSince
, last_modified_value
);
2734 // We just received some headers from the server. We may have asked for a range,
2735 // in which case partial_ has an object. This could be the first network request
2736 // we make to fulfill the original request, or we may be already reading (from
2737 // the net and / or the cache). If we are not expecting a certain response, we
2738 // just bypass the cache for this request (but again, maybe we are reading), and
2739 // delete partial_ (so we are not able to "fix" the headers that we return to
2740 // the user). This results in either a weird response for the caller (we don't
2741 // expect it after all), or maybe a range that was not exactly what it was asked
2744 // If the server is simply telling us that the resource has changed, we delete
2745 // the cached entry and restart the request as the caller intended (by returning
2746 // false from this method). However, we may not be able to do that at any point,
2747 // for instance if we already returned the headers to the user.
2749 // WARNING: Whenever this code returns false, it has to make sure that the next
2750 // time it is called it will return true so that we don't keep retrying the
2752 bool HttpCache::Transaction::ValidatePartialResponse() {
2753 const HttpResponseHeaders
* headers
= new_response_
->headers
.get();
2754 int response_code
= headers
->response_code();
2755 bool partial_response
= (response_code
== 206);
2756 handling_206_
= false;
2758 if (!entry_
|| request_
->method
!= "GET")
2761 if (invalid_range_
) {
2762 // We gave up trying to match this request with the stored data. If the
2763 // server is ok with the request, delete the entry, otherwise just ignore
2766 if (partial_response
|| response_code
== 200) {
2767 DoomPartialEntry(true);
2770 if (response_code
== 304)
2772 IgnoreRangeRequest();
2777 if (!partial_
.get()) {
2778 // We are not expecting 206 but we may have one.
2779 if (partial_response
)
2780 IgnoreRangeRequest();
2785 // TODO(rvargas): Do we need to consider other results here?.
2786 bool failure
= response_code
== 200 || response_code
== 416;
2788 if (partial_
->IsCurrentRangeCached()) {
2789 // We asked for "If-None-Match: " so a 206 means a new object.
2790 if (partial_response
)
2793 if (response_code
== 304 && partial_
->ResponseHeadersOK(headers
))
2796 // We asked for "If-Range: " so a 206 means just another range.
2797 if (partial_response
) {
2798 if (partial_
->ResponseHeadersOK(headers
)) {
2799 handling_206_
= true;
2806 if (!reading_
&& !is_sparse_
&& !partial_response
) {
2807 // See if we can ignore the fact that we issued a byte range request.
2808 // If the server sends 200, just store it. If it sends an error, redirect
2809 // or something else, we may store the response as long as we didn't have
2810 // anything already stored.
2811 if (response_code
== 200 ||
2812 (!truncated_
&& response_code
!= 304 && response_code
!= 416)) {
2813 // The server is sending something else, and we can save it.
2814 DCHECK((truncated_
&& !partial_
->IsLastRange()) || range_requested_
);
2821 // 304 is not expected here, but we'll spare the entry (unless it was
2828 // We cannot truncate this entry, it has to be deleted.
2829 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2831 if (is_sparse_
|| truncated_
) {
2832 // There was something cached to start with, either sparsed data (206), or
2833 // a truncated 200, which means that we probably modified the request,
2834 // adding a byte range or modifying the range requested by the caller.
2835 if (!reading_
&& !partial_
->IsLastRange()) {
2836 // We have not returned anything to the caller yet so it should be safe
2837 // to issue another network request, this time without us messing up the
2839 ResetPartialState(true);
2842 LOG(WARNING
) << "Failed to revalidate partial entry";
2844 DoomPartialEntry(true);
2848 IgnoreRangeRequest();
2852 void HttpCache::Transaction::IgnoreRangeRequest() {
2853 // We have a problem. We may or may not be reading already (in which case we
2854 // returned the headers), but we'll just pretend that this request is not
2855 // using the cache and see what happens. Most likely this is the first
2856 // response from the server (it's not changing its mind midway, right?).
2857 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2859 DoneWritingToEntry(mode_
!= WRITE
);
2860 else if (mode_
& READ
&& entry_
)
2861 cache_
->DoneReadingFromEntry(entry_
, this);
2863 partial_
.reset(NULL
);
2868 void HttpCache::Transaction::FixHeadersForHead() {
2869 if (response_
.headers
->response_code() == 206) {
2870 response_
.headers
->RemoveHeader("Content-Range");
2871 response_
.headers
->ReplaceStatusLine("HTTP/1.1 200 OK");
2875 void HttpCache::Transaction::TriggerAsyncValidation() {
2876 DCHECK(!request_
->upload_data_stream
);
2877 BoundNetLog
async_revalidation_net_log(
2878 BoundNetLog::Make(net_log_
.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION
));
2880 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC
,
2881 async_revalidation_net_log
.source().ToEventParametersCallback());
2882 async_revalidation_net_log
.BeginEvent(
2883 NetLog::TYPE_ASYNC_REVALIDATION
,
2885 &NetLogAsyncRevalidationInfoCallback
, net_log_
.source(), request_
));
2886 base::MessageLoop::current()->PostTask(
2888 base::Bind(&HttpCache::PerformAsyncValidation
,
2889 cache_
, // cache_ is a weak pointer.
2891 async_revalidation_net_log
));
2894 void HttpCache::Transaction::FailRangeRequest() {
2895 response_
= *new_response_
;
2896 partial_
->FixResponseHeaders(response_
.headers
.get(), false);
2899 int HttpCache::Transaction::SetupEntryForRead() {
2901 ResetNetworkTransaction();
2902 if (partial_
.get()) {
2903 if (truncated_
|| is_sparse_
|| !invalid_range_
) {
2904 // We are going to return the saved response headers to the caller, so
2905 // we may need to adjust them first.
2906 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
2912 cache_
->ConvertWriterToReader(entry_
);
2915 if (request_
->method
== "HEAD")
2916 FixHeadersForHead();
2918 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2919 next_state_
= STATE_CACHE_READ_METADATA
;
2924 int HttpCache::Transaction::ReadFromNetwork(IOBuffer
* data
, int data_len
) {
2926 io_buf_len_
= data_len
;
2927 next_state_
= STATE_NETWORK_READ
;
2931 int HttpCache::Transaction::ReadFromEntry(IOBuffer
* data
, int data_len
) {
2932 if (request_
->method
== "HEAD")
2936 io_buf_len_
= data_len
;
2937 next_state_
= STATE_CACHE_READ_DATA
;
2941 int HttpCache::Transaction::WriteToEntry(int index
, int offset
,
2942 IOBuffer
* data
, int data_len
,
2943 const CompletionCallback
& callback
) {
2948 if (!partial_
.get() || !data_len
) {
2949 rv
= entry_
->disk_entry
->WriteData(index
, offset
, data
, data_len
, callback
,
2952 rv
= partial_
->CacheWrite(entry_
->disk_entry
, data
, data_len
, callback
);
2957 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated
) {
2958 next_state_
= STATE_CACHE_WRITE_RESPONSE_COMPLETE
;
2962 // Do not cache no-store content. Do not cache content with cert errors
2963 // either. This is to prevent not reporting net errors when loading a
2964 // resource from the cache. When we load a page over HTTPS with a cert error
2965 // we show an SSL blocking page. If the user clicks proceed we reload the
2966 // resource ignoring the errors. The loaded resource is then cached. If that
2967 // resource is subsequently loaded from the cache, no net error is reported
2968 // (even though the cert status contains the actual errors) and no SSL
2969 // blocking page is shown. An alternative would be to reverse-map the cert
2970 // status to a net error and replay the net error.
2971 if ((response_
.headers
->HasHeaderValue("cache-control", "no-store")) ||
2972 net::IsCertStatusError(response_
.ssl_info
.cert_status
)) {
2973 DoneWritingToEntry(false);
2974 if (net_log_
.IsLogging())
2975 net_log_
.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
2979 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2980 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
2984 DCHECK_EQ(200, response_
.headers
->response_code());
2986 // When writing headers, we normally only write the non-transient headers.
2987 bool skip_transient_headers
= true;
2988 scoped_refptr
<PickledIOBuffer
> data(new PickledIOBuffer());
2989 response_
.Persist(data
->pickle(), skip_transient_headers
, truncated
);
2992 io_buf_len_
= data
->pickle()->size();
2993 return entry_
->disk_entry
->WriteData(kResponseInfoIndex
, 0, data
.get(),
2994 io_buf_len_
, io_callback_
, true);
2997 int HttpCache::Transaction::AppendResponseDataToEntry(
2998 IOBuffer
* data
, int data_len
, const CompletionCallback
& callback
) {
2999 if (!entry_
|| !data_len
)
3002 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
3003 return WriteToEntry(kResponseContentIndex
, current_size
, data
, data_len
,
3007 void HttpCache::Transaction::DoneWritingToEntry(bool success
) {
3013 cache_
->DoneWritingToEntry(entry_
, success
);
3015 mode_
= NONE
; // switch to 'pass through' mode
3018 int HttpCache::Transaction::OnCacheReadError(int result
, bool restart
) {
3019 DLOG(ERROR
) << "ReadData failed: " << result
;
3020 const int result_for_histogram
= std::max(0, -result
);
3022 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
3023 result_for_histogram
);
3025 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
3026 result_for_histogram
);
3029 // Avoid using this entry in the future.
3031 cache_
->DoomActiveEntry(cache_key_
);
3035 DCHECK(!network_trans_
.get());
3036 cache_
->DoneWithEntry(entry_
, this, false);
3040 next_state_
= STATE_GET_BACKEND
;
3044 return ERR_CACHE_READ_FAILURE
;
3047 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time
) {
3048 if (entry_lock_waiting_since_
!= start_time
)
3051 DCHECK_EQ(next_state_
, STATE_ADD_TO_ENTRY_COMPLETE
);
3056 cache_
->RemovePendingTransaction(this);
3057 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT
);
3060 void HttpCache::Transaction::DoomPartialEntry(bool delete_object
) {
3061 DVLOG(2) << "DoomPartialEntry";
3062 int rv
= cache_
->DoomEntry(cache_key_
, NULL
);
3064 cache_
->DoneWithEntry(entry_
, this, false);
3069 partial_
.reset(NULL
);
3072 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result
) {
3073 partial_
->OnNetworkReadCompleted(result
);
3076 // We need to move on to the next range.
3077 ResetNetworkTransaction();
3078 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
3083 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result
) {
3084 partial_
->OnCacheReadCompleted(result
);
3086 if (result
== 0 && mode_
== READ_WRITE
) {
3087 // We need to move on to the next range.
3088 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
3089 } else if (result
< 0) {
3090 return OnCacheReadError(result
, false);
3095 int HttpCache::Transaction::DoRestartPartialRequest() {
3096 // The stored data cannot be used. Get rid of it and restart this request.
3097 net_log_
.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST
);
3099 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
3100 // to Doom the entry again).
3102 ResetPartialState(!range_requested_
);
3103 next_state_
= STATE_CREATE_ENTRY
;
3107 void HttpCache::Transaction::ResetPartialState(bool delete_object
) {
3108 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
3109 DoomPartialEntry(delete_object
);
3111 if (!delete_object
) {
3112 // The simplest way to re-initialize partial_ is to create a new object.
3113 partial_
.reset(new PartialData());
3114 if (partial_
->Init(request_
->extra_headers
))
3115 partial_
->SetHeaders(custom_request_
->extra_headers
);
3121 void HttpCache::Transaction::ResetNetworkTransaction() {
3122 DCHECK(!old_network_trans_load_timing_
);
3123 DCHECK(network_trans_
);
3124 LoadTimingInfo load_timing
;
3125 if (network_trans_
->GetLoadTimingInfo(&load_timing
))
3126 old_network_trans_load_timing_
.reset(new LoadTimingInfo(load_timing
));
3127 total_received_bytes_
+= network_trans_
->GetTotalReceivedBytes();
3128 network_trans_
.reset();
3131 // Histogram data from the end of 2010 show the following distribution of
3132 // response headers:
3134 // Content-Length............... 87%
3135 // Date......................... 98%
3136 // Last-Modified................ 49%
3137 // Etag......................... 19%
3138 // Accept-Ranges: bytes......... 25%
3139 // Accept-Ranges: none.......... 0.4%
3140 // Strong Validator............. 50%
3141 // Strong Validator + ranges.... 24%
3142 // Strong Validator + CL........ 49%
3144 bool HttpCache::Transaction::CanResume(bool has_data
) {
3145 // Double check that there is something worth keeping.
3146 if (has_data
&& !entry_
->disk_entry
->GetDataSize(kResponseContentIndex
))
3149 if (request_
->method
!= "GET")
3152 // Note that if this is a 206, content-length was already fixed after calling
3153 // PartialData::ResponseHeadersOK().
3154 if (response_
.headers
->GetContentLength() <= 0 ||
3155 response_
.headers
->HasHeaderValue("Accept-Ranges", "none") ||
3156 !response_
.headers
->HasStrongValidators()) {
3163 void HttpCache::Transaction::UpdateTransactionPattern(
3164 TransactionPattern new_transaction_pattern
) {
3165 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
3167 DCHECK(transaction_pattern_
== PATTERN_UNDEFINED
||
3168 new_transaction_pattern
== PATTERN_NOT_COVERED
);
3169 transaction_pattern_
= new_transaction_pattern
;
3172 void HttpCache::Transaction::RecordHistograms() {
3173 DCHECK_NE(PATTERN_UNDEFINED
, transaction_pattern_
);
3174 if (!cache_
.get() || !cache_
->GetCurrentBackend() ||
3175 cache_
->GetCurrentBackend()->GetCacheType() != DISK_CACHE
||
3176 cache_
->mode() != NORMAL
|| request_
->method
!= "GET") {
3179 UMA_HISTOGRAM_ENUMERATION(
3180 "HttpCache.Pattern", transaction_pattern_
, PATTERN_MAX
);
3181 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
3183 DCHECK(!range_requested_
);
3184 DCHECK(!first_cache_access_since_
.is_null());
3186 TimeDelta total_time
= base::TimeTicks::Now() - first_cache_access_since_
;
3188 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time
);
3190 bool did_send_request
= !send_request_since_
.is_null();
3192 (did_send_request
&&
3193 (transaction_pattern_
== PATTERN_ENTRY_NOT_CACHED
||
3194 transaction_pattern_
== PATTERN_ENTRY_VALIDATED
||
3195 transaction_pattern_
== PATTERN_ENTRY_UPDATED
||
3196 transaction_pattern_
== PATTERN_ENTRY_CANT_CONDITIONALIZE
)) ||
3197 (!did_send_request
&& transaction_pattern_
== PATTERN_ENTRY_USED
));
3199 if (!did_send_request
) {
3200 DCHECK(transaction_pattern_
== PATTERN_ENTRY_USED
);
3201 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time
);
3205 TimeDelta before_send_time
= send_request_since_
- first_cache_access_since_
;
3206 int64 before_send_percent
= (total_time
.ToInternalValue() == 0) ?
3207 0 : before_send_time
* 100 / total_time
;
3208 DCHECK_GE(before_send_percent
, 0);
3209 DCHECK_LE(before_send_percent
, 100);
3210 base::HistogramBase::Sample before_send_sample
=
3211 static_cast<base::HistogramBase::Sample
>(before_send_percent
);
3213 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time
);
3214 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time
);
3215 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample
);
3217 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
3218 // below this comment after we have received initial data.
3219 switch (transaction_pattern_
) {
3220 case PATTERN_ENTRY_CANT_CONDITIONALIZE
: {
3221 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
3223 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
3224 before_send_sample
);
3227 case PATTERN_ENTRY_NOT_CACHED
: {
3228 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time
);
3229 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
3230 before_send_sample
);
3233 case PATTERN_ENTRY_VALIDATED
: {
3234 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time
);
3235 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
3236 before_send_sample
);
3239 case PATTERN_ENTRY_UPDATED
: {
3240 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time
);
3241 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
3242 before_send_sample
);
3250 void HttpCache::Transaction::OnIOComplete(int result
) {
3251 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
3252 tracked_objects::ScopedTracker
tracking_profile(
3253 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 Transaction::OnIOComplete"));