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/metrics/field_trial.h"
21 #include "base/metrics/histogram.h"
22 #include "base/metrics/sparse_histogram.h"
23 #include "base/rand_util.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_piece.h"
26 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h"
28 #include "base/time/time.h"
29 #include "net/base/completion_callback.h"
30 #include "net/base/io_buffer.h"
31 #include "net/base/load_flags.h"
32 #include "net/base/load_timing_info.h"
33 #include "net/base/net_errors.h"
34 #include "net/base/net_log.h"
35 #include "net/base/upload_data_stream.h"
36 #include "net/cert/cert_status_flags.h"
37 #include "net/disk_cache/disk_cache.h"
38 #include "net/http/disk_based_cert_cache.h"
39 #include "net/http/http_network_session.h"
40 #include "net/http/http_request_info.h"
41 #include "net/http/http_response_headers.h"
42 #include "net/http/http_transaction.h"
43 #include "net/http/http_util.h"
44 #include "net/http/partial_data.h"
45 #include "net/ssl/ssl_cert_request_info.h"
46 #include "net/ssl/ssl_config_service.h"
49 using base::TimeDelta
;
50 using base::TimeTicks
;
54 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
55 static const char kFreshnessHeader
[] = "Resource-Freshness";
57 // Stores data relevant to the statistics of writing and reading entire
58 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
59 // of certificates in the chain that have pending operations in the
60 // DiskBasedCertCache. |start_time| is the time that the read and write
61 // commands began being issued to the DiskBasedCertCache.
62 // TODO(brandonsalmon): Remove this when it is no longer necessary to
64 class SharedChainData
: public base::RefCounted
<SharedChainData
> {
66 SharedChainData(int num_ops
, TimeTicks start
)
67 : num_pending_ops(num_ops
), start_time(start
) {}
73 friend class base::RefCounted
<SharedChainData
>;
75 DISALLOW_COPY_AND_ASSIGN(SharedChainData
);
78 // Used to obtain a cache entry key for an OSCertHandle.
79 // TODO(brandonsalmon): Remove this when cache keys are stored
80 // and no longer have to be recomputed to retrieve the OSCertHandle
82 std::string
GetCacheKeyForCert(net::X509Certificate::OSCertHandle cert_handle
) {
83 net::SHA1HashValue fingerprint
=
84 net::X509Certificate::CalculateFingerprint(cert_handle
);
87 base::HexEncode(fingerprint
.data
, arraysize(fingerprint
.data
));
90 // |dist_from_root| indicates the position of the read certificate in the
91 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
92 // whether or not the read certificate was the leaf of the chain.
93 // |shared_chain_data| contains data shared by each certificate in
95 void OnCertReadIOComplete(
98 const scoped_refptr
<SharedChainData
>& shared_chain_data
,
99 net::X509Certificate::OSCertHandle cert_handle
) {
100 // If |num_pending_ops| is one, this was the last pending read operation
101 // for this chain of certificates. The total time used to read the chain
102 // can be calculated by subtracting the starting time from Now().
103 shared_chain_data
->num_pending_ops
--;
104 if (!shared_chain_data
->num_pending_ops
) {
105 const TimeDelta read_chain_wait
=
106 TimeTicks::Now() - shared_chain_data
->start_time
;
107 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
109 base::TimeDelta::FromMilliseconds(1),
110 base::TimeDelta::FromMinutes(10),
114 bool success
= (cert_handle
!= NULL
);
116 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success
);
119 UMA_HISTOGRAM_CUSTOM_COUNTS(
120 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root
, 0, 10, 7);
122 UMA_HISTOGRAM_CUSTOM_COUNTS(
123 "DiskBasedCertCache.CertIoReadFailure", dist_from_root
, 0, 10, 7);
126 // |dist_from_root| indicates the position of the written certificate in the
127 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
128 // whether or not the written certificate was the leaf of the chain.
129 // |shared_chain_data| contains data shared by each certificate in
131 void OnCertWriteIOComplete(
134 const scoped_refptr
<SharedChainData
>& shared_chain_data
,
135 const std::string
& key
) {
136 // If |num_pending_ops| is one, this was the last pending write operation
137 // for this chain of certificates. The total time used to write the chain
138 // can be calculated by subtracting the starting time from Now().
139 shared_chain_data
->num_pending_ops
--;
140 if (!shared_chain_data
->num_pending_ops
) {
141 const TimeDelta write_chain_wait
=
142 TimeTicks::Now() - shared_chain_data
->start_time
;
143 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
145 base::TimeDelta::FromMilliseconds(1),
146 base::TimeDelta::FromMinutes(10),
150 bool success
= !key
.empty();
152 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success
);
155 UMA_HISTOGRAM_CUSTOM_COUNTS(
156 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root
, 0, 10, 7);
158 UMA_HISTOGRAM_CUSTOM_COUNTS(
159 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root
, 0, 10, 7);
162 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
163 // a "non-error response" is one with a 2xx (Successful) or 3xx
164 // (Redirection) status code.
165 bool NonErrorResponse(int status_code
) {
166 int status_code_range
= status_code
/ 100;
167 return status_code_range
== 2 || status_code_range
== 3;
170 // Error codes that will be considered indicative of a page being offline/
171 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
172 bool IsOfflineError(int error
) {
173 return (error
== net::ERR_NAME_NOT_RESOLVED
||
174 error
== net::ERR_INTERNET_DISCONNECTED
||
175 error
== net::ERR_ADDRESS_UNREACHABLE
||
176 error
== net::ERR_CONNECTION_TIMED_OUT
);
179 // Enum for UMA, indicating the status (with regard to offline mode) of
180 // a particular request.
181 enum RequestOfflineStatus
{
182 // A cache transaction hit in cache (data was present and not stale)
184 OFFLINE_STATUS_FRESH_CACHE
,
186 // A network request was required for a cache entry, and it succeeded.
187 OFFLINE_STATUS_NETWORK_SUCCEEDED
,
189 // A network request was required for a cache entry, and it failed with
190 // a non-offline error.
191 OFFLINE_STATUS_NETWORK_FAILED
,
193 // A network request was required for a cache entry, it failed with an
194 // offline error, and we could serve stale data if
195 // LOAD_FROM_CACHE_IF_OFFLINE was set.
196 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
,
198 // A network request was required for a cache entry, it failed with
199 // an offline error, and there was no servable data in cache (even
201 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
,
203 OFFLINE_STATUS_MAX_ENTRIES
206 void RecordOfflineStatus(int load_flags
, RequestOfflineStatus status
) {
207 // Restrict to main frame to keep statistics close to
208 // "would have shown them something useful if offline mode was enabled".
209 if (load_flags
& net::LOAD_MAIN_FRAME
) {
210 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status
,
211 OFFLINE_STATUS_MAX_ENTRIES
);
215 // TODO(rvargas): Remove once we get the data.
216 void RecordVaryHeaderHistogram(const net::HttpResponseInfo
* response
) {
223 VaryType vary
= VARY_NOT_PRESENT
;
224 if (response
->vary_data
.is_valid()) {
226 if (response
->headers
->HasHeaderValue("vary", "user-agent"))
229 UMA_HISTOGRAM_ENUMERATION("HttpCache.Vary", vary
, VARY_MAX
);
232 void RecordNoStoreHeaderHistogram(int load_flags
,
233 const net::HttpResponseInfo
* response
) {
234 if (load_flags
& net::LOAD_MAIN_FRAME
) {
235 UMA_HISTOGRAM_BOOLEAN(
236 "Net.MainFrameNoStore",
237 response
->headers
->HasHeaderValue("cache-control", "no-store"));
245 struct HeaderNameAndValue
{
250 // If the request includes one of these request headers, then avoid caching
251 // to avoid getting confused.
252 static const HeaderNameAndValue kPassThroughHeaders
[] = {
253 { "if-unmodified-since", NULL
}, // causes unexpected 412s
254 { "if-match", NULL
}, // causes unexpected 412s
255 { "if-range", NULL
},
259 struct ValidationHeaderInfo
{
260 const char* request_header_name
;
261 const char* related_response_header_name
;
264 static const ValidationHeaderInfo kValidationHeaders
[] = {
265 { "if-modified-since", "last-modified" },
266 { "if-none-match", "etag" },
269 // If the request includes one of these request headers, then avoid reusing
270 // our cached copy if any.
271 static const HeaderNameAndValue kForceFetchHeaders
[] = {
272 { "cache-control", "no-cache" },
273 { "pragma", "no-cache" },
277 // If the request includes one of these request headers, then force our
278 // cached copy (if any) to be revalidated before reusing it.
279 static const HeaderNameAndValue kForceValidateHeaders
[] = {
280 { "cache-control", "max-age=0" },
284 static bool HeaderMatches(const HttpRequestHeaders
& headers
,
285 const HeaderNameAndValue
* search
) {
286 for (; search
->name
; ++search
) {
287 std::string header_value
;
288 if (!headers
.GetHeader(search
->name
, &header_value
))
294 HttpUtil::ValuesIterator
v(header_value
.begin(), header_value
.end(), ',');
295 while (v
.GetNext()) {
296 if (LowerCaseEqualsASCII(v
.value_begin(), v
.value_end(), search
->value
))
303 //-----------------------------------------------------------------------------
305 HttpCache::Transaction::Transaction(
306 RequestPriority priority
,
308 : next_state_(STATE_NONE
),
311 cache_(cache
->GetWeakPtr()),
316 target_state_(STATE_NONE
),
318 invalid_range_(false),
321 range_requested_(false),
322 handling_206_(false),
323 cache_pending_(false),
324 done_reading_(false),
325 vary_mismatch_(false),
326 couldnt_conditionalize_request_(false),
327 bypass_lock_for_test_(false),
330 effective_load_flags_(0),
333 io_callback_(base::Bind(&Transaction::OnIOComplete
,
334 weak_factory_
.GetWeakPtr())),
335 transaction_pattern_(PATTERN_UNDEFINED
),
336 total_received_bytes_(0),
337 websocket_handshake_stream_base_create_helper_(NULL
) {
338 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders
==
339 arraysize(kValidationHeaders
),
340 Invalid_number_of_validation_headers
);
343 HttpCache::Transaction::~Transaction() {
344 // We may have to issue another IO, but we should never invoke the callback_
350 bool cancel_request
= reading_
&& response_
.headers
;
351 if (cancel_request
) {
353 entry_
->disk_entry
->CancelSparseIO();
355 cancel_request
&= (response_
.headers
->response_code() == 200);
359 cache_
->DoneWithEntry(entry_
, this, cancel_request
);
360 } else if (cache_pending_
) {
361 cache_
->RemovePendingTransaction(this);
366 int HttpCache::Transaction::WriteMetadata(IOBuffer
* buf
, int buf_len
,
367 const CompletionCallback
& callback
) {
369 DCHECK_GT(buf_len
, 0);
370 DCHECK(!callback
.is_null());
371 if (!cache_
.get() || !entry_
)
372 return ERR_UNEXPECTED
;
374 // We don't need to track this operation for anything.
375 // It could be possible to check if there is something already written and
376 // avoid writing again (it should be the same, right?), but let's allow the
377 // caller to "update" the contents with something new.
378 return entry_
->disk_entry
->WriteData(kMetadataIndex
, 0, buf
, buf_len
,
382 bool HttpCache::Transaction::AddTruncatedFlag() {
383 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
385 // Don't set the flag for sparse entries.
386 if (partial_
.get() && !truncated_
)
389 if (!CanResume(true))
392 // We may have received the whole resource already.
397 target_state_
= STATE_NONE
;
398 next_state_
= STATE_CACHE_WRITE_TRUNCATED_RESPONSE
;
403 LoadState
HttpCache::Transaction::GetWriterLoadState() const {
404 if (network_trans_
.get())
405 return network_trans_
->GetLoadState();
406 if (entry_
|| !request_
)
407 return LOAD_STATE_IDLE
;
408 return LOAD_STATE_WAITING_FOR_CACHE
;
411 const BoundNetLog
& HttpCache::Transaction::net_log() const {
415 int HttpCache::Transaction::Start(const HttpRequestInfo
* request
,
416 const CompletionCallback
& callback
,
417 const BoundNetLog
& net_log
) {
419 DCHECK(!callback
.is_null());
421 // Ensure that we only have one asynchronous call at a time.
422 DCHECK(callback_
.is_null());
424 DCHECK(!network_trans_
.get());
428 return ERR_UNEXPECTED
;
430 SetRequest(net_log
, request
);
432 // We have to wait until the backend is initialized so we start the SM.
433 next_state_
= STATE_GET_BACKEND
;
436 // Setting this here allows us to check for the existence of a callback_ to
437 // determine if we are still inside Start.
438 if (rv
== ERR_IO_PENDING
)
439 callback_
= callback
;
444 int HttpCache::Transaction::RestartIgnoringLastError(
445 const CompletionCallback
& callback
) {
446 DCHECK(!callback
.is_null());
448 // Ensure that we only have one asynchronous call at a time.
449 DCHECK(callback_
.is_null());
452 return ERR_UNEXPECTED
;
454 int rv
= RestartNetworkRequest();
456 if (rv
== ERR_IO_PENDING
)
457 callback_
= callback
;
462 int HttpCache::Transaction::RestartWithCertificate(
463 X509Certificate
* client_cert
,
464 const CompletionCallback
& callback
) {
465 DCHECK(!callback
.is_null());
467 // Ensure that we only have one asynchronous call at a time.
468 DCHECK(callback_
.is_null());
471 return ERR_UNEXPECTED
;
473 int rv
= RestartNetworkRequestWithCertificate(client_cert
);
475 if (rv
== ERR_IO_PENDING
)
476 callback_
= callback
;
481 int HttpCache::Transaction::RestartWithAuth(
482 const AuthCredentials
& credentials
,
483 const CompletionCallback
& callback
) {
484 DCHECK(auth_response_
.headers
.get());
485 DCHECK(!callback
.is_null());
487 // Ensure that we only have one asynchronous call at a time.
488 DCHECK(callback_
.is_null());
491 return ERR_UNEXPECTED
;
493 // Clear the intermediate response since we are going to start over.
494 auth_response_
= HttpResponseInfo();
496 int rv
= RestartNetworkRequestWithAuth(credentials
);
498 if (rv
== ERR_IO_PENDING
)
499 callback_
= callback
;
504 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
505 if (!network_trans_
.get())
507 return network_trans_
->IsReadyToRestartForAuth();
510 int HttpCache::Transaction::Read(IOBuffer
* buf
, int buf_len
,
511 const CompletionCallback
& callback
) {
513 DCHECK_GT(buf_len
, 0);
514 DCHECK(!callback
.is_null());
516 DCHECK(callback_
.is_null());
519 return ERR_UNEXPECTED
;
521 // If we have an intermediate auth response at this point, then it means the
522 // user wishes to read the network response (the error page). If there is a
523 // previous response in the cache then we should leave it intact.
524 if (auth_response_
.headers
.get() && mode_
!= NONE
) {
525 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
526 DCHECK(mode_
& WRITE
);
527 DoneWritingToEntry(mode_
== READ_WRITE
);
536 DCHECK(partial_
.get());
537 if (!network_trans_
.get()) {
538 // We are just reading from the cache, but we may be writing later.
539 rv
= ReadFromEntry(buf
, buf_len
);
544 DCHECK(network_trans_
.get());
545 rv
= ReadFromNetwork(buf
, buf_len
);
548 rv
= ReadFromEntry(buf
, buf_len
);
555 if (rv
== ERR_IO_PENDING
) {
556 DCHECK(callback_
.is_null());
557 callback_
= callback
;
562 void HttpCache::Transaction::StopCaching() {
563 // We really don't know where we are now. Hopefully there is no operation in
564 // progress, but nothing really prevents this method to be called after we
565 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
566 // point because we need the state machine for that (and even if we are really
567 // free, that would be an asynchronous operation). In other words, keep the
568 // entry how it is (it will be marked as truncated at destruction), and let
569 // the next piece of code that executes know that we are now reading directly
571 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
572 // future request for the resource will be blocked on this one.
574 if (cache_
.get() && entry_
&& (mode_
& WRITE
) && network_trans_
.get() &&
575 !is_sparse_
&& !range_requested_
) {
580 bool HttpCache::Transaction::GetFullRequestHeaders(
581 HttpRequestHeaders
* headers
) const {
583 return network_trans_
->GetFullRequestHeaders(headers
);
585 // TODO(ttuttle): Read headers from cache.
589 int64
HttpCache::Transaction::GetTotalReceivedBytes() const {
590 int64 total_received_bytes
= total_received_bytes_
;
592 total_received_bytes
+= network_trans_
->GetTotalReceivedBytes();
593 return total_received_bytes
;
596 void HttpCache::Transaction::DoneReading() {
597 if (cache_
.get() && entry_
) {
598 DCHECK_NE(mode_
, UPDATE
);
600 DoneWritingToEntry(true);
601 } else if (mode_
& READ
) {
602 // It is necessary to check mode_ & READ because it is possible
603 // for mode_ to be NONE and entry_ non-NULL with a write entry
604 // if StopCaching was called.
605 cache_
->DoneReadingFromEntry(entry_
, this);
611 const HttpResponseInfo
* HttpCache::Transaction::GetResponseInfo() const {
612 // Null headers means we encountered an error or haven't a response yet
613 if (auth_response_
.headers
.get())
614 return &auth_response_
;
615 return (response_
.headers
.get() || response_
.ssl_info
.cert
.get() ||
616 response_
.cert_request_info
.get())
621 LoadState
HttpCache::Transaction::GetLoadState() const {
622 LoadState state
= GetWriterLoadState();
623 if (state
!= LOAD_STATE_WAITING_FOR_CACHE
)
627 return cache_
->GetLoadStateForPendingTransaction(this);
629 return LOAD_STATE_IDLE
;
632 UploadProgress
HttpCache::Transaction::GetUploadProgress() const {
633 if (network_trans_
.get())
634 return network_trans_
->GetUploadProgress();
635 return final_upload_progress_
;
638 void HttpCache::Transaction::SetQuicServerInfo(
639 QuicServerInfo
* quic_server_info
) {}
641 bool HttpCache::Transaction::GetLoadTimingInfo(
642 LoadTimingInfo
* load_timing_info
) const {
644 return network_trans_
->GetLoadTimingInfo(load_timing_info
);
646 if (old_network_trans_load_timing_
) {
647 *load_timing_info
= *old_network_trans_load_timing_
;
651 if (first_cache_access_since_
.is_null())
654 // If the cache entry was opened, return that time.
655 load_timing_info
->send_start
= first_cache_access_since_
;
656 // This time doesn't make much sense when reading from the cache, so just use
657 // the same time as send_start.
658 load_timing_info
->send_end
= first_cache_access_since_
;
662 void HttpCache::Transaction::SetPriority(RequestPriority priority
) {
663 priority_
= priority
;
665 network_trans_
->SetPriority(priority_
);
668 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
669 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
670 websocket_handshake_stream_base_create_helper_
= create_helper
;
672 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(create_helper
);
675 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
676 const BeforeNetworkStartCallback
& callback
) {
677 DCHECK(!network_trans_
);
678 before_network_start_callback_
= callback
;
681 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
682 const BeforeProxyHeadersSentCallback
& callback
) {
683 DCHECK(!network_trans_
);
684 before_proxy_headers_sent_callback_
= callback
;
687 int HttpCache::Transaction::ResumeNetworkStart() {
689 return network_trans_
->ResumeNetworkStart();
690 return ERR_UNEXPECTED
;
693 //-----------------------------------------------------------------------------
695 void HttpCache::Transaction::DoCallback(int rv
) {
696 DCHECK(rv
!= ERR_IO_PENDING
);
697 DCHECK(!callback_
.is_null());
699 read_buf_
= NULL
; // Release the buffer before invoking the callback.
701 // Since Run may result in Read being called, clear callback_ up front.
702 CompletionCallback c
= callback_
;
707 int HttpCache::Transaction::HandleResult(int rv
) {
708 DCHECK(rv
!= ERR_IO_PENDING
);
709 if (!callback_
.is_null())
715 // A few common patterns: (Foo* means Foo -> FooComplete)
717 // 1. Not-cached entry:
719 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
720 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
721 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
722 // PartialHeadersReceived
725 // NetworkRead* -> CacheWriteData*
727 // 2. Cached entry, no validation:
729 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
730 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
731 // SetupEntryForRead()
736 // 3. Cached entry, validation (304):
738 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
739 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
740 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
741 // CacheWriteResponse* -> UpdateCachedResponseComplete ->
742 // OverwriteCachedResponse -> PartialHeadersReceived
747 // 4. Cached entry, validation and replace (200):
749 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
750 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
751 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
752 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
753 // PartialHeadersReceived
756 // NetworkRead* -> CacheWriteData*
758 // 5. Sparse entry, partially cached, byte range request:
760 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
761 // -> BeginPartialCacheValidation() -> CacheQueryData* ->
762 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
763 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
764 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
765 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
766 // PartialHeadersReceived
769 // NetworkRead* -> CacheWriteData*
772 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
773 // CompletePartialCacheValidation -> CacheReadData* ->
776 // CacheReadData* -> StartPartialCacheValidation ->
777 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
778 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
779 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
781 // 6. HEAD. Not-cached entry:
782 // Pass through. Don't save a HEAD by itself.
784 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
786 // 7. HEAD. Cached entry, no validation:
788 // The same flow as for a GET request (example #2)
791 // CacheReadData (returns 0)
793 // 8. HEAD. Cached entry, validation (304):
794 // The request updates the stored headers.
795 // Start(): Same as for a GET request (example #3)
798 // CacheReadData (returns 0)
800 // 9. HEAD. Cached entry, validation and replace (200):
801 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
804 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
805 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
806 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse
808 // 10. HEAD. Sparse entry, partially cached:
809 // Serve the request from the cache, as long as it doesn't require
810 // revalidation. Ignore missing ranges when deciding to revalidate. If the
811 // entry requires revalidation, ignore the whole request and go to full pass
812 // through (the result of the HEAD request will NOT update the entry).
814 // Start(): Basically the same as example 7, as we never create a partial_
815 // object for this request.
817 int HttpCache::Transaction::DoLoop(int result
) {
818 DCHECK(next_state_
!= STATE_NONE
);
822 State state
= next_state_
;
823 next_state_
= STATE_NONE
;
825 case STATE_GET_BACKEND
:
829 case STATE_GET_BACKEND_COMPLETE
:
830 rv
= DoGetBackendComplete(rv
);
832 case STATE_SEND_REQUEST
:
834 rv
= DoSendRequest();
836 case STATE_SEND_REQUEST_COMPLETE
:
837 rv
= DoSendRequestComplete(rv
);
839 case STATE_SUCCESSFUL_SEND_REQUEST
:
841 rv
= DoSuccessfulSendRequest();
843 case STATE_NETWORK_READ
:
845 rv
= DoNetworkRead();
847 case STATE_NETWORK_READ_COMPLETE
:
848 rv
= DoNetworkReadComplete(rv
);
850 case STATE_INIT_ENTRY
:
854 case STATE_OPEN_ENTRY
:
858 case STATE_OPEN_ENTRY_COMPLETE
:
859 rv
= DoOpenEntryComplete(rv
);
861 case STATE_CREATE_ENTRY
:
863 rv
= DoCreateEntry();
865 case STATE_CREATE_ENTRY_COMPLETE
:
866 rv
= DoCreateEntryComplete(rv
);
868 case STATE_DOOM_ENTRY
:
872 case STATE_DOOM_ENTRY_COMPLETE
:
873 rv
= DoDoomEntryComplete(rv
);
875 case STATE_ADD_TO_ENTRY
:
879 case STATE_ADD_TO_ENTRY_COMPLETE
:
880 rv
= DoAddToEntryComplete(rv
);
882 case STATE_START_PARTIAL_CACHE_VALIDATION
:
884 rv
= DoStartPartialCacheValidation();
886 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
:
887 rv
= DoCompletePartialCacheValidation(rv
);
889 case STATE_UPDATE_CACHED_RESPONSE
:
891 rv
= DoUpdateCachedResponse();
893 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE
:
894 rv
= DoUpdateCachedResponseComplete(rv
);
896 case STATE_OVERWRITE_CACHED_RESPONSE
:
898 rv
= DoOverwriteCachedResponse();
900 case STATE_TRUNCATE_CACHED_DATA
:
902 rv
= DoTruncateCachedData();
904 case STATE_TRUNCATE_CACHED_DATA_COMPLETE
:
905 rv
= DoTruncateCachedDataComplete(rv
);
907 case STATE_TRUNCATE_CACHED_METADATA
:
909 rv
= DoTruncateCachedMetadata();
911 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE
:
912 rv
= DoTruncateCachedMetadataComplete(rv
);
914 case STATE_PARTIAL_HEADERS_RECEIVED
:
916 rv
= DoPartialHeadersReceived();
918 case STATE_CACHE_READ_RESPONSE
:
920 rv
= DoCacheReadResponse();
922 case STATE_CACHE_READ_RESPONSE_COMPLETE
:
923 rv
= DoCacheReadResponseComplete(rv
);
925 case STATE_CACHE_WRITE_RESPONSE
:
927 rv
= DoCacheWriteResponse();
929 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
931 rv
= DoCacheWriteTruncatedResponse();
933 case STATE_CACHE_WRITE_RESPONSE_COMPLETE
:
934 rv
= DoCacheWriteResponseComplete(rv
);
936 case STATE_CACHE_READ_METADATA
:
938 rv
= DoCacheReadMetadata();
940 case STATE_CACHE_READ_METADATA_COMPLETE
:
941 rv
= DoCacheReadMetadataComplete(rv
);
943 case STATE_CACHE_QUERY_DATA
:
945 rv
= DoCacheQueryData();
947 case STATE_CACHE_QUERY_DATA_COMPLETE
:
948 rv
= DoCacheQueryDataComplete(rv
);
950 case STATE_CACHE_READ_DATA
:
952 rv
= DoCacheReadData();
954 case STATE_CACHE_READ_DATA_COMPLETE
:
955 rv
= DoCacheReadDataComplete(rv
);
957 case STATE_CACHE_WRITE_DATA
:
958 rv
= DoCacheWriteData(rv
);
960 case STATE_CACHE_WRITE_DATA_COMPLETE
:
961 rv
= DoCacheWriteDataComplete(rv
);
964 NOTREACHED() << "bad state";
968 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
970 if (rv
!= ERR_IO_PENDING
)
976 int HttpCache::Transaction::DoGetBackend() {
977 cache_pending_
= true;
978 next_state_
= STATE_GET_BACKEND_COMPLETE
;
979 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
);
980 return cache_
->GetBackendForTransaction(this);
983 int HttpCache::Transaction::DoGetBackendComplete(int result
) {
984 DCHECK(result
== OK
|| result
== ERR_FAILED
);
985 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
,
987 cache_pending_
= false;
989 if (!ShouldPassThrough()) {
990 cache_key_
= cache_
->GenerateCacheKey(request_
);
992 // Requested cache access mode.
993 if (effective_load_flags_
& LOAD_ONLY_FROM_CACHE
) {
995 } else if (effective_load_flags_
& LOAD_BYPASS_CACHE
) {
1001 // Downgrade to UPDATE if the request has been externally conditionalized.
1002 if (external_validation_
.initialized
) {
1003 if (mode_
& WRITE
) {
1004 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1005 // in case READ was off).
1013 // Use PUT and DELETE only to invalidate existing stored entries.
1014 if ((request_
->method
== "PUT" || request_
->method
== "DELETE") &&
1015 mode_
!= READ_WRITE
&& mode_
!= WRITE
) {
1019 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1020 // transaction behaves the same for GET and HEAD requests at this point: if it
1021 // was not modified, the entry is updated and a response is not returned from
1022 // the cache. If we receive 200, it doesn't matter if there was a validation
1024 if (request_
->method
== "HEAD" && mode_
== WRITE
)
1027 // If must use cache, then we must fail. This can happen for back/forward
1028 // navigations to a page generated via a form post.
1029 if (!(mode_
& READ
) && effective_load_flags_
& LOAD_ONLY_FROM_CACHE
)
1030 return ERR_CACHE_MISS
;
1032 if (mode_
== NONE
) {
1033 if (partial_
.get()) {
1034 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1037 next_state_
= STATE_SEND_REQUEST
;
1039 next_state_
= STATE_INIT_ENTRY
;
1042 // This is only set if we have something to do with the response.
1043 range_requested_
= (partial_
.get() != NULL
);
1048 int HttpCache::Transaction::DoSendRequest() {
1049 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1050 DCHECK(!network_trans_
.get());
1052 send_request_since_
= TimeTicks::Now();
1054 // Create a network transaction.
1055 int rv
= cache_
->network_layer_
->CreateTransaction(priority_
,
1059 network_trans_
->SetBeforeNetworkStartCallback(before_network_start_callback_
);
1060 network_trans_
->SetBeforeProxyHeadersSentCallback(
1061 before_proxy_headers_sent_callback_
);
1063 // Old load timing information, if any, is now obsolete.
1064 old_network_trans_load_timing_
.reset();
1066 if (websocket_handshake_stream_base_create_helper_
)
1067 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(
1068 websocket_handshake_stream_base_create_helper_
);
1070 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1071 rv
= network_trans_
->Start(request_
, io_callback_
, net_log_
);
1075 int HttpCache::Transaction::DoSendRequestComplete(int result
) {
1077 return ERR_UNEXPECTED
;
1079 // If requested, and we have a readable cache entry, and we have
1080 // an error indicating that we're offline as opposed to in contact
1081 // with a bad server, read from cache anyway.
1082 if (IsOfflineError(result
)) {
1083 if (mode_
== READ_WRITE
&& entry_
&& !partial_
) {
1084 RecordOfflineStatus(effective_load_flags_
,
1085 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
);
1086 if (effective_load_flags_
& LOAD_FROM_CACHE_IF_OFFLINE
) {
1087 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1088 response_
.server_data_unavailable
= true;
1089 return SetupEntryForRead();
1092 RecordOfflineStatus(effective_load_flags_
,
1093 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
);
1096 RecordOfflineStatus(effective_load_flags_
,
1097 (result
== OK
? OFFLINE_STATUS_NETWORK_SUCCEEDED
:
1098 OFFLINE_STATUS_NETWORK_FAILED
));
1101 // If we tried to conditionalize the request and failed, we know
1102 // we won't be reading from the cache after this point.
1103 if (couldnt_conditionalize_request_
)
1107 next_state_
= STATE_SUCCESSFUL_SEND_REQUEST
;
1111 // Do not record requests that have network errors or restarts.
1112 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1113 if (IsCertificateError(result
)) {
1114 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
1115 // If we get a certificate error, then there is a certificate in ssl_info,
1116 // so GetResponseInfo() should never return NULL here.
1118 response_
.ssl_info
= response
->ssl_info
;
1119 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
1120 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
1122 response_
.cert_request_info
= response
->cert_request_info
;
1123 } else if (response_
.was_cached
) {
1124 DoneWritingToEntry(true);
1129 // We received the response headers and there is no error.
1130 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1131 DCHECK(!new_response_
);
1132 const HttpResponseInfo
* new_response
= network_trans_
->GetResponseInfo();
1133 bool authentication_failure
= false;
1135 if (new_response
->headers
->response_code() == 401 ||
1136 new_response
->headers
->response_code() == 407) {
1137 auth_response_
= *new_response
;
1141 // We initiated a second request the caller doesn't know about. We should be
1142 // able to authenticate this request because we should have authenticated
1143 // this URL moments ago.
1144 if (IsReadyToRestartForAuth()) {
1145 DCHECK(!response_
.auth_challenge
.get());
1146 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1147 // In theory we should check to see if there are new cookies, but there
1148 // is no way to do that from here.
1149 return network_trans_
->RestartWithAuth(AuthCredentials(), io_callback_
);
1152 // We have to perform cleanup at this point so that at least the next
1153 // request can succeed.
1154 authentication_failure
= true;
1156 DoomPartialEntry(false);
1161 new_response_
= new_response
;
1162 if (authentication_failure
||
1163 (!ValidatePartialResponse() && !auth_response_
.headers
.get())) {
1164 // Something went wrong with this request and we have to restart it.
1165 // If we have an authentication response, we are exposed to weird things
1166 // hapenning if the user cancels the authentication before we receive
1167 // the new response.
1168 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1169 response_
= HttpResponseInfo();
1170 ResetNetworkTransaction();
1171 new_response_
= NULL
;
1172 next_state_
= STATE_SEND_REQUEST
;
1176 if (handling_206_
&& mode_
== READ_WRITE
&& !truncated_
&& !is_sparse_
) {
1177 // We have stored the full entry, but it changed and the server is
1178 // sending a range. We have to delete the old entry.
1179 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1180 DoneWritingToEntry(false);
1183 if (mode_
== WRITE
&&
1184 transaction_pattern_
!= PATTERN_ENTRY_CANT_CONDITIONALIZE
) {
1185 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED
);
1188 if (mode_
== WRITE
&&
1189 (request_
->method
== "PUT" || request_
->method
== "DELETE")) {
1190 if (NonErrorResponse(new_response
->headers
->response_code())) {
1191 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1194 cache_
->DoneWritingToEntry(entry_
, true);
1199 if (request_
->method
== "POST" &&
1200 NonErrorResponse(new_response
->headers
->response_code())) {
1201 cache_
->DoomMainEntryForUrl(request_
->url
);
1204 RecordVaryHeaderHistogram(new_response
);
1205 RecordNoStoreHeaderHistogram(request_
->load_flags
, new_response
);
1207 if (new_response_
->headers
->response_code() == 416 &&
1208 (request_
->method
== "GET" || request_
->method
== "POST")) {
1209 // If there is an active entry it may be destroyed with this transaction.
1210 response_
= *new_response_
;
1214 // Are we expecting a response to a conditional query?
1215 if (mode_
== READ_WRITE
|| mode_
== UPDATE
) {
1216 if (new_response
->headers
->response_code() == 304 || handling_206_
) {
1217 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED
);
1218 next_state_
= STATE_UPDATE_CACHED_RESPONSE
;
1221 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED
);
1225 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1229 int HttpCache::Transaction::DoNetworkRead() {
1230 next_state_
= STATE_NETWORK_READ_COMPLETE
;
1231 return network_trans_
->Read(read_buf_
.get(), io_buf_len_
, io_callback_
);
1234 int HttpCache::Transaction::DoNetworkReadComplete(int result
) {
1235 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1238 return ERR_UNEXPECTED
;
1240 // If there is an error or we aren't saving the data, we are done; just wait
1241 // until the destructor runs to see if we can keep the data.
1242 if (mode_
== NONE
|| result
< 0)
1245 next_state_
= STATE_CACHE_WRITE_DATA
;
1249 int HttpCache::Transaction::DoInitEntry() {
1250 DCHECK(!new_entry_
);
1253 return ERR_UNEXPECTED
;
1255 if (mode_
== WRITE
) {
1256 next_state_
= STATE_DOOM_ENTRY
;
1260 next_state_
= STATE_OPEN_ENTRY
;
1264 int HttpCache::Transaction::DoOpenEntry() {
1265 DCHECK(!new_entry_
);
1266 next_state_
= STATE_OPEN_ENTRY_COMPLETE
;
1267 cache_pending_
= true;
1268 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
);
1269 first_cache_access_since_
= TimeTicks::Now();
1270 return cache_
->OpenEntry(cache_key_
, &new_entry_
, this);
1273 int HttpCache::Transaction::DoOpenEntryComplete(int result
) {
1274 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1275 // OK, otherwise the cache will end up with an active entry without any
1276 // transaction attached.
1277 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
, result
);
1278 cache_pending_
= false;
1280 next_state_
= STATE_ADD_TO_ENTRY
;
1284 if (result
== ERR_CACHE_RACE
) {
1285 next_state_
= STATE_INIT_ENTRY
;
1289 if (request_
->method
== "PUT" || request_
->method
== "DELETE" ||
1290 (request_
->method
== "HEAD" && mode_
== READ_WRITE
)) {
1291 DCHECK(mode_
== READ_WRITE
|| mode_
== WRITE
|| request_
->method
== "HEAD");
1293 next_state_
= STATE_SEND_REQUEST
;
1297 if (mode_
== READ_WRITE
) {
1299 next_state_
= STATE_CREATE_ENTRY
;
1302 if (mode_
== UPDATE
) {
1303 // There is no cache entry to update; proceed without caching.
1305 next_state_
= STATE_SEND_REQUEST
;
1308 if (cache_
->mode() == PLAYBACK
)
1309 DVLOG(1) << "Playback Cache Miss: " << request_
->url
;
1311 // The entry does not exist, and we are not permitted to create a new entry,
1313 return ERR_CACHE_MISS
;
1316 int HttpCache::Transaction::DoCreateEntry() {
1317 DCHECK(!new_entry_
);
1318 next_state_
= STATE_CREATE_ENTRY_COMPLETE
;
1319 cache_pending_
= true;
1320 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
);
1321 return cache_
->CreateEntry(cache_key_
, &new_entry_
, this);
1324 int HttpCache::Transaction::DoCreateEntryComplete(int result
) {
1325 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1326 // OK, otherwise the cache will end up with an active entry without any
1327 // transaction attached.
1328 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
,
1330 cache_pending_
= false;
1331 next_state_
= STATE_ADD_TO_ENTRY
;
1333 if (result
== ERR_CACHE_RACE
) {
1334 next_state_
= STATE_INIT_ENTRY
;
1339 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", false);
1341 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", true);
1342 // We have a race here: Maybe we failed to open the entry and decided to
1343 // create one, but by the time we called create, another transaction already
1344 // created the entry. If we want to eliminate this issue, we need an atomic
1345 // OpenOrCreate() method exposed by the disk cache.
1346 DLOG(WARNING
) << "Unable to create cache entry";
1349 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1350 next_state_
= STATE_SEND_REQUEST
;
1355 int HttpCache::Transaction::DoDoomEntry() {
1356 next_state_
= STATE_DOOM_ENTRY_COMPLETE
;
1357 cache_pending_
= true;
1358 if (first_cache_access_since_
.is_null())
1359 first_cache_access_since_
= TimeTicks::Now();
1360 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
);
1361 return cache_
->DoomEntry(cache_key_
, this);
1364 int HttpCache::Transaction::DoDoomEntryComplete(int result
) {
1365 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
, result
);
1366 next_state_
= STATE_CREATE_ENTRY
;
1367 cache_pending_
= false;
1368 if (result
== ERR_CACHE_RACE
)
1369 next_state_
= STATE_INIT_ENTRY
;
1373 int HttpCache::Transaction::DoAddToEntry() {
1375 cache_pending_
= true;
1376 next_state_
= STATE_ADD_TO_ENTRY_COMPLETE
;
1377 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
);
1378 DCHECK(entry_lock_waiting_since_
.is_null());
1379 entry_lock_waiting_since_
= TimeTicks::Now();
1380 int rv
= cache_
->AddTransactionToEntry(new_entry_
, this);
1381 if (rv
== ERR_IO_PENDING
) {
1382 if (bypass_lock_for_test_
) {
1383 OnAddToEntryTimeout(entry_lock_waiting_since_
);
1385 const int kTimeoutSeconds
= 20;
1386 base::MessageLoop::current()->PostDelayedTask(
1388 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout
,
1389 weak_factory_
.GetWeakPtr(), entry_lock_waiting_since_
),
1390 TimeDelta::FromSeconds(kTimeoutSeconds
));
1396 int HttpCache::Transaction::DoAddToEntryComplete(int result
) {
1397 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
,
1399 const TimeDelta entry_lock_wait
=
1400 TimeTicks::Now() - entry_lock_waiting_since_
;
1401 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait
);
1403 entry_lock_waiting_since_
= TimeTicks();
1405 cache_pending_
= false;
1408 entry_
= new_entry_
;
1410 // If there is a failure, the cache should have taken care of new_entry_.
1413 if (result
== ERR_CACHE_RACE
) {
1414 next_state_
= STATE_INIT_ENTRY
;
1418 if (result
== ERR_CACHE_LOCK_TIMEOUT
) {
1419 // The cache is busy, bypass it for this transaction.
1421 next_state_
= STATE_SEND_REQUEST
;
1423 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1434 if (mode_
== WRITE
) {
1436 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1437 next_state_
= STATE_SEND_REQUEST
;
1439 // We have to read the headers from the cached entry.
1440 DCHECK(mode_
& READ_META
);
1441 next_state_
= STATE_CACHE_READ_RESPONSE
;
1446 // We may end up here multiple times for a given request.
1447 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1451 next_state_
= STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
;
1452 return partial_
->ShouldValidateCache(entry_
->disk_entry
, io_callback_
);
1455 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result
) {
1457 // This is the end of the request.
1458 if (mode_
& WRITE
) {
1459 DoneWritingToEntry(true);
1461 cache_
->DoneReadingFromEntry(entry_
, this);
1470 partial_
->PrepareCacheValidation(entry_
->disk_entry
,
1471 &custom_request_
->extra_headers
);
1473 if (reading_
&& partial_
->IsCurrentRangeCached()) {
1474 next_state_
= STATE_CACHE_READ_DATA
;
1478 return BeginCacheValidation();
1481 // We received 304 or 206 and we want to update the cached response headers.
1482 int HttpCache::Transaction::DoUpdateCachedResponse() {
1483 next_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1485 // Update cached response based on headers in new_response.
1486 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1487 response_
.headers
->Update(*new_response_
->headers
.get());
1488 response_
.response_time
= new_response_
->response_time
;
1489 response_
.request_time
= new_response_
->request_time
;
1490 response_
.network_accessed
= new_response_
->network_accessed
;
1492 if (response_
.headers
->HasHeaderValue("cache-control", "no-store")) {
1493 if (!entry_
->doomed
) {
1494 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1498 // If we are already reading, we already updated the headers for this
1499 // request; doing it again will change Content-Length.
1501 target_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1502 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1509 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result
) {
1510 if (mode_
== UPDATE
) {
1511 DCHECK(!handling_206_
);
1512 // We got a "not modified" response and already updated the corresponding
1513 // cache entry above.
1515 // By closing the cached entry now, we make sure that the 304 rather than
1516 // the cached 200 response, is what will be returned to the user.
1517 DoneWritingToEntry(true);
1518 } else if (entry_
&& !handling_206_
) {
1519 DCHECK_EQ(READ_WRITE
, mode_
);
1520 if (!partial_
.get() || partial_
->IsLastRange()) {
1521 cache_
->ConvertWriterToReader(entry_
);
1524 // We no longer need the network transaction, so destroy it.
1525 final_upload_progress_
= network_trans_
->GetUploadProgress();
1526 ResetNetworkTransaction();
1527 } else if (entry_
&& handling_206_
&& truncated_
&&
1528 partial_
->initial_validation()) {
1529 // We just finished the validation of a truncated entry, and the server
1530 // is willing to resume the operation. Now we go back and start serving
1531 // the first part to the user.
1532 ResetNetworkTransaction();
1533 new_response_
= NULL
;
1534 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
1535 partial_
->SetRangeToStartDownload();
1538 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1542 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1544 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1548 // We change the value of Content-Length for partial content.
1549 if (handling_206_
&& partial_
.get())
1550 partial_
->FixContentLength(new_response_
->headers
.get());
1552 response_
= *new_response_
;
1554 if (request_
->method
== "HEAD") {
1555 // This response is replacing the cached one.
1556 DoneWritingToEntry(false);
1558 new_response_
= NULL
;
1562 if (handling_206_
&& !CanResume(false)) {
1563 // There is no point in storing this resource because it will never be used.
1564 DoneWritingToEntry(false);
1566 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1567 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1571 target_state_
= STATE_TRUNCATE_CACHED_DATA
;
1572 next_state_
= truncated_
? STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
1573 STATE_CACHE_WRITE_RESPONSE
;
1577 int HttpCache::Transaction::DoTruncateCachedData() {
1578 next_state_
= STATE_TRUNCATE_CACHED_DATA_COMPLETE
;
1581 if (net_log_
.IsLogging())
1582 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1583 // Truncate the stream.
1584 return WriteToEntry(kResponseContentIndex
, 0, NULL
, 0, io_callback_
);
1587 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result
) {
1589 if (net_log_
.IsLogging()) {
1590 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1595 next_state_
= STATE_TRUNCATE_CACHED_METADATA
;
1599 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1600 next_state_
= STATE_TRUNCATE_CACHED_METADATA_COMPLETE
;
1604 if (net_log_
.IsLogging())
1605 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1606 return WriteToEntry(kMetadataIndex
, 0, NULL
, 0, io_callback_
);
1609 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result
) {
1611 if (net_log_
.IsLogging()) {
1612 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1617 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1621 int HttpCache::Transaction::DoPartialHeadersReceived() {
1622 new_response_
= NULL
;
1623 if (entry_
&& !partial_
.get() &&
1624 entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
1625 next_state_
= STATE_CACHE_READ_METADATA
;
1627 if (!partial_
.get())
1631 if (network_trans_
.get()) {
1632 next_state_
= STATE_NETWORK_READ
;
1634 next_state_
= STATE_CACHE_READ_DATA
;
1636 } else if (mode_
!= NONE
) {
1637 // We are about to return the headers for a byte-range request to the user,
1638 // so let's fix them.
1639 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1644 int HttpCache::Transaction::DoCacheReadResponse() {
1646 next_state_
= STATE_CACHE_READ_RESPONSE_COMPLETE
;
1648 io_buf_len_
= entry_
->disk_entry
->GetDataSize(kResponseInfoIndex
);
1649 read_buf_
= new IOBuffer(io_buf_len_
);
1651 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1652 return entry_
->disk_entry
->ReadData(kResponseInfoIndex
, 0, read_buf_
.get(),
1653 io_buf_len_
, io_callback_
);
1656 int HttpCache::Transaction::DoCacheReadResponseComplete(int result
) {
1657 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1658 if (result
!= io_buf_len_
||
1659 !HttpCache::ParseResponseInfo(read_buf_
->data(), io_buf_len_
,
1660 &response_
, &truncated_
)) {
1661 return OnCacheReadError(result
, true);
1664 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1665 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
1668 // Some resources may have slipped in as truncated when they're not.
1669 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1670 if (response_
.headers
->GetContentLength() == current_size
)
1673 // We now have access to the cache entry.
1675 // o if we are a reader for the transaction, then we can start reading the
1678 // o if we can read or write, then we should check if the cache entry needs
1679 // to be validated and then issue a network request if needed or just read
1680 // from the cache if the cache entry is already valid.
1682 // o if we are set to UPDATE, then we are handling an externally
1683 // conditionalized request (if-modified-since / if-none-match). We check
1684 // if the request headers define a validation request.
1688 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
1689 result
= BeginCacheRead();
1692 result
= BeginPartialCacheValidation();
1695 result
= BeginExternallyConditionalizedRequest();
1700 result
= ERR_FAILED
;
1705 int HttpCache::Transaction::DoCacheWriteResponse() {
1707 if (net_log_
.IsLogging())
1708 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1710 return WriteResponseInfoToEntry(false);
1713 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1715 if (net_log_
.IsLogging())
1716 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1718 return WriteResponseInfoToEntry(true);
1721 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result
) {
1722 next_state_
= target_state_
;
1723 target_state_
= STATE_NONE
;
1726 if (net_log_
.IsLogging()) {
1727 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1731 // Balance the AddRef from WriteResponseInfoToEntry.
1732 if (result
!= io_buf_len_
) {
1733 DLOG(ERROR
) << "failed to write response info to cache";
1734 DoneWritingToEntry(false);
1739 int HttpCache::Transaction::DoCacheReadMetadata() {
1741 DCHECK(!response_
.metadata
.get());
1742 next_state_
= STATE_CACHE_READ_METADATA_COMPLETE
;
1744 response_
.metadata
=
1745 new IOBufferWithSize(entry_
->disk_entry
->GetDataSize(kMetadataIndex
));
1747 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1748 return entry_
->disk_entry
->ReadData(kMetadataIndex
, 0,
1749 response_
.metadata
.get(),
1750 response_
.metadata
->size(),
1754 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result
) {
1755 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1756 if (result
!= response_
.metadata
->size())
1757 return OnCacheReadError(result
, false);
1761 int HttpCache::Transaction::DoCacheQueryData() {
1762 next_state_
= STATE_CACHE_QUERY_DATA_COMPLETE
;
1763 return entry_
->disk_entry
->ReadyForSparseIO(io_callback_
);
1766 int HttpCache::Transaction::DoCacheQueryDataComplete(int result
) {
1767 if (result
== ERR_NOT_IMPLEMENTED
) {
1768 // Restart the request overwriting the cache entry.
1769 // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1770 // supports Sparse IO.
1771 return DoRestartPartialRequest();
1773 DCHECK_EQ(OK
, result
);
1775 return ERR_UNEXPECTED
;
1777 return ValidateEntryHeadersAndContinue();
1780 int HttpCache::Transaction::DoCacheReadData() {
1782 next_state_
= STATE_CACHE_READ_DATA_COMPLETE
;
1784 if (net_log_
.IsLogging())
1785 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA
);
1786 if (partial_
.get()) {
1787 return partial_
->CacheRead(entry_
->disk_entry
, read_buf_
.get(), io_buf_len_
,
1791 return entry_
->disk_entry
->ReadData(kResponseContentIndex
, read_offset_
,
1792 read_buf_
.get(), io_buf_len_
,
1796 int HttpCache::Transaction::DoCacheReadDataComplete(int result
) {
1797 if (net_log_
.IsLogging()) {
1798 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA
,
1803 return ERR_UNEXPECTED
;
1805 if (partial_
.get()) {
1806 // Partial requests are confusing to report in histograms because they may
1807 // have multiple underlying requests.
1808 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1809 return DoPartialCacheReadCompleted(result
);
1813 read_offset_
+= result
;
1814 } else if (result
== 0) { // End of file.
1816 cache_
->DoneReadingFromEntry(entry_
, this);
1819 return OnCacheReadError(result
, false);
1824 int HttpCache::Transaction::DoCacheWriteData(int num_bytes
) {
1825 next_state_
= STATE_CACHE_WRITE_DATA_COMPLETE
;
1826 write_len_
= num_bytes
;
1828 if (net_log_
.IsLogging())
1829 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1832 return AppendResponseDataToEntry(read_buf_
.get(), num_bytes
, io_callback_
);
1835 int HttpCache::Transaction::DoCacheWriteDataComplete(int result
) {
1837 if (net_log_
.IsLogging()) {
1838 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1842 // Balance the AddRef from DoCacheWriteData.
1844 return ERR_UNEXPECTED
;
1846 if (result
!= write_len_
) {
1847 DLOG(ERROR
) << "failed to write response data to cache";
1848 DoneWritingToEntry(false);
1850 // We want to ignore errors writing to disk and just keep reading from
1852 result
= write_len_
;
1853 } else if (!done_reading_
&& entry_
) {
1854 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1855 int64 body_size
= response_
.headers
->GetContentLength();
1856 if (body_size
>= 0 && body_size
<= current_size
)
1857 done_reading_
= true;
1860 if (partial_
.get()) {
1861 // This may be the last request.
1862 if (!(result
== 0 && !truncated_
&&
1863 (partial_
->IsLastRange() || mode_
== WRITE
)))
1864 return DoPartialNetworkReadCompleted(result
);
1868 // End of file. This may be the result of a connection problem so see if we
1869 // have to keep the entry around to be flagged as truncated later on.
1870 if (done_reading_
|| !entry_
|| partial_
.get() ||
1871 response_
.headers
->GetContentLength() <= 0)
1872 DoneWritingToEntry(true);
1878 //-----------------------------------------------------------------------------
1880 void HttpCache::Transaction::ReadCertChain() {
1882 GetCacheKeyForCert(response_
.ssl_info
.cert
->os_cert_handle());
1883 const X509Certificate::OSCertHandles
& intermediates
=
1884 response_
.ssl_info
.cert
->GetIntermediateCertificates();
1885 int dist_from_root
= intermediates
.size();
1887 scoped_refptr
<SharedChainData
> shared_chain_data(
1888 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
1889 cache_
->cert_cache()->GetCertificate(key
,
1890 base::Bind(&OnCertReadIOComplete
,
1893 shared_chain_data
));
1895 for (X509Certificate::OSCertHandles::const_iterator it
=
1896 intermediates
.begin();
1897 it
!= intermediates
.end();
1900 key
= GetCacheKeyForCert(*it
);
1901 cache_
->cert_cache()->GetCertificate(key
,
1902 base::Bind(&OnCertReadIOComplete
,
1904 false /* is not leaf */,
1905 shared_chain_data
));
1907 DCHECK_EQ(0, dist_from_root
);
1910 void HttpCache::Transaction::WriteCertChain() {
1911 const X509Certificate::OSCertHandles
& intermediates
=
1912 response_
.ssl_info
.cert
->GetIntermediateCertificates();
1913 int dist_from_root
= intermediates
.size();
1915 scoped_refptr
<SharedChainData
> shared_chain_data(
1916 new SharedChainData(intermediates
.size() + 1, TimeTicks::Now()));
1917 cache_
->cert_cache()->SetCertificate(
1918 response_
.ssl_info
.cert
->os_cert_handle(),
1919 base::Bind(&OnCertWriteIOComplete
,
1922 shared_chain_data
));
1923 for (X509Certificate::OSCertHandles::const_iterator it
=
1924 intermediates
.begin();
1925 it
!= intermediates
.end();
1928 cache_
->cert_cache()->SetCertificate(*it
,
1929 base::Bind(&OnCertWriteIOComplete
,
1931 false /* is not leaf */,
1932 shared_chain_data
));
1934 DCHECK_EQ(0, dist_from_root
);
1937 void HttpCache::Transaction::SetRequest(const BoundNetLog
& net_log
,
1938 const HttpRequestInfo
* request
) {
1941 effective_load_flags_
= request_
->load_flags
;
1943 switch (cache_
->mode()) {
1947 // When in record mode, we want to NEVER load from the cache.
1948 // The reason for this is because we save the Set-Cookie headers
1949 // (intentionally). If we read from the cache, we replay them
1951 effective_load_flags_
|= LOAD_BYPASS_CACHE
;
1954 // When in playback mode, we want to load exclusively from the cache.
1955 effective_load_flags_
|= LOAD_ONLY_FROM_CACHE
;
1958 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
1962 // Some headers imply load flags. The order here is significant.
1964 // LOAD_DISABLE_CACHE : no cache read or write
1965 // LOAD_BYPASS_CACHE : no cache read
1966 // LOAD_VALIDATE_CACHE : no cache read unless validation
1968 // The former modes trump latter modes, so if we find a matching header we
1969 // can stop iterating kSpecialHeaders.
1971 static const struct {
1972 const HeaderNameAndValue
* search
;
1974 } kSpecialHeaders
[] = {
1975 { kPassThroughHeaders
, LOAD_DISABLE_CACHE
},
1976 { kForceFetchHeaders
, LOAD_BYPASS_CACHE
},
1977 { kForceValidateHeaders
, LOAD_VALIDATE_CACHE
},
1980 bool range_found
= false;
1981 bool external_validation_error
= false;
1983 if (request_
->extra_headers
.HasHeader(HttpRequestHeaders::kRange
))
1986 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kSpecialHeaders
); ++i
) {
1987 if (HeaderMatches(request_
->extra_headers
, kSpecialHeaders
[i
].search
)) {
1988 effective_load_flags_
|= kSpecialHeaders
[i
].load_flag
;
1993 // Check for conditionalization headers which may correspond with a
1994 // cache validation request.
1995 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); ++i
) {
1996 const ValidationHeaderInfo
& info
= kValidationHeaders
[i
];
1997 std::string validation_value
;
1998 if (request_
->extra_headers
.GetHeader(
1999 info
.request_header_name
, &validation_value
)) {
2000 if (!external_validation_
.values
[i
].empty() ||
2001 validation_value
.empty()) {
2002 external_validation_error
= true;
2004 external_validation_
.values
[i
] = validation_value
;
2005 external_validation_
.initialized
= true;
2009 // We don't support ranges and validation headers.
2010 if (range_found
&& external_validation_
.initialized
) {
2011 LOG(WARNING
) << "Byte ranges AND validation headers found.";
2012 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2015 // If there is more than one validation header, we can't treat this request as
2016 // a cache validation, since we don't know for sure which header the server
2017 // will give us a response for (and they could be contradictory).
2018 if (external_validation_error
) {
2019 LOG(WARNING
) << "Multiple or malformed validation headers found.";
2020 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2023 if (range_found
&& !(effective_load_flags_
& LOAD_DISABLE_CACHE
)) {
2024 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2025 partial_
.reset(new PartialData
);
2026 if (request_
->method
== "GET" && partial_
->Init(request_
->extra_headers
)) {
2027 // We will be modifying the actual range requested to the server, so
2028 // let's remove the header here.
2029 custom_request_
.reset(new HttpRequestInfo(*request_
));
2030 custom_request_
->extra_headers
.RemoveHeader(HttpRequestHeaders::kRange
);
2031 request_
= custom_request_
.get();
2032 partial_
->SetHeaders(custom_request_
->extra_headers
);
2034 // The range is invalid or we cannot handle it properly.
2035 VLOG(1) << "Invalid byte range found.";
2036 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
2037 partial_
.reset(NULL
);
2042 bool HttpCache::Transaction::ShouldPassThrough() {
2043 // We may have a null disk_cache if there is an error we cannot recover from,
2044 // like not enough disk space, or sharing violations.
2045 if (!cache_
->disk_cache_
.get())
2048 // When using the record/playback modes, we always use the cache
2049 // and we never pass through.
2050 if (cache_
->mode() == RECORD
|| cache_
->mode() == PLAYBACK
)
2053 if (effective_load_flags_
& LOAD_DISABLE_CACHE
)
2056 if (request_
->method
== "GET" || request_
->method
== "HEAD")
2059 if (request_
->method
== "POST" && request_
->upload_data_stream
&&
2060 request_
->upload_data_stream
->identifier()) {
2064 if (request_
->method
== "PUT" && request_
->upload_data_stream
)
2067 if (request_
->method
== "DELETE")
2073 int HttpCache::Transaction::BeginCacheRead() {
2074 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2075 if (response_
.headers
->response_code() == 206 || partial_
.get()) {
2077 return ERR_CACHE_MISS
;
2080 if (request_
->method
== "HEAD")
2081 FixHeadersForHead();
2083 // We don't have the whole resource.
2085 return ERR_CACHE_MISS
;
2087 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2088 next_state_
= STATE_CACHE_READ_METADATA
;
2093 int HttpCache::Transaction::BeginCacheValidation() {
2094 DCHECK(mode_
== READ_WRITE
);
2096 bool skip_validation
= !RequiresValidation();
2098 if (request_
->method
== "HEAD" &&
2099 (truncated_
|| response_
.headers
->response_code() == 206)) {
2101 if (skip_validation
)
2102 return SetupEntryForRead();
2105 next_state_
= STATE_SEND_REQUEST
;
2111 // Truncated entries can cause partial gets, so we shouldn't record this
2112 // load in histograms.
2113 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2114 skip_validation
= !partial_
->initial_validation();
2117 if (partial_
.get() && (is_sparse_
|| truncated_
) &&
2118 (!partial_
->IsCurrentRangeCached() || invalid_range_
)) {
2119 // Force revalidation for sparse or truncated entries. Note that we don't
2120 // want to ignore the regular validation logic just because a byte range was
2121 // part of the request.
2122 skip_validation
= false;
2125 if (skip_validation
) {
2126 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
2127 RecordOfflineStatus(effective_load_flags_
, OFFLINE_STATUS_FRESH_CACHE
);
2128 return SetupEntryForRead();
2130 // Make the network request conditional, to see if we may reuse our cached
2131 // response. If we cannot do so, then we just resort to a normal fetch.
2132 // Our mode remains READ_WRITE for a conditional request. Even if the
2133 // conditionalization fails, we don't switch to WRITE mode until we
2134 // know we won't be falling back to using the cache entry in the
2135 // LOAD_FROM_CACHE_IF_OFFLINE case.
2136 if (!ConditionalizeRequest()) {
2137 couldnt_conditionalize_request_
= true;
2138 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE
);
2140 return DoRestartPartialRequest();
2142 DCHECK_NE(206, response_
.headers
->response_code());
2144 next_state_
= STATE_SEND_REQUEST
;
2149 int HttpCache::Transaction::BeginPartialCacheValidation() {
2150 DCHECK(mode_
== READ_WRITE
);
2152 if (response_
.headers
->response_code() != 206 && !partial_
.get() &&
2154 return BeginCacheValidation();
2157 // Partial requests should not be recorded in histograms.
2158 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2159 if (range_requested_
) {
2160 next_state_
= STATE_CACHE_QUERY_DATA
;
2164 // The request is not for a range, but we have stored just ranges.
2166 if (request_
->method
== "HEAD")
2167 return BeginCacheValidation();
2169 partial_
.reset(new PartialData());
2170 partial_
->SetHeaders(request_
->extra_headers
);
2171 if (!custom_request_
.get()) {
2172 custom_request_
.reset(new HttpRequestInfo(*request_
));
2173 request_
= custom_request_
.get();
2176 return ValidateEntryHeadersAndContinue();
2179 // This should only be called once per request.
2180 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2181 DCHECK(mode_
== READ_WRITE
);
2183 if (!partial_
->UpdateFromStoredHeaders(
2184 response_
.headers
.get(), entry_
->disk_entry
, truncated_
)) {
2185 return DoRestartPartialRequest();
2188 if (response_
.headers
->response_code() == 206)
2191 if (!partial_
->IsRequestedRangeOK()) {
2192 // The stored data is fine, but the request may be invalid.
2193 invalid_range_
= true;
2196 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2200 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2201 DCHECK_EQ(UPDATE
, mode_
);
2202 DCHECK(external_validation_
.initialized
);
2204 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); i
++) {
2205 if (external_validation_
.values
[i
].empty())
2207 // Retrieve either the cached response's "etag" or "last-modified" header.
2208 std::string validator
;
2209 response_
.headers
->EnumerateHeader(
2211 kValidationHeaders
[i
].related_response_header_name
,
2214 if (response_
.headers
->response_code() != 200 || truncated_
||
2215 validator
.empty() || validator
!= external_validation_
.values
[i
]) {
2216 // The externally conditionalized request is not a validation request
2217 // for our existing cache entry. Proceed with caching disabled.
2218 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2219 DoneWritingToEntry(true);
2223 next_state_
= STATE_SEND_REQUEST
;
2227 int HttpCache::Transaction::RestartNetworkRequest() {
2228 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2229 DCHECK(network_trans_
.get());
2230 DCHECK_EQ(STATE_NONE
, next_state_
);
2232 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2233 int rv
= network_trans_
->RestartIgnoringLastError(io_callback_
);
2234 if (rv
!= ERR_IO_PENDING
)
2239 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2240 X509Certificate
* client_cert
) {
2241 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2242 DCHECK(network_trans_
.get());
2243 DCHECK_EQ(STATE_NONE
, next_state_
);
2245 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2246 int rv
= network_trans_
->RestartWithCertificate(client_cert
, io_callback_
);
2247 if (rv
!= ERR_IO_PENDING
)
2252 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2253 const AuthCredentials
& credentials
) {
2254 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
2255 DCHECK(network_trans_
.get());
2256 DCHECK_EQ(STATE_NONE
, next_state_
);
2258 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
2259 int rv
= network_trans_
->RestartWithAuth(credentials
, io_callback_
);
2260 if (rv
!= ERR_IO_PENDING
)
2265 bool HttpCache::Transaction::RequiresValidation() {
2266 // TODO(darin): need to do more work here:
2267 // - make sure we have a matching request method
2268 // - watch out for cached responses that depend on authentication
2270 // In playback mode, nothing requires validation.
2271 if (cache_
->mode() == net::HttpCache::PLAYBACK
)
2274 if (response_
.vary_data
.is_valid() &&
2275 !response_
.vary_data
.MatchesRequest(*request_
,
2276 *response_
.headers
.get())) {
2277 vary_mismatch_
= true;
2281 if (effective_load_flags_
& LOAD_PREFERRING_CACHE
)
2284 if (effective_load_flags_
& LOAD_VALIDATE_CACHE
)
2287 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2290 if (response_
.headers
->RequiresValidation(
2291 response_
.request_time
, response_
.response_time
, Time::Now())) {
2298 bool HttpCache::Transaction::ConditionalizeRequest() {
2299 DCHECK(response_
.headers
.get());
2301 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2304 // This only makes sense for cached 200 or 206 responses.
2305 if (response_
.headers
->response_code() != 200 &&
2306 response_
.headers
->response_code() != 206) {
2310 // We should have handled this case before.
2311 DCHECK(response_
.headers
->response_code() != 206 ||
2312 response_
.headers
->HasStrongValidators());
2314 // Just use the first available ETag and/or Last-Modified header value.
2315 // TODO(darin): Or should we use the last?
2317 std::string etag_value
;
2318 if (response_
.headers
->GetHttpVersion() >= HttpVersion(1, 1))
2319 response_
.headers
->EnumerateHeader(NULL
, "etag", &etag_value
);
2321 std::string last_modified_value
;
2322 if (!vary_mismatch_
) {
2323 response_
.headers
->EnumerateHeader(NULL
, "last-modified",
2324 &last_modified_value
);
2327 if (etag_value
.empty() && last_modified_value
.empty())
2330 if (!partial_
.get()) {
2331 // Need to customize the request, so this forces us to allocate :(
2332 custom_request_
.reset(new HttpRequestInfo(*request_
));
2333 request_
= custom_request_
.get();
2335 DCHECK(custom_request_
.get());
2337 bool use_if_range
= partial_
.get() && !partial_
->IsCurrentRangeCached() &&
2340 if (!use_if_range
) {
2341 // stale-while-revalidate is not useful when we only have a partial response
2342 // cached, so don't set the header in that case.
2343 TimeDelta stale_while_revalidate
;
2344 if (response_
.headers
->GetStaleWhileRevalidateValue(
2345 &stale_while_revalidate
) &&
2346 stale_while_revalidate
> TimeDelta()) {
2348 response_
.headers
->GetFreshnessLifetime(response_
.response_time
);
2349 TimeDelta current_age
= response_
.headers
->GetCurrentAge(
2350 response_
.request_time
, response_
.response_time
, Time::Now());
2352 custom_request_
->extra_headers
.SetHeader(
2354 base::StringPrintf("max-age=%" PRId64
2355 ",stale-while-revalidate=%" PRId64
",age=%" PRId64
,
2356 max_age
.InSeconds(),
2357 stale_while_revalidate
.InSeconds(),
2358 current_age
.InSeconds()));
2362 if (!etag_value
.empty()) {
2364 // We don't want to switch to WRITE mode if we don't have this block of a
2365 // byte-range request because we may have other parts cached.
2366 custom_request_
->extra_headers
.SetHeader(
2367 HttpRequestHeaders::kIfRange
, etag_value
);
2369 custom_request_
->extra_headers
.SetHeader(
2370 HttpRequestHeaders::kIfNoneMatch
, etag_value
);
2372 // For byte-range requests, make sure that we use only one way to validate
2374 if (partial_
.get() && !partial_
->IsCurrentRangeCached())
2378 if (!last_modified_value
.empty()) {
2380 custom_request_
->extra_headers
.SetHeader(
2381 HttpRequestHeaders::kIfRange
, last_modified_value
);
2383 custom_request_
->extra_headers
.SetHeader(
2384 HttpRequestHeaders::kIfModifiedSince
, last_modified_value
);
2391 // We just received some headers from the server. We may have asked for a range,
2392 // in which case partial_ has an object. This could be the first network request
2393 // we make to fulfill the original request, or we may be already reading (from
2394 // the net and / or the cache). If we are not expecting a certain response, we
2395 // just bypass the cache for this request (but again, maybe we are reading), and
2396 // delete partial_ (so we are not able to "fix" the headers that we return to
2397 // the user). This results in either a weird response for the caller (we don't
2398 // expect it after all), or maybe a range that was not exactly what it was asked
2401 // If the server is simply telling us that the resource has changed, we delete
2402 // the cached entry and restart the request as the caller intended (by returning
2403 // false from this method). However, we may not be able to do that at any point,
2404 // for instance if we already returned the headers to the user.
2406 // WARNING: Whenever this code returns false, it has to make sure that the next
2407 // time it is called it will return true so that we don't keep retrying the
2409 bool HttpCache::Transaction::ValidatePartialResponse() {
2410 const HttpResponseHeaders
* headers
= new_response_
->headers
.get();
2411 int response_code
= headers
->response_code();
2412 bool partial_response
= (response_code
== 206);
2413 handling_206_
= false;
2415 if (!entry_
|| request_
->method
!= "GET")
2418 if (invalid_range_
) {
2419 // We gave up trying to match this request with the stored data. If the
2420 // server is ok with the request, delete the entry, otherwise just ignore
2423 if (partial_response
|| response_code
== 200) {
2424 DoomPartialEntry(true);
2427 if (response_code
== 304)
2429 IgnoreRangeRequest();
2434 if (!partial_
.get()) {
2435 // We are not expecting 206 but we may have one.
2436 if (partial_response
)
2437 IgnoreRangeRequest();
2442 // TODO(rvargas): Do we need to consider other results here?.
2443 bool failure
= response_code
== 200 || response_code
== 416;
2445 if (partial_
->IsCurrentRangeCached()) {
2446 // We asked for "If-None-Match: " so a 206 means a new object.
2447 if (partial_response
)
2450 if (response_code
== 304 && partial_
->ResponseHeadersOK(headers
))
2453 // We asked for "If-Range: " so a 206 means just another range.
2454 if (partial_response
&& partial_
->ResponseHeadersOK(headers
)) {
2455 handling_206_
= true;
2459 if (!reading_
&& !is_sparse_
&& !partial_response
) {
2460 // See if we can ignore the fact that we issued a byte range request.
2461 // If the server sends 200, just store it. If it sends an error, redirect
2462 // or something else, we may store the response as long as we didn't have
2463 // anything already stored.
2464 if (response_code
== 200 ||
2465 (!truncated_
&& response_code
!= 304 && response_code
!= 416)) {
2466 // The server is sending something else, and we can save it.
2467 DCHECK((truncated_
&& !partial_
->IsLastRange()) || range_requested_
);
2474 // 304 is not expected here, but we'll spare the entry (unless it was
2481 // We cannot truncate this entry, it has to be deleted.
2482 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2483 DoomPartialEntry(false);
2485 if (!reading_
&& !partial_
->IsLastRange()) {
2486 // We'll attempt to issue another network request, this time without us
2487 // messing up the headers.
2488 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
2493 LOG(WARNING
) << "Failed to revalidate partial entry";
2498 IgnoreRangeRequest();
2502 void HttpCache::Transaction::IgnoreRangeRequest() {
2503 // We have a problem. We may or may not be reading already (in which case we
2504 // returned the headers), but we'll just pretend that this request is not
2505 // using the cache and see what happens. Most likely this is the first
2506 // response from the server (it's not changing its mind midway, right?).
2507 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2509 DoneWritingToEntry(mode_
!= WRITE
);
2510 else if (mode_
& READ
&& entry_
)
2511 cache_
->DoneReadingFromEntry(entry_
, this);
2513 partial_
.reset(NULL
);
2518 void HttpCache::Transaction::FixHeadersForHead() {
2519 if (response_
.headers
->response_code() == 206) {
2520 response_
.headers
->RemoveHeader("Content-Length");
2521 response_
.headers
->RemoveHeader("Content-Range");
2522 response_
.headers
->ReplaceStatusLine("HTTP/1.1 200 OK");
2526 void HttpCache::Transaction::FailRangeRequest() {
2527 response_
= *new_response_
;
2528 partial_
->FixResponseHeaders(response_
.headers
.get(), false);
2531 int HttpCache::Transaction::SetupEntryForRead() {
2533 ResetNetworkTransaction();
2534 if (partial_
.get()) {
2535 if (truncated_
|| is_sparse_
|| !invalid_range_
) {
2536 // We are going to return the saved response headers to the caller, so
2537 // we may need to adjust them first.
2538 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
2544 cache_
->ConvertWriterToReader(entry_
);
2547 if (request_
->method
== "HEAD")
2548 FixHeadersForHead();
2550 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2551 next_state_
= STATE_CACHE_READ_METADATA
;
2556 int HttpCache::Transaction::ReadFromNetwork(IOBuffer
* data
, int data_len
) {
2558 io_buf_len_
= data_len
;
2559 next_state_
= STATE_NETWORK_READ
;
2563 int HttpCache::Transaction::ReadFromEntry(IOBuffer
* data
, int data_len
) {
2564 if (request_
->method
== "HEAD")
2568 io_buf_len_
= data_len
;
2569 next_state_
= STATE_CACHE_READ_DATA
;
2573 int HttpCache::Transaction::WriteToEntry(int index
, int offset
,
2574 IOBuffer
* data
, int data_len
,
2575 const CompletionCallback
& callback
) {
2580 if (!partial_
.get() || !data_len
) {
2581 rv
= entry_
->disk_entry
->WriteData(index
, offset
, data
, data_len
, callback
,
2584 rv
= partial_
->CacheWrite(entry_
->disk_entry
, data
, data_len
, callback
);
2589 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated
) {
2590 next_state_
= STATE_CACHE_WRITE_RESPONSE_COMPLETE
;
2594 // Do not cache no-store content (unless we are record mode). Do not cache
2595 // content with cert errors either. This is to prevent not reporting net
2596 // errors when loading a resource from the cache. When we load a page over
2597 // HTTPS with a cert error we show an SSL blocking page. If the user clicks
2598 // proceed we reload the resource ignoring the errors. The loaded resource
2599 // is then cached. If that resource is subsequently loaded from the cache,
2600 // no net error is reported (even though the cert status contains the actual
2601 // errors) and no SSL blocking page is shown. An alternative would be to
2602 // reverse-map the cert status to a net error and replay the net error.
2603 if ((cache_
->mode() != RECORD
&&
2604 response_
.headers
->HasHeaderValue("cache-control", "no-store")) ||
2605 net::IsCertStatusError(response_
.ssl_info
.cert_status
)) {
2606 DoneWritingToEntry(false);
2607 if (net_log_
.IsLogging())
2608 net_log_
.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
2612 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2613 if (cache_
->cert_cache() && response_
.ssl_info
.is_valid())
2616 // When writing headers, we normally only write the non-transient
2617 // headers; when in record mode, record everything.
2618 bool skip_transient_headers
= (cache_
->mode() != RECORD
);
2621 DCHECK_EQ(200, response_
.headers
->response_code());
2623 scoped_refptr
<PickledIOBuffer
> data(new PickledIOBuffer());
2624 response_
.Persist(data
->pickle(), skip_transient_headers
, truncated
);
2627 io_buf_len_
= data
->pickle()->size();
2628 return entry_
->disk_entry
->WriteData(kResponseInfoIndex
, 0, data
.get(),
2629 io_buf_len_
, io_callback_
, true);
2632 int HttpCache::Transaction::AppendResponseDataToEntry(
2633 IOBuffer
* data
, int data_len
, const CompletionCallback
& callback
) {
2634 if (!entry_
|| !data_len
)
2637 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
2638 return WriteToEntry(kResponseContentIndex
, current_size
, data
, data_len
,
2642 void HttpCache::Transaction::DoneWritingToEntry(bool success
) {
2648 cache_
->DoneWritingToEntry(entry_
, success
);
2650 mode_
= NONE
; // switch to 'pass through' mode
2653 int HttpCache::Transaction::OnCacheReadError(int result
, bool restart
) {
2654 DLOG(ERROR
) << "ReadData failed: " << result
;
2655 const int result_for_histogram
= std::max(0, -result
);
2657 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2658 result_for_histogram
);
2660 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2661 result_for_histogram
);
2664 // Avoid using this entry in the future.
2666 cache_
->DoomActiveEntry(cache_key_
);
2670 DCHECK(!network_trans_
.get());
2671 cache_
->DoneWithEntry(entry_
, this, false);
2675 next_state_
= STATE_GET_BACKEND
;
2679 return ERR_CACHE_READ_FAILURE
;
2682 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time
) {
2683 if (entry_lock_waiting_since_
!= start_time
)
2686 DCHECK_EQ(next_state_
, STATE_ADD_TO_ENTRY_COMPLETE
);
2691 cache_
->RemovePendingTransaction(this);
2692 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT
);
2695 void HttpCache::Transaction::DoomPartialEntry(bool delete_object
) {
2696 DVLOG(2) << "DoomPartialEntry";
2697 int rv
= cache_
->DoomEntry(cache_key_
, NULL
);
2699 cache_
->DoneWithEntry(entry_
, this, false);
2703 partial_
.reset(NULL
);
2706 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result
) {
2707 partial_
->OnNetworkReadCompleted(result
);
2710 // We need to move on to the next range.
2711 ResetNetworkTransaction();
2712 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2717 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result
) {
2718 partial_
->OnCacheReadCompleted(result
);
2720 if (result
== 0 && mode_
== READ_WRITE
) {
2721 // We need to move on to the next range.
2722 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2723 } else if (result
< 0) {
2724 return OnCacheReadError(result
, false);
2729 int HttpCache::Transaction::DoRestartPartialRequest() {
2730 // The stored data cannot be used. Get rid of it and restart this request.
2731 // We need to also reset the |truncated_| flag as a new entry is created.
2732 DoomPartialEntry(!range_requested_
);
2735 next_state_
= STATE_INIT_ENTRY
;
2739 void HttpCache::Transaction::ResetNetworkTransaction() {
2740 DCHECK(!old_network_trans_load_timing_
);
2741 DCHECK(network_trans_
);
2742 LoadTimingInfo load_timing
;
2743 if (network_trans_
->GetLoadTimingInfo(&load_timing
))
2744 old_network_trans_load_timing_
.reset(new LoadTimingInfo(load_timing
));
2745 total_received_bytes_
+= network_trans_
->GetTotalReceivedBytes();
2746 network_trans_
.reset();
2749 // Histogram data from the end of 2010 show the following distribution of
2750 // response headers:
2752 // Content-Length............... 87%
2753 // Date......................... 98%
2754 // Last-Modified................ 49%
2755 // Etag......................... 19%
2756 // Accept-Ranges: bytes......... 25%
2757 // Accept-Ranges: none.......... 0.4%
2758 // Strong Validator............. 50%
2759 // Strong Validator + ranges.... 24%
2760 // Strong Validator + CL........ 49%
2762 bool HttpCache::Transaction::CanResume(bool has_data
) {
2763 // Double check that there is something worth keeping.
2764 if (has_data
&& !entry_
->disk_entry
->GetDataSize(kResponseContentIndex
))
2767 if (request_
->method
!= "GET")
2770 // Note that if this is a 206, content-length was already fixed after calling
2771 // PartialData::ResponseHeadersOK().
2772 if (response_
.headers
->GetContentLength() <= 0 ||
2773 response_
.headers
->HasHeaderValue("Accept-Ranges", "none") ||
2774 !response_
.headers
->HasStrongValidators()) {
2781 void HttpCache::Transaction::UpdateTransactionPattern(
2782 TransactionPattern new_transaction_pattern
) {
2783 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2785 DCHECK(transaction_pattern_
== PATTERN_UNDEFINED
||
2786 new_transaction_pattern
== PATTERN_NOT_COVERED
);
2787 transaction_pattern_
= new_transaction_pattern
;
2790 void HttpCache::Transaction::RecordHistograms() {
2791 DCHECK_NE(PATTERN_UNDEFINED
, transaction_pattern_
);
2792 if (!cache_
.get() || !cache_
->GetCurrentBackend() ||
2793 cache_
->GetCurrentBackend()->GetCacheType() != DISK_CACHE
||
2794 cache_
->mode() != NORMAL
|| request_
->method
!= "GET") {
2797 UMA_HISTOGRAM_ENUMERATION(
2798 "HttpCache.Pattern", transaction_pattern_
, PATTERN_MAX
);
2799 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2801 DCHECK(!range_requested_
);
2802 DCHECK(!first_cache_access_since_
.is_null());
2804 TimeDelta total_time
= base::TimeTicks::Now() - first_cache_access_since_
;
2806 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time
);
2808 bool did_send_request
= !send_request_since_
.is_null();
2810 (did_send_request
&&
2811 (transaction_pattern_
== PATTERN_ENTRY_NOT_CACHED
||
2812 transaction_pattern_
== PATTERN_ENTRY_VALIDATED
||
2813 transaction_pattern_
== PATTERN_ENTRY_UPDATED
||
2814 transaction_pattern_
== PATTERN_ENTRY_CANT_CONDITIONALIZE
)) ||
2815 (!did_send_request
&& transaction_pattern_
== PATTERN_ENTRY_USED
));
2817 if (!did_send_request
) {
2818 DCHECK(transaction_pattern_
== PATTERN_ENTRY_USED
);
2819 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time
);
2823 TimeDelta before_send_time
= send_request_since_
- first_cache_access_since_
;
2824 int before_send_percent
=
2825 total_time
.ToInternalValue() == 0 ? 0
2826 : before_send_time
* 100 / total_time
;
2827 DCHECK_LE(0, before_send_percent
);
2828 DCHECK_GE(100, before_send_percent
);
2830 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time
);
2831 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time
);
2832 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent
);
2834 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2835 // below this comment after we have received initial data.
2836 switch (transaction_pattern_
) {
2837 case PATTERN_ENTRY_CANT_CONDITIONALIZE
: {
2838 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2840 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2841 before_send_percent
);
2844 case PATTERN_ENTRY_NOT_CACHED
: {
2845 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time
);
2846 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2847 before_send_percent
);
2850 case PATTERN_ENTRY_VALIDATED
: {
2851 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time
);
2852 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2853 before_send_percent
);
2856 case PATTERN_ENTRY_UPDATED
: {
2857 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time
);
2858 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2859 before_send_percent
);
2867 void HttpCache::Transaction::OnIOComplete(int result
) {