Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blobd5cb94d7909b233e49bbad53cf70e3806372dccc
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" // For OS_POSIX
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/callback_helpers.h"
18 #include "base/compiler_specific.h"
19 #include "base/format_macros.h"
20 #include "base/location.h"
21 #include "base/metrics/histogram_macros.h"
22 #include "base/metrics/sparse_histogram.h"
23 #include "base/profiler/scoped_tracker.h"
24 #include "base/single_thread_task_runner.h"
25 #include "base/strings/string_number_conversions.h" // For HexEncode.
26 #include "base/strings/string_piece.h"
27 #include "base/strings/string_util.h" // For LowerCaseEqualsASCII.
28 #include "base/strings/stringprintf.h"
29 #include "base/thread_task_runner_handle.h"
30 #include "base/time/clock.h"
31 #include "base/values.h"
32 #include "net/base/auth.h"
33 #include "net/base/load_flags.h"
34 #include "net/base/load_timing_info.h"
35 #include "net/base/net_errors.h"
36 #include "net/base/upload_data_stream.h"
37 #include "net/cert/cert_status_flags.h"
38 #include "net/cert/x509_certificate.h"
39 #include "net/disk_cache/disk_cache.h"
40 #include "net/http/disk_based_cert_cache.h"
41 #include "net/http/http_network_session.h"
42 #include "net/http/http_request_info.h"
43 #include "net/http/http_util.h"
44 #include "net/ssl/ssl_cert_request_info.h"
45 #include "net/ssl/ssl_config_service.h"
47 using base::Time;
48 using base::TimeDelta;
49 using base::TimeTicks;
51 namespace net {
53 namespace {
55 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
56 static const char kFreshnessHeader[] = "Resource-Freshness";
58 // Stores data relevant to the statistics of writing and reading entire
59 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
60 // of certificates in the chain that have pending operations in the
61 // DiskBasedCertCache. |start_time| is the time that the read and write
62 // commands began being issued to the DiskBasedCertCache.
63 // TODO(brandonsalmon): Remove this when it is no longer necessary to
64 // collect data.
65 class SharedChainData : public base::RefCounted<SharedChainData> {
66 public:
67 SharedChainData(int num_ops, TimeTicks start)
68 : num_pending_ops(num_ops), start_time(start) {}
70 int num_pending_ops;
71 TimeTicks start_time;
73 private:
74 friend class base::RefCounted<SharedChainData>;
75 ~SharedChainData() {}
76 DISALLOW_COPY_AND_ASSIGN(SharedChainData);
79 // Used to obtain a cache entry key for an OSCertHandle.
80 // TODO(brandonsalmon): Remove this when cache keys are stored
81 // and no longer have to be recomputed to retrieve the OSCertHandle
82 // from the disk.
83 std::string GetCacheKeyForCert(X509Certificate::OSCertHandle cert_handle) {
84 SHA1HashValue fingerprint =
85 X509Certificate::CalculateFingerprint(cert_handle);
87 return "cert:" +
88 base::HexEncode(fingerprint.data, arraysize(fingerprint.data));
91 // |dist_from_root| indicates the position of the read certificate in the
92 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
93 // whether or not the read certificate was the leaf of the chain.
94 // |shared_chain_data| contains data shared by each certificate in
95 // the chain.
96 void OnCertReadIOComplete(
97 int dist_from_root,
98 bool is_leaf,
99 const scoped_refptr<SharedChainData>& shared_chain_data,
100 X509Certificate::OSCertHandle cert_handle) {
101 // If |num_pending_ops| is one, this was the last pending read operation
102 // for this chain of certificates. The total time used to read the chain
103 // can be calculated by subtracting the starting time from Now().
104 shared_chain_data->num_pending_ops--;
105 if (!shared_chain_data->num_pending_ops) {
106 const TimeDelta read_chain_wait =
107 TimeTicks::Now() - shared_chain_data->start_time;
108 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
109 read_chain_wait,
110 base::TimeDelta::FromMilliseconds(1),
111 base::TimeDelta::FromMinutes(10),
112 50);
115 bool success = (cert_handle != NULL);
116 if (is_leaf)
117 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success);
119 if (success)
120 UMA_HISTOGRAM_CUSTOM_COUNTS(
121 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root, 0, 10, 7);
122 else
123 UMA_HISTOGRAM_CUSTOM_COUNTS(
124 "DiskBasedCertCache.CertIoReadFailure", dist_from_root, 0, 10, 7);
127 // |dist_from_root| indicates the position of the written certificate in the
128 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
129 // whether or not the written certificate was the leaf of the chain.
130 // |shared_chain_data| contains data shared by each certificate in
131 // the chain.
132 void OnCertWriteIOComplete(
133 int dist_from_root,
134 bool is_leaf,
135 const scoped_refptr<SharedChainData>& shared_chain_data,
136 const std::string& key) {
137 // If |num_pending_ops| is one, this was the last pending write operation
138 // for this chain of certificates. The total time used to write the chain
139 // can be calculated by subtracting the starting time from Now().
140 shared_chain_data->num_pending_ops--;
141 if (!shared_chain_data->num_pending_ops) {
142 const TimeDelta write_chain_wait =
143 TimeTicks::Now() - shared_chain_data->start_time;
144 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
145 write_chain_wait,
146 base::TimeDelta::FromMilliseconds(1),
147 base::TimeDelta::FromMinutes(10),
148 50);
151 bool success = !key.empty();
152 if (is_leaf)
153 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success);
155 if (success)
156 UMA_HISTOGRAM_CUSTOM_COUNTS(
157 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root, 0, 10, 7);
158 else
159 UMA_HISTOGRAM_CUSTOM_COUNTS(
160 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root, 0, 10, 7);
163 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
164 // a "non-error response" is one with a 2xx (Successful) or 3xx
165 // (Redirection) status code.
166 bool NonErrorResponse(int status_code) {
167 int status_code_range = status_code / 100;
168 return status_code_range == 2 || status_code_range == 3;
171 void RecordNoStoreHeaderHistogram(int load_flags,
172 const HttpResponseInfo* response) {
173 if (load_flags & LOAD_MAIN_FRAME) {
174 UMA_HISTOGRAM_BOOLEAN(
175 "Net.MainFrameNoStore",
176 response->headers->HasHeaderValue("cache-control", "no-store"));
180 enum ExternallyConditionalizedType {
181 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION,
182 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE,
183 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS,
184 EXTERNALLY_CONDITIONALIZED_MAX
187 } // namespace
189 struct HeaderNameAndValue {
190 const char* name;
191 const char* value;
194 // If the request includes one of these request headers, then avoid caching
195 // to avoid getting confused.
196 static const HeaderNameAndValue kPassThroughHeaders[] = {
197 { "if-unmodified-since", NULL }, // causes unexpected 412s
198 { "if-match", NULL }, // causes unexpected 412s
199 { "if-range", NULL },
200 { NULL, NULL }
203 struct ValidationHeaderInfo {
204 const char* request_header_name;
205 const char* related_response_header_name;
208 static const ValidationHeaderInfo kValidationHeaders[] = {
209 { "if-modified-since", "last-modified" },
210 { "if-none-match", "etag" },
213 // If the request includes one of these request headers, then avoid reusing
214 // our cached copy if any.
215 static const HeaderNameAndValue kForceFetchHeaders[] = {
216 { "cache-control", "no-cache" },
217 { "pragma", "no-cache" },
218 { NULL, NULL }
221 // If the request includes one of these request headers, then force our
222 // cached copy (if any) to be revalidated before reusing it.
223 static const HeaderNameAndValue kForceValidateHeaders[] = {
224 { "cache-control", "max-age=0" },
225 { NULL, NULL }
228 static bool HeaderMatches(const HttpRequestHeaders& headers,
229 const HeaderNameAndValue* search) {
230 for (; search->name; ++search) {
231 std::string header_value;
232 if (!headers.GetHeader(search->name, &header_value))
233 continue;
235 if (!search->value)
236 return true;
238 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
239 while (v.GetNext()) {
240 if (base::LowerCaseEqualsASCII(
241 base::StringPiece(v.value_begin(), v.value_end()), search->value))
242 return true;
245 return false;
248 //-----------------------------------------------------------------------------
250 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
251 : next_state_(STATE_NONE),
252 request_(NULL),
253 priority_(priority),
254 cache_(cache->GetWeakPtr()),
255 entry_(NULL),
256 new_entry_(NULL),
257 new_response_(NULL),
258 mode_(NONE),
259 reading_(false),
260 invalid_range_(false),
261 truncated_(false),
262 is_sparse_(false),
263 range_requested_(false),
264 handling_206_(false),
265 cache_pending_(false),
266 done_reading_(false),
267 vary_mismatch_(false),
268 couldnt_conditionalize_request_(false),
269 bypass_lock_for_test_(false),
270 fail_conditionalization_for_test_(false),
271 io_buf_len_(0),
272 read_offset_(0),
273 effective_load_flags_(0),
274 write_len_(0),
275 transaction_pattern_(PATTERN_UNDEFINED),
276 total_received_bytes_(0),
277 websocket_handshake_stream_base_create_helper_(NULL),
278 weak_factory_(this) {
279 static_assert(HttpCache::Transaction::kNumValidationHeaders ==
280 arraysize(kValidationHeaders),
281 "invalid number of validation headers");
283 io_callback_ = base::Bind(&Transaction::OnIOComplete,
284 weak_factory_.GetWeakPtr());
287 HttpCache::Transaction::~Transaction() {
288 // We may have to issue another IO, but we should never invoke the callback_
289 // after this point.
290 callback_.Reset();
292 if (cache_) {
293 if (entry_) {
294 bool cancel_request = reading_ && response_.headers.get();
295 if (cancel_request) {
296 if (partial_) {
297 entry_->disk_entry->CancelSparseIO();
298 } else {
299 cancel_request &= (response_.headers->response_code() == 200);
303 cache_->DoneWithEntry(entry_, this, cancel_request);
304 } else if (cache_pending_) {
305 cache_->RemovePendingTransaction(this);
310 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
311 const CompletionCallback& callback) {
312 DCHECK(buf);
313 DCHECK_GT(buf_len, 0);
314 DCHECK(!callback.is_null());
315 if (!cache_.get() || !entry_)
316 return ERR_UNEXPECTED;
318 // We don't need to track this operation for anything.
319 // It could be possible to check if there is something already written and
320 // avoid writing again (it should be the same, right?), but let's allow the
321 // caller to "update" the contents with something new.
322 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
323 callback, true);
326 bool HttpCache::Transaction::AddTruncatedFlag() {
327 DCHECK(mode_ & WRITE || mode_ == NONE);
329 // Don't set the flag for sparse entries.
330 if (partial_ && !truncated_)
331 return true;
333 if (!CanResume(true))
334 return false;
336 // We may have received the whole resource already.
337 if (done_reading_)
338 return true;
340 truncated_ = true;
341 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
342 DoLoop(OK);
343 return true;
346 LoadState HttpCache::Transaction::GetWriterLoadState() const {
347 if (network_trans_.get())
348 return network_trans_->GetLoadState();
349 if (entry_ || !request_)
350 return LOAD_STATE_IDLE;
351 return LOAD_STATE_WAITING_FOR_CACHE;
354 const BoundNetLog& HttpCache::Transaction::net_log() const {
355 return net_log_;
358 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
359 const CompletionCallback& callback,
360 const BoundNetLog& net_log) {
361 DCHECK(request);
362 DCHECK(!callback.is_null());
364 // Ensure that we only have one asynchronous call at a time.
365 DCHECK(callback_.is_null());
366 DCHECK(!reading_);
367 DCHECK(!network_trans_.get());
368 DCHECK(!entry_);
369 DCHECK_EQ(next_state_, STATE_NONE);
371 if (!cache_.get())
372 return ERR_UNEXPECTED;
374 SetRequest(net_log, request);
376 // We have to wait until the backend is initialized so we start the SM.
377 next_state_ = STATE_GET_BACKEND;
378 int rv = DoLoop(OK);
380 // Setting this here allows us to check for the existence of a callback_ to
381 // determine if we are still inside Start.
382 if (rv == ERR_IO_PENDING)
383 callback_ = callback;
385 return rv;
388 int HttpCache::Transaction::RestartIgnoringLastError(
389 const CompletionCallback& callback) {
390 DCHECK(!callback.is_null());
392 // Ensure that we only have one asynchronous call at a time.
393 DCHECK(callback_.is_null());
395 if (!cache_.get())
396 return ERR_UNEXPECTED;
398 int rv = RestartNetworkRequest();
400 if (rv == ERR_IO_PENDING)
401 callback_ = callback;
403 return rv;
406 int HttpCache::Transaction::RestartWithCertificate(
407 X509Certificate* client_cert,
408 const CompletionCallback& callback) {
409 DCHECK(!callback.is_null());
411 // Ensure that we only have one asynchronous call at a time.
412 DCHECK(callback_.is_null());
414 if (!cache_.get())
415 return ERR_UNEXPECTED;
417 int rv = RestartNetworkRequestWithCertificate(client_cert);
419 if (rv == ERR_IO_PENDING)
420 callback_ = callback;
422 return rv;
425 int HttpCache::Transaction::RestartWithAuth(
426 const AuthCredentials& credentials,
427 const CompletionCallback& callback) {
428 DCHECK(auth_response_.headers.get());
429 DCHECK(!callback.is_null());
431 // Ensure that we only have one asynchronous call at a time.
432 DCHECK(callback_.is_null());
434 if (!cache_.get())
435 return ERR_UNEXPECTED;
437 // Clear the intermediate response since we are going to start over.
438 auth_response_ = HttpResponseInfo();
440 int rv = RestartNetworkRequestWithAuth(credentials);
442 if (rv == ERR_IO_PENDING)
443 callback_ = callback;
445 return rv;
448 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
449 if (!network_trans_.get())
450 return false;
451 return network_trans_->IsReadyToRestartForAuth();
454 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
455 const CompletionCallback& callback) {
456 DCHECK_EQ(next_state_, STATE_NONE);
457 DCHECK(buf);
458 DCHECK_GT(buf_len, 0);
459 DCHECK(!callback.is_null());
461 DCHECK(callback_.is_null());
463 if (!cache_.get())
464 return ERR_UNEXPECTED;
466 // If we have an intermediate auth response at this point, then it means the
467 // user wishes to read the network response (the error page). If there is a
468 // previous response in the cache then we should leave it intact.
469 if (auth_response_.headers.get() && mode_ != NONE) {
470 UpdateTransactionPattern(PATTERN_NOT_COVERED);
471 DCHECK(mode_ & WRITE);
472 DoneWritingToEntry(mode_ == READ_WRITE);
473 mode_ = NONE;
476 reading_ = true;
477 read_buf_ = buf;
478 io_buf_len_ = buf_len;
479 if (network_trans_) {
480 DCHECK(mode_ == WRITE || mode_ == NONE ||
481 (mode_ == READ_WRITE && partial_));
482 next_state_ = STATE_NETWORK_READ;
483 } else {
484 DCHECK(mode_ == READ || (mode_ == READ_WRITE && partial_));
485 next_state_ = STATE_CACHE_READ_DATA;
488 int rv = DoLoop(OK);
490 if (rv == ERR_IO_PENDING) {
491 DCHECK(callback_.is_null());
492 callback_ = callback;
494 return rv;
497 void HttpCache::Transaction::StopCaching() {
498 // We really don't know where we are now. Hopefully there is no operation in
499 // progress, but nothing really prevents this method to be called after we
500 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
501 // point because we need the state machine for that (and even if we are really
502 // free, that would be an asynchronous operation). In other words, keep the
503 // entry how it is (it will be marked as truncated at destruction), and let
504 // the next piece of code that executes know that we are now reading directly
505 // from the net.
506 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
507 // future request for the resource will be blocked on this one.
508 // Fix this.
509 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
510 !is_sparse_ && !range_requested_) {
511 mode_ = NONE;
515 bool HttpCache::Transaction::GetFullRequestHeaders(
516 HttpRequestHeaders* headers) const {
517 if (network_trans_)
518 return network_trans_->GetFullRequestHeaders(headers);
520 // TODO(ttuttle): Read headers from cache.
521 return false;
524 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
525 int64 total_received_bytes = total_received_bytes_;
526 if (network_trans_)
527 total_received_bytes += network_trans_->GetTotalReceivedBytes();
528 return total_received_bytes;
531 void HttpCache::Transaction::DoneReading() {
532 if (cache_.get() && entry_) {
533 DCHECK_NE(mode_, UPDATE);
534 if (mode_ & WRITE) {
535 DoneWritingToEntry(true);
536 } else if (mode_ & READ) {
537 // It is necessary to check mode_ & READ because it is possible
538 // for mode_ to be NONE and entry_ non-NULL with a write entry
539 // if StopCaching was called.
540 cache_->DoneReadingFromEntry(entry_, this);
541 entry_ = NULL;
546 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
547 // Null headers means we encountered an error or haven't a response yet
548 if (auth_response_.headers.get())
549 return &auth_response_;
550 return &response_;
553 LoadState HttpCache::Transaction::GetLoadState() const {
554 LoadState state = GetWriterLoadState();
555 if (state != LOAD_STATE_WAITING_FOR_CACHE)
556 return state;
558 if (cache_.get())
559 return cache_->GetLoadStateForPendingTransaction(this);
561 return LOAD_STATE_IDLE;
564 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
565 if (network_trans_.get())
566 return network_trans_->GetUploadProgress();
567 return final_upload_progress_;
570 void HttpCache::Transaction::SetQuicServerInfo(
571 QuicServerInfo* quic_server_info) {}
573 bool HttpCache::Transaction::GetLoadTimingInfo(
574 LoadTimingInfo* load_timing_info) const {
575 if (network_trans_)
576 return network_trans_->GetLoadTimingInfo(load_timing_info);
578 if (old_network_trans_load_timing_) {
579 *load_timing_info = *old_network_trans_load_timing_;
580 return true;
583 if (first_cache_access_since_.is_null())
584 return false;
586 // If the cache entry was opened, return that time.
587 load_timing_info->send_start = first_cache_access_since_;
588 // This time doesn't make much sense when reading from the cache, so just use
589 // the same time as send_start.
590 load_timing_info->send_end = first_cache_access_since_;
591 return true;
594 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
595 priority_ = priority;
596 if (network_trans_)
597 network_trans_->SetPriority(priority_);
600 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
601 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
602 websocket_handshake_stream_base_create_helper_ = create_helper;
603 if (network_trans_)
604 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
607 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
608 const BeforeNetworkStartCallback& callback) {
609 DCHECK(!network_trans_);
610 before_network_start_callback_ = callback;
613 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
614 const BeforeProxyHeadersSentCallback& callback) {
615 DCHECK(!network_trans_);
616 before_proxy_headers_sent_callback_ = callback;
619 int HttpCache::Transaction::ResumeNetworkStart() {
620 if (network_trans_)
621 return network_trans_->ResumeNetworkStart();
622 return ERR_UNEXPECTED;
625 void HttpCache::Transaction::GetConnectionAttempts(
626 ConnectionAttempts* out) const {
627 ConnectionAttempts new_connection_attempts;
628 if (network_trans_)
629 network_trans_->GetConnectionAttempts(&new_connection_attempts);
631 out->swap(new_connection_attempts);
632 out->insert(out->begin(), old_connection_attempts_.begin(),
633 old_connection_attempts_.end());
636 //-----------------------------------------------------------------------------
638 // A few common patterns: (Foo* means Foo -> FooComplete)
640 // 1. Not-cached entry:
641 // Start():
642 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
643 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
644 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
645 // PartialHeadersReceived
647 // Read():
648 // NetworkRead* -> CacheWriteData*
650 // 2. Cached entry, no validation:
651 // Start():
652 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
653 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
654 // BeginCacheValidation() -> SetupEntryForRead()
656 // Read():
657 // CacheReadData*
659 // 3. Cached entry, validation (304):
660 // Start():
661 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
662 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
663 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
664 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
665 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
666 // PartialHeadersReceived
668 // Read():
669 // CacheReadData*
671 // 4. Cached entry, validation and replace (200):
672 // Start():
673 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
674 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
675 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
676 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
677 // TruncateCachedMetadata* -> PartialHeadersReceived
679 // Read():
680 // NetworkRead* -> CacheWriteData*
682 // 5. Sparse entry, partially cached, byte range request:
683 // Start():
684 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
685 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
686 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
687 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
688 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
689 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
690 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
691 // PartialHeadersReceived
693 // Read() 1:
694 // NetworkRead* -> CacheWriteData*
696 // Read() 2:
697 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
698 // CompletePartialCacheValidation -> CacheReadData* ->
700 // Read() 3:
701 // CacheReadData* -> StartPartialCacheValidation ->
702 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
703 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
704 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
706 // 6. HEAD. Not-cached entry:
707 // Pass through. Don't save a HEAD by itself.
708 // Start():
709 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
711 // 7. HEAD. Cached entry, no validation:
712 // Start():
713 // The same flow as for a GET request (example #2)
715 // Read():
716 // CacheReadData (returns 0)
718 // 8. HEAD. Cached entry, validation (304):
719 // The request updates the stored headers.
720 // Start(): Same as for a GET request (example #3)
722 // Read():
723 // CacheReadData (returns 0)
725 // 9. HEAD. Cached entry, validation and replace (200):
726 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
727 // itself.
728 // Start():
729 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
730 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
731 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
732 // OverwriteCachedResponse
734 // 10. HEAD. Sparse entry, partially cached:
735 // Serve the request from the cache, as long as it doesn't require
736 // revalidation. Ignore missing ranges when deciding to revalidate. If the
737 // entry requires revalidation, ignore the whole request and go to full pass
738 // through (the result of the HEAD request will NOT update the entry).
740 // Start(): Basically the same as example 7, as we never create a partial_
741 // object for this request.
743 // 11. Prefetch, not-cached entry:
744 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
745 // UpdateCachedResponse.
747 // 12. Prefetch, cached entry:
748 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
749 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
750 // bit is unset.
752 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
753 // Skip validation, similar to example 2.
754 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
755 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
756 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
757 // SetupEntryForRead()
759 // Read():
760 // CacheReadData*
762 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
763 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
764 // CacheReadResponse* and CacheDispatchValidation.
765 int HttpCache::Transaction::DoLoop(int result) {
766 DCHECK(next_state_ != STATE_NONE);
768 int rv = result;
769 do {
770 State state = next_state_;
771 next_state_ = STATE_NONE;
772 switch (state) {
773 case STATE_GET_BACKEND:
774 DCHECK_EQ(OK, rv);
775 rv = DoGetBackend();
776 break;
777 case STATE_GET_BACKEND_COMPLETE:
778 rv = DoGetBackendComplete(rv);
779 break;
780 case STATE_INIT_ENTRY:
781 DCHECK_EQ(OK, rv);
782 rv = DoInitEntry();
783 break;
784 case STATE_OPEN_ENTRY:
785 DCHECK_EQ(OK, rv);
786 rv = DoOpenEntry();
787 break;
788 case STATE_OPEN_ENTRY_COMPLETE:
789 rv = DoOpenEntryComplete(rv);
790 break;
791 case STATE_DOOM_ENTRY:
792 DCHECK_EQ(OK, rv);
793 rv = DoDoomEntry();
794 break;
795 case STATE_DOOM_ENTRY_COMPLETE:
796 rv = DoDoomEntryComplete(rv);
797 break;
798 case STATE_CREATE_ENTRY:
799 DCHECK_EQ(OK, rv);
800 rv = DoCreateEntry();
801 break;
802 case STATE_CREATE_ENTRY_COMPLETE:
803 rv = DoCreateEntryComplete(rv);
804 break;
805 case STATE_ADD_TO_ENTRY:
806 DCHECK_EQ(OK, rv);
807 rv = DoAddToEntry();
808 break;
809 case STATE_ADD_TO_ENTRY_COMPLETE:
810 rv = DoAddToEntryComplete(rv);
811 break;
812 case STATE_CACHE_READ_RESPONSE:
813 DCHECK_EQ(OK, rv);
814 rv = DoCacheReadResponse();
815 break;
816 case STATE_CACHE_READ_RESPONSE_COMPLETE:
817 rv = DoCacheReadResponseComplete(rv);
818 break;
819 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH:
820 DCHECK_EQ(OK, rv);
821 rv = DoCacheToggleUnusedSincePrefetch();
822 break;
823 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE:
824 rv = DoCacheToggleUnusedSincePrefetchComplete(rv);
825 break;
826 case STATE_CACHE_DISPATCH_VALIDATION:
827 DCHECK_EQ(OK, rv);
828 rv = DoCacheDispatchValidation();
829 break;
830 case STATE_CACHE_QUERY_DATA:
831 DCHECK_EQ(OK, rv);
832 rv = DoCacheQueryData();
833 break;
834 case STATE_CACHE_QUERY_DATA_COMPLETE:
835 rv = DoCacheQueryDataComplete(rv);
836 break;
837 case STATE_START_PARTIAL_CACHE_VALIDATION:
838 DCHECK_EQ(OK, rv);
839 rv = DoStartPartialCacheValidation();
840 break;
841 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
842 rv = DoCompletePartialCacheValidation(rv);
843 break;
844 case STATE_SEND_REQUEST:
845 DCHECK_EQ(OK, rv);
846 rv = DoSendRequest();
847 break;
848 case STATE_SEND_REQUEST_COMPLETE:
849 rv = DoSendRequestComplete(rv);
850 break;
851 case STATE_SUCCESSFUL_SEND_REQUEST:
852 DCHECK_EQ(OK, rv);
853 rv = DoSuccessfulSendRequest();
854 break;
855 case STATE_UPDATE_CACHED_RESPONSE:
856 DCHECK_EQ(OK, rv);
857 rv = DoUpdateCachedResponse();
858 break;
859 case STATE_CACHE_WRITE_UPDATED_RESPONSE:
860 DCHECK_EQ(OK, rv);
861 rv = DoCacheWriteUpdatedResponse();
862 break;
863 case STATE_CACHE_WRITE_UPDATED_RESPONSE_COMPLETE:
864 rv = DoCacheWriteUpdatedResponseComplete(rv);
865 break;
866 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
867 rv = DoUpdateCachedResponseComplete(rv);
868 break;
869 case STATE_OVERWRITE_CACHED_RESPONSE:
870 DCHECK_EQ(OK, rv);
871 rv = DoOverwriteCachedResponse();
872 break;
873 case STATE_CACHE_WRITE_RESPONSE:
874 DCHECK_EQ(OK, rv);
875 rv = DoCacheWriteResponse();
876 break;
877 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
878 rv = DoCacheWriteResponseComplete(rv);
879 break;
880 case STATE_TRUNCATE_CACHED_DATA:
881 DCHECK_EQ(OK, rv);
882 rv = DoTruncateCachedData();
883 break;
884 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
885 rv = DoTruncateCachedDataComplete(rv);
886 break;
887 case STATE_TRUNCATE_CACHED_METADATA:
888 DCHECK_EQ(OK, rv);
889 rv = DoTruncateCachedMetadata();
890 break;
891 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
892 rv = DoTruncateCachedMetadataComplete(rv);
893 break;
894 case STATE_PARTIAL_HEADERS_RECEIVED:
895 DCHECK_EQ(OK, rv);
896 rv = DoPartialHeadersReceived();
897 break;
898 case STATE_CACHE_READ_METADATA:
899 DCHECK_EQ(OK, rv);
900 rv = DoCacheReadMetadata();
901 break;
902 case STATE_CACHE_READ_METADATA_COMPLETE:
903 rv = DoCacheReadMetadataComplete(rv);
904 break;
905 case STATE_NETWORK_READ:
906 DCHECK_EQ(OK, rv);
907 rv = DoNetworkRead();
908 break;
909 case STATE_NETWORK_READ_COMPLETE:
910 rv = DoNetworkReadComplete(rv);
911 break;
912 case STATE_CACHE_READ_DATA:
913 DCHECK_EQ(OK, rv);
914 rv = DoCacheReadData();
915 break;
916 case STATE_CACHE_READ_DATA_COMPLETE:
917 rv = DoCacheReadDataComplete(rv);
918 break;
919 case STATE_CACHE_WRITE_DATA:
920 rv = DoCacheWriteData(rv);
921 break;
922 case STATE_CACHE_WRITE_DATA_COMPLETE:
923 rv = DoCacheWriteDataComplete(rv);
924 break;
925 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
926 DCHECK_EQ(OK, rv);
927 rv = DoCacheWriteTruncatedResponse();
928 break;
929 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE:
930 rv = DoCacheWriteTruncatedResponseComplete(rv);
931 break;
932 default:
933 NOTREACHED() << "bad state";
934 rv = ERR_FAILED;
935 break;
937 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
939 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
940 read_buf_ = NULL; // Release the buffer before invoking the callback.
941 base::ResetAndReturn(&callback_).Run(rv);
944 return rv;
947 int HttpCache::Transaction::DoGetBackend() {
948 cache_pending_ = true;
949 next_state_ = STATE_GET_BACKEND_COMPLETE;
950 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
951 return cache_->GetBackendForTransaction(this);
954 int HttpCache::Transaction::DoGetBackendComplete(int result) {
955 DCHECK(result == OK || result == ERR_FAILED);
956 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
957 result);
958 cache_pending_ = false;
960 if (!ShouldPassThrough()) {
961 cache_key_ = cache_->GenerateCacheKey(request_);
963 // Requested cache access mode.
964 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
965 mode_ = READ;
966 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
967 mode_ = WRITE;
968 } else {
969 mode_ = READ_WRITE;
972 // Downgrade to UPDATE if the request has been externally conditionalized.
973 if (external_validation_.initialized) {
974 if (mode_ & WRITE) {
975 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
976 // in case READ was off).
977 mode_ = UPDATE;
978 } else {
979 mode_ = NONE;
984 // Use PUT and DELETE only to invalidate existing stored entries.
985 if ((request_->method == "PUT" || request_->method == "DELETE") &&
986 mode_ != READ_WRITE && mode_ != WRITE) {
987 mode_ = NONE;
990 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
991 // transaction behaves the same for GET and HEAD requests at this point: if it
992 // was not modified, the entry is updated and a response is not returned from
993 // the cache. If we receive 200, it doesn't matter if there was a validation
994 // header or not.
995 if (request_->method == "HEAD" && mode_ == WRITE)
996 mode_ = NONE;
998 // If must use cache, then we must fail. This can happen for back/forward
999 // navigations to a page generated via a form post.
1000 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
1001 return ERR_CACHE_MISS;
1003 if (mode_ == NONE) {
1004 if (partial_) {
1005 partial_->RestoreHeaders(&custom_request_->extra_headers);
1006 partial_.reset();
1008 next_state_ = STATE_SEND_REQUEST;
1009 } else {
1010 next_state_ = STATE_INIT_ENTRY;
1013 // This is only set if we have something to do with the response.
1014 range_requested_ = (partial_.get() != NULL);
1016 return OK;
1019 int HttpCache::Transaction::DoInitEntry() {
1020 DCHECK(!new_entry_);
1022 if (!cache_.get())
1023 return ERR_UNEXPECTED;
1025 if (mode_ == WRITE) {
1026 next_state_ = STATE_DOOM_ENTRY;
1027 return OK;
1030 next_state_ = STATE_OPEN_ENTRY;
1031 return OK;
1034 int HttpCache::Transaction::DoOpenEntry() {
1035 DCHECK(!new_entry_);
1036 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1037 cache_pending_ = true;
1038 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1039 first_cache_access_since_ = TimeTicks::Now();
1040 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1043 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1044 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1045 // OK, otherwise the cache will end up with an active entry without any
1046 // transaction attached.
1047 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1048 cache_pending_ = false;
1049 if (result == OK) {
1050 next_state_ = STATE_ADD_TO_ENTRY;
1051 return OK;
1054 if (result == ERR_CACHE_RACE) {
1055 next_state_ = STATE_INIT_ENTRY;
1056 return OK;
1059 if (request_->method == "PUT" || request_->method == "DELETE" ||
1060 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1061 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1062 mode_ = NONE;
1063 next_state_ = STATE_SEND_REQUEST;
1064 return OK;
1067 if (mode_ == READ_WRITE) {
1068 mode_ = WRITE;
1069 next_state_ = STATE_CREATE_ENTRY;
1070 return OK;
1072 if (mode_ == UPDATE) {
1073 // There is no cache entry to update; proceed without caching.
1074 mode_ = NONE;
1075 next_state_ = STATE_SEND_REQUEST;
1076 return OK;
1079 // The entry does not exist, and we are not permitted to create a new entry,
1080 // so we must fail.
1081 return ERR_CACHE_MISS;
1084 int HttpCache::Transaction::DoDoomEntry() {
1085 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1086 cache_pending_ = true;
1087 if (first_cache_access_since_.is_null())
1088 first_cache_access_since_ = TimeTicks::Now();
1089 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1090 return cache_->DoomEntry(cache_key_, this);
1093 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1094 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1095 next_state_ = STATE_CREATE_ENTRY;
1096 cache_pending_ = false;
1097 if (result == ERR_CACHE_RACE)
1098 next_state_ = STATE_INIT_ENTRY;
1099 return OK;
1102 int HttpCache::Transaction::DoCreateEntry() {
1103 DCHECK(!new_entry_);
1104 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1105 cache_pending_ = true;
1106 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1107 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1110 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1111 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1112 // OK, otherwise the cache will end up with an active entry without any
1113 // transaction attached.
1114 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1115 result);
1116 cache_pending_ = false;
1117 switch (result) {
1118 case OK:
1119 next_state_ = STATE_ADD_TO_ENTRY;
1120 break;
1122 case ERR_CACHE_RACE:
1123 next_state_ = STATE_INIT_ENTRY;
1124 break;
1126 default:
1127 // We have a race here: Maybe we failed to open the entry and decided to
1128 // create one, but by the time we called create, another transaction
1129 // already created the entry. If we want to eliminate this issue, we
1130 // need an atomic OpenOrCreate() method exposed by the disk cache.
1131 DLOG(WARNING) << "Unable to create cache entry";
1132 mode_ = NONE;
1133 if (partial_)
1134 partial_->RestoreHeaders(&custom_request_->extra_headers);
1135 next_state_ = STATE_SEND_REQUEST;
1137 return OK;
1140 int HttpCache::Transaction::DoAddToEntry() {
1141 DCHECK(new_entry_);
1142 cache_pending_ = true;
1143 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1144 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1145 DCHECK(entry_lock_waiting_since_.is_null());
1146 entry_lock_waiting_since_ = TimeTicks::Now();
1147 int rv = cache_->AddTransactionToEntry(new_entry_, this);
1148 if (rv == ERR_IO_PENDING) {
1149 if (bypass_lock_for_test_) {
1150 OnAddToEntryTimeout(entry_lock_waiting_since_);
1151 } else {
1152 int timeout_milliseconds = 20 * 1000;
1153 if (partial_ && new_entry_->writer &&
1154 new_entry_->writer->range_requested_) {
1155 // Quickly timeout and bypass the cache if we're a range request and
1156 // we're blocked by the reader/writer lock. Doing so eliminates a long
1157 // running issue, http://crbug.com/31014, where two of the same media
1158 // resources could not be played back simultaneously due to one locking
1159 // the cache entry until the entire video was downloaded.
1161 // Bypassing the cache is not ideal, as we are now ignoring the cache
1162 // entirely for all range requests to a resource beyond the first. This
1163 // is however a much more succinct solution than the alternatives, which
1164 // would require somewhat significant changes to the http caching logic.
1166 // Allow some timeout slack for the entry addition to complete in case
1167 // the writer lock is imminently released; we want to avoid skipping
1168 // the cache if at all possible. See http://crbug.com/408765
1169 timeout_milliseconds = 25;
1171 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
1172 FROM_HERE,
1173 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout,
1174 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_),
1175 TimeDelta::FromMilliseconds(timeout_milliseconds));
1178 return rv;
1181 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1182 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1183 result);
1184 const TimeDelta entry_lock_wait =
1185 TimeTicks::Now() - entry_lock_waiting_since_;
1186 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1188 entry_lock_waiting_since_ = TimeTicks();
1189 DCHECK(new_entry_);
1190 cache_pending_ = false;
1192 if (result == OK)
1193 entry_ = new_entry_;
1195 // If there is a failure, the cache should have taken care of new_entry_.
1196 new_entry_ = NULL;
1198 if (result == ERR_CACHE_RACE) {
1199 next_state_ = STATE_INIT_ENTRY;
1200 return OK;
1203 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1204 // The cache is busy, bypass it for this transaction.
1205 mode_ = NONE;
1206 next_state_ = STATE_SEND_REQUEST;
1207 if (partial_) {
1208 partial_->RestoreHeaders(&custom_request_->extra_headers);
1209 partial_.reset();
1211 return OK;
1214 if (result != OK) {
1215 NOTREACHED();
1216 return result;
1219 if (mode_ == WRITE) {
1220 if (partial_)
1221 partial_->RestoreHeaders(&custom_request_->extra_headers);
1222 next_state_ = STATE_SEND_REQUEST;
1223 } else {
1224 // We have to read the headers from the cached entry.
1225 DCHECK(mode_ & READ_META);
1226 next_state_ = STATE_CACHE_READ_RESPONSE;
1228 return OK;
1231 int HttpCache::Transaction::DoCacheReadResponse() {
1232 DCHECK(entry_);
1233 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1235 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1236 read_buf_ = new IOBuffer(io_buf_len_);
1238 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1239 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1240 io_buf_len_, io_callback_);
1243 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1244 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1245 if (result != io_buf_len_ ||
1246 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, &response_,
1247 &truncated_)) {
1248 return OnCacheReadError(result, true);
1251 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1252 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1253 ReadCertChain();
1255 // Some resources may have slipped in as truncated when they're not.
1256 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1257 if (response_.headers->GetContentLength() == current_size)
1258 truncated_ = false;
1260 if ((response_.unused_since_prefetch &&
1261 !(request_->load_flags & LOAD_PREFETCH)) ||
1262 (!response_.unused_since_prefetch &&
1263 (request_->load_flags & LOAD_PREFETCH))) {
1264 // Either this is the first use of an entry since it was prefetched or
1265 // this is a prefetch. The value of response.unused_since_prefetch is valid
1266 // for this transaction but the bit needs to be flipped in storage.
1267 next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH;
1268 return OK;
1271 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1272 return OK;
1275 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1276 // Write back the toggled value for the next use of this entry.
1277 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1279 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1280 // transaction then metadata will be written to cache twice. If prefetching
1281 // becomes more common, consider combining the writes.
1283 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1284 tracked_objects::ScopedTracker tracking_profile(
1285 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1286 "422516 HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch"));
1288 next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE;
1289 return WriteResponseInfoToEntry(false);
1292 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1293 int result) {
1294 // Restore the original value for this transaction.
1295 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1296 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1297 return OnWriteResponseInfoToEntryComplete(result);
1300 int HttpCache::Transaction::DoCacheDispatchValidation() {
1301 // We now have access to the cache entry.
1303 // o if we are a reader for the transaction, then we can start reading the
1304 // cache entry.
1306 // o if we can read or write, then we should check if the cache entry needs
1307 // to be validated and then issue a network request if needed or just read
1308 // from the cache if the cache entry is already valid.
1310 // o if we are set to UPDATE, then we are handling an externally
1311 // conditionalized request (if-modified-since / if-none-match). We check
1312 // if the request headers define a validation request.
1314 int result = ERR_FAILED;
1315 switch (mode_) {
1316 case READ:
1317 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1318 result = BeginCacheRead();
1319 break;
1320 case READ_WRITE:
1321 result = BeginPartialCacheValidation();
1322 break;
1323 case UPDATE:
1324 result = BeginExternallyConditionalizedRequest();
1325 break;
1326 case WRITE:
1327 default:
1328 NOTREACHED();
1330 return result;
1333 int HttpCache::Transaction::DoCacheQueryData() {
1334 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1335 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1338 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1339 DCHECK_EQ(OK, result);
1340 if (!cache_.get())
1341 return ERR_UNEXPECTED;
1343 return ValidateEntryHeadersAndContinue();
1346 // We may end up here multiple times for a given request.
1347 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1348 if (mode_ == NONE)
1349 return OK;
1351 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1352 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1355 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1356 if (!result) {
1357 // This is the end of the request.
1358 if (mode_ & WRITE) {
1359 DoneWritingToEntry(true);
1360 } else {
1361 cache_->DoneReadingFromEntry(entry_, this);
1362 entry_ = NULL;
1364 return result;
1367 if (result < 0)
1368 return result;
1370 partial_->PrepareCacheValidation(entry_->disk_entry,
1371 &custom_request_->extra_headers);
1373 if (reading_ && partial_->IsCurrentRangeCached()) {
1374 next_state_ = STATE_CACHE_READ_DATA;
1375 return OK;
1378 return BeginCacheValidation();
1381 int HttpCache::Transaction::DoSendRequest() {
1382 DCHECK(mode_ & WRITE || mode_ == NONE);
1383 DCHECK(!network_trans_.get());
1385 send_request_since_ = TimeTicks::Now();
1387 // Create a network transaction.
1388 int rv =
1389 cache_->network_layer_->CreateTransaction(priority_, &network_trans_);
1390 if (rv != OK)
1391 return rv;
1392 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1393 network_trans_->SetBeforeProxyHeadersSentCallback(
1394 before_proxy_headers_sent_callback_);
1396 // Old load timing information, if any, is now obsolete.
1397 old_network_trans_load_timing_.reset();
1399 if (websocket_handshake_stream_base_create_helper_)
1400 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1401 websocket_handshake_stream_base_create_helper_);
1403 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1404 rv = network_trans_->Start(request_, io_callback_, net_log_);
1405 return rv;
1408 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1409 if (!cache_.get())
1410 return ERR_UNEXPECTED;
1412 // If we tried to conditionalize the request and failed, we know
1413 // we won't be reading from the cache after this point.
1414 if (couldnt_conditionalize_request_)
1415 mode_ = WRITE;
1417 if (result == OK) {
1418 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
1419 return OK;
1422 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1423 response_.network_accessed = response->network_accessed;
1425 // Do not record requests that have network errors or restarts.
1426 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1427 if (IsCertificateError(result)) {
1428 // If we get a certificate error, then there is a certificate in ssl_info,
1429 // so GetResponseInfo() should never return NULL here.
1430 DCHECK(response);
1431 response_.ssl_info = response->ssl_info;
1432 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1433 DCHECK(response);
1434 response_.cert_request_info = response->cert_request_info;
1435 } else if (response_.was_cached) {
1436 DoneWritingToEntry(true);
1439 return result;
1442 // We received the response headers and there is no error.
1443 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1444 DCHECK(!new_response_);
1445 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1447 if (new_response->headers->response_code() == 401 ||
1448 new_response->headers->response_code() == 407) {
1449 auth_response_ = *new_response;
1450 if (!reading_)
1451 return OK;
1453 // We initiated a second request the caller doesn't know about. We should be
1454 // able to authenticate this request because we should have authenticated
1455 // this URL moments ago.
1456 if (IsReadyToRestartForAuth()) {
1457 DCHECK(!response_.auth_challenge.get());
1458 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1459 // In theory we should check to see if there are new cookies, but there
1460 // is no way to do that from here.
1461 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1464 // We have to perform cleanup at this point so that at least the next
1465 // request can succeed. We do not retry at this point, because data
1466 // has been read and we have no way to gather credentials. We would
1467 // fail again, and potentially loop. This can happen if the credentials
1468 // expire while chrome is suspended.
1469 if (entry_)
1470 DoomPartialEntry(false);
1471 mode_ = NONE;
1472 partial_.reset();
1473 ResetNetworkTransaction();
1474 return ERR_CACHE_AUTH_FAILURE_AFTER_READ;
1477 new_response_ = new_response;
1478 if (!ValidatePartialResponse() && !auth_response_.headers.get()) {
1479 // Something went wrong with this request and we have to restart it.
1480 // If we have an authentication response, we are exposed to weird things
1481 // hapenning if the user cancels the authentication before we receive
1482 // the new response.
1483 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST);
1484 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1485 response_ = HttpResponseInfo();
1486 ResetNetworkTransaction();
1487 new_response_ = NULL;
1488 next_state_ = STATE_SEND_REQUEST;
1489 return OK;
1492 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1493 // We have stored the full entry, but it changed and the server is
1494 // sending a range. We have to delete the old entry.
1495 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1496 DoneWritingToEntry(false);
1499 if (mode_ == WRITE &&
1500 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1501 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1504 // Invalidate any cached GET with a successful PUT or DELETE.
1505 if (mode_ == WRITE &&
1506 (request_->method == "PUT" || request_->method == "DELETE")) {
1507 if (NonErrorResponse(new_response->headers->response_code())) {
1508 int ret = cache_->DoomEntry(cache_key_, NULL);
1509 DCHECK_EQ(OK, ret);
1511 cache_->DoneWritingToEntry(entry_, true);
1512 entry_ = NULL;
1513 mode_ = NONE;
1516 // Invalidate any cached GET with a successful POST.
1517 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1518 request_->method == "POST" &&
1519 NonErrorResponse(new_response->headers->response_code())) {
1520 cache_->DoomMainEntryForUrl(request_->url);
1523 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1525 if (new_response_->headers->response_code() == 416 &&
1526 (request_->method == "GET" || request_->method == "POST")) {
1527 // If there is an active entry it may be destroyed with this transaction.
1528 response_ = *new_response_;
1529 return OK;
1532 // Are we expecting a response to a conditional query?
1533 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1534 if (new_response->headers->response_code() == 304 || handling_206_) {
1535 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1536 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1537 return OK;
1539 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1540 mode_ = WRITE;
1543 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1544 return OK;
1547 // We received 304 or 206 and we want to update the cached response headers.
1548 int HttpCache::Transaction::DoUpdateCachedResponse() {
1549 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1550 int rv = OK;
1551 // Update the cached response based on the headers and properties of
1552 // new_response_.
1553 response_.headers->Update(*new_response_->headers.get());
1554 response_.response_time = new_response_->response_time;
1555 response_.request_time = new_response_->request_time;
1556 response_.network_accessed = new_response_->network_accessed;
1557 response_.unused_since_prefetch = new_response_->unused_since_prefetch;
1558 response_.ssl_info = new_response_->ssl_info;
1559 if (new_response_->vary_data.is_valid()) {
1560 response_.vary_data = new_response_->vary_data;
1561 } else if (response_.vary_data.is_valid()) {
1562 // There is a vary header in the stored response but not in the current one.
1563 // Update the data with the new request headers.
1564 HttpVaryData new_vary_data;
1565 new_vary_data.Init(*request_, *response_.headers.get());
1566 response_.vary_data = new_vary_data;
1569 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1570 if (!entry_->doomed) {
1571 int ret = cache_->DoomEntry(cache_key_, NULL);
1572 DCHECK_EQ(OK, ret);
1574 } else {
1575 // If we are already reading, we already updated the headers for this
1576 // request; doing it again will change Content-Length.
1577 if (!reading_) {
1578 next_state_ = STATE_CACHE_WRITE_UPDATED_RESPONSE;
1579 rv = OK;
1582 return rv;
1585 int HttpCache::Transaction::DoCacheWriteUpdatedResponse() {
1586 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1587 tracked_objects::ScopedTracker tracking_profile(
1588 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1589 "422516 HttpCache::Transaction::DoCacheWriteUpdatedResponse"));
1591 next_state_ = STATE_CACHE_WRITE_UPDATED_RESPONSE_COMPLETE;
1592 return WriteResponseInfoToEntry(false);
1595 int HttpCache::Transaction::DoCacheWriteUpdatedResponseComplete(int result) {
1596 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1597 return OnWriteResponseInfoToEntryComplete(result);
1600 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1601 if (mode_ == UPDATE) {
1602 DCHECK(!handling_206_);
1603 // We got a "not modified" response and already updated the corresponding
1604 // cache entry above.
1606 // By closing the cached entry now, we make sure that the 304 rather than
1607 // the cached 200 response, is what will be returned to the user.
1608 DoneWritingToEntry(true);
1609 } else if (entry_ && !handling_206_) {
1610 DCHECK_EQ(READ_WRITE, mode_);
1611 if (!partial_ || partial_->IsLastRange()) {
1612 cache_->ConvertWriterToReader(entry_);
1613 mode_ = READ;
1615 // We no longer need the network transaction, so destroy it.
1616 final_upload_progress_ = network_trans_->GetUploadProgress();
1617 ResetNetworkTransaction();
1618 } else if (entry_ && handling_206_ && truncated_ &&
1619 partial_->initial_validation()) {
1620 // We just finished the validation of a truncated entry, and the server
1621 // is willing to resume the operation. Now we go back and start serving
1622 // the first part to the user.
1623 ResetNetworkTransaction();
1624 new_response_ = NULL;
1625 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1626 partial_->SetRangeToStartDownload();
1627 return OK;
1629 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1630 return OK;
1633 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1634 if (mode_ & READ) {
1635 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1636 return OK;
1639 // We change the value of Content-Length for partial content.
1640 if (handling_206_ && partial_)
1641 partial_->FixContentLength(new_response_->headers.get());
1643 response_ = *new_response_;
1645 if (request_->method == "HEAD") {
1646 // This response is replacing the cached one.
1647 DoneWritingToEntry(false);
1648 mode_ = NONE;
1649 new_response_ = NULL;
1650 return OK;
1653 if (handling_206_ && !CanResume(false)) {
1654 // There is no point in storing this resource because it will never be used.
1655 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1656 DoneWritingToEntry(false);
1657 if (partial_)
1658 partial_->FixResponseHeaders(response_.headers.get(), true);
1659 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1660 return OK;
1663 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1664 return OK;
1667 int HttpCache::Transaction::DoCacheWriteResponse() {
1668 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
1669 tracked_objects::ScopedTracker tracking_profile(
1670 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1671 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1673 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
1674 return WriteResponseInfoToEntry(truncated_);
1677 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1678 next_state_ = STATE_TRUNCATE_CACHED_DATA;
1679 return OnWriteResponseInfoToEntryComplete(result);
1682 int HttpCache::Transaction::DoTruncateCachedData() {
1683 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1684 if (!entry_)
1685 return OK;
1686 if (net_log_.IsCapturing())
1687 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1688 // Truncate the stream.
1689 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1692 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1693 if (entry_) {
1694 if (net_log_.IsCapturing()) {
1695 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1696 result);
1700 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1701 return OK;
1704 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1705 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1706 if (!entry_)
1707 return OK;
1709 if (net_log_.IsCapturing())
1710 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1711 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1714 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1715 if (entry_) {
1716 if (net_log_.IsCapturing()) {
1717 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1718 result);
1722 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1723 return OK;
1726 int HttpCache::Transaction::DoPartialHeadersReceived() {
1727 new_response_ = NULL;
1728 if (entry_ && !partial_ && entry_->disk_entry->GetDataSize(kMetadataIndex))
1729 next_state_ = STATE_CACHE_READ_METADATA;
1731 if (!partial_)
1732 return OK;
1734 if (reading_) {
1735 if (network_trans_.get()) {
1736 next_state_ = STATE_NETWORK_READ;
1737 } else {
1738 next_state_ = STATE_CACHE_READ_DATA;
1740 } else if (mode_ != NONE) {
1741 // We are about to return the headers for a byte-range request to the user,
1742 // so let's fix them.
1743 partial_->FixResponseHeaders(response_.headers.get(), true);
1745 return OK;
1748 int HttpCache::Transaction::DoCacheReadMetadata() {
1749 DCHECK(entry_);
1750 DCHECK(!response_.metadata.get());
1751 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1753 response_.metadata =
1754 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1756 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1757 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1758 response_.metadata.get(),
1759 response_.metadata->size(),
1760 io_callback_);
1763 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1764 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1765 if (result != response_.metadata->size())
1766 return OnCacheReadError(result, false);
1767 return OK;
1770 int HttpCache::Transaction::DoNetworkRead() {
1771 next_state_ = STATE_NETWORK_READ_COMPLETE;
1772 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1775 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1776 DCHECK(mode_ & WRITE || mode_ == NONE);
1778 if (!cache_.get())
1779 return ERR_UNEXPECTED;
1781 // If there is an error or we aren't saving the data, we are done; just wait
1782 // until the destructor runs to see if we can keep the data.
1783 if (mode_ == NONE || result < 0)
1784 return result;
1786 next_state_ = STATE_CACHE_WRITE_DATA;
1787 return result;
1790 int HttpCache::Transaction::DoCacheReadData() {
1791 if (request_->method == "HEAD")
1792 return 0;
1794 DCHECK(entry_);
1795 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1797 if (net_log_.IsCapturing())
1798 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1799 if (partial_) {
1800 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1801 io_callback_);
1804 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1805 read_buf_.get(), io_buf_len_,
1806 io_callback_);
1809 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1810 if (net_log_.IsCapturing()) {
1811 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1812 result);
1815 if (!cache_.get())
1816 return ERR_UNEXPECTED;
1818 if (partial_) {
1819 // Partial requests are confusing to report in histograms because they may
1820 // have multiple underlying requests.
1821 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1822 return DoPartialCacheReadCompleted(result);
1825 if (result > 0) {
1826 read_offset_ += result;
1827 } else if (result == 0) { // End of file.
1828 RecordHistograms();
1829 cache_->DoneReadingFromEntry(entry_, this);
1830 entry_ = NULL;
1831 } else {
1832 return OnCacheReadError(result, false);
1834 return result;
1837 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1838 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1839 write_len_ = num_bytes;
1840 if (entry_) {
1841 if (net_log_.IsCapturing())
1842 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1845 if (!entry_ || !num_bytes)
1846 return num_bytes;
1848 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1849 return WriteToEntry(kResponseContentIndex, current_size, read_buf_.get(),
1850 num_bytes, io_callback_);
1853 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1854 if (entry_) {
1855 if (net_log_.IsCapturing()) {
1856 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1857 result);
1860 if (!cache_.get())
1861 return ERR_UNEXPECTED;
1863 if (result != write_len_) {
1864 DLOG(ERROR) << "failed to write response data to cache";
1865 DoneWritingToEntry(false);
1867 // We want to ignore errors writing to disk and just keep reading from
1868 // the network.
1869 result = write_len_;
1870 } else if (!done_reading_ && entry_) {
1871 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1872 int64 body_size = response_.headers->GetContentLength();
1873 if (body_size >= 0 && body_size <= current_size)
1874 done_reading_ = true;
1877 if (partial_) {
1878 // This may be the last request.
1879 if (result != 0 || truncated_ ||
1880 !(partial_->IsLastRange() || mode_ == WRITE)) {
1881 return DoPartialNetworkReadCompleted(result);
1885 if (result == 0) {
1886 // End of file. This may be the result of a connection problem so see if we
1887 // have to keep the entry around to be flagged as truncated later on.
1888 if (done_reading_ || !entry_ || partial_ ||
1889 response_.headers->GetContentLength() <= 0) {
1890 DoneWritingToEntry(true);
1894 return result;
1897 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1898 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE;
1899 return WriteResponseInfoToEntry(true);
1902 int HttpCache::Transaction::DoCacheWriteTruncatedResponseComplete(int result) {
1903 return OnWriteResponseInfoToEntryComplete(result);
1906 //-----------------------------------------------------------------------------
1908 void HttpCache::Transaction::ReadCertChain() {
1909 std::string key =
1910 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
1911 const X509Certificate::OSCertHandles& intermediates =
1912 response_.ssl_info.cert->GetIntermediateCertificates();
1913 int dist_from_root = intermediates.size();
1915 scoped_refptr<SharedChainData> shared_chain_data(
1916 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1917 cache_->cert_cache()->GetCertificate(key,
1918 base::Bind(&OnCertReadIOComplete,
1919 dist_from_root,
1920 true /* is leaf */,
1921 shared_chain_data));
1923 for (X509Certificate::OSCertHandles::const_iterator it =
1924 intermediates.begin();
1925 it != intermediates.end();
1926 ++it) {
1927 --dist_from_root;
1928 key = GetCacheKeyForCert(*it);
1929 cache_->cert_cache()->GetCertificate(key,
1930 base::Bind(&OnCertReadIOComplete,
1931 dist_from_root,
1932 false /* is not leaf */,
1933 shared_chain_data));
1935 DCHECK_EQ(0, dist_from_root);
1938 void HttpCache::Transaction::WriteCertChain() {
1939 const X509Certificate::OSCertHandles& intermediates =
1940 response_.ssl_info.cert->GetIntermediateCertificates();
1941 int dist_from_root = intermediates.size();
1943 scoped_refptr<SharedChainData> shared_chain_data(
1944 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
1945 cache_->cert_cache()->SetCertificate(
1946 response_.ssl_info.cert->os_cert_handle(),
1947 base::Bind(&OnCertWriteIOComplete,
1948 dist_from_root,
1949 true /* is leaf */,
1950 shared_chain_data));
1951 for (X509Certificate::OSCertHandles::const_iterator it =
1952 intermediates.begin();
1953 it != intermediates.end();
1954 ++it) {
1955 --dist_from_root;
1956 cache_->cert_cache()->SetCertificate(*it,
1957 base::Bind(&OnCertWriteIOComplete,
1958 dist_from_root,
1959 false /* is not leaf */,
1960 shared_chain_data));
1962 DCHECK_EQ(0, dist_from_root);
1965 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1966 const HttpRequestInfo* request) {
1967 net_log_ = net_log;
1968 request_ = request;
1969 effective_load_flags_ = request_->load_flags;
1971 if (cache_->mode() == DISABLE)
1972 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1974 // Some headers imply load flags. The order here is significant.
1976 // LOAD_DISABLE_CACHE : no cache read or write
1977 // LOAD_BYPASS_CACHE : no cache read
1978 // LOAD_VALIDATE_CACHE : no cache read unless validation
1980 // The former modes trump latter modes, so if we find a matching header we
1981 // can stop iterating kSpecialHeaders.
1983 static const struct {
1984 const HeaderNameAndValue* search;
1985 int load_flag;
1986 } kSpecialHeaders[] = {
1987 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
1988 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
1989 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
1992 bool range_found = false;
1993 bool external_validation_error = false;
1994 bool special_headers = false;
1996 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
1997 range_found = true;
1999 for (size_t i = 0; i < arraysize(kSpecialHeaders); ++i) {
2000 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
2001 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
2002 special_headers = true;
2003 break;
2007 // Check for conditionalization headers which may correspond with a
2008 // cache validation request.
2009 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
2010 const ValidationHeaderInfo& info = kValidationHeaders[i];
2011 std::string validation_value;
2012 if (request_->extra_headers.GetHeader(
2013 info.request_header_name, &validation_value)) {
2014 if (!external_validation_.values[i].empty() ||
2015 validation_value.empty()) {
2016 external_validation_error = true;
2018 external_validation_.values[i] = validation_value;
2019 external_validation_.initialized = true;
2023 if (range_found || special_headers || external_validation_.initialized) {
2024 // Log the headers before request_ is modified.
2025 std::string empty;
2026 net_log_.AddEvent(
2027 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS,
2028 base::Bind(&HttpRequestHeaders::NetLogCallback,
2029 base::Unretained(&request_->extra_headers), &empty));
2032 // We don't support ranges and validation headers.
2033 if (range_found && external_validation_.initialized) {
2034 LOG(WARNING) << "Byte ranges AND validation headers found.";
2035 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2038 // If there is more than one validation header, we can't treat this request as
2039 // a cache validation, since we don't know for sure which header the server
2040 // will give us a response for (and they could be contradictory).
2041 if (external_validation_error) {
2042 LOG(WARNING) << "Multiple or malformed validation headers found.";
2043 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2046 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2047 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2048 partial_.reset(new PartialData);
2049 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
2050 // We will be modifying the actual range requested to the server, so
2051 // let's remove the header here.
2052 custom_request_.reset(new HttpRequestInfo(*request_));
2053 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2054 request_ = custom_request_.get();
2055 partial_->SetHeaders(custom_request_->extra_headers);
2056 } else {
2057 // The range is invalid or we cannot handle it properly.
2058 VLOG(1) << "Invalid byte range found.";
2059 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2060 partial_.reset(NULL);
2065 bool HttpCache::Transaction::ShouldPassThrough() {
2066 // We may have a null disk_cache if there is an error we cannot recover from,
2067 // like not enough disk space, or sharing violations.
2068 if (!cache_->disk_cache_.get())
2069 return true;
2071 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2072 return true;
2074 if (request_->method == "GET" || request_->method == "HEAD")
2075 return false;
2077 if (request_->method == "POST" && request_->upload_data_stream &&
2078 request_->upload_data_stream->identifier()) {
2079 return false;
2082 if (request_->method == "PUT" && request_->upload_data_stream)
2083 return false;
2085 if (request_->method == "DELETE")
2086 return false;
2088 return true;
2091 int HttpCache::Transaction::BeginCacheRead() {
2092 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2093 if (response_.headers->response_code() == 206 || partial_) {
2094 NOTREACHED();
2095 return ERR_CACHE_MISS;
2098 if (request_->method == "HEAD")
2099 FixHeadersForHead();
2101 // We don't have the whole resource.
2102 if (truncated_)
2103 return ERR_CACHE_MISS;
2105 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2106 next_state_ = STATE_CACHE_READ_METADATA;
2108 return OK;
2111 int HttpCache::Transaction::BeginCacheValidation() {
2112 DCHECK_EQ(mode_, READ_WRITE);
2114 ValidationType required_validation = RequiresValidation();
2116 bool skip_validation = (required_validation == VALIDATION_NONE);
2118 if (request_->method == "HEAD" &&
2119 (truncated_ || response_.headers->response_code() == 206)) {
2120 DCHECK(!partial_);
2121 if (skip_validation)
2122 return SetupEntryForRead();
2124 // Bail out!
2125 next_state_ = STATE_SEND_REQUEST;
2126 mode_ = NONE;
2127 return OK;
2130 if (truncated_) {
2131 // Truncated entries can cause partial gets, so we shouldn't record this
2132 // load in histograms.
2133 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2134 skip_validation = !partial_->initial_validation();
2137 if (partial_ && (is_sparse_ || truncated_) &&
2138 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
2139 // Force revalidation for sparse or truncated entries. Note that we don't
2140 // want to ignore the regular validation logic just because a byte range was
2141 // part of the request.
2142 skip_validation = false;
2145 if (skip_validation) {
2146 UpdateTransactionPattern(PATTERN_ENTRY_USED);
2147 return SetupEntryForRead();
2148 } else {
2149 // Make the network request conditional, to see if we may reuse our cached
2150 // response. If we cannot do so, then we just resort to a normal fetch.
2151 // Our mode remains READ_WRITE for a conditional request. Even if the
2152 // conditionalization fails, we don't switch to WRITE mode until we
2153 // know we won't be falling back to using the cache entry in the
2154 // LOAD_FROM_CACHE_IF_OFFLINE case.
2155 if (!ConditionalizeRequest()) {
2156 couldnt_conditionalize_request_ = true;
2157 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
2158 if (partial_)
2159 return DoRestartPartialRequest();
2161 DCHECK_NE(206, response_.headers->response_code());
2163 next_state_ = STATE_SEND_REQUEST;
2165 return OK;
2168 int HttpCache::Transaction::BeginPartialCacheValidation() {
2169 DCHECK_EQ(mode_, READ_WRITE);
2171 if (response_.headers->response_code() != 206 && !partial_ && !truncated_)
2172 return BeginCacheValidation();
2174 // Partial requests should not be recorded in histograms.
2175 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2176 if (request_->method == "HEAD")
2177 return BeginCacheValidation();
2179 if (!range_requested_) {
2180 // The request is not for a range, but we have stored just ranges.
2182 partial_.reset(new PartialData());
2183 partial_->SetHeaders(request_->extra_headers);
2184 if (!custom_request_.get()) {
2185 custom_request_.reset(new HttpRequestInfo(*request_));
2186 request_ = custom_request_.get();
2190 next_state_ = STATE_CACHE_QUERY_DATA;
2191 return OK;
2194 // This should only be called once per request.
2195 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2196 DCHECK_EQ(mode_, READ_WRITE);
2198 if (!partial_->UpdateFromStoredHeaders(
2199 response_.headers.get(), entry_->disk_entry, truncated_)) {
2200 return DoRestartPartialRequest();
2203 if (response_.headers->response_code() == 206)
2204 is_sparse_ = true;
2206 if (!partial_->IsRequestedRangeOK()) {
2207 // The stored data is fine, but the request may be invalid.
2208 invalid_range_ = true;
2211 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2212 return OK;
2215 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2216 DCHECK_EQ(UPDATE, mode_);
2217 DCHECK(external_validation_.initialized);
2219 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
2220 if (external_validation_.values[i].empty())
2221 continue;
2222 // Retrieve either the cached response's "etag" or "last-modified" header.
2223 std::string validator;
2224 response_.headers->EnumerateHeader(
2225 NULL,
2226 kValidationHeaders[i].related_response_header_name,
2227 &validator);
2229 if (response_.headers->response_code() != 200 || truncated_ ||
2230 validator.empty() || validator != external_validation_.values[i]) {
2231 // The externally conditionalized request is not a validation request
2232 // for our existing cache entry. Proceed with caching disabled.
2233 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2234 DoneWritingToEntry(true);
2238 // TODO(ricea): This calculation is expensive to perform just to collect
2239 // statistics. Either remove it or use the result, depending on the result of
2240 // the experiment.
2241 ExternallyConditionalizedType type =
2242 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE;
2243 if (mode_ == NONE)
2244 type = EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS;
2245 else if (RequiresValidation())
2246 type = EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION;
2248 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2249 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2250 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2251 type,
2252 EXTERNALLY_CONDITIONALIZED_MAX);
2254 next_state_ = STATE_SEND_REQUEST;
2255 return OK;
2258 int HttpCache::Transaction::RestartNetworkRequest() {
2259 DCHECK(mode_ & WRITE || mode_ == NONE);
2260 DCHECK(network_trans_.get());
2261 DCHECK_EQ(STATE_NONE, next_state_);
2263 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2264 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
2265 if (rv != ERR_IO_PENDING)
2266 return DoLoop(rv);
2267 return rv;
2270 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2271 X509Certificate* client_cert) {
2272 DCHECK(mode_ & WRITE || mode_ == NONE);
2273 DCHECK(network_trans_.get());
2274 DCHECK_EQ(STATE_NONE, next_state_);
2276 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2277 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
2278 if (rv != ERR_IO_PENDING)
2279 return DoLoop(rv);
2280 return rv;
2283 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2284 const AuthCredentials& credentials) {
2285 DCHECK(mode_ & WRITE || mode_ == NONE);
2286 DCHECK(network_trans_.get());
2287 DCHECK_EQ(STATE_NONE, next_state_);
2289 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2290 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
2291 if (rv != ERR_IO_PENDING)
2292 return DoLoop(rv);
2293 return rv;
2296 ValidationType HttpCache::Transaction::RequiresValidation() {
2297 // TODO(darin): need to do more work here:
2298 // - make sure we have a matching request method
2299 // - watch out for cached responses that depend on authentication
2301 if (response_.vary_data.is_valid() &&
2302 !response_.vary_data.MatchesRequest(*request_,
2303 *response_.headers.get())) {
2304 vary_mismatch_ = true;
2305 return VALIDATION_SYNCHRONOUS;
2308 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
2309 return VALIDATION_NONE;
2311 if (response_.unused_since_prefetch &&
2312 !(effective_load_flags_ & LOAD_PREFETCH) &&
2313 response_.headers->GetCurrentAge(
2314 response_.request_time, response_.response_time,
2315 cache_->clock_->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins)) {
2316 // The first use of a resource after prefetch within a short window skips
2317 // validation.
2318 return VALIDATION_NONE;
2321 if (effective_load_flags_ & LOAD_VALIDATE_CACHE)
2322 return VALIDATION_SYNCHRONOUS;
2324 if (request_->method == "PUT" || request_->method == "DELETE")
2325 return VALIDATION_SYNCHRONOUS;
2327 ValidationType validation_required_by_headers =
2328 response_.headers->RequiresValidation(response_.request_time,
2329 response_.response_time,
2330 cache_->clock_->Now());
2332 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) {
2333 // Asynchronous revalidation is only supported for GET and HEAD methods.
2334 if (request_->method != "GET" && request_->method != "HEAD")
2335 return VALIDATION_SYNCHRONOUS;
2338 return validation_required_by_headers;
2341 bool HttpCache::Transaction::ConditionalizeRequest() {
2342 DCHECK(response_.headers.get());
2344 if (request_->method == "PUT" || request_->method == "DELETE")
2345 return false;
2347 // This only makes sense for cached 200 or 206 responses.
2348 if (response_.headers->response_code() != 200 &&
2349 response_.headers->response_code() != 206) {
2350 return false;
2353 if (fail_conditionalization_for_test_)
2354 return false;
2356 DCHECK(response_.headers->response_code() != 206 ||
2357 response_.headers->HasStrongValidators());
2359 // Just use the first available ETag and/or Last-Modified header value.
2360 // TODO(darin): Or should we use the last?
2362 std::string etag_value;
2363 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2364 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2366 std::string last_modified_value;
2367 if (!vary_mismatch_) {
2368 response_.headers->EnumerateHeader(NULL, "last-modified",
2369 &last_modified_value);
2372 if (etag_value.empty() && last_modified_value.empty())
2373 return false;
2375 if (!partial_) {
2376 // Need to customize the request, so this forces us to allocate :(
2377 custom_request_.reset(new HttpRequestInfo(*request_));
2378 request_ = custom_request_.get();
2380 DCHECK(custom_request_.get());
2382 bool use_if_range =
2383 partial_ && !partial_->IsCurrentRangeCached() && !invalid_range_;
2385 if (!use_if_range) {
2386 // stale-while-revalidate is not useful when we only have a partial response
2387 // cached, so don't set the header in that case.
2388 HttpResponseHeaders::FreshnessLifetimes lifetimes =
2389 response_.headers->GetFreshnessLifetimes(response_.response_time);
2390 if (lifetimes.staleness > TimeDelta()) {
2391 TimeDelta current_age = response_.headers->GetCurrentAge(
2392 response_.request_time, response_.response_time,
2393 cache_->clock_->Now());
2395 custom_request_->extra_headers.SetHeader(
2396 kFreshnessHeader,
2397 base::StringPrintf("max-age=%" PRId64
2398 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2399 lifetimes.freshness.InSeconds(),
2400 lifetimes.staleness.InSeconds(),
2401 current_age.InSeconds()));
2405 if (!etag_value.empty()) {
2406 if (use_if_range) {
2407 // We don't want to switch to WRITE mode if we don't have this block of a
2408 // byte-range request because we may have other parts cached.
2409 custom_request_->extra_headers.SetHeader(
2410 HttpRequestHeaders::kIfRange, etag_value);
2411 } else {
2412 custom_request_->extra_headers.SetHeader(
2413 HttpRequestHeaders::kIfNoneMatch, etag_value);
2415 // For byte-range requests, make sure that we use only one way to validate
2416 // the request.
2417 if (partial_ && !partial_->IsCurrentRangeCached())
2418 return true;
2421 if (!last_modified_value.empty()) {
2422 if (use_if_range) {
2423 custom_request_->extra_headers.SetHeader(
2424 HttpRequestHeaders::kIfRange, last_modified_value);
2425 } else {
2426 custom_request_->extra_headers.SetHeader(
2427 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2431 return true;
2434 // We just received some headers from the server. We may have asked for a range,
2435 // in which case partial_ has an object. This could be the first network request
2436 // we make to fulfill the original request, or we may be already reading (from
2437 // the net and / or the cache). If we are not expecting a certain response, we
2438 // just bypass the cache for this request (but again, maybe we are reading), and
2439 // delete partial_ (so we are not able to "fix" the headers that we return to
2440 // the user). This results in either a weird response for the caller (we don't
2441 // expect it after all), or maybe a range that was not exactly what it was asked
2442 // for.
2444 // If the server is simply telling us that the resource has changed, we delete
2445 // the cached entry and restart the request as the caller intended (by returning
2446 // false from this method). However, we may not be able to do that at any point,
2447 // for instance if we already returned the headers to the user.
2449 // WARNING: Whenever this code returns false, it has to make sure that the next
2450 // time it is called it will return true so that we don't keep retrying the
2451 // request.
2452 bool HttpCache::Transaction::ValidatePartialResponse() {
2453 const HttpResponseHeaders* headers = new_response_->headers.get();
2454 int response_code = headers->response_code();
2455 bool partial_response = (response_code == 206);
2456 handling_206_ = false;
2458 if (!entry_ || request_->method != "GET")
2459 return true;
2461 if (invalid_range_) {
2462 // We gave up trying to match this request with the stored data. If the
2463 // server is ok with the request, delete the entry, otherwise just ignore
2464 // this request
2465 DCHECK(!reading_);
2466 if (partial_response || response_code == 200) {
2467 DoomPartialEntry(true);
2468 mode_ = NONE;
2469 } else {
2470 if (response_code == 304) {
2471 // Change the response code of the request to be 416 (Requested range
2472 // not satisfiable).
2473 response_ = *new_response_;
2474 partial_->FixResponseHeaders(response_.headers.get(), false);
2476 IgnoreRangeRequest();
2478 return true;
2481 if (!partial_) {
2482 // We are not expecting 206 but we may have one.
2483 if (partial_response)
2484 IgnoreRangeRequest();
2486 return true;
2489 // TODO(rvargas): Do we need to consider other results here?.
2490 bool failure = response_code == 200 || response_code == 416;
2492 if (partial_->IsCurrentRangeCached()) {
2493 // We asked for "If-None-Match: " so a 206 means a new object.
2494 if (partial_response)
2495 failure = true;
2497 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2498 return true;
2499 } else {
2500 // We asked for "If-Range: " so a 206 means just another range.
2501 if (partial_response) {
2502 if (partial_->ResponseHeadersOK(headers)) {
2503 handling_206_ = true;
2504 return true;
2505 } else {
2506 failure = true;
2510 if (!reading_ && !is_sparse_ && !partial_response) {
2511 // See if we can ignore the fact that we issued a byte range request.
2512 // If the server sends 200, just store it. If it sends an error, redirect
2513 // or something else, we may store the response as long as we didn't have
2514 // anything already stored.
2515 if (response_code == 200 ||
2516 (!truncated_ && response_code != 304 && response_code != 416)) {
2517 // The server is sending something else, and we can save it.
2518 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2519 partial_.reset();
2520 truncated_ = false;
2521 return true;
2525 // 304 is not expected here, but we'll spare the entry (unless it was
2526 // truncated).
2527 if (truncated_)
2528 failure = true;
2531 if (failure) {
2532 // We cannot truncate this entry, it has to be deleted.
2533 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2534 mode_ = NONE;
2535 if (is_sparse_ || truncated_) {
2536 // There was something cached to start with, either sparsed data (206), or
2537 // a truncated 200, which means that we probably modified the request,
2538 // adding a byte range or modifying the range requested by the caller.
2539 if (!reading_ && !partial_->IsLastRange()) {
2540 // We have not returned anything to the caller yet so it should be safe
2541 // to issue another network request, this time without us messing up the
2542 // headers.
2543 ResetPartialState(true);
2544 return false;
2546 LOG(WARNING) << "Failed to revalidate partial entry";
2548 DoomPartialEntry(true);
2549 return true;
2552 IgnoreRangeRequest();
2553 return true;
2556 void HttpCache::Transaction::IgnoreRangeRequest() {
2557 // We have a problem. We may or may not be reading already (in which case we
2558 // returned the headers), but we'll just pretend that this request is not
2559 // using the cache and see what happens. Most likely this is the first
2560 // response from the server (it's not changing its mind midway, right?).
2561 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2562 if (mode_ & WRITE)
2563 DoneWritingToEntry(mode_ != WRITE);
2564 else if (mode_ & READ && entry_)
2565 cache_->DoneReadingFromEntry(entry_, this);
2567 partial_.reset(NULL);
2568 entry_ = NULL;
2569 mode_ = NONE;
2572 void HttpCache::Transaction::FixHeadersForHead() {
2573 if (response_.headers->response_code() == 206) {
2574 response_.headers->RemoveHeader("Content-Range");
2575 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2579 int HttpCache::Transaction::SetupEntryForRead() {
2580 if (network_trans_)
2581 ResetNetworkTransaction();
2582 if (partial_) {
2583 if (truncated_ || is_sparse_ || !invalid_range_) {
2584 // We are going to return the saved response headers to the caller, so
2585 // we may need to adjust them first.
2586 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2587 return OK;
2588 } else {
2589 partial_.reset();
2592 cache_->ConvertWriterToReader(entry_);
2593 mode_ = READ;
2595 if (request_->method == "HEAD")
2596 FixHeadersForHead();
2598 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2599 next_state_ = STATE_CACHE_READ_METADATA;
2600 return OK;
2603 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2604 IOBuffer* data, int data_len,
2605 const CompletionCallback& callback) {
2606 if (!entry_)
2607 return data_len;
2609 int rv = 0;
2610 if (!partial_ || !data_len) {
2611 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2612 true);
2613 } else {
2614 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2616 return rv;
2619 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2620 if (!entry_)
2621 return OK;
2623 if (net_log_.IsCapturing())
2624 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2626 // Do not cache no-store content. Do not cache content with cert errors
2627 // either. This is to prevent not reporting net errors when loading a
2628 // resource from the cache. When we load a page over HTTPS with a cert error
2629 // we show an SSL blocking page. If the user clicks proceed we reload the
2630 // resource ignoring the errors. The loaded resource is then cached. If that
2631 // resource is subsequently loaded from the cache, no net error is reported
2632 // (even though the cert status contains the actual errors) and no SSL
2633 // blocking page is shown. An alternative would be to reverse-map the cert
2634 // status to a net error and replay the net error.
2635 if ((response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2636 IsCertStatusError(response_.ssl_info.cert_status)) {
2637 DoneWritingToEntry(false);
2638 if (net_log_.IsCapturing())
2639 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2640 return OK;
2643 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2644 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2645 WriteCertChain();
2647 if (truncated)
2648 DCHECK_EQ(200, response_.headers->response_code());
2650 // When writing headers, we normally only write the non-transient headers.
2651 bool skip_transient_headers = true;
2652 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2653 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2654 data->Done();
2656 io_buf_len_ = data->pickle()->size();
2657 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2658 io_buf_len_, io_callback_, true);
2661 int HttpCache::Transaction::OnWriteResponseInfoToEntryComplete(int result) {
2662 if (!entry_)
2663 return OK;
2664 if (net_log_.IsCapturing()) {
2665 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
2666 result);
2669 if (result != io_buf_len_) {
2670 DLOG(ERROR) << "failed to write response info to cache";
2671 DoneWritingToEntry(false);
2673 return OK;
2676 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2677 if (!entry_)
2678 return;
2680 RecordHistograms();
2682 cache_->DoneWritingToEntry(entry_, success);
2683 entry_ = NULL;
2684 mode_ = NONE; // switch to 'pass through' mode
2687 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2688 DLOG(ERROR) << "ReadData failed: " << result;
2689 const int result_for_histogram = std::max(0, -result);
2690 if (restart) {
2691 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2692 result_for_histogram);
2693 } else {
2694 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2695 result_for_histogram);
2698 // Avoid using this entry in the future.
2699 if (cache_.get())
2700 cache_->DoomActiveEntry(cache_key_);
2702 if (restart) {
2703 DCHECK(!reading_);
2704 DCHECK(!network_trans_.get());
2705 cache_->DoneWithEntry(entry_, this, false);
2706 entry_ = NULL;
2707 is_sparse_ = false;
2708 partial_.reset();
2709 next_state_ = STATE_GET_BACKEND;
2710 return OK;
2713 return ERR_CACHE_READ_FAILURE;
2716 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
2717 if (entry_lock_waiting_since_ != start_time)
2718 return;
2720 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
2722 if (!cache_)
2723 return;
2725 cache_->RemovePendingTransaction(this);
2726 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
2729 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2730 DVLOG(2) << "DoomPartialEntry";
2731 int rv = cache_->DoomEntry(cache_key_, NULL);
2732 DCHECK_EQ(OK, rv);
2733 cache_->DoneWithEntry(entry_, this, false);
2734 entry_ = NULL;
2735 is_sparse_ = false;
2736 truncated_ = false;
2737 if (delete_object)
2738 partial_.reset(NULL);
2741 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2742 partial_->OnNetworkReadCompleted(result);
2744 if (result == 0) {
2745 // We need to move on to the next range.
2746 ResetNetworkTransaction();
2747 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2749 return result;
2752 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2753 partial_->OnCacheReadCompleted(result);
2755 if (result == 0 && mode_ == READ_WRITE) {
2756 // We need to move on to the next range.
2757 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2758 } else if (result < 0) {
2759 return OnCacheReadError(result, false);
2761 return result;
2764 int HttpCache::Transaction::DoRestartPartialRequest() {
2765 // The stored data cannot be used. Get rid of it and restart this request.
2766 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST);
2768 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
2769 // to Doom the entry again).
2770 mode_ = WRITE;
2771 ResetPartialState(!range_requested_);
2772 next_state_ = STATE_CREATE_ENTRY;
2773 return OK;
2776 void HttpCache::Transaction::ResetPartialState(bool delete_object) {
2777 partial_->RestoreHeaders(&custom_request_->extra_headers);
2778 DoomPartialEntry(delete_object);
2780 if (!delete_object) {
2781 // The simplest way to re-initialize partial_ is to create a new object.
2782 partial_.reset(new PartialData());
2783 if (partial_->Init(request_->extra_headers))
2784 partial_->SetHeaders(custom_request_->extra_headers);
2785 else
2786 partial_.reset();
2790 void HttpCache::Transaction::ResetNetworkTransaction() {
2791 DCHECK(!old_network_trans_load_timing_);
2792 DCHECK(network_trans_);
2793 LoadTimingInfo load_timing;
2794 if (network_trans_->GetLoadTimingInfo(&load_timing))
2795 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2796 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2797 ConnectionAttempts attempts;
2798 network_trans_->GetConnectionAttempts(&attempts);
2799 for (const auto& attempt : attempts)
2800 old_connection_attempts_.push_back(attempt);
2801 network_trans_.reset();
2804 // Histogram data from the end of 2010 show the following distribution of
2805 // response headers:
2807 // Content-Length............... 87%
2808 // Date......................... 98%
2809 // Last-Modified................ 49%
2810 // Etag......................... 19%
2811 // Accept-Ranges: bytes......... 25%
2812 // Accept-Ranges: none.......... 0.4%
2813 // Strong Validator............. 50%
2814 // Strong Validator + ranges.... 24%
2815 // Strong Validator + CL........ 49%
2817 bool HttpCache::Transaction::CanResume(bool has_data) {
2818 // Double check that there is something worth keeping.
2819 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2820 return false;
2822 if (request_->method != "GET")
2823 return false;
2825 // Note that if this is a 206, content-length was already fixed after calling
2826 // PartialData::ResponseHeadersOK().
2827 if (response_.headers->GetContentLength() <= 0 ||
2828 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2829 !response_.headers->HasStrongValidators()) {
2830 return false;
2833 return true;
2836 void HttpCache::Transaction::UpdateTransactionPattern(
2837 TransactionPattern new_transaction_pattern) {
2838 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2839 return;
2840 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2841 new_transaction_pattern == PATTERN_NOT_COVERED);
2842 transaction_pattern_ = new_transaction_pattern;
2845 void HttpCache::Transaction::RecordHistograms() {
2846 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2847 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2848 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2849 cache_->mode() != NORMAL || request_->method != "GET") {
2850 return;
2852 UMA_HISTOGRAM_ENUMERATION(
2853 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2854 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2855 return;
2856 DCHECK(!range_requested_);
2857 DCHECK(!first_cache_access_since_.is_null());
2859 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2861 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2863 bool did_send_request = !send_request_since_.is_null();
2864 DCHECK(
2865 (did_send_request &&
2866 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2867 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2868 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2869 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2870 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2872 if (!did_send_request) {
2873 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2874 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2875 return;
2878 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2879 int64 before_send_percent = (total_time.ToInternalValue() == 0) ?
2880 0 : before_send_time * 100 / total_time;
2881 DCHECK_GE(before_send_percent, 0);
2882 DCHECK_LE(before_send_percent, 100);
2883 base::HistogramBase::Sample before_send_sample =
2884 static_cast<base::HistogramBase::Sample>(before_send_percent);
2886 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2887 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2888 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample);
2890 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2891 // below this comment after we have received initial data.
2892 switch (transaction_pattern_) {
2893 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2894 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2895 before_send_time);
2896 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2897 before_send_sample);
2898 break;
2900 case PATTERN_ENTRY_NOT_CACHED: {
2901 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2902 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2903 before_send_sample);
2904 break;
2906 case PATTERN_ENTRY_VALIDATED: {
2907 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2908 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2909 before_send_sample);
2910 break;
2912 case PATTERN_ENTRY_UPDATED: {
2913 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2914 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2915 before_send_sample);
2916 break;
2918 default:
2919 NOTREACHED();
2923 void HttpCache::Transaction::OnIOComplete(int result) {
2924 DoLoop(result);
2927 } // namespace net