Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blob2346d42b6ea278f8b9cc55eeb06baf9c1fed4641
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"
9 #if defined(OS_POSIX)
10 #include <unistd.h>
11 #endif
13 #include <algorithm>
14 #include <string>
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/format_macros.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/metrics/field_trial.h"
21 #include "base/metrics/histogram.h"
22 #include "base/metrics/sparse_histogram.h"
23 #include "base/rand_util.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_piece.h"
26 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h"
28 #include "base/time/time.h"
29 #include "net/base/completion_callback.h"
30 #include "net/base/io_buffer.h"
31 #include "net/base/load_flags.h"
32 #include "net/base/load_timing_info.h"
33 #include "net/base/net_errors.h"
34 #include "net/base/net_log.h"
35 #include "net/base/upload_data_stream.h"
36 #include "net/cert/cert_status_flags.h"
37 #include "net/disk_cache/disk_cache.h"
38 #include "net/http/disk_based_cert_cache.h"
39 #include "net/http/http_network_session.h"
40 #include "net/http/http_request_info.h"
41 #include "net/http/http_response_headers.h"
42 #include "net/http/http_transaction.h"
43 #include "net/http/http_util.h"
44 #include "net/http/partial_data.h"
45 #include "net/ssl/ssl_cert_request_info.h"
46 #include "net/ssl/ssl_config_service.h"
48 using base::Time;
49 using base::TimeDelta;
50 using base::TimeTicks;
52 namespace {
54 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
55 static const char kFreshnessHeader[] = "Resource-Freshness";
57 // Stores data relevant to the statistics of writing and reading entire
58 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
59 // of certificates in the chain that have pending operations in the
60 // DiskBasedCertCache. |start_time| is the time that the read and write
61 // commands began being issued to the DiskBasedCertCache.
62 // TODO(brandonsalmon): Remove this when it is no longer necessary to
63 // collect data.
64 class SharedChainData : public base::RefCounted<SharedChainData> {
65 public:
66 SharedChainData(int num_ops, TimeTicks start)
67 : num_pending_ops(num_ops), start_time(start) {}
69 int num_pending_ops;
70 TimeTicks start_time;
72 private:
73 friend class base::RefCounted<SharedChainData>;
74 ~SharedChainData() {}
75 DISALLOW_COPY_AND_ASSIGN(SharedChainData);
78 // Used to obtain a cache entry key for an OSCertHandle.
79 // TODO(brandonsalmon): Remove this when cache keys are stored
80 // and no longer have to be recomputed to retrieve the OSCertHandle
81 // from the disk.
82 std::string GetCacheKeyForCert(net::X509Certificate::OSCertHandle cert_handle) {
83 net::SHA1HashValue fingerprint =
84 net::X509Certificate::CalculateFingerprint(cert_handle);
86 return "cert:" +
87 base::HexEncode(fingerprint.data, arraysize(fingerprint.data));
90 // |dist_from_root| indicates the position of the read certificate in the
91 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
92 // whether or not the read certificate was the leaf of the chain.
93 // |shared_chain_data| contains data shared by each certificate in
94 // the chain.
95 void OnCertReadIOComplete(
96 int dist_from_root,
97 bool is_leaf,
98 const scoped_refptr<SharedChainData>& shared_chain_data,
99 net::X509Certificate::OSCertHandle cert_handle) {
100 // If |num_pending_ops| is one, this was the last pending read operation
101 // for this chain of certificates. The total time used to read the chain
102 // can be calculated by subtracting the starting time from Now().
103 shared_chain_data->num_pending_ops--;
104 if (!shared_chain_data->num_pending_ops) {
105 const TimeDelta read_chain_wait =
106 TimeTicks::Now() - shared_chain_data->start_time;
107 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
108 read_chain_wait,
109 base::TimeDelta::FromMilliseconds(1),
110 base::TimeDelta::FromMinutes(10),
111 50);
114 bool success = (cert_handle != NULL);
115 if (is_leaf)
116 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success);
118 if (success)
119 UMA_HISTOGRAM_CUSTOM_COUNTS(
120 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root, 0, 10, 7);
121 else
122 UMA_HISTOGRAM_CUSTOM_COUNTS(
123 "DiskBasedCertCache.CertIoReadFailure", dist_from_root, 0, 10, 7);
126 // |dist_from_root| indicates the position of the written certificate in the
127 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
128 // whether or not the written certificate was the leaf of the chain.
129 // |shared_chain_data| contains data shared by each certificate in
130 // the chain.
131 void OnCertWriteIOComplete(
132 int dist_from_root,
133 bool is_leaf,
134 const scoped_refptr<SharedChainData>& shared_chain_data,
135 const std::string& key) {
136 // If |num_pending_ops| is one, this was the last pending write operation
137 // for this chain of certificates. The total time used to write the chain
138 // can be calculated by subtracting the starting time from Now().
139 shared_chain_data->num_pending_ops--;
140 if (!shared_chain_data->num_pending_ops) {
141 const TimeDelta write_chain_wait =
142 TimeTicks::Now() - shared_chain_data->start_time;
143 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
144 write_chain_wait,
145 base::TimeDelta::FromMilliseconds(1),
146 base::TimeDelta::FromMinutes(10),
147 50);
150 bool success = !key.empty();
151 if (is_leaf)
152 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success);
154 if (success)
155 UMA_HISTOGRAM_CUSTOM_COUNTS(
156 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root, 0, 10, 7);
157 else
158 UMA_HISTOGRAM_CUSTOM_COUNTS(
159 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root, 0, 10, 7);
162 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
163 // a "non-error response" is one with a 2xx (Successful) or 3xx
164 // (Redirection) status code.
165 bool NonErrorResponse(int status_code) {
166 int status_code_range = status_code / 100;
167 return status_code_range == 2 || status_code_range == 3;
170 // Error codes that will be considered indicative of a page being offline/
171 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
172 bool IsOfflineError(int error) {
173 return (error == net::ERR_NAME_NOT_RESOLVED ||
174 error == net::ERR_INTERNET_DISCONNECTED ||
175 error == net::ERR_ADDRESS_UNREACHABLE ||
176 error == net::ERR_CONNECTION_TIMED_OUT);
179 // Enum for UMA, indicating the status (with regard to offline mode) of
180 // a particular request.
181 enum RequestOfflineStatus {
182 // A cache transaction hit in cache (data was present and not stale)
183 // and returned it.
184 OFFLINE_STATUS_FRESH_CACHE,
186 // A network request was required for a cache entry, and it succeeded.
187 OFFLINE_STATUS_NETWORK_SUCCEEDED,
189 // A network request was required for a cache entry, and it failed with
190 // a non-offline error.
191 OFFLINE_STATUS_NETWORK_FAILED,
193 // A network request was required for a cache entry, it failed with an
194 // offline error, and we could serve stale data if
195 // LOAD_FROM_CACHE_IF_OFFLINE was set.
196 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE,
198 // A network request was required for a cache entry, it failed with
199 // an offline error, and there was no servable data in cache (even
200 // stale data).
201 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE,
203 OFFLINE_STATUS_MAX_ENTRIES
206 void RecordOfflineStatus(int load_flags, RequestOfflineStatus status) {
207 // Restrict to main frame to keep statistics close to
208 // "would have shown them something useful if offline mode was enabled".
209 if (load_flags & net::LOAD_MAIN_FRAME) {
210 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status,
211 OFFLINE_STATUS_MAX_ENTRIES);
215 // TODO(rvargas): Remove once we get the data.
216 void RecordVaryHeaderHistogram(const net::HttpResponseInfo* response) {
217 enum VaryType {
218 VARY_NOT_PRESENT,
219 VARY_UA,
220 VARY_OTHER,
221 VARY_MAX
223 VaryType vary = VARY_NOT_PRESENT;
224 if (response->vary_data.is_valid()) {
225 vary = VARY_OTHER;
226 if (response->headers->HasHeaderValue("vary", "user-agent"))
227 vary = VARY_UA;
229 UMA_HISTOGRAM_ENUMERATION("HttpCache.Vary", vary, VARY_MAX);
232 void RecordNoStoreHeaderHistogram(int load_flags,
233 const net::HttpResponseInfo* response) {
234 if (load_flags & net::LOAD_MAIN_FRAME) {
235 UMA_HISTOGRAM_BOOLEAN(
236 "Net.MainFrameNoStore",
237 response->headers->HasHeaderValue("cache-control", "no-store"));
241 } // namespace
243 namespace net {
245 struct HeaderNameAndValue {
246 const char* name;
247 const char* value;
250 // If the request includes one of these request headers, then avoid caching
251 // to avoid getting confused.
252 static const HeaderNameAndValue kPassThroughHeaders[] = {
253 { "if-unmodified-since", NULL }, // causes unexpected 412s
254 { "if-match", NULL }, // causes unexpected 412s
255 { "if-range", NULL },
256 { NULL, NULL }
259 struct ValidationHeaderInfo {
260 const char* request_header_name;
261 const char* related_response_header_name;
264 static const ValidationHeaderInfo kValidationHeaders[] = {
265 { "if-modified-since", "last-modified" },
266 { "if-none-match", "etag" },
269 // If the request includes one of these request headers, then avoid reusing
270 // our cached copy if any.
271 static const HeaderNameAndValue kForceFetchHeaders[] = {
272 { "cache-control", "no-cache" },
273 { "pragma", "no-cache" },
274 { NULL, NULL }
277 // If the request includes one of these request headers, then force our
278 // cached copy (if any) to be revalidated before reusing it.
279 static const HeaderNameAndValue kForceValidateHeaders[] = {
280 { "cache-control", "max-age=0" },
281 { NULL, NULL }
284 static bool HeaderMatches(const HttpRequestHeaders& headers,
285 const HeaderNameAndValue* search) {
286 for (; search->name; ++search) {
287 std::string header_value;
288 if (!headers.GetHeader(search->name, &header_value))
289 continue;
291 if (!search->value)
292 return true;
294 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
295 while (v.GetNext()) {
296 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
297 return true;
300 return false;
303 //-----------------------------------------------------------------------------
305 HttpCache::Transaction::Transaction(
306 RequestPriority priority,
307 HttpCache* cache)
308 : next_state_(STATE_NONE),
309 request_(NULL),
310 priority_(priority),
311 cache_(cache->GetWeakPtr()),
312 entry_(NULL),
313 new_entry_(NULL),
314 new_response_(NULL),
315 mode_(NONE),
316 target_state_(STATE_NONE),
317 reading_(false),
318 invalid_range_(false),
319 truncated_(false),
320 is_sparse_(false),
321 range_requested_(false),
322 handling_206_(false),
323 cache_pending_(false),
324 done_reading_(false),
325 vary_mismatch_(false),
326 couldnt_conditionalize_request_(false),
327 bypass_lock_for_test_(false),
328 io_buf_len_(0),
329 read_offset_(0),
330 effective_load_flags_(0),
331 write_len_(0),
332 transaction_pattern_(PATTERN_UNDEFINED),
333 total_received_bytes_(0),
334 websocket_handshake_stream_base_create_helper_(NULL),
335 weak_factory_(this) {
336 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders ==
337 arraysize(kValidationHeaders),
338 Invalid_number_of_validation_headers);
340 io_callback_ = base::Bind(&Transaction::OnIOComplete,
341 weak_factory_.GetWeakPtr());
344 HttpCache::Transaction::~Transaction() {
345 // We may have to issue another IO, but we should never invoke the callback_
346 // after this point.
347 callback_.Reset();
349 if (cache_) {
350 if (entry_) {
351 bool cancel_request = reading_ && response_.headers.get();
352 if (cancel_request) {
353 if (partial_) {
354 entry_->disk_entry->CancelSparseIO();
355 } else {
356 cancel_request &= (response_.headers->response_code() == 200);
360 cache_->DoneWithEntry(entry_, this, cancel_request);
361 } else if (cache_pending_) {
362 cache_->RemovePendingTransaction(this);
367 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
368 const CompletionCallback& callback) {
369 DCHECK(buf);
370 DCHECK_GT(buf_len, 0);
371 DCHECK(!callback.is_null());
372 if (!cache_.get() || !entry_)
373 return ERR_UNEXPECTED;
375 // We don't need to track this operation for anything.
376 // It could be possible to check if there is something already written and
377 // avoid writing again (it should be the same, right?), but let's allow the
378 // caller to "update" the contents with something new.
379 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
380 callback, true);
383 bool HttpCache::Transaction::AddTruncatedFlag() {
384 DCHECK(mode_ & WRITE || mode_ == NONE);
386 // Don't set the flag for sparse entries.
387 if (partial_.get() && !truncated_)
388 return true;
390 if (!CanResume(true))
391 return false;
393 // We may have received the whole resource already.
394 if (done_reading_)
395 return true;
397 truncated_ = true;
398 target_state_ = STATE_NONE;
399 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
400 DoLoop(OK);
401 return true;
404 LoadState HttpCache::Transaction::GetWriterLoadState() const {
405 if (network_trans_.get())
406 return network_trans_->GetLoadState();
407 if (entry_ || !request_)
408 return LOAD_STATE_IDLE;
409 return LOAD_STATE_WAITING_FOR_CACHE;
412 const BoundNetLog& HttpCache::Transaction::net_log() const {
413 return net_log_;
416 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
417 const CompletionCallback& callback,
418 const BoundNetLog& net_log) {
419 DCHECK(request);
420 DCHECK(!callback.is_null());
422 // Ensure that we only have one asynchronous call at a time.
423 DCHECK(callback_.is_null());
424 DCHECK(!reading_);
425 DCHECK(!network_trans_.get());
426 DCHECK(!entry_);
428 if (!cache_.get())
429 return ERR_UNEXPECTED;
431 SetRequest(net_log, request);
433 // We have to wait until the backend is initialized so we start the SM.
434 next_state_ = STATE_GET_BACKEND;
435 int rv = DoLoop(OK);
437 // Setting this here allows us to check for the existence of a callback_ to
438 // determine if we are still inside Start.
439 if (rv == ERR_IO_PENDING)
440 callback_ = callback;
442 return rv;
445 int HttpCache::Transaction::RestartIgnoringLastError(
446 const CompletionCallback& callback) {
447 DCHECK(!callback.is_null());
449 // Ensure that we only have one asynchronous call at a time.
450 DCHECK(callback_.is_null());
452 if (!cache_.get())
453 return ERR_UNEXPECTED;
455 int rv = RestartNetworkRequest();
457 if (rv == ERR_IO_PENDING)
458 callback_ = callback;
460 return rv;
463 int HttpCache::Transaction::RestartWithCertificate(
464 X509Certificate* client_cert,
465 const CompletionCallback& callback) {
466 DCHECK(!callback.is_null());
468 // Ensure that we only have one asynchronous call at a time.
469 DCHECK(callback_.is_null());
471 if (!cache_.get())
472 return ERR_UNEXPECTED;
474 int rv = RestartNetworkRequestWithCertificate(client_cert);
476 if (rv == ERR_IO_PENDING)
477 callback_ = callback;
479 return rv;
482 int HttpCache::Transaction::RestartWithAuth(
483 const AuthCredentials& credentials,
484 const CompletionCallback& callback) {
485 DCHECK(auth_response_.headers.get());
486 DCHECK(!callback.is_null());
488 // Ensure that we only have one asynchronous call at a time.
489 DCHECK(callback_.is_null());
491 if (!cache_.get())
492 return ERR_UNEXPECTED;
494 // Clear the intermediate response since we are going to start over.
495 auth_response_ = HttpResponseInfo();
497 int rv = RestartNetworkRequestWithAuth(credentials);
499 if (rv == ERR_IO_PENDING)
500 callback_ = callback;
502 return rv;
505 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
506 if (!network_trans_.get())
507 return false;
508 return network_trans_->IsReadyToRestartForAuth();
511 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
512 const CompletionCallback& callback) {
513 DCHECK(buf);
514 DCHECK_GT(buf_len, 0);
515 DCHECK(!callback.is_null());
517 DCHECK(callback_.is_null());
519 if (!cache_.get())
520 return ERR_UNEXPECTED;
522 // If we have an intermediate auth response at this point, then it means the
523 // user wishes to read the network response (the error page). If there is a
524 // previous response in the cache then we should leave it intact.
525 if (auth_response_.headers.get() && mode_ != NONE) {
526 UpdateTransactionPattern(PATTERN_NOT_COVERED);
527 DCHECK(mode_ & WRITE);
528 DoneWritingToEntry(mode_ == READ_WRITE);
529 mode_ = NONE;
532 reading_ = true;
533 int rv;
535 switch (mode_) {
536 case READ_WRITE:
537 DCHECK(partial_.get());
538 if (!network_trans_.get()) {
539 // We are just reading from the cache, but we may be writing later.
540 rv = ReadFromEntry(buf, buf_len);
541 break;
543 case NONE:
544 case WRITE:
545 DCHECK(network_trans_.get());
546 rv = ReadFromNetwork(buf, buf_len);
547 break;
548 case READ:
549 rv = ReadFromEntry(buf, buf_len);
550 break;
551 default:
552 NOTREACHED();
553 rv = ERR_FAILED;
556 if (rv == ERR_IO_PENDING) {
557 DCHECK(callback_.is_null());
558 callback_ = callback;
560 return rv;
563 void HttpCache::Transaction::StopCaching() {
564 // We really don't know where we are now. Hopefully there is no operation in
565 // progress, but nothing really prevents this method to be called after we
566 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
567 // point because we need the state machine for that (and even if we are really
568 // free, that would be an asynchronous operation). In other words, keep the
569 // entry how it is (it will be marked as truncated at destruction), and let
570 // the next piece of code that executes know that we are now reading directly
571 // from the net.
572 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
573 // future request for the resource will be blocked on this one.
574 // Fix this.
575 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
576 !is_sparse_ && !range_requested_) {
577 mode_ = NONE;
581 bool HttpCache::Transaction::GetFullRequestHeaders(
582 HttpRequestHeaders* headers) const {
583 if (network_trans_)
584 return network_trans_->GetFullRequestHeaders(headers);
586 // TODO(ttuttle): Read headers from cache.
587 return false;
590 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
591 int64 total_received_bytes = total_received_bytes_;
592 if (network_trans_)
593 total_received_bytes += network_trans_->GetTotalReceivedBytes();
594 return total_received_bytes;
597 void HttpCache::Transaction::DoneReading() {
598 if (cache_.get() && entry_) {
599 DCHECK_NE(mode_, UPDATE);
600 if (mode_ & WRITE) {
601 DoneWritingToEntry(true);
602 } else if (mode_ & READ) {
603 // It is necessary to check mode_ & READ because it is possible
604 // for mode_ to be NONE and entry_ non-NULL with a write entry
605 // if StopCaching was called.
606 cache_->DoneReadingFromEntry(entry_, this);
607 entry_ = NULL;
612 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
613 // Null headers means we encountered an error or haven't a response yet
614 if (auth_response_.headers.get())
615 return &auth_response_;
616 return (response_.headers.get() || response_.ssl_info.cert.get() ||
617 response_.cert_request_info.get())
618 ? &response_
619 : NULL;
622 LoadState HttpCache::Transaction::GetLoadState() const {
623 LoadState state = GetWriterLoadState();
624 if (state != LOAD_STATE_WAITING_FOR_CACHE)
625 return state;
627 if (cache_.get())
628 return cache_->GetLoadStateForPendingTransaction(this);
630 return LOAD_STATE_IDLE;
633 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
634 if (network_trans_.get())
635 return network_trans_->GetUploadProgress();
636 return final_upload_progress_;
639 void HttpCache::Transaction::SetQuicServerInfo(
640 QuicServerInfo* quic_server_info) {}
642 bool HttpCache::Transaction::GetLoadTimingInfo(
643 LoadTimingInfo* load_timing_info) const {
644 if (network_trans_)
645 return network_trans_->GetLoadTimingInfo(load_timing_info);
647 if (old_network_trans_load_timing_) {
648 *load_timing_info = *old_network_trans_load_timing_;
649 return true;
652 if (first_cache_access_since_.is_null())
653 return false;
655 // If the cache entry was opened, return that time.
656 load_timing_info->send_start = first_cache_access_since_;
657 // This time doesn't make much sense when reading from the cache, so just use
658 // the same time as send_start.
659 load_timing_info->send_end = first_cache_access_since_;
660 return true;
663 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
664 priority_ = priority;
665 if (network_trans_)
666 network_trans_->SetPriority(priority_);
669 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
670 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
671 websocket_handshake_stream_base_create_helper_ = create_helper;
672 if (network_trans_)
673 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
676 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
677 const BeforeNetworkStartCallback& callback) {
678 DCHECK(!network_trans_);
679 before_network_start_callback_ = callback;
682 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
683 const BeforeProxyHeadersSentCallback& callback) {
684 DCHECK(!network_trans_);
685 before_proxy_headers_sent_callback_ = callback;
688 int HttpCache::Transaction::ResumeNetworkStart() {
689 if (network_trans_)
690 return network_trans_->ResumeNetworkStart();
691 return ERR_UNEXPECTED;
694 //-----------------------------------------------------------------------------
696 void HttpCache::Transaction::DoCallback(int rv) {
697 DCHECK(rv != ERR_IO_PENDING);
698 DCHECK(!callback_.is_null());
700 read_buf_ = NULL; // Release the buffer before invoking the callback.
702 // Since Run may result in Read being called, clear callback_ up front.
703 CompletionCallback c = callback_;
704 callback_.Reset();
705 c.Run(rv);
708 int HttpCache::Transaction::HandleResult(int rv) {
709 DCHECK(rv != ERR_IO_PENDING);
710 if (!callback_.is_null())
711 DoCallback(rv);
713 return rv;
716 // A few common patterns: (Foo* means Foo -> FooComplete)
718 // 1. Not-cached entry:
719 // Start():
720 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
721 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
722 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
723 // PartialHeadersReceived
725 // Read():
726 // NetworkRead* -> CacheWriteData*
728 // 2. Cached entry, no validation:
729 // Start():
730 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
731 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
732 // SetupEntryForRead()
734 // Read():
735 // CacheReadData*
737 // 3. Cached entry, validation (304):
738 // Start():
739 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
740 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
741 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
742 // CacheWriteResponse* -> UpdateCachedResponseComplete ->
743 // OverwriteCachedResponse -> PartialHeadersReceived
745 // Read():
746 // CacheReadData*
748 // 4. Cached entry, validation and replace (200):
749 // Start():
750 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
751 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
752 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
753 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
754 // PartialHeadersReceived
756 // Read():
757 // NetworkRead* -> CacheWriteData*
759 // 5. Sparse entry, partially cached, byte range request:
760 // Start():
761 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
762 // -> BeginPartialCacheValidation() -> CacheQueryData* ->
763 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
764 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
765 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
766 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
767 // PartialHeadersReceived
769 // Read() 1:
770 // NetworkRead* -> CacheWriteData*
772 // Read() 2:
773 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
774 // CompletePartialCacheValidation -> CacheReadData* ->
776 // Read() 3:
777 // CacheReadData* -> StartPartialCacheValidation ->
778 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
779 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
780 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
782 // 6. HEAD. Not-cached entry:
783 // Pass through. Don't save a HEAD by itself.
784 // Start():
785 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
787 // 7. HEAD. Cached entry, no validation:
788 // Start():
789 // The same flow as for a GET request (example #2)
791 // Read():
792 // CacheReadData (returns 0)
794 // 8. HEAD. Cached entry, validation (304):
795 // The request updates the stored headers.
796 // Start(): Same as for a GET request (example #3)
798 // Read():
799 // CacheReadData (returns 0)
801 // 9. HEAD. Cached entry, validation and replace (200):
802 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
803 // itself.
804 // Start():
805 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
806 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
807 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse
809 // 10. HEAD. Sparse entry, partially cached:
810 // Serve the request from the cache, as long as it doesn't require
811 // revalidation. Ignore missing ranges when deciding to revalidate. If the
812 // entry requires revalidation, ignore the whole request and go to full pass
813 // through (the result of the HEAD request will NOT update the entry).
815 // Start(): Basically the same as example 7, as we never create a partial_
816 // object for this request.
818 int HttpCache::Transaction::DoLoop(int result) {
819 DCHECK(next_state_ != STATE_NONE);
821 int rv = result;
822 do {
823 State state = next_state_;
824 next_state_ = STATE_NONE;
825 switch (state) {
826 case STATE_GET_BACKEND:
827 DCHECK_EQ(OK, rv);
828 rv = DoGetBackend();
829 break;
830 case STATE_GET_BACKEND_COMPLETE:
831 rv = DoGetBackendComplete(rv);
832 break;
833 case STATE_SEND_REQUEST:
834 DCHECK_EQ(OK, rv);
835 rv = DoSendRequest();
836 break;
837 case STATE_SEND_REQUEST_COMPLETE:
838 rv = DoSendRequestComplete(rv);
839 break;
840 case STATE_SUCCESSFUL_SEND_REQUEST:
841 DCHECK_EQ(OK, rv);
842 rv = DoSuccessfulSendRequest();
843 break;
844 case STATE_NETWORK_READ:
845 DCHECK_EQ(OK, rv);
846 rv = DoNetworkRead();
847 break;
848 case STATE_NETWORK_READ_COMPLETE:
849 rv = DoNetworkReadComplete(rv);
850 break;
851 case STATE_INIT_ENTRY:
852 DCHECK_EQ(OK, rv);
853 rv = DoInitEntry();
854 break;
855 case STATE_OPEN_ENTRY:
856 DCHECK_EQ(OK, rv);
857 rv = DoOpenEntry();
858 break;
859 case STATE_OPEN_ENTRY_COMPLETE:
860 rv = DoOpenEntryComplete(rv);
861 break;
862 case STATE_CREATE_ENTRY:
863 DCHECK_EQ(OK, rv);
864 rv = DoCreateEntry();
865 break;
866 case STATE_CREATE_ENTRY_COMPLETE:
867 rv = DoCreateEntryComplete(rv);
868 break;
869 case STATE_DOOM_ENTRY:
870 DCHECK_EQ(OK, rv);
871 rv = DoDoomEntry();
872 break;
873 case STATE_DOOM_ENTRY_COMPLETE:
874 rv = DoDoomEntryComplete(rv);
875 break;
876 case STATE_ADD_TO_ENTRY:
877 DCHECK_EQ(OK, rv);
878 rv = DoAddToEntry();
879 break;
880 case STATE_ADD_TO_ENTRY_COMPLETE:
881 rv = DoAddToEntryComplete(rv);
882 break;
883 case STATE_START_PARTIAL_CACHE_VALIDATION:
884 DCHECK_EQ(OK, rv);
885 rv = DoStartPartialCacheValidation();
886 break;
887 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
888 rv = DoCompletePartialCacheValidation(rv);
889 break;
890 case STATE_UPDATE_CACHED_RESPONSE:
891 DCHECK_EQ(OK, rv);
892 rv = DoUpdateCachedResponse();
893 break;
894 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
895 rv = DoUpdateCachedResponseComplete(rv);
896 break;
897 case STATE_OVERWRITE_CACHED_RESPONSE:
898 DCHECK_EQ(OK, rv);
899 rv = DoOverwriteCachedResponse();
900 break;
901 case STATE_TRUNCATE_CACHED_DATA:
902 DCHECK_EQ(OK, rv);
903 rv = DoTruncateCachedData();
904 break;
905 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
906 rv = DoTruncateCachedDataComplete(rv);
907 break;
908 case STATE_TRUNCATE_CACHED_METADATA:
909 DCHECK_EQ(OK, rv);
910 rv = DoTruncateCachedMetadata();
911 break;
912 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
913 rv = DoTruncateCachedMetadataComplete(rv);
914 break;
915 case STATE_PARTIAL_HEADERS_RECEIVED:
916 DCHECK_EQ(OK, rv);
917 rv = DoPartialHeadersReceived();
918 break;
919 case STATE_CACHE_READ_RESPONSE:
920 DCHECK_EQ(OK, rv);
921 rv = DoCacheReadResponse();
922 break;
923 case STATE_CACHE_READ_RESPONSE_COMPLETE:
924 rv = DoCacheReadResponseComplete(rv);
925 break;
926 case STATE_CACHE_WRITE_RESPONSE:
927 DCHECK_EQ(OK, rv);
928 rv = DoCacheWriteResponse();
929 break;
930 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
931 DCHECK_EQ(OK, rv);
932 rv = DoCacheWriteTruncatedResponse();
933 break;
934 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
935 rv = DoCacheWriteResponseComplete(rv);
936 break;
937 case STATE_CACHE_READ_METADATA:
938 DCHECK_EQ(OK, rv);
939 rv = DoCacheReadMetadata();
940 break;
941 case STATE_CACHE_READ_METADATA_COMPLETE:
942 rv = DoCacheReadMetadataComplete(rv);
943 break;
944 case STATE_CACHE_QUERY_DATA:
945 DCHECK_EQ(OK, rv);
946 rv = DoCacheQueryData();
947 break;
948 case STATE_CACHE_QUERY_DATA_COMPLETE:
949 rv = DoCacheQueryDataComplete(rv);
950 break;
951 case STATE_CACHE_READ_DATA:
952 DCHECK_EQ(OK, rv);
953 rv = DoCacheReadData();
954 break;
955 case STATE_CACHE_READ_DATA_COMPLETE:
956 rv = DoCacheReadDataComplete(rv);
957 break;
958 case STATE_CACHE_WRITE_DATA:
959 rv = DoCacheWriteData(rv);
960 break;
961 case STATE_CACHE_WRITE_DATA_COMPLETE:
962 rv = DoCacheWriteDataComplete(rv);
963 break;
964 default:
965 NOTREACHED() << "bad state";
966 rv = ERR_FAILED;
967 break;
969 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
971 if (rv != ERR_IO_PENDING)
972 HandleResult(rv);
974 return rv;
977 int HttpCache::Transaction::DoGetBackend() {
978 cache_pending_ = true;
979 next_state_ = STATE_GET_BACKEND_COMPLETE;
980 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
981 return cache_->GetBackendForTransaction(this);
984 int HttpCache::Transaction::DoGetBackendComplete(int result) {
985 DCHECK(result == OK || result == ERR_FAILED);
986 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
987 result);
988 cache_pending_ = false;
990 if (!ShouldPassThrough()) {
991 cache_key_ = cache_->GenerateCacheKey(request_);
993 // Requested cache access mode.
994 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
995 mode_ = READ;
996 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
997 mode_ = WRITE;
998 } else {
999 mode_ = READ_WRITE;
1002 // Downgrade to UPDATE if the request has been externally conditionalized.
1003 if (external_validation_.initialized) {
1004 if (mode_ & WRITE) {
1005 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1006 // in case READ was off).
1007 mode_ = UPDATE;
1008 } else {
1009 mode_ = NONE;
1014 // Use PUT and DELETE only to invalidate existing stored entries.
1015 if ((request_->method == "PUT" || request_->method == "DELETE") &&
1016 mode_ != READ_WRITE && mode_ != WRITE) {
1017 mode_ = NONE;
1020 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1021 // transaction behaves the same for GET and HEAD requests at this point: if it
1022 // was not modified, the entry is updated and a response is not returned from
1023 // the cache. If we receive 200, it doesn't matter if there was a validation
1024 // header or not.
1025 if (request_->method == "HEAD" && mode_ == WRITE)
1026 mode_ = NONE;
1028 // If must use cache, then we must fail. This can happen for back/forward
1029 // navigations to a page generated via a form post.
1030 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
1031 return ERR_CACHE_MISS;
1033 if (mode_ == NONE) {
1034 if (partial_.get()) {
1035 partial_->RestoreHeaders(&custom_request_->extra_headers);
1036 partial_.reset();
1038 next_state_ = STATE_SEND_REQUEST;
1039 } else {
1040 next_state_ = STATE_INIT_ENTRY;
1043 // This is only set if we have something to do with the response.
1044 range_requested_ = (partial_.get() != NULL);
1046 return OK;
1049 int HttpCache::Transaction::DoSendRequest() {
1050 DCHECK(mode_ & WRITE || mode_ == NONE);
1051 DCHECK(!network_trans_.get());
1053 send_request_since_ = TimeTicks::Now();
1055 // Create a network transaction.
1056 int rv = cache_->network_layer_->CreateTransaction(priority_,
1057 &network_trans_);
1058 if (rv != OK)
1059 return rv;
1060 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1061 network_trans_->SetBeforeProxyHeadersSentCallback(
1062 before_proxy_headers_sent_callback_);
1064 // Old load timing information, if any, is now obsolete.
1065 old_network_trans_load_timing_.reset();
1067 if (websocket_handshake_stream_base_create_helper_)
1068 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1069 websocket_handshake_stream_base_create_helper_);
1071 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1072 rv = network_trans_->Start(request_, io_callback_, net_log_);
1073 return rv;
1076 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1077 if (!cache_.get())
1078 return ERR_UNEXPECTED;
1080 // If requested, and we have a readable cache entry, and we have
1081 // an error indicating that we're offline as opposed to in contact
1082 // with a bad server, read from cache anyway.
1083 if (IsOfflineError(result)) {
1084 if (mode_ == READ_WRITE && entry_ && !partial_) {
1085 RecordOfflineStatus(effective_load_flags_,
1086 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE);
1087 if (effective_load_flags_ & LOAD_FROM_CACHE_IF_OFFLINE) {
1088 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1089 response_.server_data_unavailable = true;
1090 return SetupEntryForRead();
1092 } else {
1093 RecordOfflineStatus(effective_load_flags_,
1094 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE);
1096 } else {
1097 RecordOfflineStatus(effective_load_flags_,
1098 (result == OK ? OFFLINE_STATUS_NETWORK_SUCCEEDED :
1099 OFFLINE_STATUS_NETWORK_FAILED));
1102 // If we tried to conditionalize the request and failed, we know
1103 // we won't be reading from the cache after this point.
1104 if (couldnt_conditionalize_request_)
1105 mode_ = WRITE;
1107 if (result == OK) {
1108 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
1109 return OK;
1112 // Do not record requests that have network errors or restarts.
1113 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1114 if (IsCertificateError(result)) {
1115 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1116 // If we get a certificate error, then there is a certificate in ssl_info,
1117 // so GetResponseInfo() should never return NULL here.
1118 DCHECK(response);
1119 response_.ssl_info = response->ssl_info;
1120 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1121 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1122 DCHECK(response);
1123 response_.cert_request_info = response->cert_request_info;
1124 } else if (response_.was_cached) {
1125 DoneWritingToEntry(true);
1127 return result;
1130 // We received the response headers and there is no error.
1131 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1132 DCHECK(!new_response_);
1133 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1134 bool authentication_failure = false;
1136 if (new_response->headers->response_code() == 401 ||
1137 new_response->headers->response_code() == 407) {
1138 auth_response_ = *new_response;
1139 if (!reading_)
1140 return OK;
1142 // We initiated a second request the caller doesn't know about. We should be
1143 // able to authenticate this request because we should have authenticated
1144 // this URL moments ago.
1145 if (IsReadyToRestartForAuth()) {
1146 DCHECK(!response_.auth_challenge.get());
1147 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1148 // In theory we should check to see if there are new cookies, but there
1149 // is no way to do that from here.
1150 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1153 // We have to perform cleanup at this point so that at least the next
1154 // request can succeed.
1155 authentication_failure = true;
1156 if (entry_)
1157 DoomPartialEntry(false);
1158 mode_ = NONE;
1159 partial_.reset();
1162 new_response_ = new_response;
1163 if (authentication_failure ||
1164 (!ValidatePartialResponse() && !auth_response_.headers.get())) {
1165 // Something went wrong with this request and we have to restart it.
1166 // If we have an authentication response, we are exposed to weird things
1167 // hapenning if the user cancels the authentication before we receive
1168 // the new response.
1169 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1170 response_ = HttpResponseInfo();
1171 ResetNetworkTransaction();
1172 new_response_ = NULL;
1173 next_state_ = STATE_SEND_REQUEST;
1174 return OK;
1177 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1178 // We have stored the full entry, but it changed and the server is
1179 // sending a range. We have to delete the old entry.
1180 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1181 DoneWritingToEntry(false);
1184 if (mode_ == WRITE &&
1185 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1186 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1189 if (mode_ == WRITE &&
1190 (request_->method == "PUT" || request_->method == "DELETE")) {
1191 if (NonErrorResponse(new_response->headers->response_code())) {
1192 int ret = cache_->DoomEntry(cache_key_, NULL);
1193 DCHECK_EQ(OK, ret);
1195 cache_->DoneWritingToEntry(entry_, true);
1196 entry_ = NULL;
1197 mode_ = NONE;
1200 if (request_->method == "POST" &&
1201 NonErrorResponse(new_response->headers->response_code())) {
1202 cache_->DoomMainEntryForUrl(request_->url);
1205 RecordVaryHeaderHistogram(new_response);
1206 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1208 if (new_response_->headers->response_code() == 416 &&
1209 (request_->method == "GET" || request_->method == "POST")) {
1210 // If there is an active entry it may be destroyed with this transaction.
1211 response_ = *new_response_;
1212 return OK;
1215 // Are we expecting a response to a conditional query?
1216 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1217 if (new_response->headers->response_code() == 304 || handling_206_) {
1218 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1219 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1220 return OK;
1222 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1223 mode_ = WRITE;
1226 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1227 return OK;
1230 int HttpCache::Transaction::DoNetworkRead() {
1231 next_state_ = STATE_NETWORK_READ_COMPLETE;
1232 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1235 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1236 DCHECK(mode_ & WRITE || mode_ == NONE);
1238 if (!cache_.get())
1239 return ERR_UNEXPECTED;
1241 // If there is an error or we aren't saving the data, we are done; just wait
1242 // until the destructor runs to see if we can keep the data.
1243 if (mode_ == NONE || result < 0)
1244 return result;
1246 next_state_ = STATE_CACHE_WRITE_DATA;
1247 return result;
1250 int HttpCache::Transaction::DoInitEntry() {
1251 DCHECK(!new_entry_);
1253 if (!cache_.get())
1254 return ERR_UNEXPECTED;
1256 if (mode_ == WRITE) {
1257 next_state_ = STATE_DOOM_ENTRY;
1258 return OK;
1261 next_state_ = STATE_OPEN_ENTRY;
1262 return OK;
1265 int HttpCache::Transaction::DoOpenEntry() {
1266 DCHECK(!new_entry_);
1267 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1268 cache_pending_ = true;
1269 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1270 first_cache_access_since_ = TimeTicks::Now();
1271 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1274 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1275 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1276 // OK, otherwise the cache will end up with an active entry without any
1277 // transaction attached.
1278 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1279 cache_pending_ = false;
1280 if (result == OK) {
1281 next_state_ = STATE_ADD_TO_ENTRY;
1282 return OK;
1285 if (result == ERR_CACHE_RACE) {
1286 next_state_ = STATE_INIT_ENTRY;
1287 return OK;
1290 if (request_->method == "PUT" || request_->method == "DELETE" ||
1291 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1292 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1293 mode_ = NONE;
1294 next_state_ = STATE_SEND_REQUEST;
1295 return OK;
1298 if (mode_ == READ_WRITE) {
1299 mode_ = WRITE;
1300 next_state_ = STATE_CREATE_ENTRY;
1301 return OK;
1303 if (mode_ == UPDATE) {
1304 // There is no cache entry to update; proceed without caching.
1305 mode_ = NONE;
1306 next_state_ = STATE_SEND_REQUEST;
1307 return OK;
1309 if (cache_->mode() == PLAYBACK)
1310 DVLOG(1) << "Playback Cache Miss: " << request_->url;
1312 // The entry does not exist, and we are not permitted to create a new entry,
1313 // so we must fail.
1314 return ERR_CACHE_MISS;
1317 int HttpCache::Transaction::DoCreateEntry() {
1318 DCHECK(!new_entry_);
1319 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1320 cache_pending_ = true;
1321 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1322 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1325 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1326 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1327 // OK, otherwise the cache will end up with an active entry without any
1328 // transaction attached.
1329 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1330 result);
1331 cache_pending_ = false;
1332 next_state_ = STATE_ADD_TO_ENTRY;
1334 if (result == ERR_CACHE_RACE) {
1335 next_state_ = STATE_INIT_ENTRY;
1336 return OK;
1339 if (result == OK) {
1340 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", false);
1341 } else {
1342 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", true);
1343 // We have a race here: Maybe we failed to open the entry and decided to
1344 // create one, but by the time we called create, another transaction already
1345 // created the entry. If we want to eliminate this issue, we need an atomic
1346 // OpenOrCreate() method exposed by the disk cache.
1347 DLOG(WARNING) << "Unable to create cache entry";
1348 mode_ = NONE;
1349 if (partial_.get())
1350 partial_->RestoreHeaders(&custom_request_->extra_headers);
1351 next_state_ = STATE_SEND_REQUEST;
1353 return OK;
1356 int HttpCache::Transaction::DoDoomEntry() {
1357 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1358 cache_pending_ = true;
1359 if (first_cache_access_since_.is_null())
1360 first_cache_access_since_ = TimeTicks::Now();
1361 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1362 return cache_->DoomEntry(cache_key_, this);
1365 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1366 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1367 next_state_ = STATE_CREATE_ENTRY;
1368 cache_pending_ = false;
1369 if (result == ERR_CACHE_RACE)
1370 next_state_ = STATE_INIT_ENTRY;
1371 return OK;
1374 int HttpCache::Transaction::DoAddToEntry() {
1375 DCHECK(new_entry_);
1376 cache_pending_ = true;
1377 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1378 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1379 DCHECK(entry_lock_waiting_since_.is_null());
1380 entry_lock_waiting_since_ = TimeTicks::Now();
1381 int rv = cache_->AddTransactionToEntry(new_entry_, this);
1382 if (rv == ERR_IO_PENDING) {
1383 if (bypass_lock_for_test_) {
1384 OnAddToEntryTimeout(entry_lock_waiting_since_);
1385 } else {
1386 int timeout_secs = 20;
1387 if (partial_ && new_entry_->writer &&
1388 new_entry_->writer->range_requested_) {
1389 // Immediately timeout and bypass the cache if we're a range request and
1390 // we're blocked by the reader/writer lock. Doing so eliminates a long
1391 // running issue, http://crbug.com/31014, where two of the same media
1392 // resources could not be played back simultaneously due to one locking
1393 // the cache entry until the entire video was downloaded.
1395 // Bypassing the cache is not ideal, as we are now ignoring the cache
1396 // entirely for all range requests to a resource beyond the first. This
1397 // is however a much more succinct solution than the alternatives, which
1398 // would require somewhat significant changes to the http caching logic.
1399 timeout_secs = 0;
1401 base::MessageLoop::current()->PostDelayedTask(
1402 FROM_HERE,
1403 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout,
1404 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_),
1405 TimeDelta::FromSeconds(timeout_secs));
1408 return rv;
1411 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1412 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1413 result);
1414 const TimeDelta entry_lock_wait =
1415 TimeTicks::Now() - entry_lock_waiting_since_;
1416 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1418 entry_lock_waiting_since_ = TimeTicks();
1419 DCHECK(new_entry_);
1420 cache_pending_ = false;
1422 if (result == OK)
1423 entry_ = new_entry_;
1425 // If there is a failure, the cache should have taken care of new_entry_.
1426 new_entry_ = NULL;
1428 if (result == ERR_CACHE_RACE) {
1429 next_state_ = STATE_INIT_ENTRY;
1430 return OK;
1433 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1434 // The cache is busy, bypass it for this transaction.
1435 mode_ = NONE;
1436 next_state_ = STATE_SEND_REQUEST;
1437 if (partial_) {
1438 partial_->RestoreHeaders(&custom_request_->extra_headers);
1439 partial_.reset();
1441 return OK;
1444 if (result != OK) {
1445 NOTREACHED();
1446 return result;
1449 if (mode_ == WRITE) {
1450 if (partial_.get())
1451 partial_->RestoreHeaders(&custom_request_->extra_headers);
1452 next_state_ = STATE_SEND_REQUEST;
1453 } else {
1454 // We have to read the headers from the cached entry.
1455 DCHECK(mode_ & READ_META);
1456 next_state_ = STATE_CACHE_READ_RESPONSE;
1458 return OK;
1461 // We may end up here multiple times for a given request.
1462 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1463 if (mode_ == NONE)
1464 return OK;
1466 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1467 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1470 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1471 if (!result) {
1472 // This is the end of the request.
1473 if (mode_ & WRITE) {
1474 DoneWritingToEntry(true);
1475 } else {
1476 cache_->DoneReadingFromEntry(entry_, this);
1477 entry_ = NULL;
1479 return result;
1482 if (result < 0)
1483 return result;
1485 partial_->PrepareCacheValidation(entry_->disk_entry,
1486 &custom_request_->extra_headers);
1488 if (reading_ && partial_->IsCurrentRangeCached()) {
1489 next_state_ = STATE_CACHE_READ_DATA;
1490 return OK;
1493 return BeginCacheValidation();
1496 // We received 304 or 206 and we want to update the cached response headers.
1497 int HttpCache::Transaction::DoUpdateCachedResponse() {
1498 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1499 int rv = OK;
1500 // Update cached response based on headers in new_response.
1501 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1502 response_.headers->Update(*new_response_->headers.get());
1503 response_.response_time = new_response_->response_time;
1504 response_.request_time = new_response_->request_time;
1505 response_.network_accessed = new_response_->network_accessed;
1507 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1508 if (!entry_->doomed) {
1509 int ret = cache_->DoomEntry(cache_key_, NULL);
1510 DCHECK_EQ(OK, ret);
1512 } else {
1513 // If we are already reading, we already updated the headers for this
1514 // request; doing it again will change Content-Length.
1515 if (!reading_) {
1516 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1517 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1518 rv = OK;
1521 return rv;
1524 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1525 if (mode_ == UPDATE) {
1526 DCHECK(!handling_206_);
1527 // We got a "not modified" response and already updated the corresponding
1528 // cache entry above.
1530 // By closing the cached entry now, we make sure that the 304 rather than
1531 // the cached 200 response, is what will be returned to the user.
1532 DoneWritingToEntry(true);
1533 } else if (entry_ && !handling_206_) {
1534 DCHECK_EQ(READ_WRITE, mode_);
1535 if (!partial_.get() || partial_->IsLastRange()) {
1536 cache_->ConvertWriterToReader(entry_);
1537 mode_ = READ;
1539 // We no longer need the network transaction, so destroy it.
1540 final_upload_progress_ = network_trans_->GetUploadProgress();
1541 ResetNetworkTransaction();
1542 } else if (entry_ && handling_206_ && truncated_ &&
1543 partial_->initial_validation()) {
1544 // We just finished the validation of a truncated entry, and the server
1545 // is willing to resume the operation. Now we go back and start serving
1546 // the first part to the user.
1547 ResetNetworkTransaction();
1548 new_response_ = NULL;
1549 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1550 partial_->SetRangeToStartDownload();
1551 return OK;
1553 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1554 return OK;
1557 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1558 if (mode_ & READ) {
1559 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1560 return OK;
1563 // We change the value of Content-Length for partial content.
1564 if (handling_206_ && partial_.get())
1565 partial_->FixContentLength(new_response_->headers.get());
1567 response_ = *new_response_;
1569 if (request_->method == "HEAD") {
1570 // This response is replacing the cached one.
1571 DoneWritingToEntry(false);
1572 mode_ = NONE;
1573 new_response_ = NULL;
1574 return OK;
1577 if (handling_206_ && !CanResume(false)) {
1578 // There is no point in storing this resource because it will never be used.
1579 DoneWritingToEntry(false);
1580 if (partial_.get())
1581 partial_->FixResponseHeaders(response_.headers.get(), true);
1582 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1583 return OK;
1586 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1587 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1588 STATE_CACHE_WRITE_RESPONSE;
1589 return OK;
1592 int HttpCache::Transaction::DoTruncateCachedData() {
1593 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1594 if (!entry_)
1595 return OK;
1596 if (net_log_.IsLogging())
1597 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1598 // Truncate the stream.
1599 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1602 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1603 if (entry_) {
1604 if (net_log_.IsLogging()) {
1605 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1606 result);
1610 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1611 return OK;
1614 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1615 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1616 if (!entry_)
1617 return OK;
1619 if (net_log_.IsLogging())
1620 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1621 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1624 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1625 if (entry_) {
1626 if (net_log_.IsLogging()) {
1627 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1628 result);
1632 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1633 return OK;
1636 int HttpCache::Transaction::DoPartialHeadersReceived() {
1637 new_response_ = NULL;
1638 if (entry_ && !partial_.get() &&
1639 entry_->disk_entry->GetDataSize(kMetadataIndex))
1640 next_state_ = STATE_CACHE_READ_METADATA;
1642 if (!partial_.get())
1643 return OK;
1645 if (reading_) {
1646 if (network_trans_.get()) {
1647 next_state_ = STATE_NETWORK_READ;
1648 } else {
1649 next_state_ = STATE_CACHE_READ_DATA;
1651 } else if (mode_ != NONE) {
1652 // We are about to return the headers for a byte-range request to the user,
1653 // so let's fix them.
1654 partial_->FixResponseHeaders(response_.headers.get(), true);
1656 return OK;
1659 int HttpCache::Transaction::DoCacheReadResponse() {
1660 DCHECK(entry_);
1661 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1663 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1664 read_buf_ = new IOBuffer(io_buf_len_);
1666 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1667 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1668 io_buf_len_, io_callback_);
1671 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1672 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1673 if (result != io_buf_len_ ||
1674 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1675 &response_, &truncated_)) {
1676 return OnCacheReadError(result, true);
1679 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1680 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1681 ReadCertChain();
1683 // Some resources may have slipped in as truncated when they're not.
1684 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1685 if (response_.headers->GetContentLength() == current_size)
1686 truncated_ = false;
1688 // We now have access to the cache entry.
1690 // o if we are a reader for the transaction, then we can start reading the
1691 // cache entry.
1693 // o if we can read or write, then we should check if the cache entry needs
1694 // to be validated and then issue a network request if needed or just read
1695 // from the cache if the cache entry is already valid.
1697 // o if we are set to UPDATE, then we are handling an externally
1698 // conditionalized request (if-modified-since / if-none-match). We check
1699 // if the request headers define a validation request.
1701 switch (mode_) {
1702 case READ:
1703 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1704 result = BeginCacheRead();
1705 break;
1706 case READ_WRITE:
1707 result = BeginPartialCacheValidation();
1708 break;
1709 case UPDATE:
1710 result = BeginExternallyConditionalizedRequest();
1711 break;
1712 case WRITE:
1713 default:
1714 NOTREACHED();
1715 result = ERR_FAILED;
1717 return result;
1720 int HttpCache::Transaction::DoCacheWriteResponse() {
1721 if (entry_) {
1722 if (net_log_.IsLogging())
1723 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1725 return WriteResponseInfoToEntry(false);
1728 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1729 if (entry_) {
1730 if (net_log_.IsLogging())
1731 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1733 return WriteResponseInfoToEntry(true);
1736 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1737 next_state_ = target_state_;
1738 target_state_ = STATE_NONE;
1739 if (!entry_)
1740 return OK;
1741 if (net_log_.IsLogging()) {
1742 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1743 result);
1746 // Balance the AddRef from WriteResponseInfoToEntry.
1747 if (result != io_buf_len_) {
1748 DLOG(ERROR) << "failed to write response info to cache";
1749 DoneWritingToEntry(false);
1751 return OK;
1754 int HttpCache::Transaction::DoCacheReadMetadata() {
1755 DCHECK(entry_);
1756 DCHECK(!response_.metadata.get());
1757 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1759 response_.metadata =
1760 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1762 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1763 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1764 response_.metadata.get(),
1765 response_.metadata->size(),
1766 io_callback_);
1769 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1770 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1771 if (result != response_.metadata->size())
1772 return OnCacheReadError(result, false);
1773 return OK;
1776 int HttpCache::Transaction::DoCacheQueryData() {
1777 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1778 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1781 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1782 if (result == ERR_NOT_IMPLEMENTED) {
1783 // Restart the request overwriting the cache entry.
1784 // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1785 // supports Sparse IO.
1786 return DoRestartPartialRequest();
1788 DCHECK_EQ(OK, result);
1789 if (!cache_.get())
1790 return ERR_UNEXPECTED;
1792 return ValidateEntryHeadersAndContinue();
1795 int HttpCache::Transaction::DoCacheReadData() {
1796 DCHECK(entry_);
1797 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1799 if (net_log_.IsLogging())
1800 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1801 if (partial_.get()) {
1802 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1803 io_callback_);
1806 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1807 read_buf_.get(), io_buf_len_,
1808 io_callback_);
1811 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1812 if (net_log_.IsLogging()) {
1813 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1814 result);
1817 if (!cache_.get())
1818 return ERR_UNEXPECTED;
1820 if (partial_.get()) {
1821 // Partial requests are confusing to report in histograms because they may
1822 // have multiple underlying requests.
1823 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1824 return DoPartialCacheReadCompleted(result);
1827 if (result > 0) {
1828 read_offset_ += result;
1829 } else if (result == 0) { // End of file.
1830 RecordHistograms();
1831 cache_->DoneReadingFromEntry(entry_, this);
1832 entry_ = NULL;
1833 } else {
1834 return OnCacheReadError(result, false);
1836 return result;
1839 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1840 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1841 write_len_ = num_bytes;
1842 if (entry_) {
1843 if (net_log_.IsLogging())
1844 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1847 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1850 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1851 if (entry_) {
1852 if (net_log_.IsLogging()) {
1853 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1854 result);
1857 // Balance the AddRef from DoCacheWriteData.
1858 if (!cache_.get())
1859 return ERR_UNEXPECTED;
1861 if (result != write_len_) {
1862 DLOG(ERROR) << "failed to write response data to cache";
1863 DoneWritingToEntry(false);
1865 // We want to ignore errors writing to disk and just keep reading from
1866 // the network.
1867 result = write_len_;
1868 } else if (!done_reading_ && entry_) {
1869 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1870 int64 body_size = response_.headers->GetContentLength();
1871 if (body_size >= 0 && body_size <= current_size)
1872 done_reading_ = true;
1875 if (partial_.get()) {
1876 // This may be the last request.
1877 if (!(result == 0 && !truncated_ &&
1878 (partial_->IsLastRange() || mode_ == WRITE)))
1879 return DoPartialNetworkReadCompleted(result);
1882 if (result == 0) {
1883 // End of file. This may be the result of a connection problem so see if we
1884 // have to keep the entry around to be flagged as truncated later on.
1885 if (done_reading_ || !entry_ || partial_.get() ||
1886 response_.headers->GetContentLength() <= 0)
1887 DoneWritingToEntry(true);
1890 return result;
1893 //-----------------------------------------------------------------------------
1895 void HttpCache::Transaction::ReadCertChain() {
1896 std::string key =
1897 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
1898 const X509Certificate::OSCertHandles& intermediates =
1899 response_.ssl_info.cert->GetIntermediateCertificates();
1900 int dist_from_root = intermediates.size();
1902 scoped_refptr<SharedChainData> shared_chain_data(
1903 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1904 cache_->cert_cache()->GetCertificate(key,
1905 base::Bind(&OnCertReadIOComplete,
1906 dist_from_root,
1907 true /* is leaf */,
1908 shared_chain_data));
1910 for (X509Certificate::OSCertHandles::const_iterator it =
1911 intermediates.begin();
1912 it != intermediates.end();
1913 ++it) {
1914 --dist_from_root;
1915 key = GetCacheKeyForCert(*it);
1916 cache_->cert_cache()->GetCertificate(key,
1917 base::Bind(&OnCertReadIOComplete,
1918 dist_from_root,
1919 false /* is not leaf */,
1920 shared_chain_data));
1922 DCHECK_EQ(0, dist_from_root);
1925 void HttpCache::Transaction::WriteCertChain() {
1926 const X509Certificate::OSCertHandles& intermediates =
1927 response_.ssl_info.cert->GetIntermediateCertificates();
1928 int dist_from_root = intermediates.size();
1930 scoped_refptr<SharedChainData> shared_chain_data(
1931 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1932 cache_->cert_cache()->SetCertificate(
1933 response_.ssl_info.cert->os_cert_handle(),
1934 base::Bind(&OnCertWriteIOComplete,
1935 dist_from_root,
1936 true /* is leaf */,
1937 shared_chain_data));
1938 for (X509Certificate::OSCertHandles::const_iterator it =
1939 intermediates.begin();
1940 it != intermediates.end();
1941 ++it) {
1942 --dist_from_root;
1943 cache_->cert_cache()->SetCertificate(*it,
1944 base::Bind(&OnCertWriteIOComplete,
1945 dist_from_root,
1946 false /* is not leaf */,
1947 shared_chain_data));
1949 DCHECK_EQ(0, dist_from_root);
1952 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1953 const HttpRequestInfo* request) {
1954 net_log_ = net_log;
1955 request_ = request;
1956 effective_load_flags_ = request_->load_flags;
1958 switch (cache_->mode()) {
1959 case NORMAL:
1960 break;
1961 case RECORD:
1962 // When in record mode, we want to NEVER load from the cache.
1963 // The reason for this is because we save the Set-Cookie headers
1964 // (intentionally). If we read from the cache, we replay them
1965 // prematurely.
1966 effective_load_flags_ |= LOAD_BYPASS_CACHE;
1967 break;
1968 case PLAYBACK:
1969 // When in playback mode, we want to load exclusively from the cache.
1970 effective_load_flags_ |= LOAD_ONLY_FROM_CACHE;
1971 break;
1972 case DISABLE:
1973 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1974 break;
1977 // Some headers imply load flags. The order here is significant.
1979 // LOAD_DISABLE_CACHE : no cache read or write
1980 // LOAD_BYPASS_CACHE : no cache read
1981 // LOAD_VALIDATE_CACHE : no cache read unless validation
1983 // The former modes trump latter modes, so if we find a matching header we
1984 // can stop iterating kSpecialHeaders.
1986 static const struct {
1987 const HeaderNameAndValue* search;
1988 int load_flag;
1989 } kSpecialHeaders[] = {
1990 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
1991 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
1992 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
1995 bool range_found = false;
1996 bool external_validation_error = false;
1998 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
1999 range_found = true;
2001 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSpecialHeaders); ++i) {
2002 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
2003 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
2004 break;
2008 // Check for conditionalization headers which may correspond with a
2009 // cache validation request.
2010 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
2011 const ValidationHeaderInfo& info = kValidationHeaders[i];
2012 std::string validation_value;
2013 if (request_->extra_headers.GetHeader(
2014 info.request_header_name, &validation_value)) {
2015 if (!external_validation_.values[i].empty() ||
2016 validation_value.empty()) {
2017 external_validation_error = true;
2019 external_validation_.values[i] = validation_value;
2020 external_validation_.initialized = true;
2024 // We don't support ranges and validation headers.
2025 if (range_found && external_validation_.initialized) {
2026 LOG(WARNING) << "Byte ranges AND validation headers found.";
2027 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2030 // If there is more than one validation header, we can't treat this request as
2031 // a cache validation, since we don't know for sure which header the server
2032 // will give us a response for (and they could be contradictory).
2033 if (external_validation_error) {
2034 LOG(WARNING) << "Multiple or malformed validation headers found.";
2035 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2038 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2039 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2040 partial_.reset(new PartialData);
2041 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
2042 // We will be modifying the actual range requested to the server, so
2043 // let's remove the header here.
2044 custom_request_.reset(new HttpRequestInfo(*request_));
2045 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2046 request_ = custom_request_.get();
2047 partial_->SetHeaders(custom_request_->extra_headers);
2048 } else {
2049 // The range is invalid or we cannot handle it properly.
2050 VLOG(1) << "Invalid byte range found.";
2051 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2052 partial_.reset(NULL);
2057 bool HttpCache::Transaction::ShouldPassThrough() {
2058 // We may have a null disk_cache if there is an error we cannot recover from,
2059 // like not enough disk space, or sharing violations.
2060 if (!cache_->disk_cache_.get())
2061 return true;
2063 // When using the record/playback modes, we always use the cache
2064 // and we never pass through.
2065 if (cache_->mode() == RECORD || cache_->mode() == PLAYBACK)
2066 return false;
2068 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2069 return true;
2071 if (request_->method == "GET" || request_->method == "HEAD")
2072 return false;
2074 if (request_->method == "POST" && request_->upload_data_stream &&
2075 request_->upload_data_stream->identifier()) {
2076 return false;
2079 if (request_->method == "PUT" && request_->upload_data_stream)
2080 return false;
2082 if (request_->method == "DELETE")
2083 return false;
2085 return true;
2088 int HttpCache::Transaction::BeginCacheRead() {
2089 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2090 if (response_.headers->response_code() == 206 || partial_.get()) {
2091 NOTREACHED();
2092 return ERR_CACHE_MISS;
2095 if (request_->method == "HEAD")
2096 FixHeadersForHead();
2098 // We don't have the whole resource.
2099 if (truncated_)
2100 return ERR_CACHE_MISS;
2102 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2103 next_state_ = STATE_CACHE_READ_METADATA;
2105 return OK;
2108 int HttpCache::Transaction::BeginCacheValidation() {
2109 DCHECK(mode_ == READ_WRITE);
2111 bool skip_validation = !RequiresValidation();
2113 if (request_->method == "HEAD" &&
2114 (truncated_ || response_.headers->response_code() == 206)) {
2115 DCHECK(!partial_);
2116 if (skip_validation)
2117 return SetupEntryForRead();
2119 // Bail out!
2120 next_state_ = STATE_SEND_REQUEST;
2121 mode_ = NONE;
2122 return OK;
2125 if (truncated_) {
2126 // Truncated entries can cause partial gets, so we shouldn't record this
2127 // load in histograms.
2128 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2129 skip_validation = !partial_->initial_validation();
2132 if (partial_.get() && (is_sparse_ || truncated_) &&
2133 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
2134 // Force revalidation for sparse or truncated entries. Note that we don't
2135 // want to ignore the regular validation logic just because a byte range was
2136 // part of the request.
2137 skip_validation = false;
2140 if (skip_validation) {
2141 UpdateTransactionPattern(PATTERN_ENTRY_USED);
2142 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE);
2143 return SetupEntryForRead();
2144 } else {
2145 // Make the network request conditional, to see if we may reuse our cached
2146 // response. If we cannot do so, then we just resort to a normal fetch.
2147 // Our mode remains READ_WRITE for a conditional request. Even if the
2148 // conditionalization fails, we don't switch to WRITE mode until we
2149 // know we won't be falling back to using the cache entry in the
2150 // LOAD_FROM_CACHE_IF_OFFLINE case.
2151 if (!ConditionalizeRequest()) {
2152 couldnt_conditionalize_request_ = true;
2153 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
2154 if (partial_.get())
2155 return DoRestartPartialRequest();
2157 DCHECK_NE(206, response_.headers->response_code());
2159 next_state_ = STATE_SEND_REQUEST;
2161 return OK;
2164 int HttpCache::Transaction::BeginPartialCacheValidation() {
2165 DCHECK(mode_ == READ_WRITE);
2167 if (response_.headers->response_code() != 206 && !partial_.get() &&
2168 !truncated_) {
2169 return BeginCacheValidation();
2172 // Partial requests should not be recorded in histograms.
2173 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2174 if (range_requested_) {
2175 next_state_ = STATE_CACHE_QUERY_DATA;
2176 return OK;
2179 // The request is not for a range, but we have stored just ranges.
2181 if (request_->method == "HEAD")
2182 return BeginCacheValidation();
2184 partial_.reset(new PartialData());
2185 partial_->SetHeaders(request_->extra_headers);
2186 if (!custom_request_.get()) {
2187 custom_request_.reset(new HttpRequestInfo(*request_));
2188 request_ = custom_request_.get();
2191 return ValidateEntryHeadersAndContinue();
2194 // This should only be called once per request.
2195 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2196 DCHECK(mode_ == READ_WRITE);
2198 if (!partial_->UpdateFromStoredHeaders(
2199 response_.headers.get(), entry_->disk_entry, truncated_)) {
2200 return DoRestartPartialRequest();
2203 if (response_.headers->response_code() == 206)
2204 is_sparse_ = true;
2206 if (!partial_->IsRequestedRangeOK()) {
2207 // The stored data is fine, but the request may be invalid.
2208 invalid_range_ = true;
2211 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2212 return OK;
2215 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2216 DCHECK_EQ(UPDATE, mode_);
2217 DCHECK(external_validation_.initialized);
2219 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
2220 if (external_validation_.values[i].empty())
2221 continue;
2222 // Retrieve either the cached response's "etag" or "last-modified" header.
2223 std::string validator;
2224 response_.headers->EnumerateHeader(
2225 NULL,
2226 kValidationHeaders[i].related_response_header_name,
2227 &validator);
2229 if (response_.headers->response_code() != 200 || truncated_ ||
2230 validator.empty() || validator != external_validation_.values[i]) {
2231 // The externally conditionalized request is not a validation request
2232 // for our existing cache entry. Proceed with caching disabled.
2233 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2234 DoneWritingToEntry(true);
2238 next_state_ = STATE_SEND_REQUEST;
2239 return OK;
2242 int HttpCache::Transaction::RestartNetworkRequest() {
2243 DCHECK(mode_ & WRITE || mode_ == NONE);
2244 DCHECK(network_trans_.get());
2245 DCHECK_EQ(STATE_NONE, next_state_);
2247 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2248 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
2249 if (rv != ERR_IO_PENDING)
2250 return DoLoop(rv);
2251 return rv;
2254 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2255 X509Certificate* client_cert) {
2256 DCHECK(mode_ & WRITE || mode_ == NONE);
2257 DCHECK(network_trans_.get());
2258 DCHECK_EQ(STATE_NONE, next_state_);
2260 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2261 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
2262 if (rv != ERR_IO_PENDING)
2263 return DoLoop(rv);
2264 return rv;
2267 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2268 const AuthCredentials& credentials) {
2269 DCHECK(mode_ & WRITE || mode_ == NONE);
2270 DCHECK(network_trans_.get());
2271 DCHECK_EQ(STATE_NONE, next_state_);
2273 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2274 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
2275 if (rv != ERR_IO_PENDING)
2276 return DoLoop(rv);
2277 return rv;
2280 bool HttpCache::Transaction::RequiresValidation() {
2281 // TODO(darin): need to do more work here:
2282 // - make sure we have a matching request method
2283 // - watch out for cached responses that depend on authentication
2285 // In playback mode, nothing requires validation.
2286 if (cache_->mode() == net::HttpCache::PLAYBACK)
2287 return false;
2289 if (response_.vary_data.is_valid() &&
2290 !response_.vary_data.MatchesRequest(*request_,
2291 *response_.headers.get())) {
2292 vary_mismatch_ = true;
2293 return true;
2296 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
2297 return false;
2299 if (effective_load_flags_ & LOAD_VALIDATE_CACHE)
2300 return true;
2302 if (request_->method == "PUT" || request_->method == "DELETE")
2303 return true;
2305 if (response_.headers->RequiresValidation(
2306 response_.request_time, response_.response_time, Time::Now())) {
2307 return true;
2310 return false;
2313 bool HttpCache::Transaction::ConditionalizeRequest() {
2314 DCHECK(response_.headers.get());
2316 if (request_->method == "PUT" || request_->method == "DELETE")
2317 return false;
2319 // This only makes sense for cached 200 or 206 responses.
2320 if (response_.headers->response_code() != 200 &&
2321 response_.headers->response_code() != 206) {
2322 return false;
2325 // We should have handled this case before.
2326 DCHECK(response_.headers->response_code() != 206 ||
2327 response_.headers->HasStrongValidators());
2329 // Just use the first available ETag and/or Last-Modified header value.
2330 // TODO(darin): Or should we use the last?
2332 std::string etag_value;
2333 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2334 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2336 std::string last_modified_value;
2337 if (!vary_mismatch_) {
2338 response_.headers->EnumerateHeader(NULL, "last-modified",
2339 &last_modified_value);
2342 if (etag_value.empty() && last_modified_value.empty())
2343 return false;
2345 if (!partial_.get()) {
2346 // Need to customize the request, so this forces us to allocate :(
2347 custom_request_.reset(new HttpRequestInfo(*request_));
2348 request_ = custom_request_.get();
2350 DCHECK(custom_request_.get());
2352 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2353 !invalid_range_;
2355 if (!use_if_range) {
2356 // stale-while-revalidate is not useful when we only have a partial response
2357 // cached, so don't set the header in that case.
2358 TimeDelta stale_while_revalidate;
2359 if (response_.headers->GetStaleWhileRevalidateValue(
2360 &stale_while_revalidate) &&
2361 stale_while_revalidate > TimeDelta()) {
2362 TimeDelta max_age =
2363 response_.headers->GetFreshnessLifetime(response_.response_time);
2364 TimeDelta current_age = response_.headers->GetCurrentAge(
2365 response_.request_time, response_.response_time, Time::Now());
2367 custom_request_->extra_headers.SetHeader(
2368 kFreshnessHeader,
2369 base::StringPrintf("max-age=%" PRId64
2370 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2371 max_age.InSeconds(),
2372 stale_while_revalidate.InSeconds(),
2373 current_age.InSeconds()));
2377 if (!etag_value.empty()) {
2378 if (use_if_range) {
2379 // We don't want to switch to WRITE mode if we don't have this block of a
2380 // byte-range request because we may have other parts cached.
2381 custom_request_->extra_headers.SetHeader(
2382 HttpRequestHeaders::kIfRange, etag_value);
2383 } else {
2384 custom_request_->extra_headers.SetHeader(
2385 HttpRequestHeaders::kIfNoneMatch, etag_value);
2387 // For byte-range requests, make sure that we use only one way to validate
2388 // the request.
2389 if (partial_.get() && !partial_->IsCurrentRangeCached())
2390 return true;
2393 if (!last_modified_value.empty()) {
2394 if (use_if_range) {
2395 custom_request_->extra_headers.SetHeader(
2396 HttpRequestHeaders::kIfRange, last_modified_value);
2397 } else {
2398 custom_request_->extra_headers.SetHeader(
2399 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2403 return true;
2406 // We just received some headers from the server. We may have asked for a range,
2407 // in which case partial_ has an object. This could be the first network request
2408 // we make to fulfill the original request, or we may be already reading (from
2409 // the net and / or the cache). If we are not expecting a certain response, we
2410 // just bypass the cache for this request (but again, maybe we are reading), and
2411 // delete partial_ (so we are not able to "fix" the headers that we return to
2412 // the user). This results in either a weird response for the caller (we don't
2413 // expect it after all), or maybe a range that was not exactly what it was asked
2414 // for.
2416 // If the server is simply telling us that the resource has changed, we delete
2417 // the cached entry and restart the request as the caller intended (by returning
2418 // false from this method). However, we may not be able to do that at any point,
2419 // for instance if we already returned the headers to the user.
2421 // WARNING: Whenever this code returns false, it has to make sure that the next
2422 // time it is called it will return true so that we don't keep retrying the
2423 // request.
2424 bool HttpCache::Transaction::ValidatePartialResponse() {
2425 const HttpResponseHeaders* headers = new_response_->headers.get();
2426 int response_code = headers->response_code();
2427 bool partial_response = (response_code == 206);
2428 handling_206_ = false;
2430 if (!entry_ || request_->method != "GET")
2431 return true;
2433 if (invalid_range_) {
2434 // We gave up trying to match this request with the stored data. If the
2435 // server is ok with the request, delete the entry, otherwise just ignore
2436 // this request
2437 DCHECK(!reading_);
2438 if (partial_response || response_code == 200) {
2439 DoomPartialEntry(true);
2440 mode_ = NONE;
2441 } else {
2442 if (response_code == 304)
2443 FailRangeRequest();
2444 IgnoreRangeRequest();
2446 return true;
2449 if (!partial_.get()) {
2450 // We are not expecting 206 but we may have one.
2451 if (partial_response)
2452 IgnoreRangeRequest();
2454 return true;
2457 // TODO(rvargas): Do we need to consider other results here?.
2458 bool failure = response_code == 200 || response_code == 416;
2460 if (partial_->IsCurrentRangeCached()) {
2461 // We asked for "If-None-Match: " so a 206 means a new object.
2462 if (partial_response)
2463 failure = true;
2465 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2466 return true;
2467 } else {
2468 // We asked for "If-Range: " so a 206 means just another range.
2469 if (partial_response && partial_->ResponseHeadersOK(headers)) {
2470 handling_206_ = true;
2471 return true;
2474 if (!reading_ && !is_sparse_ && !partial_response) {
2475 // See if we can ignore the fact that we issued a byte range request.
2476 // If the server sends 200, just store it. If it sends an error, redirect
2477 // or something else, we may store the response as long as we didn't have
2478 // anything already stored.
2479 if (response_code == 200 ||
2480 (!truncated_ && response_code != 304 && response_code != 416)) {
2481 // The server is sending something else, and we can save it.
2482 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2483 partial_.reset();
2484 truncated_ = false;
2485 return true;
2489 // 304 is not expected here, but we'll spare the entry (unless it was
2490 // truncated).
2491 if (truncated_)
2492 failure = true;
2495 if (failure) {
2496 // We cannot truncate this entry, it has to be deleted.
2497 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2498 DoomPartialEntry(false);
2499 mode_ = NONE;
2500 if (!reading_ && !partial_->IsLastRange()) {
2501 // We'll attempt to issue another network request, this time without us
2502 // messing up the headers.
2503 partial_->RestoreHeaders(&custom_request_->extra_headers);
2504 partial_.reset();
2505 truncated_ = false;
2506 return false;
2508 LOG(WARNING) << "Failed to revalidate partial entry";
2509 partial_.reset();
2510 return true;
2513 IgnoreRangeRequest();
2514 return true;
2517 void HttpCache::Transaction::IgnoreRangeRequest() {
2518 // We have a problem. We may or may not be reading already (in which case we
2519 // returned the headers), but we'll just pretend that this request is not
2520 // using the cache and see what happens. Most likely this is the first
2521 // response from the server (it's not changing its mind midway, right?).
2522 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2523 if (mode_ & WRITE)
2524 DoneWritingToEntry(mode_ != WRITE);
2525 else if (mode_ & READ && entry_)
2526 cache_->DoneReadingFromEntry(entry_, this);
2528 partial_.reset(NULL);
2529 entry_ = NULL;
2530 mode_ = NONE;
2533 void HttpCache::Transaction::FixHeadersForHead() {
2534 if (response_.headers->response_code() == 206) {
2535 response_.headers->RemoveHeader("Content-Length");
2536 response_.headers->RemoveHeader("Content-Range");
2537 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2541 void HttpCache::Transaction::FailRangeRequest() {
2542 response_ = *new_response_;
2543 partial_->FixResponseHeaders(response_.headers.get(), false);
2546 int HttpCache::Transaction::SetupEntryForRead() {
2547 if (network_trans_)
2548 ResetNetworkTransaction();
2549 if (partial_.get()) {
2550 if (truncated_ || is_sparse_ || !invalid_range_) {
2551 // We are going to return the saved response headers to the caller, so
2552 // we may need to adjust them first.
2553 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2554 return OK;
2555 } else {
2556 partial_.reset();
2559 cache_->ConvertWriterToReader(entry_);
2560 mode_ = READ;
2562 if (request_->method == "HEAD")
2563 FixHeadersForHead();
2565 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2566 next_state_ = STATE_CACHE_READ_METADATA;
2567 return OK;
2571 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2572 read_buf_ = data;
2573 io_buf_len_ = data_len;
2574 next_state_ = STATE_NETWORK_READ;
2575 return DoLoop(OK);
2578 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2579 if (request_->method == "HEAD")
2580 return 0;
2582 read_buf_ = data;
2583 io_buf_len_ = data_len;
2584 next_state_ = STATE_CACHE_READ_DATA;
2585 return DoLoop(OK);
2588 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2589 IOBuffer* data, int data_len,
2590 const CompletionCallback& callback) {
2591 if (!entry_)
2592 return data_len;
2594 int rv = 0;
2595 if (!partial_.get() || !data_len) {
2596 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2597 true);
2598 } else {
2599 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2601 return rv;
2604 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2605 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2606 if (!entry_)
2607 return OK;
2609 // Do not cache no-store content (unless we are record mode). Do not cache
2610 // content with cert errors either. This is to prevent not reporting net
2611 // errors when loading a resource from the cache. When we load a page over
2612 // HTTPS with a cert error we show an SSL blocking page. If the user clicks
2613 // proceed we reload the resource ignoring the errors. The loaded resource
2614 // is then cached. If that resource is subsequently loaded from the cache,
2615 // no net error is reported (even though the cert status contains the actual
2616 // errors) and no SSL blocking page is shown. An alternative would be to
2617 // reverse-map the cert status to a net error and replay the net error.
2618 if ((cache_->mode() != RECORD &&
2619 response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2620 net::IsCertStatusError(response_.ssl_info.cert_status)) {
2621 DoneWritingToEntry(false);
2622 if (net_log_.IsLogging())
2623 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2624 return OK;
2627 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2628 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2629 WriteCertChain();
2631 // When writing headers, we normally only write the non-transient
2632 // headers; when in record mode, record everything.
2633 bool skip_transient_headers = (cache_->mode() != RECORD);
2635 if (truncated)
2636 DCHECK_EQ(200, response_.headers->response_code());
2638 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2639 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2640 data->Done();
2642 io_buf_len_ = data->pickle()->size();
2643 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2644 io_buf_len_, io_callback_, true);
2647 int HttpCache::Transaction::AppendResponseDataToEntry(
2648 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2649 if (!entry_ || !data_len)
2650 return data_len;
2652 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2653 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2654 callback);
2657 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2658 if (!entry_)
2659 return;
2661 RecordHistograms();
2663 cache_->DoneWritingToEntry(entry_, success);
2664 entry_ = NULL;
2665 mode_ = NONE; // switch to 'pass through' mode
2668 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2669 DLOG(ERROR) << "ReadData failed: " << result;
2670 const int result_for_histogram = std::max(0, -result);
2671 if (restart) {
2672 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2673 result_for_histogram);
2674 } else {
2675 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2676 result_for_histogram);
2679 // Avoid using this entry in the future.
2680 if (cache_.get())
2681 cache_->DoomActiveEntry(cache_key_);
2683 if (restart) {
2684 DCHECK(!reading_);
2685 DCHECK(!network_trans_.get());
2686 cache_->DoneWithEntry(entry_, this, false);
2687 entry_ = NULL;
2688 is_sparse_ = false;
2689 partial_.reset();
2690 next_state_ = STATE_GET_BACKEND;
2691 return OK;
2694 return ERR_CACHE_READ_FAILURE;
2697 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
2698 if (entry_lock_waiting_since_ != start_time)
2699 return;
2701 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
2703 if (!cache_)
2704 return;
2706 cache_->RemovePendingTransaction(this);
2707 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
2710 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2711 DVLOG(2) << "DoomPartialEntry";
2712 int rv = cache_->DoomEntry(cache_key_, NULL);
2713 DCHECK_EQ(OK, rv);
2714 cache_->DoneWithEntry(entry_, this, false);
2715 entry_ = NULL;
2716 is_sparse_ = false;
2717 if (delete_object)
2718 partial_.reset(NULL);
2721 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2722 partial_->OnNetworkReadCompleted(result);
2724 if (result == 0) {
2725 // We need to move on to the next range.
2726 ResetNetworkTransaction();
2727 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2729 return result;
2732 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2733 partial_->OnCacheReadCompleted(result);
2735 if (result == 0 && mode_ == READ_WRITE) {
2736 // We need to move on to the next range.
2737 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2738 } else if (result < 0) {
2739 return OnCacheReadError(result, false);
2741 return result;
2744 int HttpCache::Transaction::DoRestartPartialRequest() {
2745 // The stored data cannot be used. Get rid of it and restart this request.
2746 // We need to also reset the |truncated_| flag as a new entry is created.
2747 DoomPartialEntry(!range_requested_);
2748 mode_ = WRITE;
2749 truncated_ = false;
2750 next_state_ = STATE_INIT_ENTRY;
2751 return OK;
2754 void HttpCache::Transaction::ResetNetworkTransaction() {
2755 DCHECK(!old_network_trans_load_timing_);
2756 DCHECK(network_trans_);
2757 LoadTimingInfo load_timing;
2758 if (network_trans_->GetLoadTimingInfo(&load_timing))
2759 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2760 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2761 network_trans_.reset();
2764 // Histogram data from the end of 2010 show the following distribution of
2765 // response headers:
2767 // Content-Length............... 87%
2768 // Date......................... 98%
2769 // Last-Modified................ 49%
2770 // Etag......................... 19%
2771 // Accept-Ranges: bytes......... 25%
2772 // Accept-Ranges: none.......... 0.4%
2773 // Strong Validator............. 50%
2774 // Strong Validator + ranges.... 24%
2775 // Strong Validator + CL........ 49%
2777 bool HttpCache::Transaction::CanResume(bool has_data) {
2778 // Double check that there is something worth keeping.
2779 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2780 return false;
2782 if (request_->method != "GET")
2783 return false;
2785 // Note that if this is a 206, content-length was already fixed after calling
2786 // PartialData::ResponseHeadersOK().
2787 if (response_.headers->GetContentLength() <= 0 ||
2788 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2789 !response_.headers->HasStrongValidators()) {
2790 return false;
2793 return true;
2796 void HttpCache::Transaction::UpdateTransactionPattern(
2797 TransactionPattern new_transaction_pattern) {
2798 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2799 return;
2800 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2801 new_transaction_pattern == PATTERN_NOT_COVERED);
2802 transaction_pattern_ = new_transaction_pattern;
2805 void HttpCache::Transaction::RecordHistograms() {
2806 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2807 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2808 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2809 cache_->mode() != NORMAL || request_->method != "GET") {
2810 return;
2812 UMA_HISTOGRAM_ENUMERATION(
2813 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2814 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2815 return;
2816 DCHECK(!range_requested_);
2817 DCHECK(!first_cache_access_since_.is_null());
2819 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2821 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2823 bool did_send_request = !send_request_since_.is_null();
2824 DCHECK(
2825 (did_send_request &&
2826 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2827 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2828 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2829 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2830 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2832 if (!did_send_request) {
2833 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2834 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2835 return;
2838 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2839 int before_send_percent =
2840 total_time.ToInternalValue() == 0 ? 0
2841 : before_send_time * 100 / total_time;
2842 DCHECK_LE(0, before_send_percent);
2843 DCHECK_GE(100, before_send_percent);
2845 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2846 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2847 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent);
2849 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2850 // below this comment after we have received initial data.
2851 switch (transaction_pattern_) {
2852 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2853 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2854 before_send_time);
2855 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2856 before_send_percent);
2857 break;
2859 case PATTERN_ENTRY_NOT_CACHED: {
2860 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2861 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2862 before_send_percent);
2863 break;
2865 case PATTERN_ENTRY_VALIDATED: {
2866 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2867 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2868 before_send_percent);
2869 break;
2871 case PATTERN_ENTRY_UPDATED: {
2872 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2873 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2874 before_send_percent);
2875 break;
2877 default:
2878 NOTREACHED();
2882 void HttpCache::Transaction::OnIOComplete(int result) {
2883 DoLoop(result);
2886 } // namespace net