Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blob7139abbdd79db0645bc37137d486365ad8fbbea8
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/memory/scoped_ptr.h"
21 #include "base/metrics/field_trial.h"
22 #include "base/metrics/histogram.h"
23 #include "base/metrics/sparse_histogram.h"
24 #include "base/profiler/scoped_tracker.h"
25 #include "base/rand_util.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_piece.h"
28 #include "base/strings/string_util.h"
29 #include "base/strings/stringprintf.h"
30 #include "base/time/time.h"
31 #include "base/values.h"
32 #include "net/base/completion_callback.h"
33 #include "net/base/io_buffer.h"
34 #include "net/base/load_flags.h"
35 #include "net/base/load_timing_info.h"
36 #include "net/base/net_errors.h"
37 #include "net/base/net_log.h"
38 #include "net/base/upload_data_stream.h"
39 #include "net/cert/cert_status_flags.h"
40 #include "net/disk_cache/disk_cache.h"
41 #include "net/http/disk_based_cert_cache.h"
42 #include "net/http/http_network_session.h"
43 #include "net/http/http_request_info.h"
44 #include "net/http/http_response_headers.h"
45 #include "net/http/http_transaction.h"
46 #include "net/http/http_util.h"
47 #include "net/http/partial_data.h"
48 #include "net/ssl/ssl_cert_request_info.h"
49 #include "net/ssl/ssl_config_service.h"
51 using base::Time;
52 using base::TimeDelta;
53 using base::TimeTicks;
55 namespace {
57 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
58 static const char kFreshnessHeader[] = "Resource-Freshness";
60 // Stores data relevant to the statistics of writing and reading entire
61 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
62 // of certificates in the chain that have pending operations in the
63 // DiskBasedCertCache. |start_time| is the time that the read and write
64 // commands began being issued to the DiskBasedCertCache.
65 // TODO(brandonsalmon): Remove this when it is no longer necessary to
66 // collect data.
67 class SharedChainData : public base::RefCounted<SharedChainData> {
68 public:
69 SharedChainData(int num_ops, TimeTicks start)
70 : num_pending_ops(num_ops), start_time(start) {}
72 int num_pending_ops;
73 TimeTicks start_time;
75 private:
76 friend class base::RefCounted<SharedChainData>;
77 ~SharedChainData() {}
78 DISALLOW_COPY_AND_ASSIGN(SharedChainData);
81 // Used to obtain a cache entry key for an OSCertHandle.
82 // TODO(brandonsalmon): Remove this when cache keys are stored
83 // and no longer have to be recomputed to retrieve the OSCertHandle
84 // from the disk.
85 std::string GetCacheKeyForCert(net::X509Certificate::OSCertHandle cert_handle) {
86 net::SHA1HashValue fingerprint =
87 net::X509Certificate::CalculateFingerprint(cert_handle);
89 return "cert:" +
90 base::HexEncode(fingerprint.data, arraysize(fingerprint.data));
93 // |dist_from_root| indicates the position of the read certificate in the
94 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
95 // whether or not the read certificate was the leaf of the chain.
96 // |shared_chain_data| contains data shared by each certificate in
97 // the chain.
98 void OnCertReadIOComplete(
99 int dist_from_root,
100 bool is_leaf,
101 const scoped_refptr<SharedChainData>& shared_chain_data,
102 net::X509Certificate::OSCertHandle cert_handle) {
103 // If |num_pending_ops| is one, this was the last pending read operation
104 // for this chain of certificates. The total time used to read the chain
105 // can be calculated by subtracting the starting time from Now().
106 shared_chain_data->num_pending_ops--;
107 if (!shared_chain_data->num_pending_ops) {
108 const TimeDelta read_chain_wait =
109 TimeTicks::Now() - shared_chain_data->start_time;
110 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
111 read_chain_wait,
112 base::TimeDelta::FromMilliseconds(1),
113 base::TimeDelta::FromMinutes(10),
114 50);
117 bool success = (cert_handle != NULL);
118 if (is_leaf)
119 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success);
121 if (success)
122 UMA_HISTOGRAM_CUSTOM_COUNTS(
123 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root, 0, 10, 7);
124 else
125 UMA_HISTOGRAM_CUSTOM_COUNTS(
126 "DiskBasedCertCache.CertIoReadFailure", dist_from_root, 0, 10, 7);
129 // |dist_from_root| indicates the position of the written certificate in the
130 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
131 // whether or not the written certificate was the leaf of the chain.
132 // |shared_chain_data| contains data shared by each certificate in
133 // the chain.
134 void OnCertWriteIOComplete(
135 int dist_from_root,
136 bool is_leaf,
137 const scoped_refptr<SharedChainData>& shared_chain_data,
138 const std::string& key) {
139 // If |num_pending_ops| is one, this was the last pending write operation
140 // for this chain of certificates. The total time used to write the chain
141 // can be calculated by subtracting the starting time from Now().
142 shared_chain_data->num_pending_ops--;
143 if (!shared_chain_data->num_pending_ops) {
144 const TimeDelta write_chain_wait =
145 TimeTicks::Now() - shared_chain_data->start_time;
146 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
147 write_chain_wait,
148 base::TimeDelta::FromMilliseconds(1),
149 base::TimeDelta::FromMinutes(10),
150 50);
153 bool success = !key.empty();
154 if (is_leaf)
155 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success);
157 if (success)
158 UMA_HISTOGRAM_CUSTOM_COUNTS(
159 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root, 0, 10, 7);
160 else
161 UMA_HISTOGRAM_CUSTOM_COUNTS(
162 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root, 0, 10, 7);
165 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
166 // a "non-error response" is one with a 2xx (Successful) or 3xx
167 // (Redirection) status code.
168 bool NonErrorResponse(int status_code) {
169 int status_code_range = status_code / 100;
170 return status_code_range == 2 || status_code_range == 3;
173 // Error codes that will be considered indicative of a page being offline/
174 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
175 bool IsOfflineError(int error) {
176 return (error == net::ERR_NAME_NOT_RESOLVED ||
177 error == net::ERR_INTERNET_DISCONNECTED ||
178 error == net::ERR_ADDRESS_UNREACHABLE ||
179 error == net::ERR_CONNECTION_TIMED_OUT);
182 // Enum for UMA, indicating the status (with regard to offline mode) of
183 // a particular request.
184 enum RequestOfflineStatus {
185 // A cache transaction hit in cache (data was present and not stale)
186 // and returned it.
187 OFFLINE_STATUS_FRESH_CACHE,
189 // A network request was required for a cache entry, and it succeeded.
190 OFFLINE_STATUS_NETWORK_SUCCEEDED,
192 // A network request was required for a cache entry, and it failed with
193 // a non-offline error.
194 OFFLINE_STATUS_NETWORK_FAILED,
196 // A network request was required for a cache entry, it failed with an
197 // offline error, and we could serve stale data if
198 // LOAD_FROM_CACHE_IF_OFFLINE was set.
199 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE,
201 // A network request was required for a cache entry, it failed with
202 // an offline error, and there was no servable data in cache (even
203 // stale data).
204 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE,
206 OFFLINE_STATUS_MAX_ENTRIES
209 void RecordOfflineStatus(int load_flags, RequestOfflineStatus status) {
210 // Restrict to main frame to keep statistics close to
211 // "would have shown them something useful if offline mode was enabled".
212 if (load_flags & net::LOAD_MAIN_FRAME) {
213 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status,
214 OFFLINE_STATUS_MAX_ENTRIES);
218 void RecordNoStoreHeaderHistogram(int load_flags,
219 const net::HttpResponseInfo* response) {
220 if (load_flags & net::LOAD_MAIN_FRAME) {
221 UMA_HISTOGRAM_BOOLEAN(
222 "Net.MainFrameNoStore",
223 response->headers->HasHeaderValue("cache-control", "no-store"));
227 base::Value* NetLogAsyncRevalidationInfoCallback(
228 const net::NetLog::Source& source,
229 const net::HttpRequestInfo* request,
230 net::NetLog::LogLevel log_level) {
231 base::DictionaryValue* dict = new base::DictionaryValue();
232 source.AddToEventParameters(dict);
234 dict->SetString("url", request->url.possibly_invalid_spec());
235 dict->SetString("method", request->method);
236 return dict;
239 enum ExternallyConditionalizedType {
240 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION,
241 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE,
242 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS,
243 EXTERNALLY_CONDITIONALIZED_MAX
246 } // namespace
248 namespace net {
250 struct HeaderNameAndValue {
251 const char* name;
252 const char* value;
255 // If the request includes one of these request headers, then avoid caching
256 // to avoid getting confused.
257 static const HeaderNameAndValue kPassThroughHeaders[] = {
258 { "if-unmodified-since", NULL }, // causes unexpected 412s
259 { "if-match", NULL }, // causes unexpected 412s
260 { "if-range", NULL },
261 { NULL, NULL }
264 struct ValidationHeaderInfo {
265 const char* request_header_name;
266 const char* related_response_header_name;
269 static const ValidationHeaderInfo kValidationHeaders[] = {
270 { "if-modified-since", "last-modified" },
271 { "if-none-match", "etag" },
274 // If the request includes one of these request headers, then avoid reusing
275 // our cached copy if any.
276 static const HeaderNameAndValue kForceFetchHeaders[] = {
277 { "cache-control", "no-cache" },
278 { "pragma", "no-cache" },
279 { NULL, NULL }
282 // If the request includes one of these request headers, then force our
283 // cached copy (if any) to be revalidated before reusing it.
284 static const HeaderNameAndValue kForceValidateHeaders[] = {
285 { "cache-control", "max-age=0" },
286 { NULL, NULL }
289 static bool HeaderMatches(const HttpRequestHeaders& headers,
290 const HeaderNameAndValue* search) {
291 for (; search->name; ++search) {
292 std::string header_value;
293 if (!headers.GetHeader(search->name, &header_value))
294 continue;
296 if (!search->value)
297 return true;
299 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
300 while (v.GetNext()) {
301 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
302 return true;
305 return false;
308 //-----------------------------------------------------------------------------
310 HttpCache::Transaction::Transaction(
311 RequestPriority priority,
312 HttpCache* cache)
313 : next_state_(STATE_NONE),
314 request_(NULL),
315 priority_(priority),
316 cache_(cache->GetWeakPtr()),
317 entry_(NULL),
318 new_entry_(NULL),
319 new_response_(NULL),
320 mode_(NONE),
321 target_state_(STATE_NONE),
322 reading_(false),
323 invalid_range_(false),
324 truncated_(false),
325 is_sparse_(false),
326 range_requested_(false),
327 handling_206_(false),
328 cache_pending_(false),
329 done_reading_(false),
330 vary_mismatch_(false),
331 couldnt_conditionalize_request_(false),
332 bypass_lock_for_test_(false),
333 io_buf_len_(0),
334 read_offset_(0),
335 effective_load_flags_(0),
336 write_len_(0),
337 transaction_pattern_(PATTERN_UNDEFINED),
338 total_received_bytes_(0),
339 websocket_handshake_stream_base_create_helper_(NULL),
340 weak_factory_(this) {
341 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders ==
342 arraysize(kValidationHeaders),
343 Invalid_number_of_validation_headers);
345 io_callback_ = base::Bind(&Transaction::OnIOComplete,
346 weak_factory_.GetWeakPtr());
349 HttpCache::Transaction::~Transaction() {
350 // We may have to issue another IO, but we should never invoke the callback_
351 // after this point.
352 callback_.Reset();
354 if (cache_) {
355 if (entry_) {
356 bool cancel_request = reading_ && response_.headers.get();
357 if (cancel_request) {
358 if (partial_) {
359 entry_->disk_entry->CancelSparseIO();
360 } else {
361 cancel_request &= (response_.headers->response_code() == 200);
365 cache_->DoneWithEntry(entry_, this, cancel_request);
366 } else if (cache_pending_) {
367 cache_->RemovePendingTransaction(this);
372 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
373 const CompletionCallback& callback) {
374 DCHECK(buf);
375 DCHECK_GT(buf_len, 0);
376 DCHECK(!callback.is_null());
377 if (!cache_.get() || !entry_)
378 return ERR_UNEXPECTED;
380 // We don't need to track this operation for anything.
381 // It could be possible to check if there is something already written and
382 // avoid writing again (it should be the same, right?), but let's allow the
383 // caller to "update" the contents with something new.
384 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
385 callback, true);
388 bool HttpCache::Transaction::AddTruncatedFlag() {
389 DCHECK(mode_ & WRITE || mode_ == NONE);
391 // Don't set the flag for sparse entries.
392 if (partial_.get() && !truncated_)
393 return true;
395 if (!CanResume(true))
396 return false;
398 // We may have received the whole resource already.
399 if (done_reading_)
400 return true;
402 truncated_ = true;
403 target_state_ = STATE_NONE;
404 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
405 DoLoop(OK);
406 return true;
409 LoadState HttpCache::Transaction::GetWriterLoadState() const {
410 if (network_trans_.get())
411 return network_trans_->GetLoadState();
412 if (entry_ || !request_)
413 return LOAD_STATE_IDLE;
414 return LOAD_STATE_WAITING_FOR_CACHE;
417 const BoundNetLog& HttpCache::Transaction::net_log() const {
418 return net_log_;
421 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
422 const CompletionCallback& callback,
423 const BoundNetLog& net_log) {
424 DCHECK(request);
425 DCHECK(!callback.is_null());
427 // Ensure that we only have one asynchronous call at a time.
428 DCHECK(callback_.is_null());
429 DCHECK(!reading_);
430 DCHECK(!network_trans_.get());
431 DCHECK(!entry_);
433 if (!cache_.get())
434 return ERR_UNEXPECTED;
436 SetRequest(net_log, request);
438 // We have to wait until the backend is initialized so we start the SM.
439 next_state_ = STATE_GET_BACKEND;
440 int rv = DoLoop(OK);
442 // Setting this here allows us to check for the existence of a callback_ to
443 // determine if we are still inside Start.
444 if (rv == ERR_IO_PENDING) {
445 callback_ = tracked_objects::ScopedTracker::TrackCallback(
446 FROM_HERE_WITH_EXPLICIT_FUNCTION(
447 "422516 HttpCache::Transaction::Start"),
448 callback);
451 return rv;
454 int HttpCache::Transaction::RestartIgnoringLastError(
455 const CompletionCallback& callback) {
456 DCHECK(!callback.is_null());
458 // Ensure that we only have one asynchronous call at a time.
459 DCHECK(callback_.is_null());
461 if (!cache_.get())
462 return ERR_UNEXPECTED;
464 int rv = RestartNetworkRequest();
466 if (rv == ERR_IO_PENDING) {
467 callback_ = tracked_objects::ScopedTracker::TrackCallback(
468 FROM_HERE_WITH_EXPLICIT_FUNCTION(
469 "422516 HttpCache::Transaction::RestartIgnoringLastError"),
470 callback);
473 return rv;
476 int HttpCache::Transaction::RestartWithCertificate(
477 X509Certificate* client_cert,
478 const CompletionCallback& callback) {
479 DCHECK(!callback.is_null());
481 // Ensure that we only have one asynchronous call at a time.
482 DCHECK(callback_.is_null());
484 if (!cache_.get())
485 return ERR_UNEXPECTED;
487 int rv = RestartNetworkRequestWithCertificate(client_cert);
489 if (rv == ERR_IO_PENDING) {
490 callback_ = tracked_objects::ScopedTracker::TrackCallback(
491 FROM_HERE_WITH_EXPLICIT_FUNCTION(
492 "422516 HttpCache::Transaction::RestartWithCertificate"),
493 callback);
496 return rv;
499 int HttpCache::Transaction::RestartWithAuth(
500 const AuthCredentials& credentials,
501 const CompletionCallback& callback) {
502 DCHECK(auth_response_.headers.get());
503 DCHECK(!callback.is_null());
505 // Ensure that we only have one asynchronous call at a time.
506 DCHECK(callback_.is_null());
508 if (!cache_.get())
509 return ERR_UNEXPECTED;
511 // Clear the intermediate response since we are going to start over.
512 auth_response_ = HttpResponseInfo();
514 int rv = RestartNetworkRequestWithAuth(credentials);
516 if (rv == ERR_IO_PENDING) {
517 callback_ = tracked_objects::ScopedTracker::TrackCallback(
518 FROM_HERE_WITH_EXPLICIT_FUNCTION(
519 "422516 HttpCache::Transaction::RestartWithAuth"),
520 callback);
523 return rv;
526 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
527 if (!network_trans_.get())
528 return false;
529 return network_trans_->IsReadyToRestartForAuth();
532 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
533 const CompletionCallback& callback) {
534 DCHECK(buf);
535 DCHECK_GT(buf_len, 0);
536 DCHECK(!callback.is_null());
538 DCHECK(callback_.is_null());
540 if (!cache_.get())
541 return ERR_UNEXPECTED;
543 // If we have an intermediate auth response at this point, then it means the
544 // user wishes to read the network response (the error page). If there is a
545 // previous response in the cache then we should leave it intact.
546 if (auth_response_.headers.get() && mode_ != NONE) {
547 UpdateTransactionPattern(PATTERN_NOT_COVERED);
548 DCHECK(mode_ & WRITE);
549 DoneWritingToEntry(mode_ == READ_WRITE);
550 mode_ = NONE;
553 reading_ = true;
554 int rv;
556 switch (mode_) {
557 case READ_WRITE:
558 DCHECK(partial_.get());
559 if (!network_trans_.get()) {
560 // We are just reading from the cache, but we may be writing later.
561 rv = ReadFromEntry(buf, buf_len);
562 break;
564 case NONE:
565 case WRITE:
566 DCHECK(network_trans_.get());
567 rv = ReadFromNetwork(buf, buf_len);
568 break;
569 case READ:
570 rv = ReadFromEntry(buf, buf_len);
571 break;
572 default:
573 NOTREACHED();
574 rv = ERR_FAILED;
577 if (rv == ERR_IO_PENDING) {
578 DCHECK(callback_.is_null());
579 callback_ = tracked_objects::ScopedTracker::TrackCallback(
580 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 HttpCache::Transaction::Read"),
581 callback);
583 return rv;
586 void HttpCache::Transaction::StopCaching() {
587 // We really don't know where we are now. Hopefully there is no operation in
588 // progress, but nothing really prevents this method to be called after we
589 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
590 // point because we need the state machine for that (and even if we are really
591 // free, that would be an asynchronous operation). In other words, keep the
592 // entry how it is (it will be marked as truncated at destruction), and let
593 // the next piece of code that executes know that we are now reading directly
594 // from the net.
595 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
596 // future request for the resource will be blocked on this one.
597 // Fix this.
598 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
599 !is_sparse_ && !range_requested_) {
600 mode_ = NONE;
604 bool HttpCache::Transaction::GetFullRequestHeaders(
605 HttpRequestHeaders* headers) const {
606 if (network_trans_)
607 return network_trans_->GetFullRequestHeaders(headers);
609 // TODO(ttuttle): Read headers from cache.
610 return false;
613 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
614 int64 total_received_bytes = total_received_bytes_;
615 if (network_trans_)
616 total_received_bytes += network_trans_->GetTotalReceivedBytes();
617 return total_received_bytes;
620 void HttpCache::Transaction::DoneReading() {
621 if (cache_.get() && entry_) {
622 DCHECK_NE(mode_, UPDATE);
623 if (mode_ & WRITE) {
624 DoneWritingToEntry(true);
625 } else if (mode_ & READ) {
626 // It is necessary to check mode_ & READ because it is possible
627 // for mode_ to be NONE and entry_ non-NULL with a write entry
628 // if StopCaching was called.
629 cache_->DoneReadingFromEntry(entry_, this);
630 entry_ = NULL;
635 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
636 // Null headers means we encountered an error or haven't a response yet
637 if (auth_response_.headers.get())
638 return &auth_response_;
639 return (response_.headers.get() || response_.ssl_info.cert.get() ||
640 response_.cert_request_info.get())
641 ? &response_
642 : NULL;
645 LoadState HttpCache::Transaction::GetLoadState() const {
646 LoadState state = GetWriterLoadState();
647 if (state != LOAD_STATE_WAITING_FOR_CACHE)
648 return state;
650 if (cache_.get())
651 return cache_->GetLoadStateForPendingTransaction(this);
653 return LOAD_STATE_IDLE;
656 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
657 if (network_trans_.get())
658 return network_trans_->GetUploadProgress();
659 return final_upload_progress_;
662 void HttpCache::Transaction::SetQuicServerInfo(
663 QuicServerInfo* quic_server_info) {}
665 bool HttpCache::Transaction::GetLoadTimingInfo(
666 LoadTimingInfo* load_timing_info) const {
667 if (network_trans_)
668 return network_trans_->GetLoadTimingInfo(load_timing_info);
670 if (old_network_trans_load_timing_) {
671 *load_timing_info = *old_network_trans_load_timing_;
672 return true;
675 if (first_cache_access_since_.is_null())
676 return false;
678 // If the cache entry was opened, return that time.
679 load_timing_info->send_start = first_cache_access_since_;
680 // This time doesn't make much sense when reading from the cache, so just use
681 // the same time as send_start.
682 load_timing_info->send_end = first_cache_access_since_;
683 return true;
686 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
687 priority_ = priority;
688 if (network_trans_)
689 network_trans_->SetPriority(priority_);
692 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
693 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
694 websocket_handshake_stream_base_create_helper_ = create_helper;
695 if (network_trans_)
696 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
699 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
700 const BeforeNetworkStartCallback& callback) {
701 DCHECK(!network_trans_);
702 before_network_start_callback_ = callback;
705 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
706 const BeforeProxyHeadersSentCallback& callback) {
707 DCHECK(!network_trans_);
708 before_proxy_headers_sent_callback_ = callback;
711 int HttpCache::Transaction::ResumeNetworkStart() {
712 if (network_trans_)
713 return network_trans_->ResumeNetworkStart();
714 return ERR_UNEXPECTED;
717 //-----------------------------------------------------------------------------
719 void HttpCache::Transaction::DoCallback(int rv) {
720 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
721 tracked_objects::ScopedTracker tracking_profile(
722 FROM_HERE_WITH_EXPLICIT_FUNCTION(
723 "422516 HttpCache::Transaction::DoCallback"));
725 DCHECK(rv != ERR_IO_PENDING);
726 DCHECK(!callback_.is_null());
728 read_buf_ = NULL; // Release the buffer before invoking the callback.
730 // Since Run may result in Read being called, clear callback_ up front.
731 CompletionCallback c = callback_;
732 callback_.Reset();
733 c.Run(rv);
736 int HttpCache::Transaction::HandleResult(int rv) {
737 DCHECK(rv != ERR_IO_PENDING);
738 if (!callback_.is_null())
739 DoCallback(rv);
741 return rv;
744 // A few common patterns: (Foo* means Foo -> FooComplete)
746 // 1. Not-cached entry:
747 // Start():
748 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
749 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
750 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
751 // PartialHeadersReceived
753 // Read():
754 // NetworkRead* -> CacheWriteData*
756 // 2. Cached entry, no validation:
757 // Start():
758 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
759 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
760 // SetupEntryForRead()
762 // Read():
763 // CacheReadData*
765 // 3. Cached entry, validation (304):
766 // Start():
767 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
768 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
769 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
770 // CacheWriteResponse* -> UpdateCachedResponseComplete ->
771 // OverwriteCachedResponse -> PartialHeadersReceived
773 // Read():
774 // CacheReadData*
776 // 4. Cached entry, validation and replace (200):
777 // Start():
778 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
779 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
780 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
781 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
782 // PartialHeadersReceived
784 // Read():
785 // NetworkRead* -> CacheWriteData*
787 // 5. Sparse entry, partially cached, byte range request:
788 // Start():
789 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
790 // -> BeginPartialCacheValidation() -> CacheQueryData* ->
791 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
792 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
793 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
794 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
795 // PartialHeadersReceived
797 // Read() 1:
798 // NetworkRead* -> CacheWriteData*
800 // Read() 2:
801 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
802 // CompletePartialCacheValidation -> CacheReadData* ->
804 // Read() 3:
805 // CacheReadData* -> StartPartialCacheValidation ->
806 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
807 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
808 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
810 // 6. HEAD. Not-cached entry:
811 // Pass through. Don't save a HEAD by itself.
812 // Start():
813 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
815 // 7. HEAD. Cached entry, no validation:
816 // Start():
817 // The same flow as for a GET request (example #2)
819 // Read():
820 // CacheReadData (returns 0)
822 // 8. HEAD. Cached entry, validation (304):
823 // The request updates the stored headers.
824 // Start(): Same as for a GET request (example #3)
826 // Read():
827 // CacheReadData (returns 0)
829 // 9. HEAD. Cached entry, validation and replace (200):
830 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
831 // itself.
832 // Start():
833 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
834 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
835 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse
837 // 10. HEAD. Sparse entry, partially cached:
838 // Serve the request from the cache, as long as it doesn't require
839 // revalidation. Ignore missing ranges when deciding to revalidate. If the
840 // entry requires revalidation, ignore the whole request and go to full pass
841 // through (the result of the HEAD request will NOT update the entry).
843 // Start(): Basically the same as example 7, as we never create a partial_
844 // object for this request.
846 int HttpCache::Transaction::DoLoop(int result) {
847 DCHECK(next_state_ != STATE_NONE);
849 int rv = result;
850 do {
851 State state = next_state_;
852 next_state_ = STATE_NONE;
853 switch (state) {
854 case STATE_GET_BACKEND:
855 DCHECK_EQ(OK, rv);
856 rv = DoGetBackend();
857 break;
858 case STATE_GET_BACKEND_COMPLETE:
859 rv = DoGetBackendComplete(rv);
860 break;
861 case STATE_SEND_REQUEST:
862 DCHECK_EQ(OK, rv);
863 rv = DoSendRequest();
864 break;
865 case STATE_SEND_REQUEST_COMPLETE:
866 rv = DoSendRequestComplete(rv);
867 break;
868 case STATE_SUCCESSFUL_SEND_REQUEST:
869 DCHECK_EQ(OK, rv);
870 rv = DoSuccessfulSendRequest();
871 break;
872 case STATE_NETWORK_READ:
873 DCHECK_EQ(OK, rv);
874 rv = DoNetworkRead();
875 break;
876 case STATE_NETWORK_READ_COMPLETE:
877 rv = DoNetworkReadComplete(rv);
878 break;
879 case STATE_INIT_ENTRY:
880 DCHECK_EQ(OK, rv);
881 rv = DoInitEntry();
882 break;
883 case STATE_OPEN_ENTRY:
884 DCHECK_EQ(OK, rv);
885 rv = DoOpenEntry();
886 break;
887 case STATE_OPEN_ENTRY_COMPLETE:
888 rv = DoOpenEntryComplete(rv);
889 break;
890 case STATE_CREATE_ENTRY:
891 DCHECK_EQ(OK, rv);
892 rv = DoCreateEntry();
893 break;
894 case STATE_CREATE_ENTRY_COMPLETE:
895 rv = DoCreateEntryComplete(rv);
896 break;
897 case STATE_DOOM_ENTRY:
898 DCHECK_EQ(OK, rv);
899 rv = DoDoomEntry();
900 break;
901 case STATE_DOOM_ENTRY_COMPLETE:
902 rv = DoDoomEntryComplete(rv);
903 break;
904 case STATE_ADD_TO_ENTRY:
905 DCHECK_EQ(OK, rv);
906 rv = DoAddToEntry();
907 break;
908 case STATE_ADD_TO_ENTRY_COMPLETE:
909 rv = DoAddToEntryComplete(rv);
910 break;
911 case STATE_START_PARTIAL_CACHE_VALIDATION:
912 DCHECK_EQ(OK, rv);
913 rv = DoStartPartialCacheValidation();
914 break;
915 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
916 rv = DoCompletePartialCacheValidation(rv);
917 break;
918 case STATE_UPDATE_CACHED_RESPONSE:
919 DCHECK_EQ(OK, rv);
920 rv = DoUpdateCachedResponse();
921 break;
922 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
923 rv = DoUpdateCachedResponseComplete(rv);
924 break;
925 case STATE_OVERWRITE_CACHED_RESPONSE:
926 DCHECK_EQ(OK, rv);
927 rv = DoOverwriteCachedResponse();
928 break;
929 case STATE_TRUNCATE_CACHED_DATA:
930 DCHECK_EQ(OK, rv);
931 rv = DoTruncateCachedData();
932 break;
933 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
934 rv = DoTruncateCachedDataComplete(rv);
935 break;
936 case STATE_TRUNCATE_CACHED_METADATA:
937 DCHECK_EQ(OK, rv);
938 rv = DoTruncateCachedMetadata();
939 break;
940 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
941 rv = DoTruncateCachedMetadataComplete(rv);
942 break;
943 case STATE_PARTIAL_HEADERS_RECEIVED:
944 DCHECK_EQ(OK, rv);
945 rv = DoPartialHeadersReceived();
946 break;
947 case STATE_CACHE_READ_RESPONSE:
948 DCHECK_EQ(OK, rv);
949 rv = DoCacheReadResponse();
950 break;
951 case STATE_CACHE_READ_RESPONSE_COMPLETE:
952 rv = DoCacheReadResponseComplete(rv);
953 break;
954 case STATE_CACHE_WRITE_RESPONSE:
955 DCHECK_EQ(OK, rv);
956 rv = DoCacheWriteResponse();
957 break;
958 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
959 DCHECK_EQ(OK, rv);
960 rv = DoCacheWriteTruncatedResponse();
961 break;
962 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
963 rv = DoCacheWriteResponseComplete(rv);
964 break;
965 case STATE_CACHE_READ_METADATA:
966 DCHECK_EQ(OK, rv);
967 rv = DoCacheReadMetadata();
968 break;
969 case STATE_CACHE_READ_METADATA_COMPLETE:
970 rv = DoCacheReadMetadataComplete(rv);
971 break;
972 case STATE_CACHE_QUERY_DATA:
973 DCHECK_EQ(OK, rv);
974 rv = DoCacheQueryData();
975 break;
976 case STATE_CACHE_QUERY_DATA_COMPLETE:
977 rv = DoCacheQueryDataComplete(rv);
978 break;
979 case STATE_CACHE_READ_DATA:
980 DCHECK_EQ(OK, rv);
981 rv = DoCacheReadData();
982 break;
983 case STATE_CACHE_READ_DATA_COMPLETE:
984 rv = DoCacheReadDataComplete(rv);
985 break;
986 case STATE_CACHE_WRITE_DATA:
987 rv = DoCacheWriteData(rv);
988 break;
989 case STATE_CACHE_WRITE_DATA_COMPLETE:
990 rv = DoCacheWriteDataComplete(rv);
991 break;
992 default:
993 NOTREACHED() << "bad state";
994 rv = ERR_FAILED;
995 break;
997 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
999 if (rv != ERR_IO_PENDING)
1000 HandleResult(rv);
1002 return rv;
1005 int HttpCache::Transaction::DoGetBackend() {
1006 cache_pending_ = true;
1007 next_state_ = STATE_GET_BACKEND_COMPLETE;
1008 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
1009 return cache_->GetBackendForTransaction(this);
1012 int HttpCache::Transaction::DoGetBackendComplete(int result) {
1013 DCHECK(result == OK || result == ERR_FAILED);
1014 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
1015 result);
1016 cache_pending_ = false;
1018 if (!ShouldPassThrough()) {
1019 cache_key_ = cache_->GenerateCacheKey(request_);
1021 // Requested cache access mode.
1022 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
1023 mode_ = READ;
1024 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
1025 mode_ = WRITE;
1026 } else {
1027 mode_ = READ_WRITE;
1030 // Downgrade to UPDATE if the request has been externally conditionalized.
1031 if (external_validation_.initialized) {
1032 if (mode_ & WRITE) {
1033 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1034 // in case READ was off).
1035 mode_ = UPDATE;
1036 } else {
1037 mode_ = NONE;
1042 // Use PUT and DELETE only to invalidate existing stored entries.
1043 if ((request_->method == "PUT" || request_->method == "DELETE") &&
1044 mode_ != READ_WRITE && mode_ != WRITE) {
1045 mode_ = NONE;
1048 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1049 // transaction behaves the same for GET and HEAD requests at this point: if it
1050 // was not modified, the entry is updated and a response is not returned from
1051 // the cache. If we receive 200, it doesn't matter if there was a validation
1052 // header or not.
1053 if (request_->method == "HEAD" && mode_ == WRITE)
1054 mode_ = NONE;
1056 // If must use cache, then we must fail. This can happen for back/forward
1057 // navigations to a page generated via a form post.
1058 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
1059 return ERR_CACHE_MISS;
1061 if (mode_ == NONE) {
1062 if (partial_.get()) {
1063 partial_->RestoreHeaders(&custom_request_->extra_headers);
1064 partial_.reset();
1066 next_state_ = STATE_SEND_REQUEST;
1067 } else {
1068 next_state_ = STATE_INIT_ENTRY;
1071 // This is only set if we have something to do with the response.
1072 range_requested_ = (partial_.get() != NULL);
1074 return OK;
1077 int HttpCache::Transaction::DoSendRequest() {
1078 DCHECK(mode_ & WRITE || mode_ == NONE);
1079 DCHECK(!network_trans_.get());
1081 send_request_since_ = TimeTicks::Now();
1083 // Create a network transaction.
1084 int rv = cache_->network_layer_->CreateTransaction(priority_,
1085 &network_trans_);
1086 if (rv != OK)
1087 return rv;
1088 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1089 network_trans_->SetBeforeProxyHeadersSentCallback(
1090 before_proxy_headers_sent_callback_);
1092 // Old load timing information, if any, is now obsolete.
1093 old_network_trans_load_timing_.reset();
1095 if (websocket_handshake_stream_base_create_helper_)
1096 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1097 websocket_handshake_stream_base_create_helper_);
1099 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1100 rv = network_trans_->Start(request_, io_callback_, net_log_);
1101 return rv;
1104 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1105 if (!cache_.get())
1106 return ERR_UNEXPECTED;
1108 // If requested, and we have a readable cache entry, and we have
1109 // an error indicating that we're offline as opposed to in contact
1110 // with a bad server, read from cache anyway.
1111 if (IsOfflineError(result)) {
1112 if (mode_ == READ_WRITE && entry_ && !partial_) {
1113 RecordOfflineStatus(effective_load_flags_,
1114 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE);
1115 if (effective_load_flags_ & LOAD_FROM_CACHE_IF_OFFLINE) {
1116 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1117 response_.server_data_unavailable = true;
1118 return SetupEntryForRead();
1120 } else {
1121 RecordOfflineStatus(effective_load_flags_,
1122 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE);
1124 } else {
1125 RecordOfflineStatus(effective_load_flags_,
1126 (result == OK ? OFFLINE_STATUS_NETWORK_SUCCEEDED :
1127 OFFLINE_STATUS_NETWORK_FAILED));
1130 // If we tried to conditionalize the request and failed, we know
1131 // we won't be reading from the cache after this point.
1132 if (couldnt_conditionalize_request_)
1133 mode_ = WRITE;
1135 if (result == OK) {
1136 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
1137 return OK;
1140 // Do not record requests that have network errors or restarts.
1141 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1142 if (IsCertificateError(result)) {
1143 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1144 // If we get a certificate error, then there is a certificate in ssl_info,
1145 // so GetResponseInfo() should never return NULL here.
1146 DCHECK(response);
1147 response_.ssl_info = response->ssl_info;
1148 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1149 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1150 DCHECK(response);
1151 response_.cert_request_info = response->cert_request_info;
1152 } else if (response_.was_cached) {
1153 DoneWritingToEntry(true);
1155 return result;
1158 // We received the response headers and there is no error.
1159 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1160 DCHECK(!new_response_);
1161 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1162 bool authentication_failure = false;
1164 if (new_response->headers->response_code() == 401 ||
1165 new_response->headers->response_code() == 407) {
1166 auth_response_ = *new_response;
1167 if (!reading_)
1168 return OK;
1170 // We initiated a second request the caller doesn't know about. We should be
1171 // able to authenticate this request because we should have authenticated
1172 // this URL moments ago.
1173 if (IsReadyToRestartForAuth()) {
1174 DCHECK(!response_.auth_challenge.get());
1175 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1176 // In theory we should check to see if there are new cookies, but there
1177 // is no way to do that from here.
1178 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1181 // We have to perform cleanup at this point so that at least the next
1182 // request can succeed.
1183 authentication_failure = true;
1184 if (entry_)
1185 DoomPartialEntry(false);
1186 mode_ = NONE;
1187 partial_.reset();
1190 new_response_ = new_response;
1191 if (authentication_failure ||
1192 (!ValidatePartialResponse() && !auth_response_.headers.get())) {
1193 // Something went wrong with this request and we have to restart it.
1194 // If we have an authentication response, we are exposed to weird things
1195 // hapenning if the user cancels the authentication before we receive
1196 // the new response.
1197 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1198 response_ = HttpResponseInfo();
1199 ResetNetworkTransaction();
1200 new_response_ = NULL;
1201 next_state_ = STATE_SEND_REQUEST;
1202 return OK;
1205 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1206 // We have stored the full entry, but it changed and the server is
1207 // sending a range. We have to delete the old entry.
1208 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1209 DoneWritingToEntry(false);
1212 if (mode_ == WRITE &&
1213 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1214 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1217 // Invalidate any cached GET with a successful PUT or DELETE.
1218 if (mode_ == WRITE &&
1219 (request_->method == "PUT" || request_->method == "DELETE")) {
1220 if (NonErrorResponse(new_response->headers->response_code())) {
1221 int ret = cache_->DoomEntry(cache_key_, NULL);
1222 DCHECK_EQ(OK, ret);
1224 cache_->DoneWritingToEntry(entry_, true);
1225 entry_ = NULL;
1226 mode_ = NONE;
1229 // Invalidate any cached GET with a successful POST.
1230 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1231 request_->method == "POST" &&
1232 NonErrorResponse(new_response->headers->response_code())) {
1233 cache_->DoomMainEntryForUrl(request_->url);
1236 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1238 if (new_response_->headers->response_code() == 416 &&
1239 (request_->method == "GET" || request_->method == "POST")) {
1240 // If there is an active entry it may be destroyed with this transaction.
1241 response_ = *new_response_;
1242 return OK;
1245 // Are we expecting a response to a conditional query?
1246 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1247 if (new_response->headers->response_code() == 304 || handling_206_) {
1248 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1249 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1250 return OK;
1252 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1253 mode_ = WRITE;
1256 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1257 return OK;
1260 int HttpCache::Transaction::DoNetworkRead() {
1261 next_state_ = STATE_NETWORK_READ_COMPLETE;
1262 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1265 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1266 DCHECK(mode_ & WRITE || mode_ == NONE);
1268 if (!cache_.get())
1269 return ERR_UNEXPECTED;
1271 // If there is an error or we aren't saving the data, we are done; just wait
1272 // until the destructor runs to see if we can keep the data.
1273 if (mode_ == NONE || result < 0)
1274 return result;
1276 next_state_ = STATE_CACHE_WRITE_DATA;
1277 return result;
1280 int HttpCache::Transaction::DoInitEntry() {
1281 DCHECK(!new_entry_);
1283 if (!cache_.get())
1284 return ERR_UNEXPECTED;
1286 if (mode_ == WRITE) {
1287 next_state_ = STATE_DOOM_ENTRY;
1288 return OK;
1291 next_state_ = STATE_OPEN_ENTRY;
1292 return OK;
1295 int HttpCache::Transaction::DoOpenEntry() {
1296 DCHECK(!new_entry_);
1297 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1298 cache_pending_ = true;
1299 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1300 first_cache_access_since_ = TimeTicks::Now();
1301 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1304 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1305 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1306 // OK, otherwise the cache will end up with an active entry without any
1307 // transaction attached.
1308 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1309 cache_pending_ = false;
1310 if (result == OK) {
1311 next_state_ = STATE_ADD_TO_ENTRY;
1312 return OK;
1315 if (result == ERR_CACHE_RACE) {
1316 next_state_ = STATE_INIT_ENTRY;
1317 return OK;
1320 if (request_->method == "PUT" || request_->method == "DELETE" ||
1321 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1322 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1323 mode_ = NONE;
1324 next_state_ = STATE_SEND_REQUEST;
1325 return OK;
1328 if (mode_ == READ_WRITE) {
1329 mode_ = WRITE;
1330 next_state_ = STATE_CREATE_ENTRY;
1331 return OK;
1333 if (mode_ == UPDATE) {
1334 // There is no cache entry to update; proceed without caching.
1335 mode_ = NONE;
1336 next_state_ = STATE_SEND_REQUEST;
1337 return OK;
1339 if (cache_->mode() == PLAYBACK)
1340 DVLOG(1) << "Playback Cache Miss: " << request_->url;
1342 // The entry does not exist, and we are not permitted to create a new entry,
1343 // so we must fail.
1344 return ERR_CACHE_MISS;
1347 int HttpCache::Transaction::DoCreateEntry() {
1348 DCHECK(!new_entry_);
1349 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1350 cache_pending_ = true;
1351 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1352 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1355 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1356 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1357 // OK, otherwise the cache will end up with an active entry without any
1358 // transaction attached.
1359 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1360 result);
1361 cache_pending_ = false;
1362 next_state_ = STATE_ADD_TO_ENTRY;
1364 if (result == ERR_CACHE_RACE) {
1365 next_state_ = STATE_INIT_ENTRY;
1366 return OK;
1369 if (result != OK) {
1370 // We have a race here: Maybe we failed to open the entry and decided to
1371 // create one, but by the time we called create, another transaction already
1372 // created the entry. If we want to eliminate this issue, we need an atomic
1373 // OpenOrCreate() method exposed by the disk cache.
1374 DLOG(WARNING) << "Unable to create cache entry";
1375 mode_ = NONE;
1376 if (partial_.get())
1377 partial_->RestoreHeaders(&custom_request_->extra_headers);
1378 next_state_ = STATE_SEND_REQUEST;
1380 return OK;
1383 int HttpCache::Transaction::DoDoomEntry() {
1384 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1385 cache_pending_ = true;
1386 if (first_cache_access_since_.is_null())
1387 first_cache_access_since_ = TimeTicks::Now();
1388 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1389 return cache_->DoomEntry(cache_key_, this);
1392 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1393 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1394 next_state_ = STATE_CREATE_ENTRY;
1395 cache_pending_ = false;
1396 if (result == ERR_CACHE_RACE)
1397 next_state_ = STATE_INIT_ENTRY;
1398 return OK;
1401 int HttpCache::Transaction::DoAddToEntry() {
1402 DCHECK(new_entry_);
1403 cache_pending_ = true;
1404 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1405 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1406 DCHECK(entry_lock_waiting_since_.is_null());
1407 entry_lock_waiting_since_ = TimeTicks::Now();
1408 int rv = cache_->AddTransactionToEntry(new_entry_, this);
1409 if (rv == ERR_IO_PENDING) {
1410 if (bypass_lock_for_test_) {
1411 OnAddToEntryTimeout(entry_lock_waiting_since_);
1412 } else {
1413 int timeout_milliseconds = 20 * 1000;
1414 if (partial_ && new_entry_->writer &&
1415 new_entry_->writer->range_requested_) {
1416 // Quickly timeout and bypass the cache if we're a range request and
1417 // we're blocked by the reader/writer lock. Doing so eliminates a long
1418 // running issue, http://crbug.com/31014, where two of the same media
1419 // resources could not be played back simultaneously due to one locking
1420 // the cache entry until the entire video was downloaded.
1422 // Bypassing the cache is not ideal, as we are now ignoring the cache
1423 // entirely for all range requests to a resource beyond the first. This
1424 // is however a much more succinct solution than the alternatives, which
1425 // would require somewhat significant changes to the http caching logic.
1427 // Allow some timeout slack for the entry addition to complete in case
1428 // the writer lock is imminently released; we want to avoid skipping
1429 // the cache if at all possible. See http://crbug.com/408765
1430 timeout_milliseconds = 25;
1432 base::MessageLoop::current()->PostDelayedTask(
1433 FROM_HERE,
1434 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout,
1435 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_),
1436 TimeDelta::FromMilliseconds(timeout_milliseconds));
1439 return rv;
1442 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1443 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1444 result);
1445 const TimeDelta entry_lock_wait =
1446 TimeTicks::Now() - entry_lock_waiting_since_;
1447 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1449 entry_lock_waiting_since_ = TimeTicks();
1450 DCHECK(new_entry_);
1451 cache_pending_ = false;
1453 if (result == OK)
1454 entry_ = new_entry_;
1456 // If there is a failure, the cache should have taken care of new_entry_.
1457 new_entry_ = NULL;
1459 if (result == ERR_CACHE_RACE) {
1460 next_state_ = STATE_INIT_ENTRY;
1461 return OK;
1464 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1465 // The cache is busy, bypass it for this transaction.
1466 mode_ = NONE;
1467 next_state_ = STATE_SEND_REQUEST;
1468 if (partial_) {
1469 partial_->RestoreHeaders(&custom_request_->extra_headers);
1470 partial_.reset();
1472 return OK;
1475 if (result != OK) {
1476 NOTREACHED();
1477 return result;
1480 if (mode_ == WRITE) {
1481 if (partial_.get())
1482 partial_->RestoreHeaders(&custom_request_->extra_headers);
1483 next_state_ = STATE_SEND_REQUEST;
1484 } else {
1485 // We have to read the headers from the cached entry.
1486 DCHECK(mode_ & READ_META);
1487 next_state_ = STATE_CACHE_READ_RESPONSE;
1489 return OK;
1492 // We may end up here multiple times for a given request.
1493 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1494 if (mode_ == NONE)
1495 return OK;
1497 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1498 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1501 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1502 if (!result) {
1503 // This is the end of the request.
1504 if (mode_ & WRITE) {
1505 DoneWritingToEntry(true);
1506 } else {
1507 cache_->DoneReadingFromEntry(entry_, this);
1508 entry_ = NULL;
1510 return result;
1513 if (result < 0)
1514 return result;
1516 partial_->PrepareCacheValidation(entry_->disk_entry,
1517 &custom_request_->extra_headers);
1519 if (reading_ && partial_->IsCurrentRangeCached()) {
1520 next_state_ = STATE_CACHE_READ_DATA;
1521 return OK;
1524 return BeginCacheValidation();
1527 // We received 304 or 206 and we want to update the cached response headers.
1528 int HttpCache::Transaction::DoUpdateCachedResponse() {
1529 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1530 int rv = OK;
1531 // Update cached response based on headers in new_response.
1532 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1533 response_.headers->Update(*new_response_->headers.get());
1534 response_.response_time = new_response_->response_time;
1535 response_.request_time = new_response_->request_time;
1536 response_.network_accessed = new_response_->network_accessed;
1538 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1539 if (!entry_->doomed) {
1540 int ret = cache_->DoomEntry(cache_key_, NULL);
1541 DCHECK_EQ(OK, ret);
1543 } else {
1544 // If we are already reading, we already updated the headers for this
1545 // request; doing it again will change Content-Length.
1546 if (!reading_) {
1547 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1548 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1549 rv = OK;
1552 return rv;
1555 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1556 if (mode_ == UPDATE) {
1557 DCHECK(!handling_206_);
1558 // We got a "not modified" response and already updated the corresponding
1559 // cache entry above.
1561 // By closing the cached entry now, we make sure that the 304 rather than
1562 // the cached 200 response, is what will be returned to the user.
1563 DoneWritingToEntry(true);
1564 } else if (entry_ && !handling_206_) {
1565 DCHECK_EQ(READ_WRITE, mode_);
1566 if (!partial_.get() || partial_->IsLastRange()) {
1567 cache_->ConvertWriterToReader(entry_);
1568 mode_ = READ;
1570 // We no longer need the network transaction, so destroy it.
1571 final_upload_progress_ = network_trans_->GetUploadProgress();
1572 ResetNetworkTransaction();
1573 } else if (entry_ && handling_206_ && truncated_ &&
1574 partial_->initial_validation()) {
1575 // We just finished the validation of a truncated entry, and the server
1576 // is willing to resume the operation. Now we go back and start serving
1577 // the first part to the user.
1578 ResetNetworkTransaction();
1579 new_response_ = NULL;
1580 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1581 partial_->SetRangeToStartDownload();
1582 return OK;
1584 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1585 return OK;
1588 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1589 if (mode_ & READ) {
1590 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1591 return OK;
1594 // We change the value of Content-Length for partial content.
1595 if (handling_206_ && partial_.get())
1596 partial_->FixContentLength(new_response_->headers.get());
1598 response_ = *new_response_;
1600 if (request_->method == "HEAD") {
1601 // This response is replacing the cached one.
1602 DoneWritingToEntry(false);
1603 mode_ = NONE;
1604 new_response_ = NULL;
1605 return OK;
1608 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1609 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1610 STATE_CACHE_WRITE_RESPONSE;
1611 return OK;
1614 int HttpCache::Transaction::DoTruncateCachedData() {
1615 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1616 if (!entry_)
1617 return OK;
1618 if (net_log_.IsLogging())
1619 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1620 // Truncate the stream.
1621 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1624 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1625 if (entry_) {
1626 if (net_log_.IsLogging()) {
1627 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1628 result);
1632 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1633 return OK;
1636 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1637 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1638 if (!entry_)
1639 return OK;
1641 if (net_log_.IsLogging())
1642 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1643 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1646 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1647 if (entry_) {
1648 if (net_log_.IsLogging()) {
1649 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1650 result);
1654 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1655 return OK;
1658 int HttpCache::Transaction::DoPartialHeadersReceived() {
1659 new_response_ = NULL;
1660 if (entry_ && !partial_.get() &&
1661 entry_->disk_entry->GetDataSize(kMetadataIndex))
1662 next_state_ = STATE_CACHE_READ_METADATA;
1664 if (!partial_.get())
1665 return OK;
1667 if (reading_) {
1668 if (network_trans_.get()) {
1669 next_state_ = STATE_NETWORK_READ;
1670 } else {
1671 next_state_ = STATE_CACHE_READ_DATA;
1673 } else if (mode_ != NONE) {
1674 // We are about to return the headers for a byte-range request to the user,
1675 // so let's fix them.
1676 partial_->FixResponseHeaders(response_.headers.get(), true);
1678 return OK;
1681 int HttpCache::Transaction::DoCacheReadResponse() {
1682 DCHECK(entry_);
1683 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1685 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1686 read_buf_ = new IOBuffer(io_buf_len_);
1688 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1689 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1690 io_buf_len_, io_callback_);
1693 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1694 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1695 if (result != io_buf_len_ ||
1696 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1697 &response_, &truncated_)) {
1698 return OnCacheReadError(result, true);
1701 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1702 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1703 ReadCertChain();
1705 // Some resources may have slipped in as truncated when they're not.
1706 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1707 if (response_.headers->GetContentLength() == current_size)
1708 truncated_ = false;
1710 // We now have access to the cache entry.
1712 // o if we are a reader for the transaction, then we can start reading the
1713 // cache entry.
1715 // o if we can read or write, then we should check if the cache entry needs
1716 // to be validated and then issue a network request if needed or just read
1717 // from the cache if the cache entry is already valid.
1719 // o if we are set to UPDATE, then we are handling an externally
1720 // conditionalized request (if-modified-since / if-none-match). We check
1721 // if the request headers define a validation request.
1723 switch (mode_) {
1724 case READ:
1725 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1726 result = BeginCacheRead();
1727 break;
1728 case READ_WRITE:
1729 result = BeginPartialCacheValidation();
1730 break;
1731 case UPDATE:
1732 result = BeginExternallyConditionalizedRequest();
1733 break;
1734 case WRITE:
1735 default:
1736 NOTREACHED();
1737 result = ERR_FAILED;
1739 return result;
1742 int HttpCache::Transaction::DoCacheWriteResponse() {
1743 if (entry_) {
1744 if (net_log_.IsLogging())
1745 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1747 return WriteResponseInfoToEntry(false);
1750 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1751 if (entry_) {
1752 if (net_log_.IsLogging())
1753 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1755 return WriteResponseInfoToEntry(true);
1758 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1759 next_state_ = target_state_;
1760 target_state_ = STATE_NONE;
1761 if (!entry_)
1762 return OK;
1763 if (net_log_.IsLogging()) {
1764 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1765 result);
1768 // Balance the AddRef from WriteResponseInfoToEntry.
1769 if (result != io_buf_len_) {
1770 DLOG(ERROR) << "failed to write response info to cache";
1771 DoneWritingToEntry(false);
1773 return OK;
1776 int HttpCache::Transaction::DoCacheReadMetadata() {
1777 DCHECK(entry_);
1778 DCHECK(!response_.metadata.get());
1779 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1781 response_.metadata =
1782 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1784 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1785 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1786 response_.metadata.get(),
1787 response_.metadata->size(),
1788 io_callback_);
1791 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1792 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1793 if (result != response_.metadata->size())
1794 return OnCacheReadError(result, false);
1795 return OK;
1798 int HttpCache::Transaction::DoCacheQueryData() {
1799 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1800 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1803 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1804 if (result == ERR_NOT_IMPLEMENTED) {
1805 // Restart the request overwriting the cache entry.
1806 // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1807 // supports Sparse IO.
1808 return DoRestartPartialRequest();
1810 DCHECK_EQ(OK, result);
1811 if (!cache_.get())
1812 return ERR_UNEXPECTED;
1814 return ValidateEntryHeadersAndContinue();
1817 int HttpCache::Transaction::DoCacheReadData() {
1818 DCHECK(entry_);
1819 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1821 if (net_log_.IsLogging())
1822 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1823 if (partial_.get()) {
1824 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1825 io_callback_);
1828 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1829 read_buf_.get(), io_buf_len_,
1830 io_callback_);
1833 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1834 if (net_log_.IsLogging()) {
1835 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1836 result);
1839 if (!cache_.get())
1840 return ERR_UNEXPECTED;
1842 if (partial_.get()) {
1843 // Partial requests are confusing to report in histograms because they may
1844 // have multiple underlying requests.
1845 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1846 return DoPartialCacheReadCompleted(result);
1849 if (result > 0) {
1850 read_offset_ += result;
1851 } else if (result == 0) { // End of file.
1852 RecordHistograms();
1853 cache_->DoneReadingFromEntry(entry_, this);
1854 entry_ = NULL;
1855 } else {
1856 return OnCacheReadError(result, false);
1858 return result;
1861 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1862 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1863 write_len_ = num_bytes;
1864 if (entry_) {
1865 if (net_log_.IsLogging())
1866 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1869 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1872 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1873 if (entry_) {
1874 if (net_log_.IsLogging()) {
1875 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1876 result);
1879 // Balance the AddRef from DoCacheWriteData.
1880 if (!cache_.get())
1881 return ERR_UNEXPECTED;
1883 if (result != write_len_) {
1884 DLOG(ERROR) << "failed to write response data to cache";
1885 DoneWritingToEntry(false);
1887 // We want to ignore errors writing to disk and just keep reading from
1888 // the network.
1889 result = write_len_;
1890 } else if (!done_reading_ && entry_) {
1891 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1892 int64 body_size = response_.headers->GetContentLength();
1893 if (body_size >= 0 && body_size <= current_size)
1894 done_reading_ = true;
1897 if (partial_.get()) {
1898 // This may be the last request.
1899 if (!(result == 0 && !truncated_ &&
1900 (partial_->IsLastRange() || mode_ == WRITE)))
1901 return DoPartialNetworkReadCompleted(result);
1904 if (result == 0) {
1905 // End of file. This may be the result of a connection problem so see if we
1906 // have to keep the entry around to be flagged as truncated later on.
1907 if (done_reading_ || !entry_ || partial_.get() ||
1908 response_.headers->GetContentLength() <= 0)
1909 DoneWritingToEntry(true);
1912 return result;
1915 //-----------------------------------------------------------------------------
1917 void HttpCache::Transaction::ReadCertChain() {
1918 std::string key =
1919 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
1920 const X509Certificate::OSCertHandles& intermediates =
1921 response_.ssl_info.cert->GetIntermediateCertificates();
1922 int dist_from_root = intermediates.size();
1924 scoped_refptr<SharedChainData> shared_chain_data(
1925 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1926 cache_->cert_cache()->GetCertificate(key,
1927 base::Bind(&OnCertReadIOComplete,
1928 dist_from_root,
1929 true /* is leaf */,
1930 shared_chain_data));
1932 for (X509Certificate::OSCertHandles::const_iterator it =
1933 intermediates.begin();
1934 it != intermediates.end();
1935 ++it) {
1936 --dist_from_root;
1937 key = GetCacheKeyForCert(*it);
1938 cache_->cert_cache()->GetCertificate(key,
1939 base::Bind(&OnCertReadIOComplete,
1940 dist_from_root,
1941 false /* is not leaf */,
1942 shared_chain_data));
1944 DCHECK_EQ(0, dist_from_root);
1947 void HttpCache::Transaction::WriteCertChain() {
1948 const X509Certificate::OSCertHandles& intermediates =
1949 response_.ssl_info.cert->GetIntermediateCertificates();
1950 int dist_from_root = intermediates.size();
1952 scoped_refptr<SharedChainData> shared_chain_data(
1953 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1954 cache_->cert_cache()->SetCertificate(
1955 response_.ssl_info.cert->os_cert_handle(),
1956 base::Bind(&OnCertWriteIOComplete,
1957 dist_from_root,
1958 true /* is leaf */,
1959 shared_chain_data));
1960 for (X509Certificate::OSCertHandles::const_iterator it =
1961 intermediates.begin();
1962 it != intermediates.end();
1963 ++it) {
1964 --dist_from_root;
1965 cache_->cert_cache()->SetCertificate(*it,
1966 base::Bind(&OnCertWriteIOComplete,
1967 dist_from_root,
1968 false /* is not leaf */,
1969 shared_chain_data));
1971 DCHECK_EQ(0, dist_from_root);
1974 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1975 const HttpRequestInfo* request) {
1976 net_log_ = net_log;
1977 request_ = request;
1978 effective_load_flags_ = request_->load_flags;
1980 switch (cache_->mode()) {
1981 case NORMAL:
1982 break;
1983 case RECORD:
1984 // When in record mode, we want to NEVER load from the cache.
1985 // The reason for this is because we save the Set-Cookie headers
1986 // (intentionally). If we read from the cache, we replay them
1987 // prematurely.
1988 effective_load_flags_ |= LOAD_BYPASS_CACHE;
1989 break;
1990 case PLAYBACK:
1991 // When in playback mode, we want to load exclusively from the cache.
1992 effective_load_flags_ |= LOAD_ONLY_FROM_CACHE;
1993 break;
1994 case DISABLE:
1995 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1996 break;
1999 // Some headers imply load flags. The order here is significant.
2001 // LOAD_DISABLE_CACHE : no cache read or write
2002 // LOAD_BYPASS_CACHE : no cache read
2003 // LOAD_VALIDATE_CACHE : no cache read unless validation
2005 // The former modes trump latter modes, so if we find a matching header we
2006 // can stop iterating kSpecialHeaders.
2008 static const struct {
2009 const HeaderNameAndValue* search;
2010 int load_flag;
2011 } kSpecialHeaders[] = {
2012 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
2013 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
2014 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
2017 bool range_found = false;
2018 bool external_validation_error = false;
2020 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
2021 range_found = true;
2023 for (size_t i = 0; i < arraysize(kSpecialHeaders); ++i) {
2024 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
2025 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
2026 break;
2030 // Check for conditionalization headers which may correspond with a
2031 // cache validation request.
2032 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
2033 const ValidationHeaderInfo& info = kValidationHeaders[i];
2034 std::string validation_value;
2035 if (request_->extra_headers.GetHeader(
2036 info.request_header_name, &validation_value)) {
2037 if (!external_validation_.values[i].empty() ||
2038 validation_value.empty()) {
2039 external_validation_error = true;
2041 external_validation_.values[i] = validation_value;
2042 external_validation_.initialized = true;
2046 // We don't support ranges and validation headers.
2047 if (range_found && external_validation_.initialized) {
2048 LOG(WARNING) << "Byte ranges AND validation headers found.";
2049 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2052 // If there is more than one validation header, we can't treat this request as
2053 // a cache validation, since we don't know for sure which header the server
2054 // will give us a response for (and they could be contradictory).
2055 if (external_validation_error) {
2056 LOG(WARNING) << "Multiple or malformed validation headers found.";
2057 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2060 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2061 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2062 partial_.reset(new PartialData);
2063 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
2064 // We will be modifying the actual range requested to the server, so
2065 // let's remove the header here.
2066 custom_request_.reset(new HttpRequestInfo(*request_));
2067 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2068 request_ = custom_request_.get();
2069 partial_->SetHeaders(custom_request_->extra_headers);
2070 } else {
2071 // The range is invalid or we cannot handle it properly.
2072 VLOG(1) << "Invalid byte range found.";
2073 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2074 partial_.reset(NULL);
2079 bool HttpCache::Transaction::ShouldPassThrough() {
2080 // We may have a null disk_cache if there is an error we cannot recover from,
2081 // like not enough disk space, or sharing violations.
2082 if (!cache_->disk_cache_.get())
2083 return true;
2085 // When using the record/playback modes, we always use the cache
2086 // and we never pass through.
2087 if (cache_->mode() == RECORD || cache_->mode() == PLAYBACK)
2088 return false;
2090 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2091 return true;
2093 if (request_->method == "GET" || request_->method == "HEAD")
2094 return false;
2096 if (request_->method == "POST" && request_->upload_data_stream &&
2097 request_->upload_data_stream->identifier()) {
2098 return false;
2101 if (request_->method == "PUT" && request_->upload_data_stream)
2102 return false;
2104 if (request_->method == "DELETE")
2105 return false;
2107 return true;
2110 int HttpCache::Transaction::BeginCacheRead() {
2111 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2112 if (response_.headers->response_code() == 206 || partial_.get()) {
2113 NOTREACHED();
2114 return ERR_CACHE_MISS;
2117 if (request_->method == "HEAD")
2118 FixHeadersForHead();
2120 // We don't have the whole resource.
2121 if (truncated_)
2122 return ERR_CACHE_MISS;
2124 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2125 next_state_ = STATE_CACHE_READ_METADATA;
2127 return OK;
2130 int HttpCache::Transaction::BeginCacheValidation() {
2131 DCHECK(mode_ == READ_WRITE);
2133 ValidationType required_validation = RequiresValidation();
2135 bool skip_validation = (required_validation == VALIDATION_NONE);
2137 if (required_validation == VALIDATION_ASYNCHRONOUS &&
2138 !(request_->method == "GET" && (truncated_ || partial_)) && cache_ &&
2139 cache_->use_stale_while_revalidate()) {
2140 TriggerAsyncValidation();
2141 skip_validation = true;
2144 if (request_->method == "HEAD" &&
2145 (truncated_ || response_.headers->response_code() == 206)) {
2146 DCHECK(!partial_);
2147 if (skip_validation)
2148 return SetupEntryForRead();
2150 // Bail out!
2151 next_state_ = STATE_SEND_REQUEST;
2152 mode_ = NONE;
2153 return OK;
2156 if (truncated_) {
2157 // Truncated entries can cause partial gets, so we shouldn't record this
2158 // load in histograms.
2159 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2160 skip_validation = !partial_->initial_validation();
2163 if (partial_.get() && (is_sparse_ || truncated_) &&
2164 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
2165 // Force revalidation for sparse or truncated entries. Note that we don't
2166 // want to ignore the regular validation logic just because a byte range was
2167 // part of the request.
2168 skip_validation = false;
2171 if (skip_validation) {
2172 // TODO(ricea): Is this pattern okay for asynchronous revalidations?
2173 UpdateTransactionPattern(PATTERN_ENTRY_USED);
2174 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE);
2175 return SetupEntryForRead();
2176 } else {
2177 // Make the network request conditional, to see if we may reuse our cached
2178 // response. If we cannot do so, then we just resort to a normal fetch.
2179 // Our mode remains READ_WRITE for a conditional request. Even if the
2180 // conditionalization fails, we don't switch to WRITE mode until we
2181 // know we won't be falling back to using the cache entry in the
2182 // LOAD_FROM_CACHE_IF_OFFLINE case.
2183 if (!ConditionalizeRequest()) {
2184 couldnt_conditionalize_request_ = true;
2185 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
2186 if (partial_.get())
2187 return DoRestartPartialRequest();
2189 DCHECK_NE(206, response_.headers->response_code());
2191 next_state_ = STATE_SEND_REQUEST;
2193 return OK;
2196 int HttpCache::Transaction::BeginPartialCacheValidation() {
2197 DCHECK(mode_ == READ_WRITE);
2199 if (response_.headers->response_code() != 206 && !partial_.get() &&
2200 !truncated_) {
2201 return BeginCacheValidation();
2204 // Partial requests should not be recorded in histograms.
2205 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2206 if (range_requested_) {
2207 next_state_ = STATE_CACHE_QUERY_DATA;
2208 return OK;
2211 // The request is not for a range, but we have stored just ranges.
2213 if (request_->method == "HEAD")
2214 return BeginCacheValidation();
2216 partial_.reset(new PartialData());
2217 partial_->SetHeaders(request_->extra_headers);
2218 if (!custom_request_.get()) {
2219 custom_request_.reset(new HttpRequestInfo(*request_));
2220 request_ = custom_request_.get();
2223 return ValidateEntryHeadersAndContinue();
2226 // This should only be called once per request.
2227 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2228 DCHECK(mode_ == READ_WRITE);
2230 if (!partial_->UpdateFromStoredHeaders(
2231 response_.headers.get(), entry_->disk_entry, truncated_)) {
2232 return DoRestartPartialRequest();
2235 if (response_.headers->response_code() == 206)
2236 is_sparse_ = true;
2238 if (!partial_->IsRequestedRangeOK()) {
2239 // The stored data is fine, but the request may be invalid.
2240 invalid_range_ = true;
2243 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2244 return OK;
2247 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2248 DCHECK_EQ(UPDATE, mode_);
2249 DCHECK(external_validation_.initialized);
2251 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
2252 if (external_validation_.values[i].empty())
2253 continue;
2254 // Retrieve either the cached response's "etag" or "last-modified" header.
2255 std::string validator;
2256 response_.headers->EnumerateHeader(
2257 NULL,
2258 kValidationHeaders[i].related_response_header_name,
2259 &validator);
2261 if (response_.headers->response_code() != 200 || truncated_ ||
2262 validator.empty() || validator != external_validation_.values[i]) {
2263 // The externally conditionalized request is not a validation request
2264 // for our existing cache entry. Proceed with caching disabled.
2265 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2266 DoneWritingToEntry(true);
2270 // TODO(ricea): This calculation is expensive to perform just to collect
2271 // statistics. Either remove it or use the result, depending on the result of
2272 // the experiment.
2273 ExternallyConditionalizedType type =
2274 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE;
2275 if (mode_ == NONE)
2276 type = EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS;
2277 else if (RequiresValidation())
2278 type = EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION;
2280 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2281 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2282 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2283 type,
2284 EXTERNALLY_CONDITIONALIZED_MAX);
2286 next_state_ = STATE_SEND_REQUEST;
2287 return OK;
2290 int HttpCache::Transaction::RestartNetworkRequest() {
2291 DCHECK(mode_ & WRITE || mode_ == NONE);
2292 DCHECK(network_trans_.get());
2293 DCHECK_EQ(STATE_NONE, next_state_);
2295 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2296 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
2297 if (rv != ERR_IO_PENDING)
2298 return DoLoop(rv);
2299 return rv;
2302 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2303 X509Certificate* client_cert) {
2304 DCHECK(mode_ & WRITE || mode_ == NONE);
2305 DCHECK(network_trans_.get());
2306 DCHECK_EQ(STATE_NONE, next_state_);
2308 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2309 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
2310 if (rv != ERR_IO_PENDING)
2311 return DoLoop(rv);
2312 return rv;
2315 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2316 const AuthCredentials& credentials) {
2317 DCHECK(mode_ & WRITE || mode_ == NONE);
2318 DCHECK(network_trans_.get());
2319 DCHECK_EQ(STATE_NONE, next_state_);
2321 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2322 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
2323 if (rv != ERR_IO_PENDING)
2324 return DoLoop(rv);
2325 return rv;
2328 ValidationType HttpCache::Transaction::RequiresValidation() {
2329 // TODO(darin): need to do more work here:
2330 // - make sure we have a matching request method
2331 // - watch out for cached responses that depend on authentication
2333 // In playback mode, nothing requires validation.
2334 if (cache_->mode() == net::HttpCache::PLAYBACK)
2335 return VALIDATION_NONE;
2337 if (response_.vary_data.is_valid() &&
2338 !response_.vary_data.MatchesRequest(*request_,
2339 *response_.headers.get())) {
2340 vary_mismatch_ = true;
2341 return VALIDATION_SYNCHRONOUS;
2344 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
2345 return VALIDATION_NONE;
2347 if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION))
2348 return VALIDATION_SYNCHRONOUS;
2350 if (request_->method == "PUT" || request_->method == "DELETE")
2351 return VALIDATION_SYNCHRONOUS;
2353 ValidationType validation_required_by_headers =
2354 response_.headers->RequiresValidation(
2355 response_.request_time, response_.response_time, Time::Now());
2357 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) {
2358 // Asynchronous revalidation is only supported for GET and HEAD methods.
2359 if (request_->method != "GET" && request_->method != "HEAD")
2360 return VALIDATION_SYNCHRONOUS;
2363 return validation_required_by_headers;
2366 bool HttpCache::Transaction::ConditionalizeRequest() {
2367 DCHECK(response_.headers.get());
2369 if (request_->method == "PUT" || request_->method == "DELETE")
2370 return false;
2372 // This only makes sense for cached 200 or 206 responses.
2373 if (response_.headers->response_code() != 200 &&
2374 response_.headers->response_code() != 206) {
2375 return false;
2378 if (response_.headers->response_code() == 206 &&
2379 !response_.headers->HasStrongValidators()) {
2380 return false;
2383 // Just use the first available ETag and/or Last-Modified header value.
2384 // TODO(darin): Or should we use the last?
2386 std::string etag_value;
2387 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2388 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2390 std::string last_modified_value;
2391 if (!vary_mismatch_) {
2392 response_.headers->EnumerateHeader(NULL, "last-modified",
2393 &last_modified_value);
2396 if (etag_value.empty() && last_modified_value.empty())
2397 return false;
2399 if (!partial_.get()) {
2400 // Need to customize the request, so this forces us to allocate :(
2401 custom_request_.reset(new HttpRequestInfo(*request_));
2402 request_ = custom_request_.get();
2404 DCHECK(custom_request_.get());
2406 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2407 !invalid_range_;
2409 if (!use_if_range) {
2410 // stale-while-revalidate is not useful when we only have a partial response
2411 // cached, so don't set the header in that case.
2412 HttpResponseHeaders::FreshnessLifetimes lifetimes =
2413 response_.headers->GetFreshnessLifetimes(response_.response_time);
2414 if (lifetimes.staleness > TimeDelta()) {
2415 TimeDelta current_age = response_.headers->GetCurrentAge(
2416 response_.request_time, response_.response_time, Time::Now());
2418 custom_request_->extra_headers.SetHeader(
2419 kFreshnessHeader,
2420 base::StringPrintf("max-age=%" PRId64
2421 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2422 lifetimes.freshness.InSeconds(),
2423 lifetimes.staleness.InSeconds(),
2424 current_age.InSeconds()));
2428 if (!etag_value.empty()) {
2429 if (use_if_range) {
2430 // We don't want to switch to WRITE mode if we don't have this block of a
2431 // byte-range request because we may have other parts cached.
2432 custom_request_->extra_headers.SetHeader(
2433 HttpRequestHeaders::kIfRange, etag_value);
2434 } else {
2435 custom_request_->extra_headers.SetHeader(
2436 HttpRequestHeaders::kIfNoneMatch, etag_value);
2438 // For byte-range requests, make sure that we use only one way to validate
2439 // the request.
2440 if (partial_.get() && !partial_->IsCurrentRangeCached())
2441 return true;
2444 if (!last_modified_value.empty()) {
2445 if (use_if_range) {
2446 custom_request_->extra_headers.SetHeader(
2447 HttpRequestHeaders::kIfRange, last_modified_value);
2448 } else {
2449 custom_request_->extra_headers.SetHeader(
2450 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2454 return true;
2457 // We just received some headers from the server. We may have asked for a range,
2458 // in which case partial_ has an object. This could be the first network request
2459 // we make to fulfill the original request, or we may be already reading (from
2460 // the net and / or the cache). If we are not expecting a certain response, we
2461 // just bypass the cache for this request (but again, maybe we are reading), and
2462 // delete partial_ (so we are not able to "fix" the headers that we return to
2463 // the user). This results in either a weird response for the caller (we don't
2464 // expect it after all), or maybe a range that was not exactly what it was asked
2465 // for.
2467 // If the server is simply telling us that the resource has changed, we delete
2468 // the cached entry and restart the request as the caller intended (by returning
2469 // false from this method). However, we may not be able to do that at any point,
2470 // for instance if we already returned the headers to the user.
2472 // WARNING: Whenever this code returns false, it has to make sure that the next
2473 // time it is called it will return true so that we don't keep retrying the
2474 // request.
2475 bool HttpCache::Transaction::ValidatePartialResponse() {
2476 const HttpResponseHeaders* headers = new_response_->headers.get();
2477 int response_code = headers->response_code();
2478 bool partial_response = (response_code == 206);
2479 handling_206_ = false;
2481 if (!entry_ || request_->method != "GET")
2482 return true;
2484 if (invalid_range_) {
2485 // We gave up trying to match this request with the stored data. If the
2486 // server is ok with the request, delete the entry, otherwise just ignore
2487 // this request
2488 DCHECK(!reading_);
2489 if (partial_response || response_code == 200) {
2490 DoomPartialEntry(true);
2491 mode_ = NONE;
2492 } else {
2493 if (response_code == 304)
2494 FailRangeRequest();
2495 IgnoreRangeRequest();
2497 return true;
2500 if (!partial_.get()) {
2501 // We are not expecting 206 but we may have one.
2502 if (partial_response)
2503 IgnoreRangeRequest();
2505 return true;
2508 // TODO(rvargas): Do we need to consider other results here?.
2509 bool failure = response_code == 200 || response_code == 416;
2511 if (partial_->IsCurrentRangeCached()) {
2512 // We asked for "If-None-Match: " so a 206 means a new object.
2513 if (partial_response)
2514 failure = true;
2516 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2517 return true;
2518 } else {
2519 // We asked for "If-Range: " so a 206 means just another range.
2520 if (partial_response && partial_->ResponseHeadersOK(headers)) {
2521 handling_206_ = true;
2522 return true;
2525 if (!reading_ && !is_sparse_ && !partial_response) {
2526 // See if we can ignore the fact that we issued a byte range request.
2527 // If the server sends 200, just store it. If it sends an error, redirect
2528 // or something else, we may store the response as long as we didn't have
2529 // anything already stored.
2530 if (response_code == 200 ||
2531 (!truncated_ && response_code != 304 && response_code != 416)) {
2532 // The server is sending something else, and we can save it.
2533 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2534 partial_.reset();
2535 truncated_ = false;
2536 return true;
2540 // 304 is not expected here, but we'll spare the entry (unless it was
2541 // truncated).
2542 if (truncated_)
2543 failure = true;
2546 if (failure) {
2547 // We cannot truncate this entry, it has to be deleted.
2548 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2549 DoomPartialEntry(false);
2550 mode_ = NONE;
2551 if (!reading_ && !partial_->IsLastRange()) {
2552 // We'll attempt to issue another network request, this time without us
2553 // messing up the headers.
2554 partial_->RestoreHeaders(&custom_request_->extra_headers);
2555 partial_.reset();
2556 truncated_ = false;
2557 return false;
2559 LOG(WARNING) << "Failed to revalidate partial entry";
2560 partial_.reset();
2561 return true;
2564 IgnoreRangeRequest();
2565 return true;
2568 void HttpCache::Transaction::IgnoreRangeRequest() {
2569 // We have a problem. We may or may not be reading already (in which case we
2570 // returned the headers), but we'll just pretend that this request is not
2571 // using the cache and see what happens. Most likely this is the first
2572 // response from the server (it's not changing its mind midway, right?).
2573 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2574 if (mode_ & WRITE)
2575 DoneWritingToEntry(mode_ != WRITE);
2576 else if (mode_ & READ && entry_)
2577 cache_->DoneReadingFromEntry(entry_, this);
2579 partial_.reset(NULL);
2580 entry_ = NULL;
2581 mode_ = NONE;
2584 void HttpCache::Transaction::FixHeadersForHead() {
2585 if (response_.headers->response_code() == 206) {
2586 response_.headers->RemoveHeader("Content-Length");
2587 response_.headers->RemoveHeader("Content-Range");
2588 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2592 void HttpCache::Transaction::TriggerAsyncValidation() {
2593 DCHECK(!request_->upload_data_stream);
2594 BoundNetLog async_revalidation_net_log(
2595 BoundNetLog::Make(net_log_.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION));
2596 net_log_.AddEvent(
2597 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC,
2598 async_revalidation_net_log.source().ToEventParametersCallback());
2599 async_revalidation_net_log.BeginEvent(
2600 NetLog::TYPE_ASYNC_REVALIDATION,
2601 base::Bind(
2602 &NetLogAsyncRevalidationInfoCallback, net_log_.source(), request_));
2603 base::MessageLoop::current()->PostTask(
2604 FROM_HERE,
2605 base::Bind(&HttpCache::PerformAsyncValidation,
2606 cache_, // cache_ is a weak pointer.
2607 *request_,
2608 async_revalidation_net_log));
2611 void HttpCache::Transaction::FailRangeRequest() {
2612 response_ = *new_response_;
2613 partial_->FixResponseHeaders(response_.headers.get(), false);
2616 int HttpCache::Transaction::SetupEntryForRead() {
2617 if (network_trans_)
2618 ResetNetworkTransaction();
2619 if (partial_.get()) {
2620 if (truncated_ || is_sparse_ || !invalid_range_) {
2621 // We are going to return the saved response headers to the caller, so
2622 // we may need to adjust them first.
2623 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2624 return OK;
2625 } else {
2626 partial_.reset();
2629 cache_->ConvertWriterToReader(entry_);
2630 mode_ = READ;
2632 if (request_->method == "HEAD")
2633 FixHeadersForHead();
2635 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2636 next_state_ = STATE_CACHE_READ_METADATA;
2637 return OK;
2641 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2642 read_buf_ = data;
2643 io_buf_len_ = data_len;
2644 next_state_ = STATE_NETWORK_READ;
2645 return DoLoop(OK);
2648 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2649 if (request_->method == "HEAD")
2650 return 0;
2652 read_buf_ = data;
2653 io_buf_len_ = data_len;
2654 next_state_ = STATE_CACHE_READ_DATA;
2655 return DoLoop(OK);
2658 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2659 IOBuffer* data, int data_len,
2660 const CompletionCallback& callback) {
2661 if (!entry_)
2662 return data_len;
2664 int rv = 0;
2665 if (!partial_.get() || !data_len) {
2666 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2667 true);
2668 } else {
2669 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2671 return rv;
2674 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2675 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2676 if (!entry_)
2677 return OK;
2679 // Do not cache no-store content (unless we are record mode). Do not cache
2680 // content with cert errors either. This is to prevent not reporting net
2681 // errors when loading a resource from the cache. When we load a page over
2682 // HTTPS with a cert error we show an SSL blocking page. If the user clicks
2683 // proceed we reload the resource ignoring the errors. The loaded resource
2684 // is then cached. If that resource is subsequently loaded from the cache,
2685 // no net error is reported (even though the cert status contains the actual
2686 // errors) and no SSL blocking page is shown. An alternative would be to
2687 // reverse-map the cert status to a net error and replay the net error.
2688 if ((cache_->mode() != RECORD &&
2689 response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2690 net::IsCertStatusError(response_.ssl_info.cert_status)) {
2691 DoneWritingToEntry(false);
2692 if (net_log_.IsLogging())
2693 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2694 return OK;
2697 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2698 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2699 WriteCertChain();
2701 // When writing headers, we normally only write the non-transient
2702 // headers; when in record mode, record everything.
2703 bool skip_transient_headers = (cache_->mode() != RECORD);
2705 if (truncated)
2706 DCHECK_EQ(200, response_.headers->response_code());
2708 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2709 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2710 data->Done();
2712 io_buf_len_ = data->pickle()->size();
2713 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2714 io_buf_len_, io_callback_, true);
2717 int HttpCache::Transaction::AppendResponseDataToEntry(
2718 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2719 if (!entry_ || !data_len)
2720 return data_len;
2722 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2723 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2724 callback);
2727 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2728 if (!entry_)
2729 return;
2731 RecordHistograms();
2733 cache_->DoneWritingToEntry(entry_, success);
2734 entry_ = NULL;
2735 mode_ = NONE; // switch to 'pass through' mode
2738 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2739 DLOG(ERROR) << "ReadData failed: " << result;
2740 const int result_for_histogram = std::max(0, -result);
2741 if (restart) {
2742 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2743 result_for_histogram);
2744 } else {
2745 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2746 result_for_histogram);
2749 // Avoid using this entry in the future.
2750 if (cache_.get())
2751 cache_->DoomActiveEntry(cache_key_);
2753 if (restart) {
2754 DCHECK(!reading_);
2755 DCHECK(!network_trans_.get());
2756 cache_->DoneWithEntry(entry_, this, false);
2757 entry_ = NULL;
2758 is_sparse_ = false;
2759 partial_.reset();
2760 next_state_ = STATE_GET_BACKEND;
2761 return OK;
2764 return ERR_CACHE_READ_FAILURE;
2767 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
2768 if (entry_lock_waiting_since_ != start_time)
2769 return;
2771 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
2773 if (!cache_)
2774 return;
2776 cache_->RemovePendingTransaction(this);
2777 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
2780 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2781 DVLOG(2) << "DoomPartialEntry";
2782 int rv = cache_->DoomEntry(cache_key_, NULL);
2783 DCHECK_EQ(OK, rv);
2784 cache_->DoneWithEntry(entry_, this, false);
2785 entry_ = NULL;
2786 is_sparse_ = false;
2787 if (delete_object)
2788 partial_.reset(NULL);
2791 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2792 partial_->OnNetworkReadCompleted(result);
2794 if (result == 0) {
2795 // We need to move on to the next range.
2796 ResetNetworkTransaction();
2797 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2799 return result;
2802 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2803 partial_->OnCacheReadCompleted(result);
2805 if (result == 0 && mode_ == READ_WRITE) {
2806 // We need to move on to the next range.
2807 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2808 } else if (result < 0) {
2809 return OnCacheReadError(result, false);
2811 return result;
2814 int HttpCache::Transaction::DoRestartPartialRequest() {
2815 // The stored data cannot be used. Get rid of it and restart this request.
2816 // We need to also reset the |truncated_| flag as a new entry is created.
2817 DoomPartialEntry(!range_requested_);
2818 mode_ = WRITE;
2819 truncated_ = false;
2820 next_state_ = STATE_INIT_ENTRY;
2821 return OK;
2824 void HttpCache::Transaction::ResetNetworkTransaction() {
2825 DCHECK(!old_network_trans_load_timing_);
2826 DCHECK(network_trans_);
2827 LoadTimingInfo load_timing;
2828 if (network_trans_->GetLoadTimingInfo(&load_timing))
2829 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2830 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2831 network_trans_.reset();
2834 // Histogram data from the end of 2010 show the following distribution of
2835 // response headers:
2837 // Content-Length............... 87%
2838 // Date......................... 98%
2839 // Last-Modified................ 49%
2840 // Etag......................... 19%
2841 // Accept-Ranges: bytes......... 25%
2842 // Accept-Ranges: none.......... 0.4%
2843 // Strong Validator............. 50%
2844 // Strong Validator + ranges.... 24%
2845 // Strong Validator + CL........ 49%
2847 bool HttpCache::Transaction::CanResume(bool has_data) {
2848 // Double check that there is something worth keeping.
2849 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2850 return false;
2852 if (request_->method != "GET")
2853 return false;
2855 // Note that if this is a 206, content-length was already fixed after calling
2856 // PartialData::ResponseHeadersOK().
2857 if (response_.headers->GetContentLength() <= 0 ||
2858 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2859 !response_.headers->HasStrongValidators()) {
2860 return false;
2863 return true;
2866 void HttpCache::Transaction::UpdateTransactionPattern(
2867 TransactionPattern new_transaction_pattern) {
2868 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2869 return;
2870 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2871 new_transaction_pattern == PATTERN_NOT_COVERED);
2872 transaction_pattern_ = new_transaction_pattern;
2875 void HttpCache::Transaction::RecordHistograms() {
2876 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2877 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2878 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2879 cache_->mode() != NORMAL || request_->method != "GET") {
2880 return;
2882 UMA_HISTOGRAM_ENUMERATION(
2883 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2884 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2885 return;
2886 DCHECK(!range_requested_);
2887 DCHECK(!first_cache_access_since_.is_null());
2889 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2891 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2893 bool did_send_request = !send_request_since_.is_null();
2894 DCHECK(
2895 (did_send_request &&
2896 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2897 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2898 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2899 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2900 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2902 if (!did_send_request) {
2903 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2904 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2905 return;
2908 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2909 int64 before_send_percent = (total_time.ToInternalValue() == 0) ?
2910 0 : before_send_time * 100 / total_time;
2911 DCHECK_GE(before_send_percent, 0);
2912 DCHECK_LE(before_send_percent, 100);
2913 base::HistogramBase::Sample before_send_sample =
2914 static_cast<base::HistogramBase::Sample>(before_send_percent);
2916 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2917 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2918 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample);
2920 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2921 // below this comment after we have received initial data.
2922 switch (transaction_pattern_) {
2923 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2924 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2925 before_send_time);
2926 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2927 before_send_sample);
2928 break;
2930 case PATTERN_ENTRY_NOT_CACHED: {
2931 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2932 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2933 before_send_sample);
2934 break;
2936 case PATTERN_ENTRY_VALIDATED: {
2937 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2938 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2939 before_send_sample);
2940 break;
2942 case PATTERN_ENTRY_UPDATED: {
2943 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2944 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2945 before_send_sample);
2946 break;
2948 default:
2949 NOTREACHED();
2953 void HttpCache::Transaction::OnIOComplete(int result) {
2954 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2955 tracked_objects::ScopedTracker tracking_profile(
2956 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 Transaction::OnIOComplete"));
2958 DoLoop(result);
2961 } // namespace net