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/memory/ref_counted.h"
19 #include "base/metrics/field_trial.h"
20 #include "base/metrics/histogram.h"
21 #include "base/metrics/sparse_histogram.h"
22 #include "base/rand_util.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_util.h"
25 #include "base/time/time.h"
26 #include "net/base/completion_callback.h"
27 #include "net/base/io_buffer.h"
28 #include "net/base/load_flags.h"
29 #include "net/base/load_timing_info.h"
30 #include "net/base/net_errors.h"
31 #include "net/base/net_log.h"
32 #include "net/base/upload_data_stream.h"
33 #include "net/cert/cert_status_flags.h"
34 #include "net/disk_cache/disk_cache.h"
35 #include "net/http/http_network_session.h"
36 #include "net/http/http_request_info.h"
37 #include "net/http/http_response_headers.h"
38 #include "net/http/http_transaction.h"
39 #include "net/http/http_util.h"
40 #include "net/http/partial_data.h"
41 #include "net/ssl/ssl_cert_request_info.h"
42 #include "net/ssl/ssl_config_service.h"
45 using base::TimeDelta
;
46 using base::TimeTicks
;
50 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
51 // a "non-error response" is one with a 2xx (Successful) or 3xx
52 // (Redirection) status code.
53 bool NonErrorResponse(int status_code
) {
54 int status_code_range
= status_code
/ 100;
55 return status_code_range
== 2 || status_code_range
== 3;
58 // Error codes that will be considered indicative of a page being offline/
59 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
60 bool IsOfflineError(int error
) {
61 return (error
== net::ERR_NAME_NOT_RESOLVED
||
62 error
== net::ERR_INTERNET_DISCONNECTED
||
63 error
== net::ERR_ADDRESS_UNREACHABLE
||
64 error
== net::ERR_CONNECTION_TIMED_OUT
);
67 // Enum for UMA, indicating the status (with regard to offline mode) of
68 // a particular request.
69 enum RequestOfflineStatus
{
70 // A cache transaction hit in cache (data was present and not stale)
72 OFFLINE_STATUS_FRESH_CACHE
,
74 // A network request was required for a cache entry, and it succeeded.
75 OFFLINE_STATUS_NETWORK_SUCCEEDED
,
77 // A network request was required for a cache entry, and it failed with
78 // a non-offline error.
79 OFFLINE_STATUS_NETWORK_FAILED
,
81 // A network request was required for a cache entry, it failed with an
82 // offline error, and we could serve stale data if
83 // LOAD_FROM_CACHE_IF_OFFLINE was set.
84 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
,
86 // A network request was required for a cache entry, it failed with
87 // an offline error, and there was no servable data in cache (even
89 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
,
91 OFFLINE_STATUS_MAX_ENTRIES
94 void RecordOfflineStatus(int load_flags
, RequestOfflineStatus status
) {
95 // Restrict to main frame to keep statistics close to
96 // "would have shown them something useful if offline mode was enabled".
97 if (load_flags
& net::LOAD_MAIN_FRAME
) {
98 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status
,
99 OFFLINE_STATUS_MAX_ENTRIES
);
103 // TODO(rvargas): Remove once we get the data.
104 void RecordVaryHeaderHistogram(const net::HttpResponseInfo
* response
) {
111 VaryType vary
= VARY_NOT_PRESENT
;
112 if (response
->vary_data
.is_valid()) {
114 if (response
->headers
->HasHeaderValue("vary", "user-agent"))
117 UMA_HISTOGRAM_ENUMERATION("HttpCache.Vary", vary
, VARY_MAX
);
124 struct HeaderNameAndValue
{
129 // If the request includes one of these request headers, then avoid caching
130 // to avoid getting confused.
131 static const HeaderNameAndValue kPassThroughHeaders
[] = {
132 { "if-unmodified-since", NULL
}, // causes unexpected 412s
133 { "if-match", NULL
}, // causes unexpected 412s
134 { "if-range", NULL
},
138 struct ValidationHeaderInfo
{
139 const char* request_header_name
;
140 const char* related_response_header_name
;
143 static const ValidationHeaderInfo kValidationHeaders
[] = {
144 { "if-modified-since", "last-modified" },
145 { "if-none-match", "etag" },
148 // If the request includes one of these request headers, then avoid reusing
149 // our cached copy if any.
150 static const HeaderNameAndValue kForceFetchHeaders
[] = {
151 { "cache-control", "no-cache" },
152 { "pragma", "no-cache" },
156 // If the request includes one of these request headers, then force our
157 // cached copy (if any) to be revalidated before reusing it.
158 static const HeaderNameAndValue kForceValidateHeaders
[] = {
159 { "cache-control", "max-age=0" },
163 static bool HeaderMatches(const HttpRequestHeaders
& headers
,
164 const HeaderNameAndValue
* search
) {
165 for (; search
->name
; ++search
) {
166 std::string header_value
;
167 if (!headers
.GetHeader(search
->name
, &header_value
))
173 HttpUtil::ValuesIterator
v(header_value
.begin(), header_value
.end(), ',');
174 while (v
.GetNext()) {
175 if (LowerCaseEqualsASCII(v
.value_begin(), v
.value_end(), search
->value
))
182 //-----------------------------------------------------------------------------
184 HttpCache::Transaction::Transaction(
185 RequestPriority priority
,
187 : next_state_(STATE_NONE
),
190 cache_(cache
->AsWeakPtr()),
195 target_state_(STATE_NONE
),
197 invalid_range_(false),
200 range_requested_(false),
201 handling_206_(false),
202 cache_pending_(false),
203 done_reading_(false),
204 vary_mismatch_(false),
205 couldnt_conditionalize_request_(false),
208 effective_load_flags_(0),
211 io_callback_(base::Bind(&Transaction::OnIOComplete
,
212 weak_factory_
.GetWeakPtr())),
213 transaction_pattern_(PATTERN_UNDEFINED
),
214 total_received_bytes_(0),
215 websocket_handshake_stream_base_create_helper_(NULL
) {
216 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders
==
217 arraysize(kValidationHeaders
),
218 Invalid_number_of_validation_headers
);
221 HttpCache::Transaction::~Transaction() {
222 // We may have to issue another IO, but we should never invoke the callback_
228 bool cancel_request
= reading_
&& response_
.headers
;
229 if (cancel_request
) {
231 entry_
->disk_entry
->CancelSparseIO();
233 cancel_request
&= (response_
.headers
->response_code() == 200);
237 cache_
->DoneWithEntry(entry_
, this, cancel_request
);
238 } else if (cache_pending_
) {
239 cache_
->RemovePendingTransaction(this);
244 int HttpCache::Transaction::WriteMetadata(IOBuffer
* buf
, int buf_len
,
245 const CompletionCallback
& callback
) {
247 DCHECK_GT(buf_len
, 0);
248 DCHECK(!callback
.is_null());
249 if (!cache_
.get() || !entry_
)
250 return ERR_UNEXPECTED
;
252 // We don't need to track this operation for anything.
253 // It could be possible to check if there is something already written and
254 // avoid writing again (it should be the same, right?), but let's allow the
255 // caller to "update" the contents with something new.
256 return entry_
->disk_entry
->WriteData(kMetadataIndex
, 0, buf
, buf_len
,
260 bool HttpCache::Transaction::AddTruncatedFlag() {
261 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
263 // Don't set the flag for sparse entries.
264 if (partial_
.get() && !truncated_
)
267 if (!CanResume(true))
270 // We may have received the whole resource already.
275 target_state_
= STATE_NONE
;
276 next_state_
= STATE_CACHE_WRITE_TRUNCATED_RESPONSE
;
281 LoadState
HttpCache::Transaction::GetWriterLoadState() const {
282 if (network_trans_
.get())
283 return network_trans_
->GetLoadState();
284 if (entry_
|| !request_
)
285 return LOAD_STATE_IDLE
;
286 return LOAD_STATE_WAITING_FOR_CACHE
;
289 const BoundNetLog
& HttpCache::Transaction::net_log() const {
293 int HttpCache::Transaction::Start(const HttpRequestInfo
* request
,
294 const CompletionCallback
& callback
,
295 const BoundNetLog
& net_log
) {
297 DCHECK(!callback
.is_null());
299 // Ensure that we only have one asynchronous call at a time.
300 DCHECK(callback_
.is_null());
302 DCHECK(!network_trans_
.get());
306 return ERR_UNEXPECTED
;
308 SetRequest(net_log
, request
);
310 // We have to wait until the backend is initialized so we start the SM.
311 next_state_
= STATE_GET_BACKEND
;
314 // Setting this here allows us to check for the existence of a callback_ to
315 // determine if we are still inside Start.
316 if (rv
== ERR_IO_PENDING
)
317 callback_
= callback
;
322 int HttpCache::Transaction::RestartIgnoringLastError(
323 const CompletionCallback
& callback
) {
324 DCHECK(!callback
.is_null());
326 // Ensure that we only have one asynchronous call at a time.
327 DCHECK(callback_
.is_null());
330 return ERR_UNEXPECTED
;
332 int rv
= RestartNetworkRequest();
334 if (rv
== ERR_IO_PENDING
)
335 callback_
= callback
;
340 int HttpCache::Transaction::RestartWithCertificate(
341 X509Certificate
* client_cert
,
342 const CompletionCallback
& callback
) {
343 DCHECK(!callback
.is_null());
345 // Ensure that we only have one asynchronous call at a time.
346 DCHECK(callback_
.is_null());
349 return ERR_UNEXPECTED
;
351 int rv
= RestartNetworkRequestWithCertificate(client_cert
);
353 if (rv
== ERR_IO_PENDING
)
354 callback_
= callback
;
359 int HttpCache::Transaction::RestartWithAuth(
360 const AuthCredentials
& credentials
,
361 const CompletionCallback
& callback
) {
362 DCHECK(auth_response_
.headers
.get());
363 DCHECK(!callback
.is_null());
365 // Ensure that we only have one asynchronous call at a time.
366 DCHECK(callback_
.is_null());
369 return ERR_UNEXPECTED
;
371 // Clear the intermediate response since we are going to start over.
372 auth_response_
= HttpResponseInfo();
374 int rv
= RestartNetworkRequestWithAuth(credentials
);
376 if (rv
== ERR_IO_PENDING
)
377 callback_
= callback
;
382 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
383 if (!network_trans_
.get())
385 return network_trans_
->IsReadyToRestartForAuth();
388 int HttpCache::Transaction::Read(IOBuffer
* buf
, int buf_len
,
389 const CompletionCallback
& callback
) {
391 DCHECK_GT(buf_len
, 0);
392 DCHECK(!callback
.is_null());
394 DCHECK(callback_
.is_null());
397 return ERR_UNEXPECTED
;
399 // If we have an intermediate auth response at this point, then it means the
400 // user wishes to read the network response (the error page). If there is a
401 // previous response in the cache then we should leave it intact.
402 if (auth_response_
.headers
.get() && mode_
!= NONE
) {
403 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
404 DCHECK(mode_
& WRITE
);
405 DoneWritingToEntry(mode_
== READ_WRITE
);
414 DCHECK(partial_
.get());
415 if (!network_trans_
.get()) {
416 // We are just reading from the cache, but we may be writing later.
417 rv
= ReadFromEntry(buf
, buf_len
);
422 DCHECK(network_trans_
.get());
423 rv
= ReadFromNetwork(buf
, buf_len
);
426 rv
= ReadFromEntry(buf
, buf_len
);
433 if (rv
== ERR_IO_PENDING
) {
434 DCHECK(callback_
.is_null());
435 callback_
= callback
;
440 void HttpCache::Transaction::StopCaching() {
441 // We really don't know where we are now. Hopefully there is no operation in
442 // progress, but nothing really prevents this method to be called after we
443 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
444 // point because we need the state machine for that (and even if we are really
445 // free, that would be an asynchronous operation). In other words, keep the
446 // entry how it is (it will be marked as truncated at destruction), and let
447 // the next piece of code that executes know that we are now reading directly
449 if (cache_
.get() && entry_
&& (mode_
& WRITE
) && network_trans_
.get() &&
450 !is_sparse_
&& !range_requested_
) {
455 bool HttpCache::Transaction::GetFullRequestHeaders(
456 HttpRequestHeaders
* headers
) const {
458 return network_trans_
->GetFullRequestHeaders(headers
);
460 // TODO(ttuttle): Read headers from cache.
464 int64
HttpCache::Transaction::GetTotalReceivedBytes() const {
465 int64 total_received_bytes
= total_received_bytes_
;
467 total_received_bytes
+= network_trans_
->GetTotalReceivedBytes();
468 return total_received_bytes
;
471 void HttpCache::Transaction::DoneReading() {
472 if (cache_
.get() && entry_
) {
473 DCHECK_NE(mode_
, UPDATE
);
475 DoneWritingToEntry(true);
476 } else if (mode_
& READ
) {
477 // It is necessary to check mode_ & READ because it is possible
478 // for mode_ to be NONE and entry_ non-NULL with a write entry
479 // if StopCaching was called.
480 cache_
->DoneReadingFromEntry(entry_
, this);
486 const HttpResponseInfo
* HttpCache::Transaction::GetResponseInfo() const {
487 // Null headers means we encountered an error or haven't a response yet
488 if (auth_response_
.headers
.get())
489 return &auth_response_
;
490 return (response_
.headers
.get() || response_
.ssl_info
.cert
.get() ||
491 response_
.cert_request_info
.get())
496 LoadState
HttpCache::Transaction::GetLoadState() const {
497 LoadState state
= GetWriterLoadState();
498 if (state
!= LOAD_STATE_WAITING_FOR_CACHE
)
502 return cache_
->GetLoadStateForPendingTransaction(this);
504 return LOAD_STATE_IDLE
;
507 UploadProgress
HttpCache::Transaction::GetUploadProgress() const {
508 if (network_trans_
.get())
509 return network_trans_
->GetUploadProgress();
510 return final_upload_progress_
;
513 void HttpCache::Transaction::SetQuicServerInfo(
514 QuicServerInfo
* quic_server_info
) {}
516 bool HttpCache::Transaction::GetLoadTimingInfo(
517 LoadTimingInfo
* load_timing_info
) const {
519 return network_trans_
->GetLoadTimingInfo(load_timing_info
);
521 if (old_network_trans_load_timing_
) {
522 *load_timing_info
= *old_network_trans_load_timing_
;
526 if (first_cache_access_since_
.is_null())
529 // If the cache entry was opened, return that time.
530 load_timing_info
->send_start
= first_cache_access_since_
;
531 // This time doesn't make much sense when reading from the cache, so just use
532 // the same time as send_start.
533 load_timing_info
->send_end
= first_cache_access_since_
;
537 void HttpCache::Transaction::SetPriority(RequestPriority priority
) {
538 priority_
= priority
;
540 network_trans_
->SetPriority(priority_
);
543 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
544 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
545 websocket_handshake_stream_base_create_helper_
= create_helper
;
547 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(create_helper
);
550 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
551 const BeforeNetworkStartCallback
& callback
) {
552 DCHECK(!network_trans_
);
553 before_network_start_callback_
= callback
;
556 int HttpCache::Transaction::ResumeNetworkStart() {
558 return network_trans_
->ResumeNetworkStart();
559 return ERR_UNEXPECTED
;
562 //-----------------------------------------------------------------------------
564 void HttpCache::Transaction::DoCallback(int rv
) {
565 DCHECK(rv
!= ERR_IO_PENDING
);
566 DCHECK(!callback_
.is_null());
568 read_buf_
= NULL
; // Release the buffer before invoking the callback.
570 // Since Run may result in Read being called, clear callback_ up front.
571 CompletionCallback c
= callback_
;
576 int HttpCache::Transaction::HandleResult(int rv
) {
577 DCHECK(rv
!= ERR_IO_PENDING
);
578 if (!callback_
.is_null())
584 // A few common patterns: (Foo* means Foo -> FooComplete)
588 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
589 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
590 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
591 // PartialHeadersReceived
594 // NetworkRead* -> CacheWriteData*
596 // Cached entry, no validation:
598 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
599 // -> BeginPartialCacheValidation() -> BeginCacheValidation()
604 // Cached entry, validation (304):
606 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
607 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
608 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
609 // CacheWriteResponse* -> UpdateCachedResponseComplete ->
610 // OverwriteCachedResponse -> PartialHeadersReceived
615 // Cached entry, validation and replace (200):
617 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
618 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
619 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
620 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
621 // PartialHeadersReceived
624 // NetworkRead* -> CacheWriteData*
626 // Sparse entry, partially cached, byte range request:
628 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
629 // -> BeginPartialCacheValidation() -> CacheQueryData* ->
630 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
631 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
632 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
633 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
634 // PartialHeadersReceived
637 // NetworkRead* -> CacheWriteData*
640 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
641 // CompletePartialCacheValidation -> CacheReadData* ->
644 // CacheReadData* -> StartPartialCacheValidation ->
645 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
646 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
647 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
649 int HttpCache::Transaction::DoLoop(int result
) {
650 DCHECK(next_state_
!= STATE_NONE
);
654 State state
= next_state_
;
655 next_state_
= STATE_NONE
;
657 case STATE_GET_BACKEND
:
661 case STATE_GET_BACKEND_COMPLETE
:
662 rv
= DoGetBackendComplete(rv
);
664 case STATE_SEND_REQUEST
:
666 rv
= DoSendRequest();
668 case STATE_SEND_REQUEST_COMPLETE
:
669 rv
= DoSendRequestComplete(rv
);
671 case STATE_SUCCESSFUL_SEND_REQUEST
:
673 rv
= DoSuccessfulSendRequest();
675 case STATE_NETWORK_READ
:
677 rv
= DoNetworkRead();
679 case STATE_NETWORK_READ_COMPLETE
:
680 rv
= DoNetworkReadComplete(rv
);
682 case STATE_INIT_ENTRY
:
686 case STATE_OPEN_ENTRY
:
690 case STATE_OPEN_ENTRY_COMPLETE
:
691 rv
= DoOpenEntryComplete(rv
);
693 case STATE_CREATE_ENTRY
:
695 rv
= DoCreateEntry();
697 case STATE_CREATE_ENTRY_COMPLETE
:
698 rv
= DoCreateEntryComplete(rv
);
700 case STATE_DOOM_ENTRY
:
704 case STATE_DOOM_ENTRY_COMPLETE
:
705 rv
= DoDoomEntryComplete(rv
);
707 case STATE_ADD_TO_ENTRY
:
711 case STATE_ADD_TO_ENTRY_COMPLETE
:
712 rv
= DoAddToEntryComplete(rv
);
714 case STATE_START_PARTIAL_CACHE_VALIDATION
:
716 rv
= DoStartPartialCacheValidation();
718 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
:
719 rv
= DoCompletePartialCacheValidation(rv
);
721 case STATE_UPDATE_CACHED_RESPONSE
:
723 rv
= DoUpdateCachedResponse();
725 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE
:
726 rv
= DoUpdateCachedResponseComplete(rv
);
728 case STATE_OVERWRITE_CACHED_RESPONSE
:
730 rv
= DoOverwriteCachedResponse();
732 case STATE_TRUNCATE_CACHED_DATA
:
734 rv
= DoTruncateCachedData();
736 case STATE_TRUNCATE_CACHED_DATA_COMPLETE
:
737 rv
= DoTruncateCachedDataComplete(rv
);
739 case STATE_TRUNCATE_CACHED_METADATA
:
741 rv
= DoTruncateCachedMetadata();
743 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE
:
744 rv
= DoTruncateCachedMetadataComplete(rv
);
746 case STATE_PARTIAL_HEADERS_RECEIVED
:
748 rv
= DoPartialHeadersReceived();
750 case STATE_CACHE_READ_RESPONSE
:
752 rv
= DoCacheReadResponse();
754 case STATE_CACHE_READ_RESPONSE_COMPLETE
:
755 rv
= DoCacheReadResponseComplete(rv
);
757 case STATE_CACHE_WRITE_RESPONSE
:
759 rv
= DoCacheWriteResponse();
761 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
763 rv
= DoCacheWriteTruncatedResponse();
765 case STATE_CACHE_WRITE_RESPONSE_COMPLETE
:
766 rv
= DoCacheWriteResponseComplete(rv
);
768 case STATE_CACHE_READ_METADATA
:
770 rv
= DoCacheReadMetadata();
772 case STATE_CACHE_READ_METADATA_COMPLETE
:
773 rv
= DoCacheReadMetadataComplete(rv
);
775 case STATE_CACHE_QUERY_DATA
:
777 rv
= DoCacheQueryData();
779 case STATE_CACHE_QUERY_DATA_COMPLETE
:
780 rv
= DoCacheQueryDataComplete(rv
);
782 case STATE_CACHE_READ_DATA
:
784 rv
= DoCacheReadData();
786 case STATE_CACHE_READ_DATA_COMPLETE
:
787 rv
= DoCacheReadDataComplete(rv
);
789 case STATE_CACHE_WRITE_DATA
:
790 rv
= DoCacheWriteData(rv
);
792 case STATE_CACHE_WRITE_DATA_COMPLETE
:
793 rv
= DoCacheWriteDataComplete(rv
);
796 NOTREACHED() << "bad state";
800 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
802 if (rv
!= ERR_IO_PENDING
)
808 int HttpCache::Transaction::DoGetBackend() {
809 cache_pending_
= true;
810 next_state_
= STATE_GET_BACKEND_COMPLETE
;
811 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
);
812 return cache_
->GetBackendForTransaction(this);
815 int HttpCache::Transaction::DoGetBackendComplete(int result
) {
816 DCHECK(result
== OK
|| result
== ERR_FAILED
);
817 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND
,
819 cache_pending_
= false;
821 if (!ShouldPassThrough()) {
822 cache_key_
= cache_
->GenerateCacheKey(request_
);
824 // Requested cache access mode.
825 if (effective_load_flags_
& LOAD_ONLY_FROM_CACHE
) {
827 } else if (effective_load_flags_
& LOAD_BYPASS_CACHE
) {
833 // Downgrade to UPDATE if the request has been externally conditionalized.
834 if (external_validation_
.initialized
) {
836 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
837 // in case READ was off).
845 // Use PUT and DELETE only to invalidate existing stored entries.
846 if ((request_
->method
== "PUT" || request_
->method
== "DELETE") &&
847 mode_
!= READ_WRITE
&& mode_
!= WRITE
) {
851 // If must use cache, then we must fail. This can happen for back/forward
852 // navigations to a page generated via a form post.
853 if (!(mode_
& READ
) && effective_load_flags_
& LOAD_ONLY_FROM_CACHE
)
854 return ERR_CACHE_MISS
;
857 if (partial_
.get()) {
858 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
861 next_state_
= STATE_SEND_REQUEST
;
863 next_state_
= STATE_INIT_ENTRY
;
866 // This is only set if we have something to do with the response.
867 range_requested_
= (partial_
.get() != NULL
);
872 int HttpCache::Transaction::DoSendRequest() {
873 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
874 DCHECK(!network_trans_
.get());
876 send_request_since_
= TimeTicks::Now();
878 // Create a network transaction.
879 int rv
= cache_
->network_layer_
->CreateTransaction(priority_
,
883 network_trans_
->SetBeforeNetworkStartCallback(before_network_start_callback_
);
885 // Old load timing information, if any, is now obsolete.
886 old_network_trans_load_timing_
.reset();
888 if (websocket_handshake_stream_base_create_helper_
)
889 network_trans_
->SetWebSocketHandshakeStreamCreateHelper(
890 websocket_handshake_stream_base_create_helper_
);
892 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
893 rv
= network_trans_
->Start(request_
, io_callback_
, net_log_
);
897 int HttpCache::Transaction::DoSendRequestComplete(int result
) {
899 return ERR_UNEXPECTED
;
901 // If requested, and we have a readable cache entry, and we have
902 // an error indicating that we're offline as opposed to in contact
903 // with a bad server, read from cache anyway.
904 if (IsOfflineError(result
)) {
905 if (mode_
== READ_WRITE
&& entry_
&& !partial_
) {
906 RecordOfflineStatus(effective_load_flags_
,
907 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE
);
908 if (effective_load_flags_
& LOAD_FROM_CACHE_IF_OFFLINE
) {
909 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
910 response_
.server_data_unavailable
= true;
911 return SetupEntryForRead();
914 RecordOfflineStatus(effective_load_flags_
,
915 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE
);
918 RecordOfflineStatus(effective_load_flags_
,
919 (result
== OK
? OFFLINE_STATUS_NETWORK_SUCCEEDED
:
920 OFFLINE_STATUS_NETWORK_FAILED
));
923 // If we tried to conditionalize the request and failed, we know
924 // we won't be reading from the cache after this point.
925 if (couldnt_conditionalize_request_
)
929 next_state_
= STATE_SUCCESSFUL_SEND_REQUEST
;
933 // Do not record requests that have network errors or restarts.
934 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
935 if (IsCertificateError(result
)) {
936 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
937 // If we get a certificate error, then there is a certificate in ssl_info,
938 // so GetResponseInfo() should never return NULL here.
940 response_
.ssl_info
= response
->ssl_info
;
941 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
942 const HttpResponseInfo
* response
= network_trans_
->GetResponseInfo();
944 response_
.cert_request_info
= response
->cert_request_info
;
945 } else if (response_
.was_cached
) {
946 DoneWritingToEntry(true);
951 // We received the response headers and there is no error.
952 int HttpCache::Transaction::DoSuccessfulSendRequest() {
953 DCHECK(!new_response_
);
954 const HttpResponseInfo
* new_response
= network_trans_
->GetResponseInfo();
955 bool authentication_failure
= false;
957 if (new_response
->headers
->response_code() == 401 ||
958 new_response
->headers
->response_code() == 407) {
959 auth_response_
= *new_response
;
963 // We initiated a second request the caller doesn't know about. We should be
964 // able to authenticate this request because we should have authenticated
965 // this URL moments ago.
966 if (IsReadyToRestartForAuth()) {
967 DCHECK(!response_
.auth_challenge
.get());
968 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
969 // In theory we should check to see if there are new cookies, but there
970 // is no way to do that from here.
971 return network_trans_
->RestartWithAuth(AuthCredentials(), io_callback_
);
974 // We have to perform cleanup at this point so that at least the next
975 // request can succeed.
976 authentication_failure
= true;
978 DoomPartialEntry(false);
983 new_response_
= new_response
;
984 if (authentication_failure
||
985 (!ValidatePartialResponse() && !auth_response_
.headers
.get())) {
986 // Something went wrong with this request and we have to restart it.
987 // If we have an authentication response, we are exposed to weird things
988 // hapenning if the user cancels the authentication before we receive
990 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
991 response_
= HttpResponseInfo();
992 ResetNetworkTransaction();
993 new_response_
= NULL
;
994 next_state_
= STATE_SEND_REQUEST
;
998 if (handling_206_
&& mode_
== READ_WRITE
&& !truncated_
&& !is_sparse_
) {
999 // We have stored the full entry, but it changed and the server is
1000 // sending a range. We have to delete the old entry.
1001 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1002 DoneWritingToEntry(false);
1005 if (mode_
== WRITE
&&
1006 transaction_pattern_
!= PATTERN_ENTRY_CANT_CONDITIONALIZE
) {
1007 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED
);
1010 if (mode_
== WRITE
&&
1011 (request_
->method
== "PUT" || request_
->method
== "DELETE")) {
1012 if (NonErrorResponse(new_response
->headers
->response_code())) {
1013 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1016 cache_
->DoneWritingToEntry(entry_
, true);
1021 if (request_
->method
== "POST" &&
1022 NonErrorResponse(new_response
->headers
->response_code())) {
1023 cache_
->DoomMainEntryForUrl(request_
->url
);
1026 RecordVaryHeaderHistogram(new_response
);
1028 if (new_response_
->headers
->response_code() == 416 &&
1029 (request_
->method
== "GET" || request_
->method
== "POST")) {
1030 // If there is an ective entry it may be destroyed with this transaction.
1031 response_
= *new_response_
;
1035 // Are we expecting a response to a conditional query?
1036 if (mode_
== READ_WRITE
|| mode_
== UPDATE
) {
1037 if (new_response
->headers
->response_code() == 304 || handling_206_
) {
1038 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED
);
1039 next_state_
= STATE_UPDATE_CACHED_RESPONSE
;
1042 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED
);
1046 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1050 int HttpCache::Transaction::DoNetworkRead() {
1051 next_state_
= STATE_NETWORK_READ_COMPLETE
;
1052 return network_trans_
->Read(read_buf_
.get(), io_buf_len_
, io_callback_
);
1055 int HttpCache::Transaction::DoNetworkReadComplete(int result
) {
1056 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1059 return ERR_UNEXPECTED
;
1061 // If there is an error or we aren't saving the data, we are done; just wait
1062 // until the destructor runs to see if we can keep the data.
1063 if (mode_
== NONE
|| result
< 0)
1066 next_state_
= STATE_CACHE_WRITE_DATA
;
1070 int HttpCache::Transaction::DoInitEntry() {
1071 DCHECK(!new_entry_
);
1074 return ERR_UNEXPECTED
;
1076 if (mode_
== WRITE
) {
1077 next_state_
= STATE_DOOM_ENTRY
;
1081 next_state_
= STATE_OPEN_ENTRY
;
1085 int HttpCache::Transaction::DoOpenEntry() {
1086 DCHECK(!new_entry_
);
1087 next_state_
= STATE_OPEN_ENTRY_COMPLETE
;
1088 cache_pending_
= true;
1089 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
);
1090 first_cache_access_since_
= TimeTicks::Now();
1091 return cache_
->OpenEntry(cache_key_
, &new_entry_
, this);
1094 int HttpCache::Transaction::DoOpenEntryComplete(int result
) {
1095 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1096 // OK, otherwise the cache will end up with an active entry without any
1097 // transaction attached.
1098 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY
, result
);
1099 cache_pending_
= false;
1101 next_state_
= STATE_ADD_TO_ENTRY
;
1105 if (result
== ERR_CACHE_RACE
) {
1106 next_state_
= STATE_INIT_ENTRY
;
1110 if (request_
->method
== "PUT" || request_
->method
== "DELETE") {
1111 DCHECK(mode_
== READ_WRITE
|| mode_
== WRITE
);
1113 next_state_
= STATE_SEND_REQUEST
;
1117 if (mode_
== READ_WRITE
) {
1119 next_state_
= STATE_CREATE_ENTRY
;
1122 if (mode_
== UPDATE
) {
1123 // There is no cache entry to update; proceed without caching.
1125 next_state_
= STATE_SEND_REQUEST
;
1128 if (cache_
->mode() == PLAYBACK
)
1129 DVLOG(1) << "Playback Cache Miss: " << request_
->url
;
1131 // The entry does not exist, and we are not permitted to create a new entry,
1133 return ERR_CACHE_MISS
;
1136 int HttpCache::Transaction::DoCreateEntry() {
1137 DCHECK(!new_entry_
);
1138 next_state_
= STATE_CREATE_ENTRY_COMPLETE
;
1139 cache_pending_
= true;
1140 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
);
1141 return cache_
->CreateEntry(cache_key_
, &new_entry_
, this);
1144 int HttpCache::Transaction::DoCreateEntryComplete(int result
) {
1145 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1146 // OK, otherwise the cache will end up with an active entry without any
1147 // transaction attached.
1148 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY
,
1150 cache_pending_
= false;
1151 next_state_
= STATE_ADD_TO_ENTRY
;
1153 if (result
== ERR_CACHE_RACE
) {
1154 next_state_
= STATE_INIT_ENTRY
;
1159 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", false);
1161 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", true);
1162 // We have a race here: Maybe we failed to open the entry and decided to
1163 // create one, but by the time we called create, another transaction already
1164 // created the entry. If we want to eliminate this issue, we need an atomic
1165 // OpenOrCreate() method exposed by the disk cache.
1166 DLOG(WARNING
) << "Unable to create cache entry";
1169 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1170 next_state_
= STATE_SEND_REQUEST
;
1175 int HttpCache::Transaction::DoDoomEntry() {
1176 next_state_
= STATE_DOOM_ENTRY_COMPLETE
;
1177 cache_pending_
= true;
1178 if (first_cache_access_since_
.is_null())
1179 first_cache_access_since_
= TimeTicks::Now();
1180 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
);
1181 return cache_
->DoomEntry(cache_key_
, this);
1184 int HttpCache::Transaction::DoDoomEntryComplete(int result
) {
1185 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY
, result
);
1186 next_state_
= STATE_CREATE_ENTRY
;
1187 cache_pending_
= false;
1188 if (result
== ERR_CACHE_RACE
)
1189 next_state_
= STATE_INIT_ENTRY
;
1193 int HttpCache::Transaction::DoAddToEntry() {
1195 cache_pending_
= true;
1196 next_state_
= STATE_ADD_TO_ENTRY_COMPLETE
;
1197 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
);
1198 DCHECK(entry_lock_waiting_since_
.is_null());
1199 entry_lock_waiting_since_
= TimeTicks::Now();
1200 return cache_
->AddTransactionToEntry(new_entry_
, this);
1203 int HttpCache::Transaction::DoAddToEntryComplete(int result
) {
1204 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY
,
1206 const TimeDelta entry_lock_wait
=
1207 TimeTicks::Now() - entry_lock_waiting_since_
;
1208 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait
);
1210 entry_lock_waiting_since_
= TimeTicks();
1212 cache_pending_
= false;
1215 entry_
= new_entry_
;
1217 // If there is a failure, the cache should have taken care of new_entry_.
1220 if (result
== ERR_CACHE_RACE
) {
1221 next_state_
= STATE_INIT_ENTRY
;
1230 if (mode_
== WRITE
) {
1232 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
1233 next_state_
= STATE_SEND_REQUEST
;
1235 // We have to read the headers from the cached entry.
1236 DCHECK(mode_
& READ_META
);
1237 next_state_
= STATE_CACHE_READ_RESPONSE
;
1242 // We may end up here multiple times for a given request.
1243 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1247 next_state_
= STATE_COMPLETE_PARTIAL_CACHE_VALIDATION
;
1248 return partial_
->ShouldValidateCache(entry_
->disk_entry
, io_callback_
);
1251 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result
) {
1253 // This is the end of the request.
1254 if (mode_
& WRITE
) {
1255 DoneWritingToEntry(true);
1257 cache_
->DoneReadingFromEntry(entry_
, this);
1266 partial_
->PrepareCacheValidation(entry_
->disk_entry
,
1267 &custom_request_
->extra_headers
);
1269 if (reading_
&& partial_
->IsCurrentRangeCached()) {
1270 next_state_
= STATE_CACHE_READ_DATA
;
1274 return BeginCacheValidation();
1277 // We received 304 or 206 and we want to update the cached response headers.
1278 int HttpCache::Transaction::DoUpdateCachedResponse() {
1279 next_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1281 // Update cached response based on headers in new_response.
1282 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1283 response_
.headers
->Update(*new_response_
->headers
.get());
1284 response_
.response_time
= new_response_
->response_time
;
1285 response_
.request_time
= new_response_
->request_time
;
1286 response_
.network_accessed
= new_response_
->network_accessed
;
1288 if (response_
.headers
->HasHeaderValue("cache-control", "no-store")) {
1289 if (!entry_
->doomed
) {
1290 int ret
= cache_
->DoomEntry(cache_key_
, NULL
);
1294 // If we are already reading, we already updated the headers for this
1295 // request; doing it again will change Content-Length.
1297 target_state_
= STATE_UPDATE_CACHED_RESPONSE_COMPLETE
;
1298 next_state_
= STATE_CACHE_WRITE_RESPONSE
;
1305 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result
) {
1306 if (mode_
== UPDATE
) {
1307 DCHECK(!handling_206_
);
1308 // We got a "not modified" response and already updated the corresponding
1309 // cache entry above.
1311 // By closing the cached entry now, we make sure that the 304 rather than
1312 // the cached 200 response, is what will be returned to the user.
1313 DoneWritingToEntry(true);
1314 } else if (entry_
&& !handling_206_
) {
1315 DCHECK_EQ(READ_WRITE
, mode_
);
1316 if (!partial_
.get() || partial_
->IsLastRange()) {
1317 cache_
->ConvertWriterToReader(entry_
);
1320 // We no longer need the network transaction, so destroy it.
1321 final_upload_progress_
= network_trans_
->GetUploadProgress();
1322 ResetNetworkTransaction();
1323 } else if (entry_
&& handling_206_
&& truncated_
&&
1324 partial_
->initial_validation()) {
1325 // We just finished the validation of a truncated entry, and the server
1326 // is willing to resume the operation. Now we go back and start serving
1327 // the first part to the user.
1328 ResetNetworkTransaction();
1329 new_response_
= NULL
;
1330 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
1331 partial_
->SetRangeToStartDownload();
1334 next_state_
= STATE_OVERWRITE_CACHED_RESPONSE
;
1338 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1340 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1344 // We change the value of Content-Length for partial content.
1345 if (handling_206_
&& partial_
.get())
1346 partial_
->FixContentLength(new_response_
->headers
.get());
1348 response_
= *new_response_
;
1350 if (handling_206_
&& !CanResume(false)) {
1351 // There is no point in storing this resource because it will never be used.
1352 DoneWritingToEntry(false);
1354 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1355 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1359 target_state_
= STATE_TRUNCATE_CACHED_DATA
;
1360 next_state_
= truncated_
? STATE_CACHE_WRITE_TRUNCATED_RESPONSE
:
1361 STATE_CACHE_WRITE_RESPONSE
;
1365 int HttpCache::Transaction::DoTruncateCachedData() {
1366 next_state_
= STATE_TRUNCATE_CACHED_DATA_COMPLETE
;
1369 if (net_log_
.IsLoggingAllEvents())
1370 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1371 // Truncate the stream.
1372 return WriteToEntry(kResponseContentIndex
, 0, NULL
, 0, io_callback_
);
1375 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result
) {
1377 if (net_log_
.IsLoggingAllEvents()) {
1378 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1383 next_state_
= STATE_TRUNCATE_CACHED_METADATA
;
1387 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1388 next_state_
= STATE_TRUNCATE_CACHED_METADATA_COMPLETE
;
1392 if (net_log_
.IsLoggingAllEvents())
1393 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1394 return WriteToEntry(kMetadataIndex
, 0, NULL
, 0, io_callback_
);
1397 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result
) {
1399 if (net_log_
.IsLoggingAllEvents()) {
1400 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1405 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
1409 int HttpCache::Transaction::DoPartialHeadersReceived() {
1410 new_response_
= NULL
;
1411 if (entry_
&& !partial_
.get() &&
1412 entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
1413 next_state_
= STATE_CACHE_READ_METADATA
;
1415 if (!partial_
.get())
1419 if (network_trans_
.get()) {
1420 next_state_
= STATE_NETWORK_READ
;
1422 next_state_
= STATE_CACHE_READ_DATA
;
1424 } else if (mode_
!= NONE
) {
1425 // We are about to return the headers for a byte-range request to the user,
1426 // so let's fix them.
1427 partial_
->FixResponseHeaders(response_
.headers
.get(), true);
1432 int HttpCache::Transaction::DoCacheReadResponse() {
1434 next_state_
= STATE_CACHE_READ_RESPONSE_COMPLETE
;
1436 io_buf_len_
= entry_
->disk_entry
->GetDataSize(kResponseInfoIndex
);
1437 read_buf_
= new IOBuffer(io_buf_len_
);
1439 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1440 return entry_
->disk_entry
->ReadData(kResponseInfoIndex
, 0, read_buf_
.get(),
1441 io_buf_len_
, io_callback_
);
1444 int HttpCache::Transaction::DoCacheReadResponseComplete(int result
) {
1445 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1446 if (result
!= io_buf_len_
||
1447 !HttpCache::ParseResponseInfo(read_buf_
->data(), io_buf_len_
,
1448 &response_
, &truncated_
)) {
1449 return OnCacheReadError(result
, true);
1452 // Some resources may have slipped in as truncated when they're not.
1453 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1454 if (response_
.headers
->GetContentLength() == current_size
)
1457 // We now have access to the cache entry.
1459 // o if we are a reader for the transaction, then we can start reading the
1462 // o if we can read or write, then we should check if the cache entry needs
1463 // to be validated and then issue a network request if needed or just read
1464 // from the cache if the cache entry is already valid.
1466 // o if we are set to UPDATE, then we are handling an externally
1467 // conditionalized request (if-modified-since / if-none-match). We check
1468 // if the request headers define a validation request.
1472 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
1473 result
= BeginCacheRead();
1476 result
= BeginPartialCacheValidation();
1479 result
= BeginExternallyConditionalizedRequest();
1484 result
= ERR_FAILED
;
1489 int HttpCache::Transaction::DoCacheWriteResponse() {
1491 if (net_log_
.IsLoggingAllEvents())
1492 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1494 return WriteResponseInfoToEntry(false);
1497 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1499 if (net_log_
.IsLoggingAllEvents())
1500 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
1502 return WriteResponseInfoToEntry(true);
1505 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result
) {
1506 next_state_
= target_state_
;
1507 target_state_
= STATE_NONE
;
1510 if (net_log_
.IsLoggingAllEvents()) {
1511 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
,
1515 // Balance the AddRef from WriteResponseInfoToEntry.
1516 if (result
!= io_buf_len_
) {
1517 DLOG(ERROR
) << "failed to write response info to cache";
1518 DoneWritingToEntry(false);
1523 int HttpCache::Transaction::DoCacheReadMetadata() {
1525 DCHECK(!response_
.metadata
.get());
1526 next_state_
= STATE_CACHE_READ_METADATA_COMPLETE
;
1528 response_
.metadata
=
1529 new IOBufferWithSize(entry_
->disk_entry
->GetDataSize(kMetadataIndex
));
1531 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO
);
1532 return entry_
->disk_entry
->ReadData(kMetadataIndex
, 0,
1533 response_
.metadata
.get(),
1534 response_
.metadata
->size(),
1538 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result
) {
1539 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO
, result
);
1540 if (result
!= response_
.metadata
->size())
1541 return OnCacheReadError(result
, false);
1545 int HttpCache::Transaction::DoCacheQueryData() {
1546 next_state_
= STATE_CACHE_QUERY_DATA_COMPLETE
;
1547 return entry_
->disk_entry
->ReadyForSparseIO(io_callback_
);
1550 int HttpCache::Transaction::DoCacheQueryDataComplete(int result
) {
1551 if (result
== ERR_NOT_IMPLEMENTED
) {
1552 // Restart the request overwriting the cache entry.
1553 // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1554 // supports Sparse IO.
1555 return DoRestartPartialRequest();
1557 DCHECK_EQ(OK
, result
);
1559 return ERR_UNEXPECTED
;
1561 return ValidateEntryHeadersAndContinue();
1564 int HttpCache::Transaction::DoCacheReadData() {
1566 next_state_
= STATE_CACHE_READ_DATA_COMPLETE
;
1568 if (net_log_
.IsLoggingAllEvents())
1569 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA
);
1570 if (partial_
.get()) {
1571 return partial_
->CacheRead(entry_
->disk_entry
, read_buf_
.get(), io_buf_len_
,
1575 return entry_
->disk_entry
->ReadData(kResponseContentIndex
, read_offset_
,
1576 read_buf_
.get(), io_buf_len_
,
1580 int HttpCache::Transaction::DoCacheReadDataComplete(int result
) {
1581 if (net_log_
.IsLoggingAllEvents()) {
1582 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA
,
1587 return ERR_UNEXPECTED
;
1589 if (partial_
.get()) {
1590 // Partial requests are confusing to report in histograms because they may
1591 // have multiple underlying requests.
1592 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1593 return DoPartialCacheReadCompleted(result
);
1597 read_offset_
+= result
;
1598 } else if (result
== 0) { // End of file.
1600 cache_
->DoneReadingFromEntry(entry_
, this);
1603 return OnCacheReadError(result
, false);
1608 int HttpCache::Transaction::DoCacheWriteData(int num_bytes
) {
1609 next_state_
= STATE_CACHE_WRITE_DATA_COMPLETE
;
1610 write_len_
= num_bytes
;
1612 if (net_log_
.IsLoggingAllEvents())
1613 net_log_
.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
);
1616 return AppendResponseDataToEntry(read_buf_
.get(), num_bytes
, io_callback_
);
1619 int HttpCache::Transaction::DoCacheWriteDataComplete(int result
) {
1621 if (net_log_
.IsLoggingAllEvents()) {
1622 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA
,
1626 // Balance the AddRef from DoCacheWriteData.
1628 return ERR_UNEXPECTED
;
1630 if (result
!= write_len_
) {
1631 DLOG(ERROR
) << "failed to write response data to cache";
1632 DoneWritingToEntry(false);
1634 // We want to ignore errors writing to disk and just keep reading from
1636 result
= write_len_
;
1637 } else if (!done_reading_
&& entry_
) {
1638 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
1639 int64 body_size
= response_
.headers
->GetContentLength();
1640 if (body_size
>= 0 && body_size
<= current_size
)
1641 done_reading_
= true;
1644 if (partial_
.get()) {
1645 // This may be the last request.
1646 if (!(result
== 0 && !truncated_
&&
1647 (partial_
->IsLastRange() || mode_
== WRITE
)))
1648 return DoPartialNetworkReadCompleted(result
);
1652 // End of file. This may be the result of a connection problem so see if we
1653 // have to keep the entry around to be flagged as truncated later on.
1654 if (done_reading_
|| !entry_
|| partial_
.get() ||
1655 response_
.headers
->GetContentLength() <= 0)
1656 DoneWritingToEntry(true);
1662 //-----------------------------------------------------------------------------
1664 void HttpCache::Transaction::SetRequest(const BoundNetLog
& net_log
,
1665 const HttpRequestInfo
* request
) {
1668 effective_load_flags_
= request_
->load_flags
;
1670 switch (cache_
->mode()) {
1674 // When in record mode, we want to NEVER load from the cache.
1675 // The reason for this is beacuse we save the Set-Cookie headers
1676 // (intentionally). If we read from the cache, we replay them
1678 effective_load_flags_
|= LOAD_BYPASS_CACHE
;
1681 // When in playback mode, we want to load exclusively from the cache.
1682 effective_load_flags_
|= LOAD_ONLY_FROM_CACHE
;
1685 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
1689 // Some headers imply load flags. The order here is significant.
1691 // LOAD_DISABLE_CACHE : no cache read or write
1692 // LOAD_BYPASS_CACHE : no cache read
1693 // LOAD_VALIDATE_CACHE : no cache read unless validation
1695 // The former modes trump latter modes, so if we find a matching header we
1696 // can stop iterating kSpecialHeaders.
1698 static const struct {
1699 const HeaderNameAndValue
* search
;
1701 } kSpecialHeaders
[] = {
1702 { kPassThroughHeaders
, LOAD_DISABLE_CACHE
},
1703 { kForceFetchHeaders
, LOAD_BYPASS_CACHE
},
1704 { kForceValidateHeaders
, LOAD_VALIDATE_CACHE
},
1707 bool range_found
= false;
1708 bool external_validation_error
= false;
1710 if (request_
->extra_headers
.HasHeader(HttpRequestHeaders::kRange
))
1713 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kSpecialHeaders
); ++i
) {
1714 if (HeaderMatches(request_
->extra_headers
, kSpecialHeaders
[i
].search
)) {
1715 effective_load_flags_
|= kSpecialHeaders
[i
].load_flag
;
1720 // Check for conditionalization headers which may correspond with a
1721 // cache validation request.
1722 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); ++i
) {
1723 const ValidationHeaderInfo
& info
= kValidationHeaders
[i
];
1724 std::string validation_value
;
1725 if (request_
->extra_headers
.GetHeader(
1726 info
.request_header_name
, &validation_value
)) {
1727 if (!external_validation_
.values
[i
].empty() ||
1728 validation_value
.empty()) {
1729 external_validation_error
= true;
1731 external_validation_
.values
[i
] = validation_value
;
1732 external_validation_
.initialized
= true;
1736 // We don't support ranges and validation headers.
1737 if (range_found
&& external_validation_
.initialized
) {
1738 LOG(WARNING
) << "Byte ranges AND validation headers found.";
1739 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
1742 // If there is more than one validation header, we can't treat this request as
1743 // a cache validation, since we don't know for sure which header the server
1744 // will give us a response for (and they could be contradictory).
1745 if (external_validation_error
) {
1746 LOG(WARNING
) << "Multiple or malformed validation headers found.";
1747 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
1750 if (range_found
&& !(effective_load_flags_
& LOAD_DISABLE_CACHE
)) {
1751 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1752 partial_
.reset(new PartialData
);
1753 if (request_
->method
== "GET" && partial_
->Init(request_
->extra_headers
)) {
1754 // We will be modifying the actual range requested to the server, so
1755 // let's remove the header here.
1756 custom_request_
.reset(new HttpRequestInfo(*request_
));
1757 custom_request_
->extra_headers
.RemoveHeader(HttpRequestHeaders::kRange
);
1758 request_
= custom_request_
.get();
1759 partial_
->SetHeaders(custom_request_
->extra_headers
);
1761 // The range is invalid or we cannot handle it properly.
1762 VLOG(1) << "Invalid byte range found.";
1763 effective_load_flags_
|= LOAD_DISABLE_CACHE
;
1764 partial_
.reset(NULL
);
1769 bool HttpCache::Transaction::ShouldPassThrough() {
1770 // We may have a null disk_cache if there is an error we cannot recover from,
1771 // like not enough disk space, or sharing violations.
1772 if (!cache_
->disk_cache_
.get())
1775 // When using the record/playback modes, we always use the cache
1776 // and we never pass through.
1777 if (cache_
->mode() == RECORD
|| cache_
->mode() == PLAYBACK
)
1780 if (effective_load_flags_
& LOAD_DISABLE_CACHE
)
1783 if (request_
->method
== "GET")
1786 if (request_
->method
== "POST" && request_
->upload_data_stream
&&
1787 request_
->upload_data_stream
->identifier()) {
1791 if (request_
->method
== "PUT" && request_
->upload_data_stream
)
1794 if (request_
->method
== "DELETE")
1797 // TODO(darin): add support for caching HEAD responses
1801 int HttpCache::Transaction::BeginCacheRead() {
1802 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
1803 if (response_
.headers
->response_code() == 206 || partial_
.get()) {
1805 return ERR_CACHE_MISS
;
1808 // We don't have the whole resource.
1810 return ERR_CACHE_MISS
;
1812 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
1813 next_state_
= STATE_CACHE_READ_METADATA
;
1818 int HttpCache::Transaction::BeginCacheValidation() {
1819 DCHECK(mode_
== READ_WRITE
);
1821 bool skip_validation
= !RequiresValidation();
1824 // Truncated entries can cause partial gets, so we shouldn't record this
1825 // load in histograms.
1826 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1827 skip_validation
= !partial_
->initial_validation();
1830 if (partial_
.get() && (is_sparse_
|| truncated_
) &&
1831 (!partial_
->IsCurrentRangeCached() || invalid_range_
)) {
1832 // Force revalidation for sparse or truncated entries. Note that we don't
1833 // want to ignore the regular validation logic just because a byte range was
1834 // part of the request.
1835 skip_validation
= false;
1838 if (skip_validation
) {
1839 UpdateTransactionPattern(PATTERN_ENTRY_USED
);
1840 RecordOfflineStatus(effective_load_flags_
, OFFLINE_STATUS_FRESH_CACHE
);
1841 return SetupEntryForRead();
1843 // Make the network request conditional, to see if we may reuse our cached
1844 // response. If we cannot do so, then we just resort to a normal fetch.
1845 // Our mode remains READ_WRITE for a conditional request. Even if the
1846 // conditionalization fails, we don't switch to WRITE mode until we
1847 // know we won't be falling back to using the cache entry in the
1848 // LOAD_FROM_CACHE_IF_OFFLINE case.
1849 if (!ConditionalizeRequest()) {
1850 couldnt_conditionalize_request_
= true;
1851 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE
);
1853 return DoRestartPartialRequest();
1855 DCHECK_NE(206, response_
.headers
->response_code());
1857 next_state_
= STATE_SEND_REQUEST
;
1862 int HttpCache::Transaction::BeginPartialCacheValidation() {
1863 DCHECK(mode_
== READ_WRITE
);
1865 if (response_
.headers
->response_code() != 206 && !partial_
.get() &&
1867 return BeginCacheValidation();
1870 // Partial requests should not be recorded in histograms.
1871 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1872 if (range_requested_
) {
1873 next_state_
= STATE_CACHE_QUERY_DATA
;
1876 // The request is not for a range, but we have stored just ranges.
1877 partial_
.reset(new PartialData());
1878 partial_
->SetHeaders(request_
->extra_headers
);
1879 if (!custom_request_
.get()) {
1880 custom_request_
.reset(new HttpRequestInfo(*request_
));
1881 request_
= custom_request_
.get();
1884 return ValidateEntryHeadersAndContinue();
1887 // This should only be called once per request.
1888 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
1889 DCHECK(mode_
== READ_WRITE
);
1891 if (!partial_
->UpdateFromStoredHeaders(
1892 response_
.headers
.get(), entry_
->disk_entry
, truncated_
)) {
1893 return DoRestartPartialRequest();
1896 if (response_
.headers
->response_code() == 206)
1899 if (!partial_
->IsRequestedRangeOK()) {
1900 // The stored data is fine, but the request may be invalid.
1901 invalid_range_
= true;
1904 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
1908 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
1909 DCHECK_EQ(UPDATE
, mode_
);
1910 DCHECK(external_validation_
.initialized
);
1912 for (size_t i
= 0; i
< arraysize(kValidationHeaders
); i
++) {
1913 if (external_validation_
.values
[i
].empty())
1915 // Retrieve either the cached response's "etag" or "last-modified" header.
1916 std::string validator
;
1917 response_
.headers
->EnumerateHeader(
1919 kValidationHeaders
[i
].related_response_header_name
,
1922 if (response_
.headers
->response_code() != 200 || truncated_
||
1923 validator
.empty() || validator
!= external_validation_
.values
[i
]) {
1924 // The externally conditionalized request is not a validation request
1925 // for our existing cache entry. Proceed with caching disabled.
1926 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
1927 DoneWritingToEntry(true);
1931 next_state_
= STATE_SEND_REQUEST
;
1935 int HttpCache::Transaction::RestartNetworkRequest() {
1936 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1937 DCHECK(network_trans_
.get());
1938 DCHECK_EQ(STATE_NONE
, next_state_
);
1940 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1941 int rv
= network_trans_
->RestartIgnoringLastError(io_callback_
);
1942 if (rv
!= ERR_IO_PENDING
)
1947 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
1948 X509Certificate
* client_cert
) {
1949 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1950 DCHECK(network_trans_
.get());
1951 DCHECK_EQ(STATE_NONE
, next_state_
);
1953 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1954 int rv
= network_trans_
->RestartWithCertificate(client_cert
, io_callback_
);
1955 if (rv
!= ERR_IO_PENDING
)
1960 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
1961 const AuthCredentials
& credentials
) {
1962 DCHECK(mode_
& WRITE
|| mode_
== NONE
);
1963 DCHECK(network_trans_
.get());
1964 DCHECK_EQ(STATE_NONE
, next_state_
);
1966 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1967 int rv
= network_trans_
->RestartWithAuth(credentials
, io_callback_
);
1968 if (rv
!= ERR_IO_PENDING
)
1973 bool HttpCache::Transaction::RequiresValidation() {
1974 // TODO(darin): need to do more work here:
1975 // - make sure we have a matching request method
1976 // - watch out for cached responses that depend on authentication
1978 // In playback mode, nothing requires validation.
1979 if (cache_
->mode() == net::HttpCache::PLAYBACK
)
1982 if (response_
.vary_data
.is_valid() &&
1983 !response_
.vary_data
.MatchesRequest(*request_
,
1984 *response_
.headers
.get())) {
1985 vary_mismatch_
= true;
1989 if (effective_load_flags_
& LOAD_PREFERRING_CACHE
)
1992 if (effective_load_flags_
& LOAD_VALIDATE_CACHE
)
1995 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
1998 if (response_
.headers
->RequiresValidation(
1999 response_
.request_time
, response_
.response_time
, Time::Now())) {
2006 bool HttpCache::Transaction::ConditionalizeRequest() {
2007 DCHECK(response_
.headers
.get());
2009 if (request_
->method
== "PUT" || request_
->method
== "DELETE")
2012 // This only makes sense for cached 200 or 206 responses.
2013 if (response_
.headers
->response_code() != 200 &&
2014 response_
.headers
->response_code() != 206) {
2018 // We should have handled this case before.
2019 DCHECK(response_
.headers
->response_code() != 206 ||
2020 response_
.headers
->HasStrongValidators());
2022 // Just use the first available ETag and/or Last-Modified header value.
2023 // TODO(darin): Or should we use the last?
2025 std::string etag_value
;
2026 if (response_
.headers
->GetHttpVersion() >= HttpVersion(1, 1))
2027 response_
.headers
->EnumerateHeader(NULL
, "etag", &etag_value
);
2029 std::string last_modified_value
;
2030 if (!vary_mismatch_
) {
2031 response_
.headers
->EnumerateHeader(NULL
, "last-modified",
2032 &last_modified_value
);
2035 if (etag_value
.empty() && last_modified_value
.empty())
2038 if (!partial_
.get()) {
2039 // Need to customize the request, so this forces us to allocate :(
2040 custom_request_
.reset(new HttpRequestInfo(*request_
));
2041 request_
= custom_request_
.get();
2043 DCHECK(custom_request_
.get());
2045 bool use_if_range
= partial_
.get() && !partial_
->IsCurrentRangeCached() &&
2048 if (!etag_value
.empty()) {
2050 // We don't want to switch to WRITE mode if we don't have this block of a
2051 // byte-range request because we may have other parts cached.
2052 custom_request_
->extra_headers
.SetHeader(
2053 HttpRequestHeaders::kIfRange
, etag_value
);
2055 custom_request_
->extra_headers
.SetHeader(
2056 HttpRequestHeaders::kIfNoneMatch
, etag_value
);
2058 // For byte-range requests, make sure that we use only one way to validate
2060 if (partial_
.get() && !partial_
->IsCurrentRangeCached())
2064 if (!last_modified_value
.empty()) {
2066 custom_request_
->extra_headers
.SetHeader(
2067 HttpRequestHeaders::kIfRange
, last_modified_value
);
2069 custom_request_
->extra_headers
.SetHeader(
2070 HttpRequestHeaders::kIfModifiedSince
, last_modified_value
);
2077 // We just received some headers from the server. We may have asked for a range,
2078 // in which case partial_ has an object. This could be the first network request
2079 // we make to fulfill the original request, or we may be already reading (from
2080 // the net and / or the cache). If we are not expecting a certain response, we
2081 // just bypass the cache for this request (but again, maybe we are reading), and
2082 // delete partial_ (so we are not able to "fix" the headers that we return to
2083 // the user). This results in either a weird response for the caller (we don't
2084 // expect it after all), or maybe a range that was not exactly what it was asked
2087 // If the server is simply telling us that the resource has changed, we delete
2088 // the cached entry and restart the request as the caller intended (by returning
2089 // false from this method). However, we may not be able to do that at any point,
2090 // for instance if we already returned the headers to the user.
2092 // WARNING: Whenever this code returns false, it has to make sure that the next
2093 // time it is called it will return true so that we don't keep retrying the
2095 bool HttpCache::Transaction::ValidatePartialResponse() {
2096 const HttpResponseHeaders
* headers
= new_response_
->headers
.get();
2097 int response_code
= headers
->response_code();
2098 bool partial_response
= (response_code
== 206);
2099 handling_206_
= false;
2101 if (!entry_
|| request_
->method
!= "GET")
2104 if (invalid_range_
) {
2105 // We gave up trying to match this request with the stored data. If the
2106 // server is ok with the request, delete the entry, otherwise just ignore
2109 if (partial_response
|| response_code
== 200) {
2110 DoomPartialEntry(true);
2113 if (response_code
== 304)
2115 IgnoreRangeRequest();
2120 if (!partial_
.get()) {
2121 // We are not expecting 206 but we may have one.
2122 if (partial_response
)
2123 IgnoreRangeRequest();
2128 // TODO(rvargas): Do we need to consider other results here?.
2129 bool failure
= response_code
== 200 || response_code
== 416;
2131 if (partial_
->IsCurrentRangeCached()) {
2132 // We asked for "If-None-Match: " so a 206 means a new object.
2133 if (partial_response
)
2136 if (response_code
== 304 && partial_
->ResponseHeadersOK(headers
))
2139 // We asked for "If-Range: " so a 206 means just another range.
2140 if (partial_response
&& partial_
->ResponseHeadersOK(headers
)) {
2141 handling_206_
= true;
2145 if (!reading_
&& !is_sparse_
&& !partial_response
) {
2146 // See if we can ignore the fact that we issued a byte range request.
2147 // If the server sends 200, just store it. If it sends an error, redirect
2148 // or something else, we may store the response as long as we didn't have
2149 // anything already stored.
2150 if (response_code
== 200 ||
2151 (!truncated_
&& response_code
!= 304 && response_code
!= 416)) {
2152 // The server is sending something else, and we can save it.
2153 DCHECK((truncated_
&& !partial_
->IsLastRange()) || range_requested_
);
2160 // 304 is not expected here, but we'll spare the entry (unless it was
2167 // We cannot truncate this entry, it has to be deleted.
2168 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2169 DoomPartialEntry(false);
2171 if (!reading_
&& !partial_
->IsLastRange()) {
2172 // We'll attempt to issue another network request, this time without us
2173 // messing up the headers.
2174 partial_
->RestoreHeaders(&custom_request_
->extra_headers
);
2179 LOG(WARNING
) << "Failed to revalidate partial entry";
2184 IgnoreRangeRequest();
2188 void HttpCache::Transaction::IgnoreRangeRequest() {
2189 // We have a problem. We may or may not be reading already (in which case we
2190 // returned the headers), but we'll just pretend that this request is not
2191 // using the cache and see what happens. Most likely this is the first
2192 // response from the server (it's not changing its mind midway, right?).
2193 UpdateTransactionPattern(PATTERN_NOT_COVERED
);
2195 DoneWritingToEntry(mode_
!= WRITE
);
2196 else if (mode_
& READ
&& entry_
)
2197 cache_
->DoneReadingFromEntry(entry_
, this);
2199 partial_
.reset(NULL
);
2204 void HttpCache::Transaction::FailRangeRequest() {
2205 response_
= *new_response_
;
2206 partial_
->FixResponseHeaders(response_
.headers
.get(), false);
2209 int HttpCache::Transaction::SetupEntryForRead() {
2211 ResetNetworkTransaction();
2212 if (partial_
.get()) {
2213 if (truncated_
|| is_sparse_
|| !invalid_range_
) {
2214 // We are going to return the saved response headers to the caller, so
2215 // we may need to adjust them first.
2216 next_state_
= STATE_PARTIAL_HEADERS_RECEIVED
;
2222 cache_
->ConvertWriterToReader(entry_
);
2225 if (entry_
->disk_entry
->GetDataSize(kMetadataIndex
))
2226 next_state_
= STATE_CACHE_READ_METADATA
;
2231 int HttpCache::Transaction::ReadFromNetwork(IOBuffer
* data
, int data_len
) {
2233 io_buf_len_
= data_len
;
2234 next_state_
= STATE_NETWORK_READ
;
2238 int HttpCache::Transaction::ReadFromEntry(IOBuffer
* data
, int data_len
) {
2240 io_buf_len_
= data_len
;
2241 next_state_
= STATE_CACHE_READ_DATA
;
2245 int HttpCache::Transaction::WriteToEntry(int index
, int offset
,
2246 IOBuffer
* data
, int data_len
,
2247 const CompletionCallback
& callback
) {
2252 if (!partial_
.get() || !data_len
) {
2253 rv
= entry_
->disk_entry
->WriteData(index
, offset
, data
, data_len
, callback
,
2256 rv
= partial_
->CacheWrite(entry_
->disk_entry
, data
, data_len
, callback
);
2261 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated
) {
2262 next_state_
= STATE_CACHE_WRITE_RESPONSE_COMPLETE
;
2266 // Do not cache no-store content (unless we are record mode). Do not cache
2267 // content with cert errors either. This is to prevent not reporting net
2268 // errors when loading a resource from the cache. When we load a page over
2269 // HTTPS with a cert error we show an SSL blocking page. If the user clicks
2270 // proceed we reload the resource ignoring the errors. The loaded resource
2271 // is then cached. If that resource is subsequently loaded from the cache,
2272 // no net error is reported (even though the cert status contains the actual
2273 // errors) and no SSL blocking page is shown. An alternative would be to
2274 // reverse-map the cert status to a net error and replay the net error.
2275 if ((cache_
->mode() != RECORD
&&
2276 response_
.headers
->HasHeaderValue("cache-control", "no-store")) ||
2277 net::IsCertStatusError(response_
.ssl_info
.cert_status
)) {
2278 DoneWritingToEntry(false);
2279 if (net_log_
.IsLoggingAllEvents())
2280 net_log_
.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO
);
2284 // When writing headers, we normally only write the non-transient
2285 // headers; when in record mode, record everything.
2286 bool skip_transient_headers
= (cache_
->mode() != RECORD
);
2289 DCHECK_EQ(200, response_
.headers
->response_code());
2291 scoped_refptr
<PickledIOBuffer
> data(new PickledIOBuffer());
2292 response_
.Persist(data
->pickle(), skip_transient_headers
, truncated
);
2295 io_buf_len_
= data
->pickle()->size();
2296 return entry_
->disk_entry
->WriteData(kResponseInfoIndex
, 0, data
.get(),
2297 io_buf_len_
, io_callback_
, true);
2300 int HttpCache::Transaction::AppendResponseDataToEntry(
2301 IOBuffer
* data
, int data_len
, const CompletionCallback
& callback
) {
2302 if (!entry_
|| !data_len
)
2305 int current_size
= entry_
->disk_entry
->GetDataSize(kResponseContentIndex
);
2306 return WriteToEntry(kResponseContentIndex
, current_size
, data
, data_len
,
2310 void HttpCache::Transaction::DoneWritingToEntry(bool success
) {
2316 cache_
->DoneWritingToEntry(entry_
, success
);
2318 mode_
= NONE
; // switch to 'pass through' mode
2321 int HttpCache::Transaction::OnCacheReadError(int result
, bool restart
) {
2322 DLOG(ERROR
) << "ReadData failed: " << result
;
2323 const int result_for_histogram
= std::max(0, -result
);
2325 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2326 result_for_histogram
);
2328 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2329 result_for_histogram
);
2332 // Avoid using this entry in the future.
2334 cache_
->DoomActiveEntry(cache_key_
);
2338 DCHECK(!network_trans_
.get());
2339 cache_
->DoneWithEntry(entry_
, this, false);
2343 next_state_
= STATE_GET_BACKEND
;
2347 return ERR_CACHE_READ_FAILURE
;
2350 void HttpCache::Transaction::DoomPartialEntry(bool delete_object
) {
2351 DVLOG(2) << "DoomPartialEntry";
2352 int rv
= cache_
->DoomEntry(cache_key_
, NULL
);
2354 cache_
->DoneWithEntry(entry_
, this, false);
2358 partial_
.reset(NULL
);
2361 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result
) {
2362 partial_
->OnNetworkReadCompleted(result
);
2365 // We need to move on to the next range.
2366 ResetNetworkTransaction();
2367 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2372 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result
) {
2373 partial_
->OnCacheReadCompleted(result
);
2375 if (result
== 0 && mode_
== READ_WRITE
) {
2376 // We need to move on to the next range.
2377 next_state_
= STATE_START_PARTIAL_CACHE_VALIDATION
;
2378 } else if (result
< 0) {
2379 return OnCacheReadError(result
, false);
2384 int HttpCache::Transaction::DoRestartPartialRequest() {
2385 // The stored data cannot be used. Get rid of it and restart this request.
2386 // We need to also reset the |truncated_| flag as a new entry is created.
2387 DoomPartialEntry(!range_requested_
);
2390 next_state_
= STATE_INIT_ENTRY
;
2394 void HttpCache::Transaction::ResetNetworkTransaction() {
2395 DCHECK(!old_network_trans_load_timing_
);
2396 DCHECK(network_trans_
);
2397 LoadTimingInfo load_timing
;
2398 if (network_trans_
->GetLoadTimingInfo(&load_timing
))
2399 old_network_trans_load_timing_
.reset(new LoadTimingInfo(load_timing
));
2400 total_received_bytes_
+= network_trans_
->GetTotalReceivedBytes();
2401 network_trans_
.reset();
2404 // Histogram data from the end of 2010 show the following distribution of
2405 // response headers:
2407 // Content-Length............... 87%
2408 // Date......................... 98%
2409 // Last-Modified................ 49%
2410 // Etag......................... 19%
2411 // Accept-Ranges: bytes......... 25%
2412 // Accept-Ranges: none.......... 0.4%
2413 // Strong Validator............. 50%
2414 // Strong Validator + ranges.... 24%
2415 // Strong Validator + CL........ 49%
2417 bool HttpCache::Transaction::CanResume(bool has_data
) {
2418 // Double check that there is something worth keeping.
2419 if (has_data
&& !entry_
->disk_entry
->GetDataSize(kResponseContentIndex
))
2422 if (request_
->method
!= "GET")
2425 // Note that if this is a 206, content-length was already fixed after calling
2426 // PartialData::ResponseHeadersOK().
2427 if (response_
.headers
->GetContentLength() <= 0 ||
2428 response_
.headers
->HasHeaderValue("Accept-Ranges", "none") ||
2429 !response_
.headers
->HasStrongValidators()) {
2436 void HttpCache::Transaction::UpdateTransactionPattern(
2437 TransactionPattern new_transaction_pattern
) {
2438 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2440 DCHECK(transaction_pattern_
== PATTERN_UNDEFINED
||
2441 new_transaction_pattern
== PATTERN_NOT_COVERED
);
2442 transaction_pattern_
= new_transaction_pattern
;
2445 void HttpCache::Transaction::RecordHistograms() {
2446 DCHECK_NE(PATTERN_UNDEFINED
, transaction_pattern_
);
2447 if (!cache_
.get() || !cache_
->GetCurrentBackend() ||
2448 cache_
->GetCurrentBackend()->GetCacheType() != DISK_CACHE
||
2449 cache_
->mode() != NORMAL
|| request_
->method
!= "GET") {
2452 UMA_HISTOGRAM_ENUMERATION(
2453 "HttpCache.Pattern", transaction_pattern_
, PATTERN_MAX
);
2454 if (transaction_pattern_
== PATTERN_NOT_COVERED
)
2456 DCHECK(!range_requested_
);
2457 DCHECK(!first_cache_access_since_
.is_null());
2459 TimeDelta total_time
= base::TimeTicks::Now() - first_cache_access_since_
;
2461 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time
);
2463 bool did_send_request
= !send_request_since_
.is_null();
2465 (did_send_request
&&
2466 (transaction_pattern_
== PATTERN_ENTRY_NOT_CACHED
||
2467 transaction_pattern_
== PATTERN_ENTRY_VALIDATED
||
2468 transaction_pattern_
== PATTERN_ENTRY_UPDATED
||
2469 transaction_pattern_
== PATTERN_ENTRY_CANT_CONDITIONALIZE
)) ||
2470 (!did_send_request
&& transaction_pattern_
== PATTERN_ENTRY_USED
));
2472 if (!did_send_request
) {
2473 DCHECK(transaction_pattern_
== PATTERN_ENTRY_USED
);
2474 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time
);
2478 TimeDelta before_send_time
= send_request_since_
- first_cache_access_since_
;
2479 int before_send_percent
=
2480 total_time
.ToInternalValue() == 0 ? 0
2481 : before_send_time
* 100 / total_time
;
2482 DCHECK_LE(0, before_send_percent
);
2483 DCHECK_GE(100, before_send_percent
);
2485 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time
);
2486 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time
);
2487 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent
);
2489 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2490 // below this comment after we have received initial data.
2491 switch (transaction_pattern_
) {
2492 case PATTERN_ENTRY_CANT_CONDITIONALIZE
: {
2493 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2495 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2496 before_send_percent
);
2499 case PATTERN_ENTRY_NOT_CACHED
: {
2500 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time
);
2501 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2502 before_send_percent
);
2505 case PATTERN_ENTRY_VALIDATED
: {
2506 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time
);
2507 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2508 before_send_percent
);
2511 case PATTERN_ENTRY_UPDATED
: {
2512 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time
);
2513 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2514 before_send_percent
);
2522 void HttpCache::Transaction::OnIOComplete(int result
) {