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->GetWeakPtr()),
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 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
450 // future request for the resource will be blocked on this one.
452 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
453 !is_sparse_ && !range_requested_) {
458 bool HttpCache::Transaction::GetFullRequestHeaders(
459 HttpRequestHeaders* headers) const {
461 return network_trans_->GetFullRequestHeaders(headers);
463 // TODO(ttuttle): Read headers from cache.
467 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
468 int64 total_received_bytes = total_received_bytes_;
470 total_received_bytes += network_trans_->GetTotalReceivedBytes();
471 return total_received_bytes;
474 void HttpCache::Transaction::DoneReading() {
475 if (cache_.get() && entry_) {
476 DCHECK_NE(mode_, UPDATE);
478 DoneWritingToEntry(true);
479 } else if (mode_ & READ) {
480 // It is necessary to check mode_ & READ because it is possible
481 // for mode_ to be NONE and entry_ non-NULL with a write entry
482 // if StopCaching was called.
483 cache_->DoneReadingFromEntry(entry_, this);
489 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
490 // Null headers means we encountered an error or haven't a response yet
491 if (auth_response_.headers.get())
492 return &auth_response_;
493 return (response_.headers.get() || response_.ssl_info.cert.get() ||
494 response_.cert_request_info.get())
499 LoadState HttpCache::Transaction::GetLoadState() const {
500 LoadState state = GetWriterLoadState();
501 if (state != LOAD_STATE_WAITING_FOR_CACHE)
505 return cache_->GetLoadStateForPendingTransaction(this);
507 return LOAD_STATE_IDLE;
510 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
511 if (network_trans_.get())
512 return network_trans_->GetUploadProgress();
513 return final_upload_progress_;
516 void HttpCache::Transaction::SetQuicServerInfo(
517 QuicServerInfo* quic_server_info) {}
519 bool HttpCache::Transaction::GetLoadTimingInfo(
520 LoadTimingInfo* load_timing_info) const {
522 return network_trans_->GetLoadTimingInfo(load_timing_info);
524 if (old_network_trans_load_timing_) {
525 *load_timing_info = *old_network_trans_load_timing_;
529 if (first_cache_access_since_.is_null())
532 // If the cache entry was opened, return that time.
533 load_timing_info->send_start = first_cache_access_since_;
534 // This time doesn't make much sense when reading from the cache, so just use
535 // the same time as send_start.
536 load_timing_info->send_end = first_cache_access_since_;
540 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
541 priority_ = priority;
543 network_trans_->SetPriority(priority_);
546 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
547 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
548 websocket_handshake_stream_base_create_helper_ = create_helper;
550 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
553 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
554 const BeforeNetworkStartCallback& callback) {
555 DCHECK(!network_trans_);
556 before_network_start_callback_ = callback;
559 int HttpCache::Transaction::ResumeNetworkStart() {
561 return network_trans_->ResumeNetworkStart();
562 return ERR_UNEXPECTED;
565 //-----------------------------------------------------------------------------
567 void HttpCache::Transaction::DoCallback(int rv) {
568 DCHECK(rv != ERR_IO_PENDING);
569 DCHECK(!callback_.is_null());
571 read_buf_ = NULL; // Release the buffer before invoking the callback.
573 // Since Run may result in Read being called, clear callback_ up front.
574 CompletionCallback c = callback_;
579 int HttpCache::Transaction::HandleResult(int rv) {
580 DCHECK(rv != ERR_IO_PENDING);
581 if (!callback_.is_null())
587 // A few common patterns: (Foo* means Foo -> FooComplete)
591 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
592 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
593 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
594 // PartialHeadersReceived
597 // NetworkRead* -> CacheWriteData*
599 // Cached entry, no validation:
601 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
602 // -> BeginPartialCacheValidation() -> BeginCacheValidation()
607 // Cached entry, validation (304):
609 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
610 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
611 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
612 // CacheWriteResponse* -> UpdateCachedResponseComplete ->
613 // OverwriteCachedResponse -> PartialHeadersReceived
618 // Cached entry, validation and replace (200):
620 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
621 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
622 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
623 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
624 // PartialHeadersReceived
627 // NetworkRead* -> CacheWriteData*
629 // Sparse entry, partially cached, byte range request:
631 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
632 // -> BeginPartialCacheValidation() -> CacheQueryData* ->
633 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
634 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
635 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
636 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
637 // PartialHeadersReceived
640 // NetworkRead* -> CacheWriteData*
643 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
644 // CompletePartialCacheValidation -> CacheReadData* ->
647 // CacheReadData* -> StartPartialCacheValidation ->
648 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
649 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
650 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
652 int HttpCache::Transaction::DoLoop(int result) {
653 DCHECK(next_state_ != STATE_NONE);
657 State state = next_state_;
658 next_state_ = STATE_NONE;
660 case STATE_GET_BACKEND:
664 case STATE_GET_BACKEND_COMPLETE:
665 rv = DoGetBackendComplete(rv);
667 case STATE_SEND_REQUEST:
669 rv = DoSendRequest();
671 case STATE_SEND_REQUEST_COMPLETE:
672 rv = DoSendRequestComplete(rv);
674 case STATE_SUCCESSFUL_SEND_REQUEST:
676 rv = DoSuccessfulSendRequest();
678 case STATE_NETWORK_READ:
680 rv = DoNetworkRead();
682 case STATE_NETWORK_READ_COMPLETE:
683 rv = DoNetworkReadComplete(rv);
685 case STATE_INIT_ENTRY:
689 case STATE_OPEN_ENTRY:
693 case STATE_OPEN_ENTRY_COMPLETE:
694 rv = DoOpenEntryComplete(rv);
696 case STATE_CREATE_ENTRY:
698 rv = DoCreateEntry();
700 case STATE_CREATE_ENTRY_COMPLETE:
701 rv = DoCreateEntryComplete(rv);
703 case STATE_DOOM_ENTRY:
707 case STATE_DOOM_ENTRY_COMPLETE:
708 rv = DoDoomEntryComplete(rv);
710 case STATE_ADD_TO_ENTRY:
714 case STATE_ADD_TO_ENTRY_COMPLETE:
715 rv = DoAddToEntryComplete(rv);
717 case STATE_START_PARTIAL_CACHE_VALIDATION:
719 rv = DoStartPartialCacheValidation();
721 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
722 rv = DoCompletePartialCacheValidation(rv);
724 case STATE_UPDATE_CACHED_RESPONSE:
726 rv = DoUpdateCachedResponse();
728 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
729 rv = DoUpdateCachedResponseComplete(rv);
731 case STATE_OVERWRITE_CACHED_RESPONSE:
733 rv = DoOverwriteCachedResponse();
735 case STATE_TRUNCATE_CACHED_DATA:
737 rv = DoTruncateCachedData();
739 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
740 rv = DoTruncateCachedDataComplete(rv);
742 case STATE_TRUNCATE_CACHED_METADATA:
744 rv = DoTruncateCachedMetadata();
746 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
747 rv = DoTruncateCachedMetadataComplete(rv);
749 case STATE_PARTIAL_HEADERS_RECEIVED:
751 rv = DoPartialHeadersReceived();
753 case STATE_CACHE_READ_RESPONSE:
755 rv = DoCacheReadResponse();
757 case STATE_CACHE_READ_RESPONSE_COMPLETE:
758 rv = DoCacheReadResponseComplete(rv);
760 case STATE_CACHE_WRITE_RESPONSE:
762 rv = DoCacheWriteResponse();
764 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
766 rv = DoCacheWriteTruncatedResponse();
768 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
769 rv = DoCacheWriteResponseComplete(rv);
771 case STATE_CACHE_READ_METADATA:
773 rv = DoCacheReadMetadata();
775 case STATE_CACHE_READ_METADATA_COMPLETE:
776 rv = DoCacheReadMetadataComplete(rv);
778 case STATE_CACHE_QUERY_DATA:
780 rv = DoCacheQueryData();
782 case STATE_CACHE_QUERY_DATA_COMPLETE:
783 rv = DoCacheQueryDataComplete(rv);
785 case STATE_CACHE_READ_DATA:
787 rv = DoCacheReadData();
789 case STATE_CACHE_READ_DATA_COMPLETE:
790 rv = DoCacheReadDataComplete(rv);
792 case STATE_CACHE_WRITE_DATA:
793 rv = DoCacheWriteData(rv);
795 case STATE_CACHE_WRITE_DATA_COMPLETE:
796 rv = DoCacheWriteDataComplete(rv);
799 NOTREACHED() << "bad state";
803 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
805 if (rv != ERR_IO_PENDING)
811 int HttpCache::Transaction::DoGetBackend() {
812 cache_pending_ = true;
813 next_state_ = STATE_GET_BACKEND_COMPLETE;
814 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
815 return cache_->GetBackendForTransaction(this);
818 int HttpCache::Transaction::DoGetBackendComplete(int result) {
819 DCHECK(result == OK || result == ERR_FAILED);
820 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
822 cache_pending_ = false;
824 if (!ShouldPassThrough()) {
825 cache_key_ = cache_->GenerateCacheKey(request_);
827 // Requested cache access mode.
828 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
830 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
836 // Downgrade to UPDATE if the request has been externally conditionalized.
837 if (external_validation_.initialized) {
839 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
840 // in case READ was off).
848 // Use PUT and DELETE only to invalidate existing stored entries.
849 if ((request_->method == "PUT" || request_->method == "DELETE") &&
850 mode_ != READ_WRITE && mode_ != WRITE) {
854 // If must use cache, then we must fail. This can happen for back/forward
855 // navigations to a page generated via a form post.
856 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
857 return ERR_CACHE_MISS;
860 if (partial_.get()) {
861 partial_->RestoreHeaders(&custom_request_->extra_headers);
864 next_state_ = STATE_SEND_REQUEST;
866 next_state_ = STATE_INIT_ENTRY;
869 // This is only set if we have something to do with the response.
870 range_requested_ = (partial_.get() != NULL);
875 int HttpCache::Transaction::DoSendRequest() {
876 DCHECK(mode_ & WRITE || mode_ == NONE);
877 DCHECK(!network_trans_.get());
879 send_request_since_ = TimeTicks::Now();
881 // Create a network transaction.
882 int rv = cache_->network_layer_->CreateTransaction(priority_,
886 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
888 // Old load timing information, if any, is now obsolete.
889 old_network_trans_load_timing_.reset();
891 if (websocket_handshake_stream_base_create_helper_)
892 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
893 websocket_handshake_stream_base_create_helper_);
895 next_state_ = STATE_SEND_REQUEST_COMPLETE;
896 rv = network_trans_->Start(request_, io_callback_, net_log_);
900 int HttpCache::Transaction::DoSendRequestComplete(int result) {
902 return ERR_UNEXPECTED;
904 // If requested, and we have a readable cache entry, and we have
905 // an error indicating that we're offline as opposed to in contact
906 // with a bad server, read from cache anyway.
907 if (IsOfflineError(result)) {
908 if (mode_ == READ_WRITE && entry_ && !partial_) {
909 RecordOfflineStatus(effective_load_flags_,
910 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE);
911 if (effective_load_flags_ & LOAD_FROM_CACHE_IF_OFFLINE) {
912 UpdateTransactionPattern(PATTERN_NOT_COVERED);
913 response_.server_data_unavailable = true;
914 return SetupEntryForRead();
917 RecordOfflineStatus(effective_load_flags_,
918 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE);
921 RecordOfflineStatus(effective_load_flags_,
922 (result == OK ? OFFLINE_STATUS_NETWORK_SUCCEEDED :
923 OFFLINE_STATUS_NETWORK_FAILED));
926 // If we tried to conditionalize the request and failed, we know
927 // we won't be reading from the cache after this point.
928 if (couldnt_conditionalize_request_)
932 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
936 // Do not record requests that have network errors or restarts.
937 UpdateTransactionPattern(PATTERN_NOT_COVERED);
938 if (IsCertificateError(result)) {
939 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
940 // If we get a certificate error, then there is a certificate in ssl_info,
941 // so GetResponseInfo() should never return NULL here.
943 response_.ssl_info = response->ssl_info;
944 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
945 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
947 response_.cert_request_info = response->cert_request_info;
948 } else if (response_.was_cached) {
949 DoneWritingToEntry(true);
954 // We received the response headers and there is no error.
955 int HttpCache::Transaction::DoSuccessfulSendRequest() {
956 DCHECK(!new_response_);
957 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
958 bool authentication_failure = false;
960 if (new_response->headers->response_code() == 401 ||
961 new_response->headers->response_code() == 407) {
962 auth_response_ = *new_response;
966 // We initiated a second request the caller doesn't know about. We should be
967 // able to authenticate this request because we should have authenticated
968 // this URL moments ago.
969 if (IsReadyToRestartForAuth()) {
970 DCHECK(!response_.auth_challenge.get());
971 next_state_ = STATE_SEND_REQUEST_COMPLETE;
972 // In theory we should check to see if there are new cookies, but there
973 // is no way to do that from here.
974 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
977 // We have to perform cleanup at this point so that at least the next
978 // request can succeed.
979 authentication_failure = true;
981 DoomPartialEntry(false);
986 new_response_ = new_response;
987 if (authentication_failure ||
988 (!ValidatePartialResponse() && !auth_response_.headers.get())) {
989 // Something went wrong with this request and we have to restart it.
990 // If we have an authentication response, we are exposed to weird things
991 // hapenning if the user cancels the authentication before we receive
993 UpdateTransactionPattern(PATTERN_NOT_COVERED);
994 response_ = HttpResponseInfo();
995 ResetNetworkTransaction();
996 new_response_ = NULL;
997 next_state_ = STATE_SEND_REQUEST;
1001 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1002 // We have stored the full entry, but it changed and the server is
1003 // sending a range. We have to delete the old entry.
1004 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1005 DoneWritingToEntry(false);
1008 if (mode_ == WRITE &&
1009 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1010 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1013 if (mode_ == WRITE &&
1014 (request_->method == "PUT" || request_->method == "DELETE")) {
1015 if (NonErrorResponse(new_response->headers->response_code())) {
1016 int ret = cache_->DoomEntry(cache_key_, NULL);
1019 cache_->DoneWritingToEntry(entry_, true);
1024 if (request_->method == "POST" &&
1025 NonErrorResponse(new_response->headers->response_code())) {
1026 cache_->DoomMainEntryForUrl(request_->url);
1029 RecordVaryHeaderHistogram(new_response);
1031 if (new_response_->headers->response_code() == 416 &&
1032 (request_->method == "GET" || request_->method == "POST")) {
1033 // If there is an ective entry it may be destroyed with this transaction.
1034 response_ = *new_response_;
1038 // Are we expecting a response to a conditional query?
1039 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1040 if (new_response->headers->response_code() == 304 || handling_206_) {
1041 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1042 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1045 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1049 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1053 int HttpCache::Transaction::DoNetworkRead() {
1054 next_state_ = STATE_NETWORK_READ_COMPLETE;
1055 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1058 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1059 DCHECK(mode_ & WRITE || mode_ == NONE);
1062 return ERR_UNEXPECTED;
1064 // If there is an error or we aren't saving the data, we are done; just wait
1065 // until the destructor runs to see if we can keep the data.
1066 if (mode_ == NONE || result < 0)
1069 next_state_ = STATE_CACHE_WRITE_DATA;
1073 int HttpCache::Transaction::DoInitEntry() {
1074 DCHECK(!new_entry_);
1077 return ERR_UNEXPECTED;
1079 if (mode_ == WRITE) {
1080 next_state_ = STATE_DOOM_ENTRY;
1084 next_state_ = STATE_OPEN_ENTRY;
1088 int HttpCache::Transaction::DoOpenEntry() {
1089 DCHECK(!new_entry_);
1090 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1091 cache_pending_ = true;
1092 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1093 first_cache_access_since_ = TimeTicks::Now();
1094 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1097 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1098 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1099 // OK, otherwise the cache will end up with an active entry without any
1100 // transaction attached.
1101 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1102 cache_pending_ = false;
1104 next_state_ = STATE_ADD_TO_ENTRY;
1108 if (result == ERR_CACHE_RACE) {
1109 next_state_ = STATE_INIT_ENTRY;
1113 if (request_->method == "PUT" || request_->method == "DELETE") {
1114 DCHECK(mode_ == READ_WRITE || mode_ == WRITE);
1116 next_state_ = STATE_SEND_REQUEST;
1120 if (mode_ == READ_WRITE) {
1122 next_state_ = STATE_CREATE_ENTRY;
1125 if (mode_ == UPDATE) {
1126 // There is no cache entry to update; proceed without caching.
1128 next_state_ = STATE_SEND_REQUEST;
1131 if (cache_->mode() == PLAYBACK)
1132 DVLOG(1) << "Playback Cache Miss: " << request_->url;
1134 // The entry does not exist, and we are not permitted to create a new entry,
1136 return ERR_CACHE_MISS;
1139 int HttpCache::Transaction::DoCreateEntry() {
1140 DCHECK(!new_entry_);
1141 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1142 cache_pending_ = true;
1143 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1144 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1147 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1148 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1149 // OK, otherwise the cache will end up with an active entry without any
1150 // transaction attached.
1151 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1153 cache_pending_ = false;
1154 next_state_ = STATE_ADD_TO_ENTRY;
1156 if (result == ERR_CACHE_RACE) {
1157 next_state_ = STATE_INIT_ENTRY;
1162 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", false);
1164 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", true);
1165 // We have a race here: Maybe we failed to open the entry and decided to
1166 // create one, but by the time we called create, another transaction already
1167 // created the entry. If we want to eliminate this issue, we need an atomic
1168 // OpenOrCreate() method exposed by the disk cache.
1169 DLOG(WARNING) << "Unable to create cache entry";
1172 partial_->RestoreHeaders(&custom_request_->extra_headers);
1173 next_state_ = STATE_SEND_REQUEST;
1178 int HttpCache::Transaction::DoDoomEntry() {
1179 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1180 cache_pending_ = true;
1181 if (first_cache_access_since_.is_null())
1182 first_cache_access_since_ = TimeTicks::Now();
1183 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1184 return cache_->DoomEntry(cache_key_, this);
1187 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1188 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1189 next_state_ = STATE_CREATE_ENTRY;
1190 cache_pending_ = false;
1191 if (result == ERR_CACHE_RACE)
1192 next_state_ = STATE_INIT_ENTRY;
1196 int HttpCache::Transaction::DoAddToEntry() {
1198 cache_pending_ = true;
1199 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1200 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1201 DCHECK(entry_lock_waiting_since_.is_null());
1202 entry_lock_waiting_since_ = TimeTicks::Now();
1203 return cache_->AddTransactionToEntry(new_entry_, this);
1206 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1207 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1209 const TimeDelta entry_lock_wait =
1210 TimeTicks::Now() - entry_lock_waiting_since_;
1211 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1213 entry_lock_waiting_since_ = TimeTicks();
1215 cache_pending_ = false;
1218 entry_ = new_entry_;
1220 // If there is a failure, the cache should have taken care of new_entry_.
1223 if (result == ERR_CACHE_RACE) {
1224 next_state_ = STATE_INIT_ENTRY;
1233 if (mode_ == WRITE) {
1235 partial_->RestoreHeaders(&custom_request_->extra_headers);
1236 next_state_ = STATE_SEND_REQUEST;
1238 // We have to read the headers from the cached entry.
1239 DCHECK(mode_ & READ_META);
1240 next_state_ = STATE_CACHE_READ_RESPONSE;
1245 // We may end up here multiple times for a given request.
1246 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1250 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1251 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1254 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1256 // This is the end of the request.
1257 if (mode_ & WRITE) {
1258 DoneWritingToEntry(true);
1260 cache_->DoneReadingFromEntry(entry_, this);
1269 partial_->PrepareCacheValidation(entry_->disk_entry,
1270 &custom_request_->extra_headers);
1272 if (reading_ && partial_->IsCurrentRangeCached()) {
1273 next_state_ = STATE_CACHE_READ_DATA;
1277 return BeginCacheValidation();
1280 // We received 304 or 206 and we want to update the cached response headers.
1281 int HttpCache::Transaction::DoUpdateCachedResponse() {
1282 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1284 // Update cached response based on headers in new_response.
1285 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1286 response_.headers->Update(*new_response_->headers.get());
1287 response_.response_time = new_response_->response_time;
1288 response_.request_time = new_response_->request_time;
1289 response_.network_accessed = new_response_->network_accessed;
1291 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1292 if (!entry_->doomed) {
1293 int ret = cache_->DoomEntry(cache_key_, NULL);
1297 // If we are already reading, we already updated the headers for this
1298 // request; doing it again will change Content-Length.
1300 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1301 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1308 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1309 if (mode_ == UPDATE) {
1310 DCHECK(!handling_206_);
1311 // We got a "not modified" response and already updated the corresponding
1312 // cache entry above.
1314 // By closing the cached entry now, we make sure that the 304 rather than
1315 // the cached 200 response, is what will be returned to the user.
1316 DoneWritingToEntry(true);
1317 } else if (entry_ && !handling_206_) {
1318 DCHECK_EQ(READ_WRITE, mode_);
1319 if (!partial_.get() || partial_->IsLastRange()) {
1320 cache_->ConvertWriterToReader(entry_);
1323 // We no longer need the network transaction, so destroy it.
1324 final_upload_progress_ = network_trans_->GetUploadProgress();
1325 ResetNetworkTransaction();
1326 } else if (entry_ && handling_206_ && truncated_ &&
1327 partial_->initial_validation()) {
1328 // We just finished the validation of a truncated entry, and the server
1329 // is willing to resume the operation. Now we go back and start serving
1330 // the first part to the user.
1331 ResetNetworkTransaction();
1332 new_response_ = NULL;
1333 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1334 partial_->SetRangeToStartDownload();
1337 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1341 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1343 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1347 // We change the value of Content-Length for partial content.
1348 if (handling_206_ && partial_.get())
1349 partial_->FixContentLength(new_response_->headers.get());
1351 response_ = *new_response_;
1353 if (handling_206_ && !CanResume(false)) {
1354 // There is no point in storing this resource because it will never be used.
1355 DoneWritingToEntry(false);
1357 partial_->FixResponseHeaders(response_.headers.get(), true);
1358 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1362 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1363 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1364 STATE_CACHE_WRITE_RESPONSE;
1368 int HttpCache::Transaction::DoTruncateCachedData() {
1369 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1372 if (net_log_.IsLogging())
1373 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1374 // Truncate the stream.
1375 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1378 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1380 if (net_log_.IsLogging()) {
1381 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1386 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1390 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1391 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1395 if (net_log_.IsLogging())
1396 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1397 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1400 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1402 if (net_log_.IsLogging()) {
1403 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1408 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1412 int HttpCache::Transaction::DoPartialHeadersReceived() {
1413 new_response_ = NULL;
1414 if (entry_ && !partial_.get() &&
1415 entry_->disk_entry->GetDataSize(kMetadataIndex))
1416 next_state_ = STATE_CACHE_READ_METADATA;
1418 if (!partial_.get())
1422 if (network_trans_.get()) {
1423 next_state_ = STATE_NETWORK_READ;
1425 next_state_ = STATE_CACHE_READ_DATA;
1427 } else if (mode_ != NONE) {
1428 // We are about to return the headers for a byte-range request to the user,
1429 // so let's fix them.
1430 partial_->FixResponseHeaders(response_.headers.get(), true);
1435 int HttpCache::Transaction::DoCacheReadResponse() {
1437 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1439 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1440 read_buf_ = new IOBuffer(io_buf_len_);
1442 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1443 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1444 io_buf_len_, io_callback_);
1447 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1448 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1449 if (result != io_buf_len_ ||
1450 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1451 &response_, &truncated_)) {
1452 return OnCacheReadError(result, true);
1455 // Some resources may have slipped in as truncated when they're not.
1456 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1457 if (response_.headers->GetContentLength() == current_size)
1460 // We now have access to the cache entry.
1462 // o if we are a reader for the transaction, then we can start reading the
1465 // o if we can read or write, then we should check if the cache entry needs
1466 // to be validated and then issue a network request if needed or just read
1467 // from the cache if the cache entry is already valid.
1469 // o if we are set to UPDATE, then we are handling an externally
1470 // conditionalized request (if-modified-since / if-none-match). We check
1471 // if the request headers define a validation request.
1475 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1476 result = BeginCacheRead();
1479 result = BeginPartialCacheValidation();
1482 result = BeginExternallyConditionalizedRequest();
1487 result = ERR_FAILED;
1492 int HttpCache::Transaction::DoCacheWriteResponse() {
1494 if (net_log_.IsLogging())
1495 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1497 return WriteResponseInfoToEntry(false);
1500 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1502 if (net_log_.IsLogging())
1503 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1505 return WriteResponseInfoToEntry(true);
1508 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1509 next_state_ = target_state_;
1510 target_state_ = STATE_NONE;
1513 if (net_log_.IsLogging()) {
1514 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1518 // Balance the AddRef from WriteResponseInfoToEntry.
1519 if (result != io_buf_len_) {
1520 DLOG(ERROR) << "failed to write response info to cache";
1521 DoneWritingToEntry(false);
1526 int HttpCache::Transaction::DoCacheReadMetadata() {
1528 DCHECK(!response_.metadata.get());
1529 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1531 response_.metadata =
1532 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1534 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1535 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1536 response_.metadata.get(),
1537 response_.metadata->size(),
1541 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1542 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1543 if (result != response_.metadata->size())
1544 return OnCacheReadError(result, false);
1548 int HttpCache::Transaction::DoCacheQueryData() {
1549 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1550 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1553 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1554 if (result == ERR_NOT_IMPLEMENTED) {
1555 // Restart the request overwriting the cache entry.
1556 // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1557 // supports Sparse IO.
1558 return DoRestartPartialRequest();
1560 DCHECK_EQ(OK, result);
1562 return ERR_UNEXPECTED;
1564 return ValidateEntryHeadersAndContinue();
1567 int HttpCache::Transaction::DoCacheReadData() {
1569 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1571 if (net_log_.IsLogging())
1572 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1573 if (partial_.get()) {
1574 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1578 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1579 read_buf_.get(), io_buf_len_,
1583 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1584 if (net_log_.IsLogging()) {
1585 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1590 return ERR_UNEXPECTED;
1592 if (partial_.get()) {
1593 // Partial requests are confusing to report in histograms because they may
1594 // have multiple underlying requests.
1595 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1596 return DoPartialCacheReadCompleted(result);
1600 read_offset_ += result;
1601 } else if (result == 0) { // End of file.
1603 cache_->DoneReadingFromEntry(entry_, this);
1606 return OnCacheReadError(result, false);
1611 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1612 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1613 write_len_ = num_bytes;
1615 if (net_log_.IsLogging())
1616 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1619 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1622 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1624 if (net_log_.IsLogging()) {
1625 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1629 // Balance the AddRef from DoCacheWriteData.
1631 return ERR_UNEXPECTED;
1633 if (result != write_len_) {
1634 DLOG(ERROR) << "failed to write response data to cache";
1635 DoneWritingToEntry(false);
1637 // We want to ignore errors writing to disk and just keep reading from
1639 result = write_len_;
1640 } else if (!done_reading_ && entry_) {
1641 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1642 int64 body_size = response_.headers->GetContentLength();
1643 if (body_size >= 0 && body_size <= current_size)
1644 done_reading_ = true;
1647 if (partial_.get()) {
1648 // This may be the last request.
1649 if (!(result == 0 && !truncated_ &&
1650 (partial_->IsLastRange() || mode_ == WRITE)))
1651 return DoPartialNetworkReadCompleted(result);
1655 // End of file. This may be the result of a connection problem so see if we
1656 // have to keep the entry around to be flagged as truncated later on.
1657 if (done_reading_ || !entry_ || partial_.get() ||
1658 response_.headers->GetContentLength() <= 0)
1659 DoneWritingToEntry(true);
1665 //-----------------------------------------------------------------------------
1667 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1668 const HttpRequestInfo* request) {
1671 effective_load_flags_ = request_->load_flags;
1673 switch (cache_->mode()) {
1677 // When in record mode, we want to NEVER load from the cache.
1678 // The reason for this is beacuse we save the Set-Cookie headers
1679 // (intentionally). If we read from the cache, we replay them
1681 effective_load_flags_ |= LOAD_BYPASS_CACHE;
1684 // When in playback mode, we want to load exclusively from the cache.
1685 effective_load_flags_ |= LOAD_ONLY_FROM_CACHE;
1688 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1692 // Some headers imply load flags. The order here is significant.
1694 // LOAD_DISABLE_CACHE : no cache read or write
1695 // LOAD_BYPASS_CACHE : no cache read
1696 // LOAD_VALIDATE_CACHE : no cache read unless validation
1698 // The former modes trump latter modes, so if we find a matching header we
1699 // can stop iterating kSpecialHeaders.
1701 static const struct {
1702 const HeaderNameAndValue* search;
1704 } kSpecialHeaders[] = {
1705 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
1706 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
1707 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
1710 bool range_found = false;
1711 bool external_validation_error = false;
1713 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
1716 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSpecialHeaders); ++i) {
1717 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
1718 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
1723 // Check for conditionalization headers which may correspond with a
1724 // cache validation request.
1725 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
1726 const ValidationHeaderInfo& info = kValidationHeaders[i];
1727 std::string validation_value;
1728 if (request_->extra_headers.GetHeader(
1729 info.request_header_name, &validation_value)) {
1730 if (!external_validation_.values[i].empty() ||
1731 validation_value.empty()) {
1732 external_validation_error = true;
1734 external_validation_.values[i] = validation_value;
1735 external_validation_.initialized = true;
1739 // We don't support ranges and validation headers.
1740 if (range_found && external_validation_.initialized) {
1741 LOG(WARNING) << "Byte ranges AND validation headers found.";
1742 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1745 // If there is more than one validation header, we can't treat this request as
1746 // a cache validation, since we don't know for sure which header the server
1747 // will give us a response for (and they could be contradictory).
1748 if (external_validation_error) {
1749 LOG(WARNING) << "Multiple or malformed validation headers found.";
1750 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1753 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
1754 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1755 partial_.reset(new PartialData);
1756 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
1757 // We will be modifying the actual range requested to the server, so
1758 // let's remove the header here.
1759 custom_request_.reset(new HttpRequestInfo(*request_));
1760 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
1761 request_ = custom_request_.get();
1762 partial_->SetHeaders(custom_request_->extra_headers);
1764 // The range is invalid or we cannot handle it properly.
1765 VLOG(1) << "Invalid byte range found.";
1766 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1767 partial_.reset(NULL);
1772 bool HttpCache::Transaction::ShouldPassThrough() {
1773 // We may have a null disk_cache if there is an error we cannot recover from,
1774 // like not enough disk space, or sharing violations.
1775 if (!cache_->disk_cache_.get())
1778 // When using the record/playback modes, we always use the cache
1779 // and we never pass through.
1780 if (cache_->mode() == RECORD || cache_->mode() == PLAYBACK)
1783 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
1786 if (request_->method == "GET")
1789 if (request_->method == "POST" && request_->upload_data_stream &&
1790 request_->upload_data_stream->identifier()) {
1794 if (request_->method == "PUT" && request_->upload_data_stream)
1797 if (request_->method == "DELETE")
1800 // TODO(darin): add support for caching HEAD responses
1804 int HttpCache::Transaction::BeginCacheRead() {
1805 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
1806 if (response_.headers->response_code() == 206 || partial_.get()) {
1808 return ERR_CACHE_MISS;
1811 // We don't have the whole resource.
1813 return ERR_CACHE_MISS;
1815 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
1816 next_state_ = STATE_CACHE_READ_METADATA;
1821 int HttpCache::Transaction::BeginCacheValidation() {
1822 DCHECK(mode_ == READ_WRITE);
1824 bool skip_validation = !RequiresValidation();
1827 // Truncated entries can cause partial gets, so we shouldn't record this
1828 // load in histograms.
1829 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1830 skip_validation = !partial_->initial_validation();
1833 if (partial_.get() && (is_sparse_ || truncated_) &&
1834 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
1835 // Force revalidation for sparse or truncated entries. Note that we don't
1836 // want to ignore the regular validation logic just because a byte range was
1837 // part of the request.
1838 skip_validation = false;
1841 if (skip_validation) {
1842 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1843 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE);
1844 return SetupEntryForRead();
1846 // Make the network request conditional, to see if we may reuse our cached
1847 // response. If we cannot do so, then we just resort to a normal fetch.
1848 // Our mode remains READ_WRITE for a conditional request. Even if the
1849 // conditionalization fails, we don't switch to WRITE mode until we
1850 // know we won't be falling back to using the cache entry in the
1851 // LOAD_FROM_CACHE_IF_OFFLINE case.
1852 if (!ConditionalizeRequest()) {
1853 couldnt_conditionalize_request_ = true;
1854 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
1856 return DoRestartPartialRequest();
1858 DCHECK_NE(206, response_.headers->response_code());
1860 next_state_ = STATE_SEND_REQUEST;
1865 int HttpCache::Transaction::BeginPartialCacheValidation() {
1866 DCHECK(mode_ == READ_WRITE);
1868 if (response_.headers->response_code() != 206 && !partial_.get() &&
1870 return BeginCacheValidation();
1873 // Partial requests should not be recorded in histograms.
1874 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1875 if (range_requested_) {
1876 next_state_ = STATE_CACHE_QUERY_DATA;
1879 // The request is not for a range, but we have stored just ranges.
1880 partial_.reset(new PartialData());
1881 partial_->SetHeaders(request_->extra_headers);
1882 if (!custom_request_.get()) {
1883 custom_request_.reset(new HttpRequestInfo(*request_));
1884 request_ = custom_request_.get();
1887 return ValidateEntryHeadersAndContinue();
1890 // This should only be called once per request.
1891 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
1892 DCHECK(mode_ == READ_WRITE);
1894 if (!partial_->UpdateFromStoredHeaders(
1895 response_.headers.get(), entry_->disk_entry, truncated_)) {
1896 return DoRestartPartialRequest();
1899 if (response_.headers->response_code() == 206)
1902 if (!partial_->IsRequestedRangeOK()) {
1903 // The stored data is fine, but the request may be invalid.
1904 invalid_range_ = true;
1907 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1911 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
1912 DCHECK_EQ(UPDATE, mode_);
1913 DCHECK(external_validation_.initialized);
1915 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
1916 if (external_validation_.values[i].empty())
1918 // Retrieve either the cached response's "etag" or "last-modified" header.
1919 std::string validator;
1920 response_.headers->EnumerateHeader(
1922 kValidationHeaders[i].related_response_header_name,
1925 if (response_.headers->response_code() != 200 || truncated_ ||
1926 validator.empty() || validator != external_validation_.values[i]) {
1927 // The externally conditionalized request is not a validation request
1928 // for our existing cache entry. Proceed with caching disabled.
1929 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1930 DoneWritingToEntry(true);
1934 next_state_ = STATE_SEND_REQUEST;
1938 int HttpCache::Transaction::RestartNetworkRequest() {
1939 DCHECK(mode_ & WRITE || mode_ == NONE);
1940 DCHECK(network_trans_.get());
1941 DCHECK_EQ(STATE_NONE, next_state_);
1943 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1944 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
1945 if (rv != ERR_IO_PENDING)
1950 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
1951 X509Certificate* client_cert) {
1952 DCHECK(mode_ & WRITE || mode_ == NONE);
1953 DCHECK(network_trans_.get());
1954 DCHECK_EQ(STATE_NONE, next_state_);
1956 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1957 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
1958 if (rv != ERR_IO_PENDING)
1963 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
1964 const AuthCredentials& credentials) {
1965 DCHECK(mode_ & WRITE || mode_ == NONE);
1966 DCHECK(network_trans_.get());
1967 DCHECK_EQ(STATE_NONE, next_state_);
1969 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1970 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
1971 if (rv != ERR_IO_PENDING)
1976 bool HttpCache::Transaction::RequiresValidation() {
1977 // TODO(darin): need to do more work here:
1978 // - make sure we have a matching request method
1979 // - watch out for cached responses that depend on authentication
1981 // In playback mode, nothing requires validation.
1982 if (cache_->mode() == net::HttpCache::PLAYBACK)
1985 if (response_.vary_data.is_valid() &&
1986 !response_.vary_data.MatchesRequest(*request_,
1987 *response_.headers.get())) {
1988 vary_mismatch_ = true;
1992 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
1995 if (effective_load_flags_ & LOAD_VALIDATE_CACHE)
1998 if (request_->method == "PUT" || request_->method == "DELETE")
2001 if (response_.headers->RequiresValidation(
2002 response_.request_time, response_.response_time, Time::Now())) {
2009 bool HttpCache::Transaction::ConditionalizeRequest() {
2010 DCHECK(response_.headers.get());
2012 if (request_->method == "PUT" || request_->method == "DELETE")
2015 // This only makes sense for cached 200 or 206 responses.
2016 if (response_.headers->response_code() != 200 &&
2017 response_.headers->response_code() != 206) {
2021 // We should have handled this case before.
2022 DCHECK(response_.headers->response_code() != 206 ||
2023 response_.headers->HasStrongValidators());
2025 // Just use the first available ETag and/or Last-Modified header value.
2026 // TODO(darin): Or should we use the last?
2028 std::string etag_value;
2029 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2030 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2032 std::string last_modified_value;
2033 if (!vary_mismatch_) {
2034 response_.headers->EnumerateHeader(NULL, "last-modified",
2035 &last_modified_value);
2038 if (etag_value.empty() && last_modified_value.empty())
2041 if (!partial_.get()) {
2042 // Need to customize the request, so this forces us to allocate :(
2043 custom_request_.reset(new HttpRequestInfo(*request_));
2044 request_ = custom_request_.get();
2046 DCHECK(custom_request_.get());
2048 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2051 if (!etag_value.empty()) {
2053 // We don't want to switch to WRITE mode if we don't have this block of a
2054 // byte-range request because we may have other parts cached.
2055 custom_request_->extra_headers.SetHeader(
2056 HttpRequestHeaders::kIfRange, etag_value);
2058 custom_request_->extra_headers.SetHeader(
2059 HttpRequestHeaders::kIfNoneMatch, etag_value);
2061 // For byte-range requests, make sure that we use only one way to validate
2063 if (partial_.get() && !partial_->IsCurrentRangeCached())
2067 if (!last_modified_value.empty()) {
2069 custom_request_->extra_headers.SetHeader(
2070 HttpRequestHeaders::kIfRange, last_modified_value);
2072 custom_request_->extra_headers.SetHeader(
2073 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2080 // We just received some headers from the server. We may have asked for a range,
2081 // in which case partial_ has an object. This could be the first network request
2082 // we make to fulfill the original request, or we may be already reading (from
2083 // the net and / or the cache). If we are not expecting a certain response, we
2084 // just bypass the cache for this request (but again, maybe we are reading), and
2085 // delete partial_ (so we are not able to "fix" the headers that we return to
2086 // the user). This results in either a weird response for the caller (we don't
2087 // expect it after all), or maybe a range that was not exactly what it was asked
2090 // If the server is simply telling us that the resource has changed, we delete
2091 // the cached entry and restart the request as the caller intended (by returning
2092 // false from this method). However, we may not be able to do that at any point,
2093 // for instance if we already returned the headers to the user.
2095 // WARNING: Whenever this code returns false, it has to make sure that the next
2096 // time it is called it will return true so that we don't keep retrying the
2098 bool HttpCache::Transaction::ValidatePartialResponse() {
2099 const HttpResponseHeaders* headers = new_response_->headers.get();
2100 int response_code = headers->response_code();
2101 bool partial_response = (response_code == 206);
2102 handling_206_ = false;
2104 if (!entry_ || request_->method != "GET")
2107 if (invalid_range_) {
2108 // We gave up trying to match this request with the stored data. If the
2109 // server is ok with the request, delete the entry, otherwise just ignore
2112 if (partial_response || response_code == 200) {
2113 DoomPartialEntry(true);
2116 if (response_code == 304)
2118 IgnoreRangeRequest();
2123 if (!partial_.get()) {
2124 // We are not expecting 206 but we may have one.
2125 if (partial_response)
2126 IgnoreRangeRequest();
2131 // TODO(rvargas): Do we need to consider other results here?.
2132 bool failure = response_code == 200 || response_code == 416;
2134 if (partial_->IsCurrentRangeCached()) {
2135 // We asked for "If-None-Match: " so a 206 means a new object.
2136 if (partial_response)
2139 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2142 // We asked for "If-Range: " so a 206 means just another range.
2143 if (partial_response && partial_->ResponseHeadersOK(headers)) {
2144 handling_206_ = true;
2148 if (!reading_ && !is_sparse_ && !partial_response) {
2149 // See if we can ignore the fact that we issued a byte range request.
2150 // If the server sends 200, just store it. If it sends an error, redirect
2151 // or something else, we may store the response as long as we didn't have
2152 // anything already stored.
2153 if (response_code == 200 ||
2154 (!truncated_ && response_code != 304 && response_code != 416)) {
2155 // The server is sending something else, and we can save it.
2156 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2163 // 304 is not expected here, but we'll spare the entry (unless it was
2170 // We cannot truncate this entry, it has to be deleted.
2171 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2172 DoomPartialEntry(false);
2174 if (!reading_ && !partial_->IsLastRange()) {
2175 // We'll attempt to issue another network request, this time without us
2176 // messing up the headers.
2177 partial_->RestoreHeaders(&custom_request_->extra_headers);
2182 LOG(WARNING) << "Failed to revalidate partial entry";
2187 IgnoreRangeRequest();
2191 void HttpCache::Transaction::IgnoreRangeRequest() {
2192 // We have a problem. We may or may not be reading already (in which case we
2193 // returned the headers), but we'll just pretend that this request is not
2194 // using the cache and see what happens. Most likely this is the first
2195 // response from the server (it's not changing its mind midway, right?).
2196 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2198 DoneWritingToEntry(mode_ != WRITE);
2199 else if (mode_ & READ && entry_)
2200 cache_->DoneReadingFromEntry(entry_, this);
2202 partial_.reset(NULL);
2207 void HttpCache::Transaction::FailRangeRequest() {
2208 response_ = *new_response_;
2209 partial_->FixResponseHeaders(response_.headers.get(), false);
2212 int HttpCache::Transaction::SetupEntryForRead() {
2214 ResetNetworkTransaction();
2215 if (partial_.get()) {
2216 if (truncated_ || is_sparse_ || !invalid_range_) {
2217 // We are going to return the saved response headers to the caller, so
2218 // we may need to adjust them first.
2219 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2225 cache_->ConvertWriterToReader(entry_);
2228 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2229 next_state_ = STATE_CACHE_READ_METADATA;
2234 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2236 io_buf_len_ = data_len;
2237 next_state_ = STATE_NETWORK_READ;
2241 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2243 io_buf_len_ = data_len;
2244 next_state_ = STATE_CACHE_READ_DATA;
2248 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2249 IOBuffer* data, int data_len,
2250 const CompletionCallback& callback) {
2255 if (!partial_.get() || !data_len) {
2256 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2259 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2264 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2265 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2269 // Do not cache no-store content (unless we are record mode). Do not cache
2270 // content with cert errors either. This is to prevent not reporting net
2271 // errors when loading a resource from the cache. When we load a page over
2272 // HTTPS with a cert error we show an SSL blocking page. If the user clicks
2273 // proceed we reload the resource ignoring the errors. The loaded resource
2274 // is then cached. If that resource is subsequently loaded from the cache,
2275 // no net error is reported (even though the cert status contains the actual
2276 // errors) and no SSL blocking page is shown. An alternative would be to
2277 // reverse-map the cert status to a net error and replay the net error.
2278 if ((cache_->mode() != RECORD &&
2279 response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2280 net::IsCertStatusError(response_.ssl_info.cert_status)) {
2281 DoneWritingToEntry(false);
2282 if (net_log_.IsLogging())
2283 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2287 // When writing headers, we normally only write the non-transient
2288 // headers; when in record mode, record everything.
2289 bool skip_transient_headers = (cache_->mode() != RECORD);
2292 DCHECK_EQ(200, response_.headers->response_code());
2294 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2295 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2298 io_buf_len_ = data->pickle()->size();
2299 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2300 io_buf_len_, io_callback_, true);
2303 int HttpCache::Transaction::AppendResponseDataToEntry(
2304 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2305 if (!entry_ || !data_len)
2308 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2309 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2313 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2319 cache_->DoneWritingToEntry(entry_, success);
2321 mode_ = NONE; // switch to 'pass through' mode
2324 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2325 DLOG(ERROR) << "ReadData failed: " << result;
2326 const int result_for_histogram = std::max(0, -result);
2328 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2329 result_for_histogram);
2331 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2332 result_for_histogram);
2335 // Avoid using this entry in the future.
2337 cache_->DoomActiveEntry(cache_key_);
2341 DCHECK(!network_trans_.get());
2342 cache_->DoneWithEntry(entry_, this, false);
2346 next_state_ = STATE_GET_BACKEND;
2350 return ERR_CACHE_READ_FAILURE;
2353 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2354 DVLOG(2) << "DoomPartialEntry";
2355 int rv = cache_->DoomEntry(cache_key_, NULL);
2357 cache_->DoneWithEntry(entry_, this, false);
2361 partial_.reset(NULL);
2364 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2365 partial_->OnNetworkReadCompleted(result);
2368 // We need to move on to the next range.
2369 ResetNetworkTransaction();
2370 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2375 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2376 partial_->OnCacheReadCompleted(result);
2378 if (result == 0 && mode_ == READ_WRITE) {
2379 // We need to move on to the next range.
2380 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2381 } else if (result < 0) {
2382 return OnCacheReadError(result, false);
2387 int HttpCache::Transaction::DoRestartPartialRequest() {
2388 // The stored data cannot be used. Get rid of it and restart this request.
2389 // We need to also reset the |truncated_| flag as a new entry is created.
2390 DoomPartialEntry(!range_requested_);
2393 next_state_ = STATE_INIT_ENTRY;
2397 void HttpCache::Transaction::ResetNetworkTransaction() {
2398 DCHECK(!old_network_trans_load_timing_);
2399 DCHECK(network_trans_);
2400 LoadTimingInfo load_timing;
2401 if (network_trans_->GetLoadTimingInfo(&load_timing))
2402 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2403 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2404 network_trans_.reset();
2407 // Histogram data from the end of 2010 show the following distribution of
2408 // response headers:
2410 // Content-Length............... 87%
2411 // Date......................... 98%
2412 // Last-Modified................ 49%
2413 // Etag......................... 19%
2414 // Accept-Ranges: bytes......... 25%
2415 // Accept-Ranges: none.......... 0.4%
2416 // Strong Validator............. 50%
2417 // Strong Validator + ranges.... 24%
2418 // Strong Validator + CL........ 49%
2420 bool HttpCache::Transaction::CanResume(bool has_data) {
2421 // Double check that there is something worth keeping.
2422 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2425 if (request_->method != "GET")
2428 // Note that if this is a 206, content-length was already fixed after calling
2429 // PartialData::ResponseHeadersOK().
2430 if (response_.headers->GetContentLength() <= 0 ||
2431 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2432 !response_.headers->HasStrongValidators()) {
2439 void HttpCache::Transaction::UpdateTransactionPattern(
2440 TransactionPattern new_transaction_pattern) {
2441 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2443 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2444 new_transaction_pattern == PATTERN_NOT_COVERED);
2445 transaction_pattern_ = new_transaction_pattern;
2448 void HttpCache::Transaction::RecordHistograms() {
2449 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2450 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2451 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2452 cache_->mode() != NORMAL || request_->method != "GET") {
2455 UMA_HISTOGRAM_ENUMERATION(
2456 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2457 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2459 DCHECK(!range_requested_);
2460 DCHECK(!first_cache_access_since_.is_null());
2462 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2464 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2466 bool did_send_request = !send_request_since_.is_null();
2468 (did_send_request &&
2469 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2470 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2471 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2472 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2473 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2475 if (!did_send_request) {
2476 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2477 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2481 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2482 int before_send_percent =
2483 total_time.ToInternalValue() == 0 ? 0
2484 : before_send_time * 100 / total_time;
2485 DCHECK_LE(0, before_send_percent);
2486 DCHECK_GE(100, before_send_percent);
2488 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2489 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2490 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent);
2492 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2493 // below this comment after we have received initial data.
2494 switch (transaction_pattern_) {
2495 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2496 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2498 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2499 before_send_percent);
2502 case PATTERN_ENTRY_NOT_CACHED: {
2503 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2504 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2505 before_send_percent);
2508 case PATTERN_ENTRY_VALIDATED: {
2509 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2510 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2511 before_send_percent);
2514 case PATTERN_ENTRY_UPDATED: {
2515 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2516 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2517 before_send_percent);
2525 void HttpCache::Transaction::OnIOComplete(int result) {