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_transaction_delegate.h"
40 #include "net/http/http_util.h"
41 #include "net/http/partial_data.h"
42 #include "net/ssl/ssl_cert_request_info.h"
43 #include "net/ssl/ssl_config_service.h"
46 using base::TimeDelta;
47 using base::TimeTicks;
51 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
52 // a "non-error response" is one with a 2xx (Successful) or 3xx
53 // (Redirection) status code.
54 bool NonErrorResponse(int status_code) {
55 int status_code_range = status_code / 100;
56 return status_code_range == 2 || status_code_range == 3;
59 // Error codes that will be considered indicative of a page being offline/
60 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
61 bool IsOfflineError(int error) {
62 return (error == net::ERR_NAME_NOT_RESOLVED ||
63 error == net::ERR_INTERNET_DISCONNECTED ||
64 error == net::ERR_ADDRESS_UNREACHABLE ||
65 error == net::ERR_CONNECTION_TIMED_OUT);
68 // Enum for UMA, indicating the status (with regard to offline mode) of
69 // a particular request.
70 enum RequestOfflineStatus {
71 // A cache transaction hit in cache (data was present and not stale)
73 OFFLINE_STATUS_FRESH_CACHE,
75 // A network request was required for a cache entry, and it succeeded.
76 OFFLINE_STATUS_NETWORK_SUCCEEDED,
78 // A network request was required for a cache entry, and it failed with
79 // a non-offline error.
80 OFFLINE_STATUS_NETWORK_FAILED,
82 // A network request was required for a cache entry, it failed with an
83 // offline error, and we could serve stale data if
84 // LOAD_FROM_CACHE_IF_OFFLINE was set.
85 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE,
87 // A network request was required for a cache entry, it failed with
88 // an offline error, and there was no servable data in cache (even
90 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE,
92 OFFLINE_STATUS_MAX_ENTRIES
95 void RecordOfflineStatus(int load_flags, RequestOfflineStatus status) {
96 // Restrict to main frame to keep statistics close to
97 // "would have shown them something useful if offline mode was enabled".
98 if (load_flags & net::LOAD_MAIN_FRAME) {
99 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status,
100 OFFLINE_STATUS_MAX_ENTRIES);
104 // TODO(rvargas): Remove once we get the data.
105 void RecordVaryHeaderHistogram(const net::HttpResponseInfo* response) {
112 VaryType vary = VARY_NOT_PRESENT;
113 if (response->vary_data.is_valid()) {
115 if (response->headers->HasHeaderValue("vary", "user-agent"))
118 UMA_HISTOGRAM_ENUMERATION("HttpCache.Vary", vary, VARY_MAX);
125 struct HeaderNameAndValue {
130 // If the request includes one of these request headers, then avoid caching
131 // to avoid getting confused.
132 static const HeaderNameAndValue kPassThroughHeaders[] = {
133 { "if-unmodified-since", NULL }, // causes unexpected 412s
134 { "if-match", NULL }, // causes unexpected 412s
135 { "if-range", NULL },
139 struct ValidationHeaderInfo {
140 const char* request_header_name;
141 const char* related_response_header_name;
144 static const ValidationHeaderInfo kValidationHeaders[] = {
145 { "if-modified-since", "last-modified" },
146 { "if-none-match", "etag" },
149 // If the request includes one of these request headers, then avoid reusing
150 // our cached copy if any.
151 static const HeaderNameAndValue kForceFetchHeaders[] = {
152 { "cache-control", "no-cache" },
153 { "pragma", "no-cache" },
157 // If the request includes one of these request headers, then force our
158 // cached copy (if any) to be revalidated before reusing it.
159 static const HeaderNameAndValue kForceValidateHeaders[] = {
160 { "cache-control", "max-age=0" },
164 static bool HeaderMatches(const HttpRequestHeaders& headers,
165 const HeaderNameAndValue* search) {
166 for (; search->name; ++search) {
167 std::string header_value;
168 if (!headers.GetHeader(search->name, &header_value))
174 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
175 while (v.GetNext()) {
176 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
183 //-----------------------------------------------------------------------------
185 HttpCache::Transaction::Transaction(
186 RequestPriority priority,
188 HttpTransactionDelegate* transaction_delegate)
189 : next_state_(STATE_NONE),
192 cache_(cache->AsWeakPtr()),
197 target_state_(STATE_NONE),
199 invalid_range_(false),
202 range_requested_(false),
203 handling_206_(false),
204 cache_pending_(false),
205 done_reading_(false),
206 vary_mismatch_(false),
207 couldnt_conditionalize_request_(false),
210 effective_load_flags_(0),
213 io_callback_(base::Bind(&Transaction::OnIOComplete,
214 weak_factory_.GetWeakPtr())),
215 transaction_pattern_(PATTERN_UNDEFINED),
216 transaction_delegate_(transaction_delegate) {
217 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders ==
218 arraysize(kValidationHeaders),
219 Invalid_number_of_validation_headers);
222 HttpCache::Transaction::~Transaction() {
223 // We may have to issue another IO, but we should never invoke the callback_
227 transaction_delegate_ = NULL;
231 bool cancel_request = reading_ && response_.headers;
232 if (cancel_request) {
234 entry_->disk_entry->CancelSparseIO();
236 cancel_request &= (response_.headers->response_code() == 200);
240 cache_->DoneWithEntry(entry_, this, cancel_request);
241 } else if (cache_pending_) {
242 cache_->RemovePendingTransaction(this);
247 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
248 const CompletionCallback& callback) {
250 DCHECK_GT(buf_len, 0);
251 DCHECK(!callback.is_null());
252 if (!cache_.get() || !entry_)
253 return ERR_UNEXPECTED;
255 // We don't need to track this operation for anything.
256 // It could be possible to check if there is something already written and
257 // avoid writing again (it should be the same, right?), but let's allow the
258 // caller to "update" the contents with something new.
259 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
263 bool HttpCache::Transaction::AddTruncatedFlag() {
264 DCHECK(mode_ & WRITE || mode_ == NONE);
266 // Don't set the flag for sparse entries.
267 if (partial_.get() && !truncated_)
270 if (!CanResume(true))
273 // We may have received the whole resource already.
278 target_state_ = STATE_NONE;
279 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
284 LoadState HttpCache::Transaction::GetWriterLoadState() const {
285 if (network_trans_.get())
286 return network_trans_->GetLoadState();
287 if (entry_ || !request_)
288 return LOAD_STATE_IDLE;
289 return LOAD_STATE_WAITING_FOR_CACHE;
292 const BoundNetLog& HttpCache::Transaction::net_log() const {
296 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
297 const CompletionCallback& callback,
298 const BoundNetLog& net_log) {
300 DCHECK(!callback.is_null());
302 // Ensure that we only have one asynchronous call at a time.
303 DCHECK(callback_.is_null());
305 DCHECK(!network_trans_.get());
309 return ERR_UNEXPECTED;
311 SetRequest(net_log, request);
313 // We have to wait until the backend is initialized so we start the SM.
314 next_state_ = STATE_GET_BACKEND;
317 // Setting this here allows us to check for the existence of a callback_ to
318 // determine if we are still inside Start.
319 if (rv == ERR_IO_PENDING)
320 callback_ = callback;
325 int HttpCache::Transaction::RestartIgnoringLastError(
326 const CompletionCallback& callback) {
327 DCHECK(!callback.is_null());
329 // Ensure that we only have one asynchronous call at a time.
330 DCHECK(callback_.is_null());
333 return ERR_UNEXPECTED;
335 int rv = RestartNetworkRequest();
337 if (rv == ERR_IO_PENDING)
338 callback_ = callback;
343 int HttpCache::Transaction::RestartWithCertificate(
344 X509Certificate* client_cert,
345 const CompletionCallback& callback) {
346 DCHECK(!callback.is_null());
348 // Ensure that we only have one asynchronous call at a time.
349 DCHECK(callback_.is_null());
352 return ERR_UNEXPECTED;
354 int rv = RestartNetworkRequestWithCertificate(client_cert);
356 if (rv == ERR_IO_PENDING)
357 callback_ = callback;
362 int HttpCache::Transaction::RestartWithAuth(
363 const AuthCredentials& credentials,
364 const CompletionCallback& callback) {
365 DCHECK(auth_response_.headers.get());
366 DCHECK(!callback.is_null());
368 // Ensure that we only have one asynchronous call at a time.
369 DCHECK(callback_.is_null());
372 return ERR_UNEXPECTED;
374 // Clear the intermediate response since we are going to start over.
375 auth_response_ = HttpResponseInfo();
377 int rv = RestartNetworkRequestWithAuth(credentials);
379 if (rv == ERR_IO_PENDING)
380 callback_ = callback;
385 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
386 if (!network_trans_.get())
388 return network_trans_->IsReadyToRestartForAuth();
391 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
392 const CompletionCallback& callback) {
394 DCHECK_GT(buf_len, 0);
395 DCHECK(!callback.is_null());
397 DCHECK(callback_.is_null());
400 return ERR_UNEXPECTED;
402 // If we have an intermediate auth response at this point, then it means the
403 // user wishes to read the network response (the error page). If there is a
404 // previous response in the cache then we should leave it intact.
405 if (auth_response_.headers.get() && mode_ != NONE) {
406 UpdateTransactionPattern(PATTERN_NOT_COVERED);
407 DCHECK(mode_ & WRITE);
408 DoneWritingToEntry(mode_ == READ_WRITE);
417 DCHECK(partial_.get());
418 if (!network_trans_.get()) {
419 // We are just reading from the cache, but we may be writing later.
420 rv = ReadFromEntry(buf, buf_len);
425 DCHECK(network_trans_.get());
426 rv = ReadFromNetwork(buf, buf_len);
429 rv = ReadFromEntry(buf, buf_len);
436 if (rv == ERR_IO_PENDING) {
437 DCHECK(callback_.is_null());
438 callback_ = callback;
443 void HttpCache::Transaction::StopCaching() {
444 // We really don't know where we are now. Hopefully there is no operation in
445 // progress, but nothing really prevents this method to be called after we
446 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
447 // point because we need the state machine for that (and even if we are really
448 // free, that would be an asynchronous operation). In other words, keep the
449 // entry how it is (it will be marked as truncated at destruction), and let
450 // the next piece of code that executes know that we are now reading directly
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 void HttpCache::Transaction::DoneReading() {
468 if (cache_.get() && entry_) {
469 DCHECK_NE(mode_, UPDATE);
471 DoneWritingToEntry(true);
472 } else if (mode_ & READ) {
473 // It is necessary to check mode_ & READ because it is possible
474 // for mode_ to be NONE and entry_ non-NULL with a write entry
475 // if StopCaching was called.
476 cache_->DoneReadingFromEntry(entry_, this);
482 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
483 // Null headers means we encountered an error or haven't a response yet
484 if (auth_response_.headers.get())
485 return &auth_response_;
486 return (response_.headers.get() || response_.ssl_info.cert.get() ||
487 response_.cert_request_info.get())
492 LoadState HttpCache::Transaction::GetLoadState() const {
493 LoadState state = GetWriterLoadState();
494 if (state != LOAD_STATE_WAITING_FOR_CACHE)
498 return cache_->GetLoadStateForPendingTransaction(this);
500 return LOAD_STATE_IDLE;
503 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
504 if (network_trans_.get())
505 return network_trans_->GetUploadProgress();
506 return final_upload_progress_;
509 bool HttpCache::Transaction::GetLoadTimingInfo(
510 LoadTimingInfo* load_timing_info) const {
512 return network_trans_->GetLoadTimingInfo(load_timing_info);
514 if (old_network_trans_load_timing_) {
515 *load_timing_info = *old_network_trans_load_timing_;
519 if (first_cache_access_since_.is_null())
522 // If the cache entry was opened, return that time.
523 load_timing_info->send_start = first_cache_access_since_;
524 // This time doesn't make much sense when reading from the cache, so just use
525 // the same time as send_start.
526 load_timing_info->send_end = first_cache_access_since_;
530 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
531 priority_ = priority;
533 network_trans_->SetPriority(priority_);
536 //-----------------------------------------------------------------------------
538 void HttpCache::Transaction::DoCallback(int rv) {
539 DCHECK(rv != ERR_IO_PENDING);
540 DCHECK(!callback_.is_null());
542 read_buf_ = NULL; // Release the buffer before invoking the callback.
544 // Since Run may result in Read being called, clear callback_ up front.
545 CompletionCallback c = callback_;
550 int HttpCache::Transaction::HandleResult(int rv) {
551 DCHECK(rv != ERR_IO_PENDING);
552 if (!callback_.is_null())
558 // A few common patterns: (Foo* means Foo -> FooComplete)
562 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
563 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
564 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
565 // PartialHeadersReceived
568 // NetworkRead* -> CacheWriteData*
570 // Cached entry, no validation:
572 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
573 // -> BeginPartialCacheValidation() -> BeginCacheValidation()
578 // Cached entry, validation (304):
580 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
581 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
582 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
583 // CacheWriteResponse* -> UpdateCachedResponseComplete ->
584 // OverwriteCachedResponse -> PartialHeadersReceived
589 // Cached entry, validation and replace (200):
591 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
592 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
593 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
594 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
595 // PartialHeadersReceived
598 // NetworkRead* -> CacheWriteData*
600 // Sparse entry, partially cached, byte range request:
602 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
603 // -> BeginPartialCacheValidation() -> CacheQueryData* ->
604 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
605 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
606 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
607 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
608 // PartialHeadersReceived
611 // NetworkRead* -> CacheWriteData*
614 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
615 // CompletePartialCacheValidation -> CacheReadData* ->
618 // CacheReadData* -> StartPartialCacheValidation ->
619 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
620 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
621 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
623 int HttpCache::Transaction::DoLoop(int result) {
624 DCHECK(next_state_ != STATE_NONE);
628 State state = next_state_;
629 next_state_ = STATE_NONE;
631 case STATE_GET_BACKEND:
635 case STATE_GET_BACKEND_COMPLETE:
636 rv = DoGetBackendComplete(rv);
638 case STATE_SEND_REQUEST:
640 rv = DoSendRequest();
642 case STATE_SEND_REQUEST_COMPLETE:
643 rv = DoSendRequestComplete(rv);
645 case STATE_SUCCESSFUL_SEND_REQUEST:
647 rv = DoSuccessfulSendRequest();
649 case STATE_NETWORK_READ:
651 rv = DoNetworkRead();
653 case STATE_NETWORK_READ_COMPLETE:
654 rv = DoNetworkReadComplete(rv);
656 case STATE_INIT_ENTRY:
660 case STATE_OPEN_ENTRY:
664 case STATE_OPEN_ENTRY_COMPLETE:
665 rv = DoOpenEntryComplete(rv);
667 case STATE_CREATE_ENTRY:
669 rv = DoCreateEntry();
671 case STATE_CREATE_ENTRY_COMPLETE:
672 rv = DoCreateEntryComplete(rv);
674 case STATE_DOOM_ENTRY:
678 case STATE_DOOM_ENTRY_COMPLETE:
679 rv = DoDoomEntryComplete(rv);
681 case STATE_ADD_TO_ENTRY:
685 case STATE_ADD_TO_ENTRY_COMPLETE:
686 rv = DoAddToEntryComplete(rv);
688 case STATE_START_PARTIAL_CACHE_VALIDATION:
690 rv = DoStartPartialCacheValidation();
692 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
693 rv = DoCompletePartialCacheValidation(rv);
695 case STATE_UPDATE_CACHED_RESPONSE:
697 rv = DoUpdateCachedResponse();
699 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
700 rv = DoUpdateCachedResponseComplete(rv);
702 case STATE_OVERWRITE_CACHED_RESPONSE:
704 rv = DoOverwriteCachedResponse();
706 case STATE_TRUNCATE_CACHED_DATA:
708 rv = DoTruncateCachedData();
710 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
711 rv = DoTruncateCachedDataComplete(rv);
713 case STATE_TRUNCATE_CACHED_METADATA:
715 rv = DoTruncateCachedMetadata();
717 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
718 rv = DoTruncateCachedMetadataComplete(rv);
720 case STATE_PARTIAL_HEADERS_RECEIVED:
722 rv = DoPartialHeadersReceived();
724 case STATE_CACHE_READ_RESPONSE:
726 rv = DoCacheReadResponse();
728 case STATE_CACHE_READ_RESPONSE_COMPLETE:
729 rv = DoCacheReadResponseComplete(rv);
731 case STATE_CACHE_WRITE_RESPONSE:
733 rv = DoCacheWriteResponse();
735 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
737 rv = DoCacheWriteTruncatedResponse();
739 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
740 rv = DoCacheWriteResponseComplete(rv);
742 case STATE_CACHE_READ_METADATA:
744 rv = DoCacheReadMetadata();
746 case STATE_CACHE_READ_METADATA_COMPLETE:
747 rv = DoCacheReadMetadataComplete(rv);
749 case STATE_CACHE_QUERY_DATA:
751 rv = DoCacheQueryData();
753 case STATE_CACHE_QUERY_DATA_COMPLETE:
754 rv = DoCacheQueryDataComplete(rv);
756 case STATE_CACHE_READ_DATA:
758 rv = DoCacheReadData();
760 case STATE_CACHE_READ_DATA_COMPLETE:
761 rv = DoCacheReadDataComplete(rv);
763 case STATE_CACHE_WRITE_DATA:
764 rv = DoCacheWriteData(rv);
766 case STATE_CACHE_WRITE_DATA_COMPLETE:
767 rv = DoCacheWriteDataComplete(rv);
770 NOTREACHED() << "bad state";
774 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
776 if (rv != ERR_IO_PENDING)
782 int HttpCache::Transaction::DoGetBackend() {
783 cache_pending_ = true;
784 next_state_ = STATE_GET_BACKEND_COMPLETE;
785 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
786 ReportCacheActionStart();
787 return cache_->GetBackendForTransaction(this);
790 int HttpCache::Transaction::DoGetBackendComplete(int result) {
791 DCHECK(result == OK || result == ERR_FAILED);
792 ReportCacheActionFinish();
793 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
795 cache_pending_ = false;
797 if (!ShouldPassThrough()) {
798 cache_key_ = cache_->GenerateCacheKey(request_);
800 // Requested cache access mode.
801 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
803 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
809 // Downgrade to UPDATE if the request has been externally conditionalized.
810 if (external_validation_.initialized) {
812 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
813 // in case READ was off).
821 // Use PUT and DELETE only to invalidate existing stored entries.
822 if ((request_->method == "PUT" || request_->method == "DELETE") &&
823 mode_ != READ_WRITE && mode_ != WRITE) {
827 // If must use cache, then we must fail. This can happen for back/forward
828 // navigations to a page generated via a form post.
829 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
830 return ERR_CACHE_MISS;
833 if (partial_.get()) {
834 partial_->RestoreHeaders(&custom_request_->extra_headers);
837 next_state_ = STATE_SEND_REQUEST;
839 next_state_ = STATE_INIT_ENTRY;
842 // This is only set if we have something to do with the response.
843 range_requested_ = (partial_.get() != NULL);
848 int HttpCache::Transaction::DoSendRequest() {
849 DCHECK(mode_ & WRITE || mode_ == NONE);
850 DCHECK(!network_trans_.get());
852 send_request_since_ = TimeTicks::Now();
854 // Create a network transaction.
855 int rv = cache_->network_layer_->CreateTransaction(
856 priority_, &network_trans_, NULL);
860 // Old load timing information, if any, is now obsolete.
861 old_network_trans_load_timing_.reset();
863 ReportNetworkActionStart();
864 next_state_ = STATE_SEND_REQUEST_COMPLETE;
865 rv = network_trans_->Start(request_, io_callback_, net_log_);
869 int HttpCache::Transaction::DoSendRequestComplete(int result) {
870 ReportNetworkActionFinish();
873 return ERR_UNEXPECTED;
875 // If requested, and we have a readable cache entry, and we have
876 // an error indicating that we're offline as opposed to in contact
877 // with a bad server, read from cache anyway.
878 if (IsOfflineError(result)) {
879 if (mode_ == READ_WRITE && entry_ && !partial_) {
880 RecordOfflineStatus(effective_load_flags_,
881 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE);
882 if (effective_load_flags_ & LOAD_FROM_CACHE_IF_OFFLINE) {
883 UpdateTransactionPattern(PATTERN_NOT_COVERED);
884 response_.server_data_unavailable = true;
885 return SetupEntryForRead();
888 RecordOfflineStatus(effective_load_flags_,
889 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE);
892 RecordOfflineStatus(effective_load_flags_,
893 (result == OK ? OFFLINE_STATUS_NETWORK_SUCCEEDED :
894 OFFLINE_STATUS_NETWORK_FAILED));
897 // If we tried to conditionalize the request and failed, we know
898 // we won't be reading from the cache after this point.
899 if (couldnt_conditionalize_request_)
903 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
907 // Do not record requests that have network errors or restarts.
908 UpdateTransactionPattern(PATTERN_NOT_COVERED);
909 if (IsCertificateError(result)) {
910 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
911 // If we get a certificate error, then there is a certificate in ssl_info,
912 // so GetResponseInfo() should never return NULL here.
914 response_.ssl_info = response->ssl_info;
915 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
916 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
918 response_.cert_request_info = response->cert_request_info;
919 } else if (response_.was_cached) {
920 DoneWritingToEntry(true);
925 // We received the response headers and there is no error.
926 int HttpCache::Transaction::DoSuccessfulSendRequest() {
927 DCHECK(!new_response_);
928 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
930 if (new_response->headers->response_code() == 401 ||
931 new_response->headers->response_code() == 407) {
932 auth_response_ = *new_response;
936 new_response_ = new_response;
937 if (!ValidatePartialResponse() && !auth_response_.headers.get()) {
938 // Something went wrong with this request and we have to restart it.
939 // If we have an authentication response, we are exposed to weird things
940 // hapenning if the user cancels the authentication before we receive
942 UpdateTransactionPattern(PATTERN_NOT_COVERED);
943 response_ = HttpResponseInfo();
944 ResetNetworkTransaction();
945 new_response_ = NULL;
946 next_state_ = STATE_SEND_REQUEST;
949 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
950 // We have stored the full entry, but it changed and the server is
951 // sending a range. We have to delete the old entry.
952 UpdateTransactionPattern(PATTERN_NOT_COVERED);
953 DoneWritingToEntry(false);
955 if (new_response_->headers->response_code() == 416 &&
956 (request_->method == "GET" || request_->method == "POST")) {
957 DCHECK_EQ(NONE, mode_);
958 response_ = *new_response_;
962 if (mode_ == WRITE &&
963 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
964 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
967 if (mode_ == WRITE &&
968 (request_->method == "PUT" || request_->method == "DELETE")) {
969 if (NonErrorResponse(new_response->headers->response_code())) {
970 int ret = cache_->DoomEntry(cache_key_, NULL);
973 cache_->DoneWritingToEntry(entry_, true);
978 if (request_->method == "POST" &&
979 NonErrorResponse(new_response->headers->response_code())) {
980 cache_->DoomMainEntryForUrl(request_->url);
983 RecordVaryHeaderHistogram(new_response);
985 // Are we expecting a response to a conditional query?
986 if (mode_ == READ_WRITE || mode_ == UPDATE) {
987 if (new_response->headers->response_code() == 304 || handling_206_) {
988 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
989 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
992 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
996 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1000 int HttpCache::Transaction::DoNetworkRead() {
1001 ReportNetworkActionStart();
1002 next_state_ = STATE_NETWORK_READ_COMPLETE;
1003 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1006 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1007 DCHECK(mode_ & WRITE || mode_ == NONE);
1009 ReportNetworkActionFinish();
1012 return ERR_UNEXPECTED;
1014 // If there is an error or we aren't saving the data, we are done; just wait
1015 // until the destructor runs to see if we can keep the data.
1016 if (mode_ == NONE || result < 0)
1019 next_state_ = STATE_CACHE_WRITE_DATA;
1023 int HttpCache::Transaction::DoInitEntry() {
1024 DCHECK(!new_entry_);
1027 return ERR_UNEXPECTED;
1029 if (mode_ == WRITE) {
1030 next_state_ = STATE_DOOM_ENTRY;
1034 next_state_ = STATE_OPEN_ENTRY;
1038 int HttpCache::Transaction::DoOpenEntry() {
1039 DCHECK(!new_entry_);
1040 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1041 cache_pending_ = true;
1042 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1043 first_cache_access_since_ = TimeTicks::Now();
1044 ReportCacheActionStart();
1045 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1048 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1049 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1050 // OK, otherwise the cache will end up with an active entry without any
1051 // transaction attached.
1052 ReportCacheActionFinish();
1053 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1054 cache_pending_ = false;
1056 next_state_ = STATE_ADD_TO_ENTRY;
1060 if (result == ERR_CACHE_RACE) {
1061 next_state_ = STATE_INIT_ENTRY;
1065 if (request_->method == "PUT" || request_->method == "DELETE") {
1066 DCHECK(mode_ == READ_WRITE || mode_ == WRITE);
1068 next_state_ = STATE_SEND_REQUEST;
1072 if (mode_ == READ_WRITE) {
1074 next_state_ = STATE_CREATE_ENTRY;
1077 if (mode_ == UPDATE) {
1078 // There is no cache entry to update; proceed without caching.
1080 next_state_ = STATE_SEND_REQUEST;
1083 if (cache_->mode() == PLAYBACK)
1084 DVLOG(1) << "Playback Cache Miss: " << request_->url;
1086 // The entry does not exist, and we are not permitted to create a new entry,
1088 return ERR_CACHE_MISS;
1091 int HttpCache::Transaction::DoCreateEntry() {
1092 DCHECK(!new_entry_);
1093 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1094 cache_pending_ = true;
1095 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1096 ReportCacheActionStart();
1097 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1100 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1101 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1102 // OK, otherwise the cache will end up with an active entry without any
1103 // transaction attached.
1104 ReportCacheActionFinish();
1105 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1107 cache_pending_ = false;
1108 next_state_ = STATE_ADD_TO_ENTRY;
1110 if (result == ERR_CACHE_RACE) {
1111 next_state_ = STATE_INIT_ENTRY;
1116 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", false);
1118 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", true);
1119 // We have a race here: Maybe we failed to open the entry and decided to
1120 // create one, but by the time we called create, another transaction already
1121 // created the entry. If we want to eliminate this issue, we need an atomic
1122 // OpenOrCreate() method exposed by the disk cache.
1123 DLOG(WARNING) << "Unable to create cache entry";
1126 partial_->RestoreHeaders(&custom_request_->extra_headers);
1127 next_state_ = STATE_SEND_REQUEST;
1132 int HttpCache::Transaction::DoDoomEntry() {
1133 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1134 cache_pending_ = true;
1135 if (first_cache_access_since_.is_null())
1136 first_cache_access_since_ = TimeTicks::Now();
1137 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1138 ReportCacheActionStart();
1139 return cache_->DoomEntry(cache_key_, this);
1142 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1143 ReportCacheActionFinish();
1144 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1145 next_state_ = STATE_CREATE_ENTRY;
1146 cache_pending_ = false;
1147 if (result == ERR_CACHE_RACE)
1148 next_state_ = STATE_INIT_ENTRY;
1152 int HttpCache::Transaction::DoAddToEntry() {
1154 cache_pending_ = true;
1155 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1156 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1157 DCHECK(entry_lock_waiting_since_.is_null());
1158 entry_lock_waiting_since_ = TimeTicks::Now();
1159 return cache_->AddTransactionToEntry(new_entry_, this);
1162 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1163 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1165 const TimeDelta entry_lock_wait =
1166 TimeTicks::Now() - entry_lock_waiting_since_;
1167 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1169 entry_lock_waiting_since_ = TimeTicks();
1171 cache_pending_ = false;
1174 entry_ = new_entry_;
1176 // If there is a failure, the cache should have taken care of new_entry_.
1179 if (result == ERR_CACHE_RACE) {
1180 next_state_ = STATE_INIT_ENTRY;
1189 if (mode_ == WRITE) {
1191 partial_->RestoreHeaders(&custom_request_->extra_headers);
1192 next_state_ = STATE_SEND_REQUEST;
1194 // We have to read the headers from the cached entry.
1195 DCHECK(mode_ & READ_META);
1196 next_state_ = STATE_CACHE_READ_RESPONSE;
1201 // We may end up here multiple times for a given request.
1202 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1206 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1207 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1210 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1212 // This is the end of the request.
1213 if (mode_ & WRITE) {
1214 DoneWritingToEntry(true);
1216 cache_->DoneReadingFromEntry(entry_, this);
1225 partial_->PrepareCacheValidation(entry_->disk_entry,
1226 &custom_request_->extra_headers);
1228 if (reading_ && partial_->IsCurrentRangeCached()) {
1229 next_state_ = STATE_CACHE_READ_DATA;
1233 return BeginCacheValidation();
1236 // We received 304 or 206 and we want to update the cached response headers.
1237 int HttpCache::Transaction::DoUpdateCachedResponse() {
1238 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1240 // Update cached response based on headers in new_response.
1241 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1242 response_.headers->Update(*new_response_->headers.get());
1243 response_.response_time = new_response_->response_time;
1244 response_.request_time = new_response_->request_time;
1245 response_.network_accessed = new_response_->network_accessed;
1247 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1248 if (!entry_->doomed) {
1249 int ret = cache_->DoomEntry(cache_key_, NULL);
1253 // If we are already reading, we already updated the headers for this
1254 // request; doing it again will change Content-Length.
1256 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1257 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1264 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1265 if (mode_ == UPDATE) {
1266 DCHECK(!handling_206_);
1267 // We got a "not modified" response and already updated the corresponding
1268 // cache entry above.
1270 // By closing the cached entry now, we make sure that the 304 rather than
1271 // the cached 200 response, is what will be returned to the user.
1272 DoneWritingToEntry(true);
1273 } else if (entry_ && !handling_206_) {
1274 DCHECK_EQ(READ_WRITE, mode_);
1275 if (!partial_.get() || partial_->IsLastRange()) {
1276 cache_->ConvertWriterToReader(entry_);
1279 // We no longer need the network transaction, so destroy it.
1280 final_upload_progress_ = network_trans_->GetUploadProgress();
1281 ResetNetworkTransaction();
1282 } else if (entry_ && handling_206_ && truncated_ &&
1283 partial_->initial_validation()) {
1284 // We just finished the validation of a truncated entry, and the server
1285 // is willing to resume the operation. Now we go back and start serving
1286 // the first part to the user.
1287 ResetNetworkTransaction();
1288 new_response_ = NULL;
1289 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1290 partial_->SetRangeToStartDownload();
1293 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1297 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1299 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1303 // We change the value of Content-Length for partial content.
1304 if (handling_206_ && partial_.get())
1305 partial_->FixContentLength(new_response_->headers.get());
1307 response_ = *new_response_;
1309 if (handling_206_ && !CanResume(false)) {
1310 // There is no point in storing this resource because it will never be used.
1311 DoneWritingToEntry(false);
1313 partial_->FixResponseHeaders(response_.headers.get(), true);
1314 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1318 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1319 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1320 STATE_CACHE_WRITE_RESPONSE;
1324 int HttpCache::Transaction::DoTruncateCachedData() {
1325 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1328 if (net_log_.IsLoggingAllEvents())
1329 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1330 ReportCacheActionStart();
1331 // Truncate the stream.
1332 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1335 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1337 ReportCacheActionFinish();
1338 if (net_log_.IsLoggingAllEvents()) {
1339 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1344 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1348 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1349 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1353 if (net_log_.IsLoggingAllEvents())
1354 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1355 ReportCacheActionStart();
1356 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1359 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1361 ReportCacheActionFinish();
1362 if (net_log_.IsLoggingAllEvents()) {
1363 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1368 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1372 int HttpCache::Transaction::DoPartialHeadersReceived() {
1373 new_response_ = NULL;
1374 if (entry_ && !partial_.get() &&
1375 entry_->disk_entry->GetDataSize(kMetadataIndex))
1376 next_state_ = STATE_CACHE_READ_METADATA;
1378 if (!partial_.get())
1382 if (network_trans_.get()) {
1383 next_state_ = STATE_NETWORK_READ;
1385 next_state_ = STATE_CACHE_READ_DATA;
1387 } else if (mode_ != NONE) {
1388 // We are about to return the headers for a byte-range request to the user,
1389 // so let's fix them.
1390 partial_->FixResponseHeaders(response_.headers.get(), true);
1395 int HttpCache::Transaction::DoCacheReadResponse() {
1397 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1399 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1400 read_buf_ = new IOBuffer(io_buf_len_);
1402 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1403 ReportCacheActionStart();
1404 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1405 io_buf_len_, io_callback_);
1408 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1409 ReportCacheActionFinish();
1410 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1411 if (result != io_buf_len_ ||
1412 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1413 &response_, &truncated_)) {
1414 return OnCacheReadError(result, true);
1417 // Some resources may have slipped in as truncated when they're not.
1418 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1419 if (response_.headers->GetContentLength() == current_size)
1422 // We now have access to the cache entry.
1424 // o if we are a reader for the transaction, then we can start reading the
1427 // o if we can read or write, then we should check if the cache entry needs
1428 // to be validated and then issue a network request if needed or just read
1429 // from the cache if the cache entry is already valid.
1431 // o if we are set to UPDATE, then we are handling an externally
1432 // conditionalized request (if-modified-since / if-none-match). We check
1433 // if the request headers define a validation request.
1437 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1438 result = BeginCacheRead();
1441 result = BeginPartialCacheValidation();
1444 result = BeginExternallyConditionalizedRequest();
1449 result = ERR_FAILED;
1454 int HttpCache::Transaction::DoCacheWriteResponse() {
1456 if (net_log_.IsLoggingAllEvents())
1457 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1458 ReportCacheActionStart();
1460 return WriteResponseInfoToEntry(false);
1463 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1465 if (net_log_.IsLoggingAllEvents())
1466 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1467 ReportCacheActionStart();
1469 return WriteResponseInfoToEntry(true);
1472 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1473 next_state_ = target_state_;
1474 target_state_ = STATE_NONE;
1477 ReportCacheActionFinish();
1478 if (net_log_.IsLoggingAllEvents()) {
1479 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1483 // Balance the AddRef from WriteResponseInfoToEntry.
1484 if (result != io_buf_len_) {
1485 DLOG(ERROR) << "failed to write response info to cache";
1486 DoneWritingToEntry(false);
1491 int HttpCache::Transaction::DoCacheReadMetadata() {
1493 DCHECK(!response_.metadata.get());
1494 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1496 response_.metadata =
1497 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1499 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1500 ReportCacheActionStart();
1501 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1502 response_.metadata.get(),
1503 response_.metadata->size(),
1507 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1508 ReportCacheActionFinish();
1509 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1510 if (result != response_.metadata->size())
1511 return OnCacheReadError(result, false);
1515 int HttpCache::Transaction::DoCacheQueryData() {
1516 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1517 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1520 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1521 if (result == ERR_NOT_IMPLEMENTED) {
1522 // Restart the request overwriting the cache entry.
1523 // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1524 // supports Sparse IO.
1525 return DoRestartPartialRequest();
1527 DCHECK_EQ(OK, result);
1529 return ERR_UNEXPECTED;
1531 return ValidateEntryHeadersAndContinue();
1534 int HttpCache::Transaction::DoCacheReadData() {
1536 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1538 if (net_log_.IsLoggingAllEvents())
1539 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1540 ReportCacheActionStart();
1541 if (partial_.get()) {
1542 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1546 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1547 read_buf_.get(), io_buf_len_,
1551 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1552 ReportCacheActionFinish();
1553 if (net_log_.IsLoggingAllEvents()) {
1554 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1559 return ERR_UNEXPECTED;
1561 if (partial_.get()) {
1562 // Partial requests are confusing to report in histograms because they may
1563 // have multiple underlying requests.
1564 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1565 return DoPartialCacheReadCompleted(result);
1569 read_offset_ += result;
1570 } else if (result == 0) { // End of file.
1572 cache_->DoneReadingFromEntry(entry_, this);
1575 return OnCacheReadError(result, false);
1580 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1581 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1582 write_len_ = num_bytes;
1584 if (net_log_.IsLoggingAllEvents())
1585 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1586 ReportCacheActionStart();
1589 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1592 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1594 ReportCacheActionFinish();
1595 if (net_log_.IsLoggingAllEvents()) {
1596 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1600 // Balance the AddRef from DoCacheWriteData.
1602 return ERR_UNEXPECTED;
1604 if (result != write_len_) {
1605 DLOG(ERROR) << "failed to write response data to cache";
1606 DoneWritingToEntry(false);
1608 // We want to ignore errors writing to disk and just keep reading from
1610 result = write_len_;
1611 } else if (!done_reading_ && entry_) {
1612 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1613 int64 body_size = response_.headers->GetContentLength();
1614 if (body_size >= 0 && body_size <= current_size)
1615 done_reading_ = true;
1618 if (partial_.get()) {
1619 // This may be the last request.
1620 if (!(result == 0 && !truncated_ &&
1621 (partial_->IsLastRange() || mode_ == WRITE)))
1622 return DoPartialNetworkReadCompleted(result);
1626 // End of file. This may be the result of a connection problem so see if we
1627 // have to keep the entry around to be flagged as truncated later on.
1628 if (done_reading_ || !entry_ || partial_.get() ||
1629 response_.headers->GetContentLength() <= 0)
1630 DoneWritingToEntry(true);
1636 //-----------------------------------------------------------------------------
1638 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1639 const HttpRequestInfo* request) {
1642 effective_load_flags_ = request_->load_flags;
1644 switch (cache_->mode()) {
1648 // When in record mode, we want to NEVER load from the cache.
1649 // The reason for this is beacuse we save the Set-Cookie headers
1650 // (intentionally). If we read from the cache, we replay them
1652 effective_load_flags_ |= LOAD_BYPASS_CACHE;
1655 // When in playback mode, we want to load exclusively from the cache.
1656 effective_load_flags_ |= LOAD_ONLY_FROM_CACHE;
1659 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1663 // Some headers imply load flags. The order here is significant.
1665 // LOAD_DISABLE_CACHE : no cache read or write
1666 // LOAD_BYPASS_CACHE : no cache read
1667 // LOAD_VALIDATE_CACHE : no cache read unless validation
1669 // The former modes trump latter modes, so if we find a matching header we
1670 // can stop iterating kSpecialHeaders.
1672 static const struct {
1673 const HeaderNameAndValue* search;
1675 } kSpecialHeaders[] = {
1676 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
1677 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
1678 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
1681 bool range_found = false;
1682 bool external_validation_error = false;
1684 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
1687 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSpecialHeaders); ++i) {
1688 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
1689 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
1694 // Check for conditionalization headers which may correspond with a
1695 // cache validation request.
1696 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
1697 const ValidationHeaderInfo& info = kValidationHeaders[i];
1698 std::string validation_value;
1699 if (request_->extra_headers.GetHeader(
1700 info.request_header_name, &validation_value)) {
1701 if (!external_validation_.values[i].empty() ||
1702 validation_value.empty()) {
1703 external_validation_error = true;
1705 external_validation_.values[i] = validation_value;
1706 external_validation_.initialized = true;
1710 // We don't support ranges and validation headers.
1711 if (range_found && external_validation_.initialized) {
1712 LOG(WARNING) << "Byte ranges AND validation headers found.";
1713 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1716 // If there is more than one validation header, we can't treat this request as
1717 // a cache validation, since we don't know for sure which header the server
1718 // will give us a response for (and they could be contradictory).
1719 if (external_validation_error) {
1720 LOG(WARNING) << "Multiple or malformed validation headers found.";
1721 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1724 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
1725 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1726 partial_.reset(new PartialData);
1727 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
1728 // We will be modifying the actual range requested to the server, so
1729 // let's remove the header here.
1730 custom_request_.reset(new HttpRequestInfo(*request_));
1731 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
1732 request_ = custom_request_.get();
1733 partial_->SetHeaders(custom_request_->extra_headers);
1735 // The range is invalid or we cannot handle it properly.
1736 VLOG(1) << "Invalid byte range found.";
1737 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1738 partial_.reset(NULL);
1743 bool HttpCache::Transaction::ShouldPassThrough() {
1744 // We may have a null disk_cache if there is an error we cannot recover from,
1745 // like not enough disk space, or sharing violations.
1746 if (!cache_->disk_cache_.get())
1749 // When using the record/playback modes, we always use the cache
1750 // and we never pass through.
1751 if (cache_->mode() == RECORD || cache_->mode() == PLAYBACK)
1754 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
1757 if (request_->method == "GET")
1760 if (request_->method == "POST" && request_->upload_data_stream &&
1761 request_->upload_data_stream->identifier()) {
1765 if (request_->method == "PUT" && request_->upload_data_stream)
1768 if (request_->method == "DELETE")
1771 // TODO(darin): add support for caching HEAD responses
1775 int HttpCache::Transaction::BeginCacheRead() {
1776 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
1777 if (response_.headers->response_code() == 206 || partial_.get()) {
1779 return ERR_CACHE_MISS;
1782 // We don't have the whole resource.
1784 return ERR_CACHE_MISS;
1786 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
1787 next_state_ = STATE_CACHE_READ_METADATA;
1792 int HttpCache::Transaction::BeginCacheValidation() {
1793 DCHECK(mode_ == READ_WRITE);
1795 bool skip_validation = !RequiresValidation();
1798 // Truncated entries can cause partial gets, so we shouldn't record this
1799 // load in histograms.
1800 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1801 skip_validation = !partial_->initial_validation();
1804 if (partial_.get() && (is_sparse_ || truncated_) &&
1805 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
1806 // Force revalidation for sparse or truncated entries. Note that we don't
1807 // want to ignore the regular validation logic just because a byte range was
1808 // part of the request.
1809 skip_validation = false;
1812 if (skip_validation) {
1813 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1814 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE);
1815 return SetupEntryForRead();
1817 // Make the network request conditional, to see if we may reuse our cached
1818 // response. If we cannot do so, then we just resort to a normal fetch.
1819 // Our mode remains READ_WRITE for a conditional request. Even if the
1820 // conditionalization fails, we don't switch to WRITE mode until we
1821 // know we won't be falling back to using the cache entry in the
1822 // LOAD_FROM_CACHE_IF_OFFLINE case.
1823 if (!ConditionalizeRequest()) {
1824 couldnt_conditionalize_request_ = true;
1825 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
1827 return DoRestartPartialRequest();
1829 DCHECK_NE(206, response_.headers->response_code());
1831 next_state_ = STATE_SEND_REQUEST;
1836 int HttpCache::Transaction::BeginPartialCacheValidation() {
1837 DCHECK(mode_ == READ_WRITE);
1839 if (response_.headers->response_code() != 206 && !partial_.get() &&
1841 return BeginCacheValidation();
1844 // Partial requests should not be recorded in histograms.
1845 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1846 if (range_requested_) {
1847 next_state_ = STATE_CACHE_QUERY_DATA;
1850 // The request is not for a range, but we have stored just ranges.
1851 partial_.reset(new PartialData());
1852 partial_->SetHeaders(request_->extra_headers);
1853 if (!custom_request_.get()) {
1854 custom_request_.reset(new HttpRequestInfo(*request_));
1855 request_ = custom_request_.get();
1858 return ValidateEntryHeadersAndContinue();
1861 // This should only be called once per request.
1862 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
1863 DCHECK(mode_ == READ_WRITE);
1865 if (!partial_->UpdateFromStoredHeaders(
1866 response_.headers.get(), entry_->disk_entry, truncated_)) {
1867 return DoRestartPartialRequest();
1870 if (response_.headers->response_code() == 206)
1873 if (!partial_->IsRequestedRangeOK()) {
1874 // The stored data is fine, but the request may be invalid.
1875 invalid_range_ = true;
1878 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1882 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
1883 DCHECK_EQ(UPDATE, mode_);
1884 DCHECK(external_validation_.initialized);
1886 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
1887 if (external_validation_.values[i].empty())
1889 // Retrieve either the cached response's "etag" or "last-modified" header.
1890 std::string validator;
1891 response_.headers->EnumerateHeader(
1893 kValidationHeaders[i].related_response_header_name,
1896 if (response_.headers->response_code() != 200 || truncated_ ||
1897 validator.empty() || validator != external_validation_.values[i]) {
1898 // The externally conditionalized request is not a validation request
1899 // for our existing cache entry. Proceed with caching disabled.
1900 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1901 DoneWritingToEntry(true);
1905 next_state_ = STATE_SEND_REQUEST;
1909 int HttpCache::Transaction::RestartNetworkRequest() {
1910 DCHECK(mode_ & WRITE || mode_ == NONE);
1911 DCHECK(network_trans_.get());
1912 DCHECK_EQ(STATE_NONE, next_state_);
1914 ReportNetworkActionStart();
1915 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1916 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
1917 if (rv != ERR_IO_PENDING)
1922 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
1923 X509Certificate* client_cert) {
1924 DCHECK(mode_ & WRITE || mode_ == NONE);
1925 DCHECK(network_trans_.get());
1926 DCHECK_EQ(STATE_NONE, next_state_);
1928 ReportNetworkActionStart();
1929 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1930 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
1931 if (rv != ERR_IO_PENDING)
1936 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
1937 const AuthCredentials& credentials) {
1938 DCHECK(mode_ & WRITE || mode_ == NONE);
1939 DCHECK(network_trans_.get());
1940 DCHECK_EQ(STATE_NONE, next_state_);
1942 ReportNetworkActionStart();
1943 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1944 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
1945 if (rv != ERR_IO_PENDING)
1950 bool HttpCache::Transaction::RequiresValidation() {
1951 // TODO(darin): need to do more work here:
1952 // - make sure we have a matching request method
1953 // - watch out for cached responses that depend on authentication
1955 // In playback mode, nothing requires validation.
1956 if (cache_->mode() == net::HttpCache::PLAYBACK)
1959 if (response_.vary_data.is_valid() &&
1960 !response_.vary_data.MatchesRequest(*request_,
1961 *response_.headers.get())) {
1962 vary_mismatch_ = true;
1966 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
1969 if (effective_load_flags_ & LOAD_VALIDATE_CACHE)
1972 if (request_->method == "PUT" || request_->method == "DELETE")
1975 if (response_.headers->RequiresValidation(
1976 response_.request_time, response_.response_time, Time::Now())) {
1983 bool HttpCache::Transaction::ConditionalizeRequest() {
1984 DCHECK(response_.headers.get());
1986 if (request_->method == "PUT" || request_->method == "DELETE")
1989 // This only makes sense for cached 200 or 206 responses.
1990 if (response_.headers->response_code() != 200 &&
1991 response_.headers->response_code() != 206) {
1995 // We should have handled this case before.
1996 DCHECK(response_.headers->response_code() != 206 ||
1997 response_.headers->HasStrongValidators());
1999 // Just use the first available ETag and/or Last-Modified header value.
2000 // TODO(darin): Or should we use the last?
2002 std::string etag_value;
2003 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2004 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2006 std::string last_modified_value;
2007 if (!vary_mismatch_) {
2008 response_.headers->EnumerateHeader(NULL, "last-modified",
2009 &last_modified_value);
2012 if (etag_value.empty() && last_modified_value.empty())
2015 if (!partial_.get()) {
2016 // Need to customize the request, so this forces us to allocate :(
2017 custom_request_.reset(new HttpRequestInfo(*request_));
2018 request_ = custom_request_.get();
2020 DCHECK(custom_request_.get());
2022 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2025 if (!etag_value.empty()) {
2027 // We don't want to switch to WRITE mode if we don't have this block of a
2028 // byte-range request because we may have other parts cached.
2029 custom_request_->extra_headers.SetHeader(
2030 HttpRequestHeaders::kIfRange, etag_value);
2032 custom_request_->extra_headers.SetHeader(
2033 HttpRequestHeaders::kIfNoneMatch, etag_value);
2035 // For byte-range requests, make sure that we use only one way to validate
2037 if (partial_.get() && !partial_->IsCurrentRangeCached())
2041 if (!last_modified_value.empty()) {
2043 custom_request_->extra_headers.SetHeader(
2044 HttpRequestHeaders::kIfRange, last_modified_value);
2046 custom_request_->extra_headers.SetHeader(
2047 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2054 // We just received some headers from the server. We may have asked for a range,
2055 // in which case partial_ has an object. This could be the first network request
2056 // we make to fulfill the original request, or we may be already reading (from
2057 // the net and / or the cache). If we are not expecting a certain response, we
2058 // just bypass the cache for this request (but again, maybe we are reading), and
2059 // delete partial_ (so we are not able to "fix" the headers that we return to
2060 // the user). This results in either a weird response for the caller (we don't
2061 // expect it after all), or maybe a range that was not exactly what it was asked
2064 // If the server is simply telling us that the resource has changed, we delete
2065 // the cached entry and restart the request as the caller intended (by returning
2066 // false from this method). However, we may not be able to do that at any point,
2067 // for instance if we already returned the headers to the user.
2069 // WARNING: Whenever this code returns false, it has to make sure that the next
2070 // time it is called it will return true so that we don't keep retrying the
2072 bool HttpCache::Transaction::ValidatePartialResponse() {
2073 const HttpResponseHeaders* headers = new_response_->headers.get();
2074 int response_code = headers->response_code();
2075 bool partial_response = (response_code == 206);
2076 handling_206_ = false;
2078 if (!entry_ || request_->method != "GET")
2081 if (invalid_range_) {
2082 // We gave up trying to match this request with the stored data. If the
2083 // server is ok with the request, delete the entry, otherwise just ignore
2086 if (partial_response || response_code == 200) {
2087 DoomPartialEntry(true);
2090 if (response_code == 304)
2092 IgnoreRangeRequest();
2097 if (!partial_.get()) {
2098 // We are not expecting 206 but we may have one.
2099 if (partial_response)
2100 IgnoreRangeRequest();
2105 // TODO(rvargas): Do we need to consider other results here?.
2106 bool failure = response_code == 200 || response_code == 416;
2108 if (partial_->IsCurrentRangeCached()) {
2109 // We asked for "If-None-Match: " so a 206 means a new object.
2110 if (partial_response)
2113 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2116 // We asked for "If-Range: " so a 206 means just another range.
2117 if (partial_response && partial_->ResponseHeadersOK(headers)) {
2118 handling_206_ = true;
2122 if (!reading_ && !is_sparse_ && !partial_response) {
2123 // See if we can ignore the fact that we issued a byte range request.
2124 // If the server sends 200, just store it. If it sends an error, redirect
2125 // or something else, we may store the response as long as we didn't have
2126 // anything already stored.
2127 if (response_code == 200 ||
2128 (!truncated_ && response_code != 304 && response_code != 416)) {
2129 // The server is sending something else, and we can save it.
2130 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2137 // 304 is not expected here, but we'll spare the entry (unless it was
2144 // We cannot truncate this entry, it has to be deleted.
2145 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2146 DoomPartialEntry(false);
2148 if (!reading_ && !partial_->IsLastRange()) {
2149 // We'll attempt to issue another network request, this time without us
2150 // messing up the headers.
2151 partial_->RestoreHeaders(&custom_request_->extra_headers);
2156 LOG(WARNING) << "Failed to revalidate partial entry";
2161 IgnoreRangeRequest();
2165 void HttpCache::Transaction::IgnoreRangeRequest() {
2166 // We have a problem. We may or may not be reading already (in which case we
2167 // returned the headers), but we'll just pretend that this request is not
2168 // using the cache and see what happens. Most likely this is the first
2169 // response from the server (it's not changing its mind midway, right?).
2170 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2172 DoneWritingToEntry(mode_ != WRITE);
2173 else if (mode_ & READ && entry_)
2174 cache_->DoneReadingFromEntry(entry_, this);
2176 partial_.reset(NULL);
2181 void HttpCache::Transaction::FailRangeRequest() {
2182 response_ = *new_response_;
2183 partial_->FixResponseHeaders(response_.headers.get(), false);
2186 int HttpCache::Transaction::SetupEntryForRead() {
2188 ResetNetworkTransaction();
2189 if (partial_.get()) {
2190 if (truncated_ || is_sparse_ || !invalid_range_) {
2191 // We are going to return the saved response headers to the caller, so
2192 // we may need to adjust them first.
2193 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2199 cache_->ConvertWriterToReader(entry_);
2202 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2203 next_state_ = STATE_CACHE_READ_METADATA;
2208 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2210 io_buf_len_ = data_len;
2211 next_state_ = STATE_NETWORK_READ;
2215 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2217 io_buf_len_ = data_len;
2218 next_state_ = STATE_CACHE_READ_DATA;
2222 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2223 IOBuffer* data, int data_len,
2224 const CompletionCallback& callback) {
2229 if (!partial_.get() || !data_len) {
2230 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2233 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2238 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2239 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2243 // Do not cache no-store content (unless we are record mode). Do not cache
2244 // content with cert errors either. This is to prevent not reporting net
2245 // errors when loading a resource from the cache. When we load a page over
2246 // HTTPS with a cert error we show an SSL blocking page. If the user clicks
2247 // proceed we reload the resource ignoring the errors. The loaded resource
2248 // is then cached. If that resource is subsequently loaded from the cache,
2249 // no net error is reported (even though the cert status contains the actual
2250 // errors) and no SSL blocking page is shown. An alternative would be to
2251 // reverse-map the cert status to a net error and replay the net error.
2252 if ((cache_->mode() != RECORD &&
2253 response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2254 net::IsCertStatusError(response_.ssl_info.cert_status)) {
2255 DoneWritingToEntry(false);
2256 ReportCacheActionFinish();
2257 if (net_log_.IsLoggingAllEvents())
2258 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2262 // When writing headers, we normally only write the non-transient
2263 // headers; when in record mode, record everything.
2264 bool skip_transient_headers = (cache_->mode() != RECORD);
2267 DCHECK_EQ(200, response_.headers->response_code());
2269 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2270 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2273 io_buf_len_ = data->pickle()->size();
2274 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2275 io_buf_len_, io_callback_, true);
2278 int HttpCache::Transaction::AppendResponseDataToEntry(
2279 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2280 if (!entry_ || !data_len)
2283 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2284 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2288 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2294 cache_->DoneWritingToEntry(entry_, success);
2296 mode_ = NONE; // switch to 'pass through' mode
2299 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2300 DLOG(ERROR) << "ReadData failed: " << result;
2301 const int result_for_histogram = std::max(0, -result);
2303 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2304 result_for_histogram);
2306 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2307 result_for_histogram);
2310 // Avoid using this entry in the future.
2312 cache_->DoomActiveEntry(cache_key_);
2316 DCHECK(!network_trans_.get());
2317 cache_->DoneWithEntry(entry_, this, false);
2321 next_state_ = STATE_GET_BACKEND;
2325 return ERR_CACHE_READ_FAILURE;
2328 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2329 DVLOG(2) << "DoomPartialEntry";
2330 int rv = cache_->DoomEntry(cache_key_, NULL);
2332 cache_->DoneWithEntry(entry_, this, false);
2336 partial_.reset(NULL);
2339 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2340 partial_->OnNetworkReadCompleted(result);
2343 // We need to move on to the next range.
2344 ResetNetworkTransaction();
2345 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2350 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2351 partial_->OnCacheReadCompleted(result);
2353 if (result == 0 && mode_ == READ_WRITE) {
2354 // We need to move on to the next range.
2355 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2356 } else if (result < 0) {
2357 return OnCacheReadError(result, false);
2362 int HttpCache::Transaction::DoRestartPartialRequest() {
2363 // The stored data cannot be used. Get rid of it and restart this request.
2364 // We need to also reset the |truncated_| flag as a new entry is created.
2365 DoomPartialEntry(!range_requested_);
2368 next_state_ = STATE_INIT_ENTRY;
2372 void HttpCache::Transaction::ResetNetworkTransaction() {
2373 DCHECK(!old_network_trans_load_timing_);
2374 DCHECK(network_trans_);
2375 LoadTimingInfo load_timing;
2376 if (network_trans_->GetLoadTimingInfo(&load_timing))
2377 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2378 network_trans_.reset();
2381 // Histogram data from the end of 2010 show the following distribution of
2382 // response headers:
2384 // Content-Length............... 87%
2385 // Date......................... 98%
2386 // Last-Modified................ 49%
2387 // Etag......................... 19%
2388 // Accept-Ranges: bytes......... 25%
2389 // Accept-Ranges: none.......... 0.4%
2390 // Strong Validator............. 50%
2391 // Strong Validator + ranges.... 24%
2392 // Strong Validator + CL........ 49%
2394 bool HttpCache::Transaction::CanResume(bool has_data) {
2395 // Double check that there is something worth keeping.
2396 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2399 if (request_->method != "GET")
2402 // Note that if this is a 206, content-length was already fixed after calling
2403 // PartialData::ResponseHeadersOK().
2404 if (response_.headers->GetContentLength() <= 0 ||
2405 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2406 !response_.headers->HasStrongValidators()) {
2413 void HttpCache::Transaction::OnIOComplete(int result) {
2417 void HttpCache::Transaction::ReportCacheActionStart() {
2418 if (transaction_delegate_)
2419 transaction_delegate_->OnCacheActionStart();
2422 void HttpCache::Transaction::ReportCacheActionFinish() {
2423 if (transaction_delegate_)
2424 transaction_delegate_->OnCacheActionFinish();
2427 void HttpCache::Transaction::ReportNetworkActionStart() {
2428 if (transaction_delegate_)
2429 transaction_delegate_->OnNetworkActionStart();
2432 void HttpCache::Transaction::ReportNetworkActionFinish() {
2433 if (transaction_delegate_)
2434 transaction_delegate_->OnNetworkActionFinish();
2437 void HttpCache::Transaction::UpdateTransactionPattern(
2438 TransactionPattern new_transaction_pattern) {
2439 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2441 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2442 new_transaction_pattern == PATTERN_NOT_COVERED);
2443 transaction_pattern_ = new_transaction_pattern;
2446 void HttpCache::Transaction::RecordHistograms() {
2447 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2448 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2449 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2450 cache_->mode() != NORMAL || request_->method != "GET") {
2453 UMA_HISTOGRAM_ENUMERATION(
2454 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2455 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2457 DCHECK(!range_requested_);
2458 DCHECK(!first_cache_access_since_.is_null());
2460 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2462 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2464 bool did_send_request = !send_request_since_.is_null();
2466 (did_send_request &&
2467 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2468 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2469 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2470 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2471 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2473 if (!did_send_request) {
2474 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2475 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2479 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2480 int before_send_percent =
2481 total_time.ToInternalValue() == 0 ? 0
2482 : before_send_time * 100 / total_time;
2483 DCHECK_LE(0, before_send_percent);
2484 DCHECK_GE(100, before_send_percent);
2486 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2487 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2488 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent);
2490 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2491 // below this comment after we have received initial data.
2492 switch (transaction_pattern_) {
2493 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2494 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2496 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2497 before_send_percent);
2500 case PATTERN_ENTRY_NOT_CACHED: {
2501 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2502 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2503 before_send_percent);
2506 case PATTERN_ENTRY_VALIDATED: {
2507 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2508 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2509 before_send_percent);
2512 case PATTERN_ENTRY_UPDATED: {
2513 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2514 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2515 before_send_percent);