Web MIDI: enable receiving functionality in Linux and Chrome OS
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blob15ad924df8f846599166ad663f6c01d3b3dbe94c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_cache_transaction.h"
7 #include "build/build_config.h"
9 #if defined(OS_POSIX)
10 #include <unistd.h>
11 #endif
13 #include <algorithm>
14 #include <string>
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/metrics/field_trial.h"
20 #include "base/metrics/histogram.h"
21 #include "base/metrics/sparse_histogram.h"
22 #include "base/rand_util.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_util.h"
25 #include "base/time/time.h"
26 #include "net/base/completion_callback.h"
27 #include "net/base/io_buffer.h"
28 #include "net/base/load_flags.h"
29 #include "net/base/load_timing_info.h"
30 #include "net/base/net_errors.h"
31 #include "net/base/net_log.h"
32 #include "net/base/upload_data_stream.h"
33 #include "net/cert/cert_status_flags.h"
34 #include "net/disk_cache/disk_cache.h"
35 #include "net/http/http_network_session.h"
36 #include "net/http/http_request_info.h"
37 #include "net/http/http_response_headers.h"
38 #include "net/http/http_transaction.h"
39 #include "net/http/http_util.h"
40 #include "net/http/partial_data.h"
41 #include "net/ssl/ssl_cert_request_info.h"
42 #include "net/ssl/ssl_config_service.h"
44 using base::Time;
45 using base::TimeDelta;
46 using base::TimeTicks;
48 namespace {
50 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
51 // a "non-error response" is one with a 2xx (Successful) or 3xx
52 // (Redirection) status code.
53 bool NonErrorResponse(int status_code) {
54 int status_code_range = status_code / 100;
55 return status_code_range == 2 || status_code_range == 3;
58 // Error codes that will be considered indicative of a page being offline/
59 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
60 bool IsOfflineError(int error) {
61 return (error == net::ERR_NAME_NOT_RESOLVED ||
62 error == net::ERR_INTERNET_DISCONNECTED ||
63 error == net::ERR_ADDRESS_UNREACHABLE ||
64 error == net::ERR_CONNECTION_TIMED_OUT);
67 // Enum for UMA, indicating the status (with regard to offline mode) of
68 // a particular request.
69 enum RequestOfflineStatus {
70 // A cache transaction hit in cache (data was present and not stale)
71 // and returned it.
72 OFFLINE_STATUS_FRESH_CACHE,
74 // A network request was required for a cache entry, and it succeeded.
75 OFFLINE_STATUS_NETWORK_SUCCEEDED,
77 // A network request was required for a cache entry, and it failed with
78 // a non-offline error.
79 OFFLINE_STATUS_NETWORK_FAILED,
81 // A network request was required for a cache entry, it failed with an
82 // offline error, and we could serve stale data if
83 // LOAD_FROM_CACHE_IF_OFFLINE was set.
84 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE,
86 // A network request was required for a cache entry, it failed with
87 // an offline error, and there was no servable data in cache (even
88 // stale data).
89 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE,
91 OFFLINE_STATUS_MAX_ENTRIES
94 void RecordOfflineStatus(int load_flags, RequestOfflineStatus status) {
95 // Restrict to main frame to keep statistics close to
96 // "would have shown them something useful if offline mode was enabled".
97 if (load_flags & net::LOAD_MAIN_FRAME) {
98 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status,
99 OFFLINE_STATUS_MAX_ENTRIES);
103 // TODO(rvargas): Remove once we get the data.
104 void RecordVaryHeaderHistogram(const net::HttpResponseInfo* response) {
105 enum VaryType {
106 VARY_NOT_PRESENT,
107 VARY_UA,
108 VARY_OTHER,
109 VARY_MAX
111 VaryType vary = VARY_NOT_PRESENT;
112 if (response->vary_data.is_valid()) {
113 vary = VARY_OTHER;
114 if (response->headers->HasHeaderValue("vary", "user-agent"))
115 vary = VARY_UA;
117 UMA_HISTOGRAM_ENUMERATION("HttpCache.Vary", vary, VARY_MAX);
120 } // namespace
122 namespace net {
124 struct HeaderNameAndValue {
125 const char* name;
126 const char* value;
129 // If the request includes one of these request headers, then avoid caching
130 // to avoid getting confused.
131 static const HeaderNameAndValue kPassThroughHeaders[] = {
132 { "if-unmodified-since", NULL }, // causes unexpected 412s
133 { "if-match", NULL }, // causes unexpected 412s
134 { "if-range", NULL },
135 { NULL, NULL }
138 struct ValidationHeaderInfo {
139 const char* request_header_name;
140 const char* related_response_header_name;
143 static const ValidationHeaderInfo kValidationHeaders[] = {
144 { "if-modified-since", "last-modified" },
145 { "if-none-match", "etag" },
148 // If the request includes one of these request headers, then avoid reusing
149 // our cached copy if any.
150 static const HeaderNameAndValue kForceFetchHeaders[] = {
151 { "cache-control", "no-cache" },
152 { "pragma", "no-cache" },
153 { NULL, NULL }
156 // If the request includes one of these request headers, then force our
157 // cached copy (if any) to be revalidated before reusing it.
158 static const HeaderNameAndValue kForceValidateHeaders[] = {
159 { "cache-control", "max-age=0" },
160 { NULL, NULL }
163 static bool HeaderMatches(const HttpRequestHeaders& headers,
164 const HeaderNameAndValue* search) {
165 for (; search->name; ++search) {
166 std::string header_value;
167 if (!headers.GetHeader(search->name, &header_value))
168 continue;
170 if (!search->value)
171 return true;
173 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
174 while (v.GetNext()) {
175 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
176 return true;
179 return false;
182 //-----------------------------------------------------------------------------
184 HttpCache::Transaction::Transaction(
185 RequestPriority priority,
186 HttpCache* cache)
187 : next_state_(STATE_NONE),
188 request_(NULL),
189 priority_(priority),
190 cache_(cache->AsWeakPtr()),
191 entry_(NULL),
192 new_entry_(NULL),
193 new_response_(NULL),
194 mode_(NONE),
195 target_state_(STATE_NONE),
196 reading_(false),
197 invalid_range_(false),
198 truncated_(false),
199 is_sparse_(false),
200 range_requested_(false),
201 handling_206_(false),
202 cache_pending_(false),
203 done_reading_(false),
204 vary_mismatch_(false),
205 couldnt_conditionalize_request_(false),
206 io_buf_len_(0),
207 read_offset_(0),
208 effective_load_flags_(0),
209 write_len_(0),
210 weak_factory_(this),
211 io_callback_(base::Bind(&Transaction::OnIOComplete,
212 weak_factory_.GetWeakPtr())),
213 transaction_pattern_(PATTERN_UNDEFINED),
214 total_received_bytes_(0),
215 websocket_handshake_stream_base_create_helper_(NULL) {
216 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders ==
217 arraysize(kValidationHeaders),
218 Invalid_number_of_validation_headers);
221 HttpCache::Transaction::~Transaction() {
222 // We may have to issue another IO, but we should never invoke the callback_
223 // after this point.
224 callback_.Reset();
226 if (cache_) {
227 if (entry_) {
228 bool cancel_request = reading_ && response_.headers;
229 if (cancel_request) {
230 if (partial_) {
231 entry_->disk_entry->CancelSparseIO();
232 } else {
233 cancel_request &= (response_.headers->response_code() == 200);
237 cache_->DoneWithEntry(entry_, this, cancel_request);
238 } else if (cache_pending_) {
239 cache_->RemovePendingTransaction(this);
244 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
245 const CompletionCallback& callback) {
246 DCHECK(buf);
247 DCHECK_GT(buf_len, 0);
248 DCHECK(!callback.is_null());
249 if (!cache_.get() || !entry_)
250 return ERR_UNEXPECTED;
252 // We don't need to track this operation for anything.
253 // It could be possible to check if there is something already written and
254 // avoid writing again (it should be the same, right?), but let's allow the
255 // caller to "update" the contents with something new.
256 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
257 callback, true);
260 bool HttpCache::Transaction::AddTruncatedFlag() {
261 DCHECK(mode_ & WRITE || mode_ == NONE);
263 // Don't set the flag for sparse entries.
264 if (partial_.get() && !truncated_)
265 return true;
267 if (!CanResume(true))
268 return false;
270 // We may have received the whole resource already.
271 if (done_reading_)
272 return true;
274 truncated_ = true;
275 target_state_ = STATE_NONE;
276 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
277 DoLoop(OK);
278 return true;
281 LoadState HttpCache::Transaction::GetWriterLoadState() const {
282 if (network_trans_.get())
283 return network_trans_->GetLoadState();
284 if (entry_ || !request_)
285 return LOAD_STATE_IDLE;
286 return LOAD_STATE_WAITING_FOR_CACHE;
289 const BoundNetLog& HttpCache::Transaction::net_log() const {
290 return net_log_;
293 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
294 const CompletionCallback& callback,
295 const BoundNetLog& net_log) {
296 DCHECK(request);
297 DCHECK(!callback.is_null());
299 // Ensure that we only have one asynchronous call at a time.
300 DCHECK(callback_.is_null());
301 DCHECK(!reading_);
302 DCHECK(!network_trans_.get());
303 DCHECK(!entry_);
305 if (!cache_.get())
306 return ERR_UNEXPECTED;
308 SetRequest(net_log, request);
310 // We have to wait until the backend is initialized so we start the SM.
311 next_state_ = STATE_GET_BACKEND;
312 int rv = DoLoop(OK);
314 // Setting this here allows us to check for the existence of a callback_ to
315 // determine if we are still inside Start.
316 if (rv == ERR_IO_PENDING)
317 callback_ = callback;
319 return rv;
322 int HttpCache::Transaction::RestartIgnoringLastError(
323 const CompletionCallback& callback) {
324 DCHECK(!callback.is_null());
326 // Ensure that we only have one asynchronous call at a time.
327 DCHECK(callback_.is_null());
329 if (!cache_.get())
330 return ERR_UNEXPECTED;
332 int rv = RestartNetworkRequest();
334 if (rv == ERR_IO_PENDING)
335 callback_ = callback;
337 return rv;
340 int HttpCache::Transaction::RestartWithCertificate(
341 X509Certificate* client_cert,
342 const CompletionCallback& callback) {
343 DCHECK(!callback.is_null());
345 // Ensure that we only have one asynchronous call at a time.
346 DCHECK(callback_.is_null());
348 if (!cache_.get())
349 return ERR_UNEXPECTED;
351 int rv = RestartNetworkRequestWithCertificate(client_cert);
353 if (rv == ERR_IO_PENDING)
354 callback_ = callback;
356 return rv;
359 int HttpCache::Transaction::RestartWithAuth(
360 const AuthCredentials& credentials,
361 const CompletionCallback& callback) {
362 DCHECK(auth_response_.headers.get());
363 DCHECK(!callback.is_null());
365 // Ensure that we only have one asynchronous call at a time.
366 DCHECK(callback_.is_null());
368 if (!cache_.get())
369 return ERR_UNEXPECTED;
371 // Clear the intermediate response since we are going to start over.
372 auth_response_ = HttpResponseInfo();
374 int rv = RestartNetworkRequestWithAuth(credentials);
376 if (rv == ERR_IO_PENDING)
377 callback_ = callback;
379 return rv;
382 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
383 if (!network_trans_.get())
384 return false;
385 return network_trans_->IsReadyToRestartForAuth();
388 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
389 const CompletionCallback& callback) {
390 DCHECK(buf);
391 DCHECK_GT(buf_len, 0);
392 DCHECK(!callback.is_null());
394 DCHECK(callback_.is_null());
396 if (!cache_.get())
397 return ERR_UNEXPECTED;
399 // If we have an intermediate auth response at this point, then it means the
400 // user wishes to read the network response (the error page). If there is a
401 // previous response in the cache then we should leave it intact.
402 if (auth_response_.headers.get() && mode_ != NONE) {
403 UpdateTransactionPattern(PATTERN_NOT_COVERED);
404 DCHECK(mode_ & WRITE);
405 DoneWritingToEntry(mode_ == READ_WRITE);
406 mode_ = NONE;
409 reading_ = true;
410 int rv;
412 switch (mode_) {
413 case READ_WRITE:
414 DCHECK(partial_.get());
415 if (!network_trans_.get()) {
416 // We are just reading from the cache, but we may be writing later.
417 rv = ReadFromEntry(buf, buf_len);
418 break;
420 case NONE:
421 case WRITE:
422 DCHECK(network_trans_.get());
423 rv = ReadFromNetwork(buf, buf_len);
424 break;
425 case READ:
426 rv = ReadFromEntry(buf, buf_len);
427 break;
428 default:
429 NOTREACHED();
430 rv = ERR_FAILED;
433 if (rv == ERR_IO_PENDING) {
434 DCHECK(callback_.is_null());
435 callback_ = callback;
437 return rv;
440 void HttpCache::Transaction::StopCaching() {
441 // We really don't know where we are now. Hopefully there is no operation in
442 // progress, but nothing really prevents this method to be called after we
443 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
444 // point because we need the state machine for that (and even if we are really
445 // free, that would be an asynchronous operation). In other words, keep the
446 // entry how it is (it will be marked as truncated at destruction), and let
447 // the next piece of code that executes know that we are now reading directly
448 // from the net.
449 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
450 !is_sparse_ && !range_requested_) {
451 mode_ = NONE;
455 bool HttpCache::Transaction::GetFullRequestHeaders(
456 HttpRequestHeaders* headers) const {
457 if (network_trans_)
458 return network_trans_->GetFullRequestHeaders(headers);
460 // TODO(ttuttle): Read headers from cache.
461 return false;
464 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
465 int64 total_received_bytes = total_received_bytes_;
466 if (network_trans_)
467 total_received_bytes += network_trans_->GetTotalReceivedBytes();
468 return total_received_bytes;
471 void HttpCache::Transaction::DoneReading() {
472 if (cache_.get() && entry_) {
473 DCHECK_NE(mode_, UPDATE);
474 if (mode_ & WRITE) {
475 DoneWritingToEntry(true);
476 } else if (mode_ & READ) {
477 // It is necessary to check mode_ & READ because it is possible
478 // for mode_ to be NONE and entry_ non-NULL with a write entry
479 // if StopCaching was called.
480 cache_->DoneReadingFromEntry(entry_, this);
481 entry_ = NULL;
486 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
487 // Null headers means we encountered an error or haven't a response yet
488 if (auth_response_.headers.get())
489 return &auth_response_;
490 return (response_.headers.get() || response_.ssl_info.cert.get() ||
491 response_.cert_request_info.get())
492 ? &response_
493 : NULL;
496 LoadState HttpCache::Transaction::GetLoadState() const {
497 LoadState state = GetWriterLoadState();
498 if (state != LOAD_STATE_WAITING_FOR_CACHE)
499 return state;
501 if (cache_.get())
502 return cache_->GetLoadStateForPendingTransaction(this);
504 return LOAD_STATE_IDLE;
507 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
508 if (network_trans_.get())
509 return network_trans_->GetUploadProgress();
510 return final_upload_progress_;
513 void HttpCache::Transaction::SetQuicServerInfo(
514 QuicServerInfo* quic_server_info) {}
516 bool HttpCache::Transaction::GetLoadTimingInfo(
517 LoadTimingInfo* load_timing_info) const {
518 if (network_trans_)
519 return network_trans_->GetLoadTimingInfo(load_timing_info);
521 if (old_network_trans_load_timing_) {
522 *load_timing_info = *old_network_trans_load_timing_;
523 return true;
526 if (first_cache_access_since_.is_null())
527 return false;
529 // If the cache entry was opened, return that time.
530 load_timing_info->send_start = first_cache_access_since_;
531 // This time doesn't make much sense when reading from the cache, so just use
532 // the same time as send_start.
533 load_timing_info->send_end = first_cache_access_since_;
534 return true;
537 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
538 priority_ = priority;
539 if (network_trans_)
540 network_trans_->SetPriority(priority_);
543 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
544 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
545 websocket_handshake_stream_base_create_helper_ = create_helper;
546 if (network_trans_)
547 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
550 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
551 const BeforeNetworkStartCallback& callback) {
552 DCHECK(!network_trans_);
553 before_network_start_callback_ = callback;
556 int HttpCache::Transaction::ResumeNetworkStart() {
557 if (network_trans_)
558 return network_trans_->ResumeNetworkStart();
559 return ERR_UNEXPECTED;
562 //-----------------------------------------------------------------------------
564 void HttpCache::Transaction::DoCallback(int rv) {
565 DCHECK(rv != ERR_IO_PENDING);
566 DCHECK(!callback_.is_null());
568 read_buf_ = NULL; // Release the buffer before invoking the callback.
570 // Since Run may result in Read being called, clear callback_ up front.
571 CompletionCallback c = callback_;
572 callback_.Reset();
573 c.Run(rv);
576 int HttpCache::Transaction::HandleResult(int rv) {
577 DCHECK(rv != ERR_IO_PENDING);
578 if (!callback_.is_null())
579 DoCallback(rv);
581 return rv;
584 // A few common patterns: (Foo* means Foo -> FooComplete)
586 // Not-cached entry:
587 // Start():
588 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
589 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
590 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
591 // PartialHeadersReceived
593 // Read():
594 // NetworkRead* -> CacheWriteData*
596 // Cached entry, no validation:
597 // Start():
598 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
599 // -> BeginPartialCacheValidation() -> BeginCacheValidation()
601 // Read():
602 // CacheReadData*
604 // Cached entry, validation (304):
605 // Start():
606 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
607 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
608 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
609 // CacheWriteResponse* -> UpdateCachedResponseComplete ->
610 // OverwriteCachedResponse -> PartialHeadersReceived
612 // Read():
613 // CacheReadData*
615 // Cached entry, validation and replace (200):
616 // Start():
617 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
618 // -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
619 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
620 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
621 // PartialHeadersReceived
623 // Read():
624 // NetworkRead* -> CacheWriteData*
626 // Sparse entry, partially cached, byte range request:
627 // Start():
628 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
629 // -> BeginPartialCacheValidation() -> CacheQueryData* ->
630 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
631 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
632 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
633 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
634 // PartialHeadersReceived
636 // Read() 1:
637 // NetworkRead* -> CacheWriteData*
639 // Read() 2:
640 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
641 // CompletePartialCacheValidation -> CacheReadData* ->
643 // Read() 3:
644 // CacheReadData* -> StartPartialCacheValidation ->
645 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
646 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
647 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
649 int HttpCache::Transaction::DoLoop(int result) {
650 DCHECK(next_state_ != STATE_NONE);
652 int rv = result;
653 do {
654 State state = next_state_;
655 next_state_ = STATE_NONE;
656 switch (state) {
657 case STATE_GET_BACKEND:
658 DCHECK_EQ(OK, rv);
659 rv = DoGetBackend();
660 break;
661 case STATE_GET_BACKEND_COMPLETE:
662 rv = DoGetBackendComplete(rv);
663 break;
664 case STATE_SEND_REQUEST:
665 DCHECK_EQ(OK, rv);
666 rv = DoSendRequest();
667 break;
668 case STATE_SEND_REQUEST_COMPLETE:
669 rv = DoSendRequestComplete(rv);
670 break;
671 case STATE_SUCCESSFUL_SEND_REQUEST:
672 DCHECK_EQ(OK, rv);
673 rv = DoSuccessfulSendRequest();
674 break;
675 case STATE_NETWORK_READ:
676 DCHECK_EQ(OK, rv);
677 rv = DoNetworkRead();
678 break;
679 case STATE_NETWORK_READ_COMPLETE:
680 rv = DoNetworkReadComplete(rv);
681 break;
682 case STATE_INIT_ENTRY:
683 DCHECK_EQ(OK, rv);
684 rv = DoInitEntry();
685 break;
686 case STATE_OPEN_ENTRY:
687 DCHECK_EQ(OK, rv);
688 rv = DoOpenEntry();
689 break;
690 case STATE_OPEN_ENTRY_COMPLETE:
691 rv = DoOpenEntryComplete(rv);
692 break;
693 case STATE_CREATE_ENTRY:
694 DCHECK_EQ(OK, rv);
695 rv = DoCreateEntry();
696 break;
697 case STATE_CREATE_ENTRY_COMPLETE:
698 rv = DoCreateEntryComplete(rv);
699 break;
700 case STATE_DOOM_ENTRY:
701 DCHECK_EQ(OK, rv);
702 rv = DoDoomEntry();
703 break;
704 case STATE_DOOM_ENTRY_COMPLETE:
705 rv = DoDoomEntryComplete(rv);
706 break;
707 case STATE_ADD_TO_ENTRY:
708 DCHECK_EQ(OK, rv);
709 rv = DoAddToEntry();
710 break;
711 case STATE_ADD_TO_ENTRY_COMPLETE:
712 rv = DoAddToEntryComplete(rv);
713 break;
714 case STATE_START_PARTIAL_CACHE_VALIDATION:
715 DCHECK_EQ(OK, rv);
716 rv = DoStartPartialCacheValidation();
717 break;
718 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
719 rv = DoCompletePartialCacheValidation(rv);
720 break;
721 case STATE_UPDATE_CACHED_RESPONSE:
722 DCHECK_EQ(OK, rv);
723 rv = DoUpdateCachedResponse();
724 break;
725 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
726 rv = DoUpdateCachedResponseComplete(rv);
727 break;
728 case STATE_OVERWRITE_CACHED_RESPONSE:
729 DCHECK_EQ(OK, rv);
730 rv = DoOverwriteCachedResponse();
731 break;
732 case STATE_TRUNCATE_CACHED_DATA:
733 DCHECK_EQ(OK, rv);
734 rv = DoTruncateCachedData();
735 break;
736 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
737 rv = DoTruncateCachedDataComplete(rv);
738 break;
739 case STATE_TRUNCATE_CACHED_METADATA:
740 DCHECK_EQ(OK, rv);
741 rv = DoTruncateCachedMetadata();
742 break;
743 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
744 rv = DoTruncateCachedMetadataComplete(rv);
745 break;
746 case STATE_PARTIAL_HEADERS_RECEIVED:
747 DCHECK_EQ(OK, rv);
748 rv = DoPartialHeadersReceived();
749 break;
750 case STATE_CACHE_READ_RESPONSE:
751 DCHECK_EQ(OK, rv);
752 rv = DoCacheReadResponse();
753 break;
754 case STATE_CACHE_READ_RESPONSE_COMPLETE:
755 rv = DoCacheReadResponseComplete(rv);
756 break;
757 case STATE_CACHE_WRITE_RESPONSE:
758 DCHECK_EQ(OK, rv);
759 rv = DoCacheWriteResponse();
760 break;
761 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
762 DCHECK_EQ(OK, rv);
763 rv = DoCacheWriteTruncatedResponse();
764 break;
765 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
766 rv = DoCacheWriteResponseComplete(rv);
767 break;
768 case STATE_CACHE_READ_METADATA:
769 DCHECK_EQ(OK, rv);
770 rv = DoCacheReadMetadata();
771 break;
772 case STATE_CACHE_READ_METADATA_COMPLETE:
773 rv = DoCacheReadMetadataComplete(rv);
774 break;
775 case STATE_CACHE_QUERY_DATA:
776 DCHECK_EQ(OK, rv);
777 rv = DoCacheQueryData();
778 break;
779 case STATE_CACHE_QUERY_DATA_COMPLETE:
780 rv = DoCacheQueryDataComplete(rv);
781 break;
782 case STATE_CACHE_READ_DATA:
783 DCHECK_EQ(OK, rv);
784 rv = DoCacheReadData();
785 break;
786 case STATE_CACHE_READ_DATA_COMPLETE:
787 rv = DoCacheReadDataComplete(rv);
788 break;
789 case STATE_CACHE_WRITE_DATA:
790 rv = DoCacheWriteData(rv);
791 break;
792 case STATE_CACHE_WRITE_DATA_COMPLETE:
793 rv = DoCacheWriteDataComplete(rv);
794 break;
795 default:
796 NOTREACHED() << "bad state";
797 rv = ERR_FAILED;
798 break;
800 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
802 if (rv != ERR_IO_PENDING)
803 HandleResult(rv);
805 return rv;
808 int HttpCache::Transaction::DoGetBackend() {
809 cache_pending_ = true;
810 next_state_ = STATE_GET_BACKEND_COMPLETE;
811 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
812 return cache_->GetBackendForTransaction(this);
815 int HttpCache::Transaction::DoGetBackendComplete(int result) {
816 DCHECK(result == OK || result == ERR_FAILED);
817 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
818 result);
819 cache_pending_ = false;
821 if (!ShouldPassThrough()) {
822 cache_key_ = cache_->GenerateCacheKey(request_);
824 // Requested cache access mode.
825 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
826 mode_ = READ;
827 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
828 mode_ = WRITE;
829 } else {
830 mode_ = READ_WRITE;
833 // Downgrade to UPDATE if the request has been externally conditionalized.
834 if (external_validation_.initialized) {
835 if (mode_ & WRITE) {
836 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
837 // in case READ was off).
838 mode_ = UPDATE;
839 } else {
840 mode_ = NONE;
845 // Use PUT and DELETE only to invalidate existing stored entries.
846 if ((request_->method == "PUT" || request_->method == "DELETE") &&
847 mode_ != READ_WRITE && mode_ != WRITE) {
848 mode_ = NONE;
851 // If must use cache, then we must fail. This can happen for back/forward
852 // navigations to a page generated via a form post.
853 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
854 return ERR_CACHE_MISS;
856 if (mode_ == NONE) {
857 if (partial_.get()) {
858 partial_->RestoreHeaders(&custom_request_->extra_headers);
859 partial_.reset();
861 next_state_ = STATE_SEND_REQUEST;
862 } else {
863 next_state_ = STATE_INIT_ENTRY;
866 // This is only set if we have something to do with the response.
867 range_requested_ = (partial_.get() != NULL);
869 return OK;
872 int HttpCache::Transaction::DoSendRequest() {
873 DCHECK(mode_ & WRITE || mode_ == NONE);
874 DCHECK(!network_trans_.get());
876 send_request_since_ = TimeTicks::Now();
878 // Create a network transaction.
879 int rv = cache_->network_layer_->CreateTransaction(priority_,
880 &network_trans_);
881 if (rv != OK)
882 return rv;
883 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
885 // Old load timing information, if any, is now obsolete.
886 old_network_trans_load_timing_.reset();
888 if (websocket_handshake_stream_base_create_helper_)
889 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
890 websocket_handshake_stream_base_create_helper_);
892 next_state_ = STATE_SEND_REQUEST_COMPLETE;
893 rv = network_trans_->Start(request_, io_callback_, net_log_);
894 return rv;
897 int HttpCache::Transaction::DoSendRequestComplete(int result) {
898 if (!cache_.get())
899 return ERR_UNEXPECTED;
901 // If requested, and we have a readable cache entry, and we have
902 // an error indicating that we're offline as opposed to in contact
903 // with a bad server, read from cache anyway.
904 if (IsOfflineError(result)) {
905 if (mode_ == READ_WRITE && entry_ && !partial_) {
906 RecordOfflineStatus(effective_load_flags_,
907 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE);
908 if (effective_load_flags_ & LOAD_FROM_CACHE_IF_OFFLINE) {
909 UpdateTransactionPattern(PATTERN_NOT_COVERED);
910 response_.server_data_unavailable = true;
911 return SetupEntryForRead();
913 } else {
914 RecordOfflineStatus(effective_load_flags_,
915 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE);
917 } else {
918 RecordOfflineStatus(effective_load_flags_,
919 (result == OK ? OFFLINE_STATUS_NETWORK_SUCCEEDED :
920 OFFLINE_STATUS_NETWORK_FAILED));
923 // If we tried to conditionalize the request and failed, we know
924 // we won't be reading from the cache after this point.
925 if (couldnt_conditionalize_request_)
926 mode_ = WRITE;
928 if (result == OK) {
929 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
930 return OK;
933 // Do not record requests that have network errors or restarts.
934 UpdateTransactionPattern(PATTERN_NOT_COVERED);
935 if (IsCertificateError(result)) {
936 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
937 // If we get a certificate error, then there is a certificate in ssl_info,
938 // so GetResponseInfo() should never return NULL here.
939 DCHECK(response);
940 response_.ssl_info = response->ssl_info;
941 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
942 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
943 DCHECK(response);
944 response_.cert_request_info = response->cert_request_info;
945 } else if (response_.was_cached) {
946 DoneWritingToEntry(true);
948 return result;
951 // We received the response headers and there is no error.
952 int HttpCache::Transaction::DoSuccessfulSendRequest() {
953 DCHECK(!new_response_);
954 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
955 bool authentication_failure = false;
957 if (new_response->headers->response_code() == 401 ||
958 new_response->headers->response_code() == 407) {
959 auth_response_ = *new_response;
960 if (!reading_)
961 return OK;
963 // We initiated a second request the caller doesn't know about. We should be
964 // able to authenticate this request because we should have authenticated
965 // this URL moments ago.
966 if (IsReadyToRestartForAuth()) {
967 DCHECK(!response_.auth_challenge.get());
968 next_state_ = STATE_SEND_REQUEST_COMPLETE;
969 // In theory we should check to see if there are new cookies, but there
970 // is no way to do that from here.
971 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
974 // We have to perform cleanup at this point so that at least the next
975 // request can succeed.
976 authentication_failure = true;
977 if (entry_)
978 DoomPartialEntry(false);
979 mode_ = NONE;
980 partial_.reset();
983 new_response_ = new_response;
984 if (authentication_failure ||
985 (!ValidatePartialResponse() && !auth_response_.headers.get())) {
986 // Something went wrong with this request and we have to restart it.
987 // If we have an authentication response, we are exposed to weird things
988 // hapenning if the user cancels the authentication before we receive
989 // the new response.
990 UpdateTransactionPattern(PATTERN_NOT_COVERED);
991 response_ = HttpResponseInfo();
992 ResetNetworkTransaction();
993 new_response_ = NULL;
994 next_state_ = STATE_SEND_REQUEST;
995 return OK;
998 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
999 // We have stored the full entry, but it changed and the server is
1000 // sending a range. We have to delete the old entry.
1001 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1002 DoneWritingToEntry(false);
1005 if (mode_ == WRITE &&
1006 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1007 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1010 if (mode_ == WRITE &&
1011 (request_->method == "PUT" || request_->method == "DELETE")) {
1012 if (NonErrorResponse(new_response->headers->response_code())) {
1013 int ret = cache_->DoomEntry(cache_key_, NULL);
1014 DCHECK_EQ(OK, ret);
1016 cache_->DoneWritingToEntry(entry_, true);
1017 entry_ = NULL;
1018 mode_ = NONE;
1021 if (request_->method == "POST" &&
1022 NonErrorResponse(new_response->headers->response_code())) {
1023 cache_->DoomMainEntryForUrl(request_->url);
1026 RecordVaryHeaderHistogram(new_response);
1028 if (new_response_->headers->response_code() == 416 &&
1029 (request_->method == "GET" || request_->method == "POST")) {
1030 // If there is an ective entry it may be destroyed with this transaction.
1031 response_ = *new_response_;
1032 return OK;
1035 // Are we expecting a response to a conditional query?
1036 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1037 if (new_response->headers->response_code() == 304 || handling_206_) {
1038 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1039 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1040 return OK;
1042 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1043 mode_ = WRITE;
1046 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1047 return OK;
1050 int HttpCache::Transaction::DoNetworkRead() {
1051 next_state_ = STATE_NETWORK_READ_COMPLETE;
1052 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1055 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1056 DCHECK(mode_ & WRITE || mode_ == NONE);
1058 if (!cache_.get())
1059 return ERR_UNEXPECTED;
1061 // If there is an error or we aren't saving the data, we are done; just wait
1062 // until the destructor runs to see if we can keep the data.
1063 if (mode_ == NONE || result < 0)
1064 return result;
1066 next_state_ = STATE_CACHE_WRITE_DATA;
1067 return result;
1070 int HttpCache::Transaction::DoInitEntry() {
1071 DCHECK(!new_entry_);
1073 if (!cache_.get())
1074 return ERR_UNEXPECTED;
1076 if (mode_ == WRITE) {
1077 next_state_ = STATE_DOOM_ENTRY;
1078 return OK;
1081 next_state_ = STATE_OPEN_ENTRY;
1082 return OK;
1085 int HttpCache::Transaction::DoOpenEntry() {
1086 DCHECK(!new_entry_);
1087 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1088 cache_pending_ = true;
1089 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1090 first_cache_access_since_ = TimeTicks::Now();
1091 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1094 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1095 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1096 // OK, otherwise the cache will end up with an active entry without any
1097 // transaction attached.
1098 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1099 cache_pending_ = false;
1100 if (result == OK) {
1101 next_state_ = STATE_ADD_TO_ENTRY;
1102 return OK;
1105 if (result == ERR_CACHE_RACE) {
1106 next_state_ = STATE_INIT_ENTRY;
1107 return OK;
1110 if (request_->method == "PUT" || request_->method == "DELETE") {
1111 DCHECK(mode_ == READ_WRITE || mode_ == WRITE);
1112 mode_ = NONE;
1113 next_state_ = STATE_SEND_REQUEST;
1114 return OK;
1117 if (mode_ == READ_WRITE) {
1118 mode_ = WRITE;
1119 next_state_ = STATE_CREATE_ENTRY;
1120 return OK;
1122 if (mode_ == UPDATE) {
1123 // There is no cache entry to update; proceed without caching.
1124 mode_ = NONE;
1125 next_state_ = STATE_SEND_REQUEST;
1126 return OK;
1128 if (cache_->mode() == PLAYBACK)
1129 DVLOG(1) << "Playback Cache Miss: " << request_->url;
1131 // The entry does not exist, and we are not permitted to create a new entry,
1132 // so we must fail.
1133 return ERR_CACHE_MISS;
1136 int HttpCache::Transaction::DoCreateEntry() {
1137 DCHECK(!new_entry_);
1138 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1139 cache_pending_ = true;
1140 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1141 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1144 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1145 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1146 // OK, otherwise the cache will end up with an active entry without any
1147 // transaction attached.
1148 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1149 result);
1150 cache_pending_ = false;
1151 next_state_ = STATE_ADD_TO_ENTRY;
1153 if (result == ERR_CACHE_RACE) {
1154 next_state_ = STATE_INIT_ENTRY;
1155 return OK;
1158 if (result == OK) {
1159 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", false);
1160 } else {
1161 UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", true);
1162 // We have a race here: Maybe we failed to open the entry and decided to
1163 // create one, but by the time we called create, another transaction already
1164 // created the entry. If we want to eliminate this issue, we need an atomic
1165 // OpenOrCreate() method exposed by the disk cache.
1166 DLOG(WARNING) << "Unable to create cache entry";
1167 mode_ = NONE;
1168 if (partial_.get())
1169 partial_->RestoreHeaders(&custom_request_->extra_headers);
1170 next_state_ = STATE_SEND_REQUEST;
1172 return OK;
1175 int HttpCache::Transaction::DoDoomEntry() {
1176 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1177 cache_pending_ = true;
1178 if (first_cache_access_since_.is_null())
1179 first_cache_access_since_ = TimeTicks::Now();
1180 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1181 return cache_->DoomEntry(cache_key_, this);
1184 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1185 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1186 next_state_ = STATE_CREATE_ENTRY;
1187 cache_pending_ = false;
1188 if (result == ERR_CACHE_RACE)
1189 next_state_ = STATE_INIT_ENTRY;
1190 return OK;
1193 int HttpCache::Transaction::DoAddToEntry() {
1194 DCHECK(new_entry_);
1195 cache_pending_ = true;
1196 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1197 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1198 DCHECK(entry_lock_waiting_since_.is_null());
1199 entry_lock_waiting_since_ = TimeTicks::Now();
1200 return cache_->AddTransactionToEntry(new_entry_, this);
1203 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1204 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1205 result);
1206 const TimeDelta entry_lock_wait =
1207 TimeTicks::Now() - entry_lock_waiting_since_;
1208 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1210 entry_lock_waiting_since_ = TimeTicks();
1211 DCHECK(new_entry_);
1212 cache_pending_ = false;
1214 if (result == OK)
1215 entry_ = new_entry_;
1217 // If there is a failure, the cache should have taken care of new_entry_.
1218 new_entry_ = NULL;
1220 if (result == ERR_CACHE_RACE) {
1221 next_state_ = STATE_INIT_ENTRY;
1222 return OK;
1225 if (result != OK) {
1226 NOTREACHED();
1227 return result;
1230 if (mode_ == WRITE) {
1231 if (partial_.get())
1232 partial_->RestoreHeaders(&custom_request_->extra_headers);
1233 next_state_ = STATE_SEND_REQUEST;
1234 } else {
1235 // We have to read the headers from the cached entry.
1236 DCHECK(mode_ & READ_META);
1237 next_state_ = STATE_CACHE_READ_RESPONSE;
1239 return OK;
1242 // We may end up here multiple times for a given request.
1243 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1244 if (mode_ == NONE)
1245 return OK;
1247 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1248 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1251 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1252 if (!result) {
1253 // This is the end of the request.
1254 if (mode_ & WRITE) {
1255 DoneWritingToEntry(true);
1256 } else {
1257 cache_->DoneReadingFromEntry(entry_, this);
1258 entry_ = NULL;
1260 return result;
1263 if (result < 0)
1264 return result;
1266 partial_->PrepareCacheValidation(entry_->disk_entry,
1267 &custom_request_->extra_headers);
1269 if (reading_ && partial_->IsCurrentRangeCached()) {
1270 next_state_ = STATE_CACHE_READ_DATA;
1271 return OK;
1274 return BeginCacheValidation();
1277 // We received 304 or 206 and we want to update the cached response headers.
1278 int HttpCache::Transaction::DoUpdateCachedResponse() {
1279 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1280 int rv = OK;
1281 // Update cached response based on headers in new_response.
1282 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1283 response_.headers->Update(*new_response_->headers.get());
1284 response_.response_time = new_response_->response_time;
1285 response_.request_time = new_response_->request_time;
1286 response_.network_accessed = new_response_->network_accessed;
1288 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1289 if (!entry_->doomed) {
1290 int ret = cache_->DoomEntry(cache_key_, NULL);
1291 DCHECK_EQ(OK, ret);
1293 } else {
1294 // If we are already reading, we already updated the headers for this
1295 // request; doing it again will change Content-Length.
1296 if (!reading_) {
1297 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1298 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1299 rv = OK;
1302 return rv;
1305 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1306 if (mode_ == UPDATE) {
1307 DCHECK(!handling_206_);
1308 // We got a "not modified" response and already updated the corresponding
1309 // cache entry above.
1311 // By closing the cached entry now, we make sure that the 304 rather than
1312 // the cached 200 response, is what will be returned to the user.
1313 DoneWritingToEntry(true);
1314 } else if (entry_ && !handling_206_) {
1315 DCHECK_EQ(READ_WRITE, mode_);
1316 if (!partial_.get() || partial_->IsLastRange()) {
1317 cache_->ConvertWriterToReader(entry_);
1318 mode_ = READ;
1320 // We no longer need the network transaction, so destroy it.
1321 final_upload_progress_ = network_trans_->GetUploadProgress();
1322 ResetNetworkTransaction();
1323 } else if (entry_ && handling_206_ && truncated_ &&
1324 partial_->initial_validation()) {
1325 // We just finished the validation of a truncated entry, and the server
1326 // is willing to resume the operation. Now we go back and start serving
1327 // the first part to the user.
1328 ResetNetworkTransaction();
1329 new_response_ = NULL;
1330 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1331 partial_->SetRangeToStartDownload();
1332 return OK;
1334 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1335 return OK;
1338 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1339 if (mode_ & READ) {
1340 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1341 return OK;
1344 // We change the value of Content-Length for partial content.
1345 if (handling_206_ && partial_.get())
1346 partial_->FixContentLength(new_response_->headers.get());
1348 response_ = *new_response_;
1350 if (handling_206_ && !CanResume(false)) {
1351 // There is no point in storing this resource because it will never be used.
1352 DoneWritingToEntry(false);
1353 if (partial_.get())
1354 partial_->FixResponseHeaders(response_.headers.get(), true);
1355 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1356 return OK;
1359 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1360 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1361 STATE_CACHE_WRITE_RESPONSE;
1362 return OK;
1365 int HttpCache::Transaction::DoTruncateCachedData() {
1366 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1367 if (!entry_)
1368 return OK;
1369 if (net_log_.IsLoggingAllEvents())
1370 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1371 // Truncate the stream.
1372 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1375 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1376 if (entry_) {
1377 if (net_log_.IsLoggingAllEvents()) {
1378 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1379 result);
1383 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1384 return OK;
1387 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1388 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1389 if (!entry_)
1390 return OK;
1392 if (net_log_.IsLoggingAllEvents())
1393 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1394 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1397 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1398 if (entry_) {
1399 if (net_log_.IsLoggingAllEvents()) {
1400 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1401 result);
1405 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1406 return OK;
1409 int HttpCache::Transaction::DoPartialHeadersReceived() {
1410 new_response_ = NULL;
1411 if (entry_ && !partial_.get() &&
1412 entry_->disk_entry->GetDataSize(kMetadataIndex))
1413 next_state_ = STATE_CACHE_READ_METADATA;
1415 if (!partial_.get())
1416 return OK;
1418 if (reading_) {
1419 if (network_trans_.get()) {
1420 next_state_ = STATE_NETWORK_READ;
1421 } else {
1422 next_state_ = STATE_CACHE_READ_DATA;
1424 } else if (mode_ != NONE) {
1425 // We are about to return the headers for a byte-range request to the user,
1426 // so let's fix them.
1427 partial_->FixResponseHeaders(response_.headers.get(), true);
1429 return OK;
1432 int HttpCache::Transaction::DoCacheReadResponse() {
1433 DCHECK(entry_);
1434 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1436 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1437 read_buf_ = new IOBuffer(io_buf_len_);
1439 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1440 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1441 io_buf_len_, io_callback_);
1444 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1445 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1446 if (result != io_buf_len_ ||
1447 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1448 &response_, &truncated_)) {
1449 return OnCacheReadError(result, true);
1452 // Some resources may have slipped in as truncated when they're not.
1453 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1454 if (response_.headers->GetContentLength() == current_size)
1455 truncated_ = false;
1457 // We now have access to the cache entry.
1459 // o if we are a reader for the transaction, then we can start reading the
1460 // cache entry.
1462 // o if we can read or write, then we should check if the cache entry needs
1463 // to be validated and then issue a network request if needed or just read
1464 // from the cache if the cache entry is already valid.
1466 // o if we are set to UPDATE, then we are handling an externally
1467 // conditionalized request (if-modified-since / if-none-match). We check
1468 // if the request headers define a validation request.
1470 switch (mode_) {
1471 case READ:
1472 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1473 result = BeginCacheRead();
1474 break;
1475 case READ_WRITE:
1476 result = BeginPartialCacheValidation();
1477 break;
1478 case UPDATE:
1479 result = BeginExternallyConditionalizedRequest();
1480 break;
1481 case WRITE:
1482 default:
1483 NOTREACHED();
1484 result = ERR_FAILED;
1486 return result;
1489 int HttpCache::Transaction::DoCacheWriteResponse() {
1490 if (entry_) {
1491 if (net_log_.IsLoggingAllEvents())
1492 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1494 return WriteResponseInfoToEntry(false);
1497 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1498 if (entry_) {
1499 if (net_log_.IsLoggingAllEvents())
1500 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1502 return WriteResponseInfoToEntry(true);
1505 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1506 next_state_ = target_state_;
1507 target_state_ = STATE_NONE;
1508 if (!entry_)
1509 return OK;
1510 if (net_log_.IsLoggingAllEvents()) {
1511 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1512 result);
1515 // Balance the AddRef from WriteResponseInfoToEntry.
1516 if (result != io_buf_len_) {
1517 DLOG(ERROR) << "failed to write response info to cache";
1518 DoneWritingToEntry(false);
1520 return OK;
1523 int HttpCache::Transaction::DoCacheReadMetadata() {
1524 DCHECK(entry_);
1525 DCHECK(!response_.metadata.get());
1526 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1528 response_.metadata =
1529 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1531 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1532 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1533 response_.metadata.get(),
1534 response_.metadata->size(),
1535 io_callback_);
1538 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1539 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1540 if (result != response_.metadata->size())
1541 return OnCacheReadError(result, false);
1542 return OK;
1545 int HttpCache::Transaction::DoCacheQueryData() {
1546 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1547 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1550 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1551 if (result == ERR_NOT_IMPLEMENTED) {
1552 // Restart the request overwriting the cache entry.
1553 // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1554 // supports Sparse IO.
1555 return DoRestartPartialRequest();
1557 DCHECK_EQ(OK, result);
1558 if (!cache_.get())
1559 return ERR_UNEXPECTED;
1561 return ValidateEntryHeadersAndContinue();
1564 int HttpCache::Transaction::DoCacheReadData() {
1565 DCHECK(entry_);
1566 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1568 if (net_log_.IsLoggingAllEvents())
1569 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1570 if (partial_.get()) {
1571 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1572 io_callback_);
1575 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1576 read_buf_.get(), io_buf_len_,
1577 io_callback_);
1580 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1581 if (net_log_.IsLoggingAllEvents()) {
1582 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1583 result);
1586 if (!cache_.get())
1587 return ERR_UNEXPECTED;
1589 if (partial_.get()) {
1590 // Partial requests are confusing to report in histograms because they may
1591 // have multiple underlying requests.
1592 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1593 return DoPartialCacheReadCompleted(result);
1596 if (result > 0) {
1597 read_offset_ += result;
1598 } else if (result == 0) { // End of file.
1599 RecordHistograms();
1600 cache_->DoneReadingFromEntry(entry_, this);
1601 entry_ = NULL;
1602 } else {
1603 return OnCacheReadError(result, false);
1605 return result;
1608 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1609 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1610 write_len_ = num_bytes;
1611 if (entry_) {
1612 if (net_log_.IsLoggingAllEvents())
1613 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1616 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1619 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1620 if (entry_) {
1621 if (net_log_.IsLoggingAllEvents()) {
1622 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1623 result);
1626 // Balance the AddRef from DoCacheWriteData.
1627 if (!cache_.get())
1628 return ERR_UNEXPECTED;
1630 if (result != write_len_) {
1631 DLOG(ERROR) << "failed to write response data to cache";
1632 DoneWritingToEntry(false);
1634 // We want to ignore errors writing to disk and just keep reading from
1635 // the network.
1636 result = write_len_;
1637 } else if (!done_reading_ && entry_) {
1638 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1639 int64 body_size = response_.headers->GetContentLength();
1640 if (body_size >= 0 && body_size <= current_size)
1641 done_reading_ = true;
1644 if (partial_.get()) {
1645 // This may be the last request.
1646 if (!(result == 0 && !truncated_ &&
1647 (partial_->IsLastRange() || mode_ == WRITE)))
1648 return DoPartialNetworkReadCompleted(result);
1651 if (result == 0) {
1652 // End of file. This may be the result of a connection problem so see if we
1653 // have to keep the entry around to be flagged as truncated later on.
1654 if (done_reading_ || !entry_ || partial_.get() ||
1655 response_.headers->GetContentLength() <= 0)
1656 DoneWritingToEntry(true);
1659 return result;
1662 //-----------------------------------------------------------------------------
1664 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1665 const HttpRequestInfo* request) {
1666 net_log_ = net_log;
1667 request_ = request;
1668 effective_load_flags_ = request_->load_flags;
1670 switch (cache_->mode()) {
1671 case NORMAL:
1672 break;
1673 case RECORD:
1674 // When in record mode, we want to NEVER load from the cache.
1675 // The reason for this is beacuse we save the Set-Cookie headers
1676 // (intentionally). If we read from the cache, we replay them
1677 // prematurely.
1678 effective_load_flags_ |= LOAD_BYPASS_CACHE;
1679 break;
1680 case PLAYBACK:
1681 // When in playback mode, we want to load exclusively from the cache.
1682 effective_load_flags_ |= LOAD_ONLY_FROM_CACHE;
1683 break;
1684 case DISABLE:
1685 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1686 break;
1689 // Some headers imply load flags. The order here is significant.
1691 // LOAD_DISABLE_CACHE : no cache read or write
1692 // LOAD_BYPASS_CACHE : no cache read
1693 // LOAD_VALIDATE_CACHE : no cache read unless validation
1695 // The former modes trump latter modes, so if we find a matching header we
1696 // can stop iterating kSpecialHeaders.
1698 static const struct {
1699 const HeaderNameAndValue* search;
1700 int load_flag;
1701 } kSpecialHeaders[] = {
1702 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
1703 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
1704 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
1707 bool range_found = false;
1708 bool external_validation_error = false;
1710 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
1711 range_found = true;
1713 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSpecialHeaders); ++i) {
1714 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
1715 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
1716 break;
1720 // Check for conditionalization headers which may correspond with a
1721 // cache validation request.
1722 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
1723 const ValidationHeaderInfo& info = kValidationHeaders[i];
1724 std::string validation_value;
1725 if (request_->extra_headers.GetHeader(
1726 info.request_header_name, &validation_value)) {
1727 if (!external_validation_.values[i].empty() ||
1728 validation_value.empty()) {
1729 external_validation_error = true;
1731 external_validation_.values[i] = validation_value;
1732 external_validation_.initialized = true;
1736 // We don't support ranges and validation headers.
1737 if (range_found && external_validation_.initialized) {
1738 LOG(WARNING) << "Byte ranges AND validation headers found.";
1739 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1742 // If there is more than one validation header, we can't treat this request as
1743 // a cache validation, since we don't know for sure which header the server
1744 // will give us a response for (and they could be contradictory).
1745 if (external_validation_error) {
1746 LOG(WARNING) << "Multiple or malformed validation headers found.";
1747 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1750 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
1751 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1752 partial_.reset(new PartialData);
1753 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
1754 // We will be modifying the actual range requested to the server, so
1755 // let's remove the header here.
1756 custom_request_.reset(new HttpRequestInfo(*request_));
1757 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
1758 request_ = custom_request_.get();
1759 partial_->SetHeaders(custom_request_->extra_headers);
1760 } else {
1761 // The range is invalid or we cannot handle it properly.
1762 VLOG(1) << "Invalid byte range found.";
1763 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1764 partial_.reset(NULL);
1769 bool HttpCache::Transaction::ShouldPassThrough() {
1770 // We may have a null disk_cache if there is an error we cannot recover from,
1771 // like not enough disk space, or sharing violations.
1772 if (!cache_->disk_cache_.get())
1773 return true;
1775 // When using the record/playback modes, we always use the cache
1776 // and we never pass through.
1777 if (cache_->mode() == RECORD || cache_->mode() == PLAYBACK)
1778 return false;
1780 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
1781 return true;
1783 if (request_->method == "GET")
1784 return false;
1786 if (request_->method == "POST" && request_->upload_data_stream &&
1787 request_->upload_data_stream->identifier()) {
1788 return false;
1791 if (request_->method == "PUT" && request_->upload_data_stream)
1792 return false;
1794 if (request_->method == "DELETE")
1795 return false;
1797 // TODO(darin): add support for caching HEAD responses
1798 return true;
1801 int HttpCache::Transaction::BeginCacheRead() {
1802 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
1803 if (response_.headers->response_code() == 206 || partial_.get()) {
1804 NOTREACHED();
1805 return ERR_CACHE_MISS;
1808 // We don't have the whole resource.
1809 if (truncated_)
1810 return ERR_CACHE_MISS;
1812 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
1813 next_state_ = STATE_CACHE_READ_METADATA;
1815 return OK;
1818 int HttpCache::Transaction::BeginCacheValidation() {
1819 DCHECK(mode_ == READ_WRITE);
1821 bool skip_validation = !RequiresValidation();
1823 if (truncated_) {
1824 // Truncated entries can cause partial gets, so we shouldn't record this
1825 // load in histograms.
1826 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1827 skip_validation = !partial_->initial_validation();
1830 if (partial_.get() && (is_sparse_ || truncated_) &&
1831 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
1832 // Force revalidation for sparse or truncated entries. Note that we don't
1833 // want to ignore the regular validation logic just because a byte range was
1834 // part of the request.
1835 skip_validation = false;
1838 if (skip_validation) {
1839 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1840 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE);
1841 return SetupEntryForRead();
1842 } else {
1843 // Make the network request conditional, to see if we may reuse our cached
1844 // response. If we cannot do so, then we just resort to a normal fetch.
1845 // Our mode remains READ_WRITE for a conditional request. Even if the
1846 // conditionalization fails, we don't switch to WRITE mode until we
1847 // know we won't be falling back to using the cache entry in the
1848 // LOAD_FROM_CACHE_IF_OFFLINE case.
1849 if (!ConditionalizeRequest()) {
1850 couldnt_conditionalize_request_ = true;
1851 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
1852 if (partial_.get())
1853 return DoRestartPartialRequest();
1855 DCHECK_NE(206, response_.headers->response_code());
1857 next_state_ = STATE_SEND_REQUEST;
1859 return OK;
1862 int HttpCache::Transaction::BeginPartialCacheValidation() {
1863 DCHECK(mode_ == READ_WRITE);
1865 if (response_.headers->response_code() != 206 && !partial_.get() &&
1866 !truncated_) {
1867 return BeginCacheValidation();
1870 // Partial requests should not be recorded in histograms.
1871 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1872 if (range_requested_) {
1873 next_state_ = STATE_CACHE_QUERY_DATA;
1874 return OK;
1876 // The request is not for a range, but we have stored just ranges.
1877 partial_.reset(new PartialData());
1878 partial_->SetHeaders(request_->extra_headers);
1879 if (!custom_request_.get()) {
1880 custom_request_.reset(new HttpRequestInfo(*request_));
1881 request_ = custom_request_.get();
1884 return ValidateEntryHeadersAndContinue();
1887 // This should only be called once per request.
1888 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
1889 DCHECK(mode_ == READ_WRITE);
1891 if (!partial_->UpdateFromStoredHeaders(
1892 response_.headers.get(), entry_->disk_entry, truncated_)) {
1893 return DoRestartPartialRequest();
1896 if (response_.headers->response_code() == 206)
1897 is_sparse_ = true;
1899 if (!partial_->IsRequestedRangeOK()) {
1900 // The stored data is fine, but the request may be invalid.
1901 invalid_range_ = true;
1904 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1905 return OK;
1908 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
1909 DCHECK_EQ(UPDATE, mode_);
1910 DCHECK(external_validation_.initialized);
1912 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
1913 if (external_validation_.values[i].empty())
1914 continue;
1915 // Retrieve either the cached response's "etag" or "last-modified" header.
1916 std::string validator;
1917 response_.headers->EnumerateHeader(
1918 NULL,
1919 kValidationHeaders[i].related_response_header_name,
1920 &validator);
1922 if (response_.headers->response_code() != 200 || truncated_ ||
1923 validator.empty() || validator != external_validation_.values[i]) {
1924 // The externally conditionalized request is not a validation request
1925 // for our existing cache entry. Proceed with caching disabled.
1926 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1927 DoneWritingToEntry(true);
1931 next_state_ = STATE_SEND_REQUEST;
1932 return OK;
1935 int HttpCache::Transaction::RestartNetworkRequest() {
1936 DCHECK(mode_ & WRITE || mode_ == NONE);
1937 DCHECK(network_trans_.get());
1938 DCHECK_EQ(STATE_NONE, next_state_);
1940 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1941 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
1942 if (rv != ERR_IO_PENDING)
1943 return DoLoop(rv);
1944 return rv;
1947 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
1948 X509Certificate* client_cert) {
1949 DCHECK(mode_ & WRITE || mode_ == NONE);
1950 DCHECK(network_trans_.get());
1951 DCHECK_EQ(STATE_NONE, next_state_);
1953 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1954 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
1955 if (rv != ERR_IO_PENDING)
1956 return DoLoop(rv);
1957 return rv;
1960 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
1961 const AuthCredentials& credentials) {
1962 DCHECK(mode_ & WRITE || mode_ == NONE);
1963 DCHECK(network_trans_.get());
1964 DCHECK_EQ(STATE_NONE, next_state_);
1966 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1967 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
1968 if (rv != ERR_IO_PENDING)
1969 return DoLoop(rv);
1970 return rv;
1973 bool HttpCache::Transaction::RequiresValidation() {
1974 // TODO(darin): need to do more work here:
1975 // - make sure we have a matching request method
1976 // - watch out for cached responses that depend on authentication
1978 // In playback mode, nothing requires validation.
1979 if (cache_->mode() == net::HttpCache::PLAYBACK)
1980 return false;
1982 if (response_.vary_data.is_valid() &&
1983 !response_.vary_data.MatchesRequest(*request_,
1984 *response_.headers.get())) {
1985 vary_mismatch_ = true;
1986 return true;
1989 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
1990 return false;
1992 if (effective_load_flags_ & LOAD_VALIDATE_CACHE)
1993 return true;
1995 if (request_->method == "PUT" || request_->method == "DELETE")
1996 return true;
1998 if (response_.headers->RequiresValidation(
1999 response_.request_time, response_.response_time, Time::Now())) {
2000 return true;
2003 return false;
2006 bool HttpCache::Transaction::ConditionalizeRequest() {
2007 DCHECK(response_.headers.get());
2009 if (request_->method == "PUT" || request_->method == "DELETE")
2010 return false;
2012 // This only makes sense for cached 200 or 206 responses.
2013 if (response_.headers->response_code() != 200 &&
2014 response_.headers->response_code() != 206) {
2015 return false;
2018 // We should have handled this case before.
2019 DCHECK(response_.headers->response_code() != 206 ||
2020 response_.headers->HasStrongValidators());
2022 // Just use the first available ETag and/or Last-Modified header value.
2023 // TODO(darin): Or should we use the last?
2025 std::string etag_value;
2026 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2027 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2029 std::string last_modified_value;
2030 if (!vary_mismatch_) {
2031 response_.headers->EnumerateHeader(NULL, "last-modified",
2032 &last_modified_value);
2035 if (etag_value.empty() && last_modified_value.empty())
2036 return false;
2038 if (!partial_.get()) {
2039 // Need to customize the request, so this forces us to allocate :(
2040 custom_request_.reset(new HttpRequestInfo(*request_));
2041 request_ = custom_request_.get();
2043 DCHECK(custom_request_.get());
2045 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2046 !invalid_range_;
2048 if (!etag_value.empty()) {
2049 if (use_if_range) {
2050 // We don't want to switch to WRITE mode if we don't have this block of a
2051 // byte-range request because we may have other parts cached.
2052 custom_request_->extra_headers.SetHeader(
2053 HttpRequestHeaders::kIfRange, etag_value);
2054 } else {
2055 custom_request_->extra_headers.SetHeader(
2056 HttpRequestHeaders::kIfNoneMatch, etag_value);
2058 // For byte-range requests, make sure that we use only one way to validate
2059 // the request.
2060 if (partial_.get() && !partial_->IsCurrentRangeCached())
2061 return true;
2064 if (!last_modified_value.empty()) {
2065 if (use_if_range) {
2066 custom_request_->extra_headers.SetHeader(
2067 HttpRequestHeaders::kIfRange, last_modified_value);
2068 } else {
2069 custom_request_->extra_headers.SetHeader(
2070 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2074 return true;
2077 // We just received some headers from the server. We may have asked for a range,
2078 // in which case partial_ has an object. This could be the first network request
2079 // we make to fulfill the original request, or we may be already reading (from
2080 // the net and / or the cache). If we are not expecting a certain response, we
2081 // just bypass the cache for this request (but again, maybe we are reading), and
2082 // delete partial_ (so we are not able to "fix" the headers that we return to
2083 // the user). This results in either a weird response for the caller (we don't
2084 // expect it after all), or maybe a range that was not exactly what it was asked
2085 // for.
2087 // If the server is simply telling us that the resource has changed, we delete
2088 // the cached entry and restart the request as the caller intended (by returning
2089 // false from this method). However, we may not be able to do that at any point,
2090 // for instance if we already returned the headers to the user.
2092 // WARNING: Whenever this code returns false, it has to make sure that the next
2093 // time it is called it will return true so that we don't keep retrying the
2094 // request.
2095 bool HttpCache::Transaction::ValidatePartialResponse() {
2096 const HttpResponseHeaders* headers = new_response_->headers.get();
2097 int response_code = headers->response_code();
2098 bool partial_response = (response_code == 206);
2099 handling_206_ = false;
2101 if (!entry_ || request_->method != "GET")
2102 return true;
2104 if (invalid_range_) {
2105 // We gave up trying to match this request with the stored data. If the
2106 // server is ok with the request, delete the entry, otherwise just ignore
2107 // this request
2108 DCHECK(!reading_);
2109 if (partial_response || response_code == 200) {
2110 DoomPartialEntry(true);
2111 mode_ = NONE;
2112 } else {
2113 if (response_code == 304)
2114 FailRangeRequest();
2115 IgnoreRangeRequest();
2117 return true;
2120 if (!partial_.get()) {
2121 // We are not expecting 206 but we may have one.
2122 if (partial_response)
2123 IgnoreRangeRequest();
2125 return true;
2128 // TODO(rvargas): Do we need to consider other results here?.
2129 bool failure = response_code == 200 || response_code == 416;
2131 if (partial_->IsCurrentRangeCached()) {
2132 // We asked for "If-None-Match: " so a 206 means a new object.
2133 if (partial_response)
2134 failure = true;
2136 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2137 return true;
2138 } else {
2139 // We asked for "If-Range: " so a 206 means just another range.
2140 if (partial_response && partial_->ResponseHeadersOK(headers)) {
2141 handling_206_ = true;
2142 return true;
2145 if (!reading_ && !is_sparse_ && !partial_response) {
2146 // See if we can ignore the fact that we issued a byte range request.
2147 // If the server sends 200, just store it. If it sends an error, redirect
2148 // or something else, we may store the response as long as we didn't have
2149 // anything already stored.
2150 if (response_code == 200 ||
2151 (!truncated_ && response_code != 304 && response_code != 416)) {
2152 // The server is sending something else, and we can save it.
2153 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2154 partial_.reset();
2155 truncated_ = false;
2156 return true;
2160 // 304 is not expected here, but we'll spare the entry (unless it was
2161 // truncated).
2162 if (truncated_)
2163 failure = true;
2166 if (failure) {
2167 // We cannot truncate this entry, it has to be deleted.
2168 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2169 DoomPartialEntry(false);
2170 mode_ = NONE;
2171 if (!reading_ && !partial_->IsLastRange()) {
2172 // We'll attempt to issue another network request, this time without us
2173 // messing up the headers.
2174 partial_->RestoreHeaders(&custom_request_->extra_headers);
2175 partial_.reset();
2176 truncated_ = false;
2177 return false;
2179 LOG(WARNING) << "Failed to revalidate partial entry";
2180 partial_.reset();
2181 return true;
2184 IgnoreRangeRequest();
2185 return true;
2188 void HttpCache::Transaction::IgnoreRangeRequest() {
2189 // We have a problem. We may or may not be reading already (in which case we
2190 // returned the headers), but we'll just pretend that this request is not
2191 // using the cache and see what happens. Most likely this is the first
2192 // response from the server (it's not changing its mind midway, right?).
2193 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2194 if (mode_ & WRITE)
2195 DoneWritingToEntry(mode_ != WRITE);
2196 else if (mode_ & READ && entry_)
2197 cache_->DoneReadingFromEntry(entry_, this);
2199 partial_.reset(NULL);
2200 entry_ = NULL;
2201 mode_ = NONE;
2204 void HttpCache::Transaction::FailRangeRequest() {
2205 response_ = *new_response_;
2206 partial_->FixResponseHeaders(response_.headers.get(), false);
2209 int HttpCache::Transaction::SetupEntryForRead() {
2210 if (network_trans_)
2211 ResetNetworkTransaction();
2212 if (partial_.get()) {
2213 if (truncated_ || is_sparse_ || !invalid_range_) {
2214 // We are going to return the saved response headers to the caller, so
2215 // we may need to adjust them first.
2216 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2217 return OK;
2218 } else {
2219 partial_.reset();
2222 cache_->ConvertWriterToReader(entry_);
2223 mode_ = READ;
2225 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2226 next_state_ = STATE_CACHE_READ_METADATA;
2227 return OK;
2231 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2232 read_buf_ = data;
2233 io_buf_len_ = data_len;
2234 next_state_ = STATE_NETWORK_READ;
2235 return DoLoop(OK);
2238 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2239 read_buf_ = data;
2240 io_buf_len_ = data_len;
2241 next_state_ = STATE_CACHE_READ_DATA;
2242 return DoLoop(OK);
2245 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2246 IOBuffer* data, int data_len,
2247 const CompletionCallback& callback) {
2248 if (!entry_)
2249 return data_len;
2251 int rv = 0;
2252 if (!partial_.get() || !data_len) {
2253 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2254 true);
2255 } else {
2256 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2258 return rv;
2261 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2262 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2263 if (!entry_)
2264 return OK;
2266 // Do not cache no-store content (unless we are record mode). Do not cache
2267 // content with cert errors either. This is to prevent not reporting net
2268 // errors when loading a resource from the cache. When we load a page over
2269 // HTTPS with a cert error we show an SSL blocking page. If the user clicks
2270 // proceed we reload the resource ignoring the errors. The loaded resource
2271 // is then cached. If that resource is subsequently loaded from the cache,
2272 // no net error is reported (even though the cert status contains the actual
2273 // errors) and no SSL blocking page is shown. An alternative would be to
2274 // reverse-map the cert status to a net error and replay the net error.
2275 if ((cache_->mode() != RECORD &&
2276 response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2277 net::IsCertStatusError(response_.ssl_info.cert_status)) {
2278 DoneWritingToEntry(false);
2279 if (net_log_.IsLoggingAllEvents())
2280 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2281 return OK;
2284 // When writing headers, we normally only write the non-transient
2285 // headers; when in record mode, record everything.
2286 bool skip_transient_headers = (cache_->mode() != RECORD);
2288 if (truncated)
2289 DCHECK_EQ(200, response_.headers->response_code());
2291 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2292 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2293 data->Done();
2295 io_buf_len_ = data->pickle()->size();
2296 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2297 io_buf_len_, io_callback_, true);
2300 int HttpCache::Transaction::AppendResponseDataToEntry(
2301 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2302 if (!entry_ || !data_len)
2303 return data_len;
2305 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2306 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2307 callback);
2310 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2311 if (!entry_)
2312 return;
2314 RecordHistograms();
2316 cache_->DoneWritingToEntry(entry_, success);
2317 entry_ = NULL;
2318 mode_ = NONE; // switch to 'pass through' mode
2321 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2322 DLOG(ERROR) << "ReadData failed: " << result;
2323 const int result_for_histogram = std::max(0, -result);
2324 if (restart) {
2325 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2326 result_for_histogram);
2327 } else {
2328 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2329 result_for_histogram);
2332 // Avoid using this entry in the future.
2333 if (cache_.get())
2334 cache_->DoomActiveEntry(cache_key_);
2336 if (restart) {
2337 DCHECK(!reading_);
2338 DCHECK(!network_trans_.get());
2339 cache_->DoneWithEntry(entry_, this, false);
2340 entry_ = NULL;
2341 is_sparse_ = false;
2342 partial_.reset();
2343 next_state_ = STATE_GET_BACKEND;
2344 return OK;
2347 return ERR_CACHE_READ_FAILURE;
2350 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2351 DVLOG(2) << "DoomPartialEntry";
2352 int rv = cache_->DoomEntry(cache_key_, NULL);
2353 DCHECK_EQ(OK, rv);
2354 cache_->DoneWithEntry(entry_, this, false);
2355 entry_ = NULL;
2356 is_sparse_ = false;
2357 if (delete_object)
2358 partial_.reset(NULL);
2361 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2362 partial_->OnNetworkReadCompleted(result);
2364 if (result == 0) {
2365 // We need to move on to the next range.
2366 ResetNetworkTransaction();
2367 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2369 return result;
2372 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2373 partial_->OnCacheReadCompleted(result);
2375 if (result == 0 && mode_ == READ_WRITE) {
2376 // We need to move on to the next range.
2377 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2378 } else if (result < 0) {
2379 return OnCacheReadError(result, false);
2381 return result;
2384 int HttpCache::Transaction::DoRestartPartialRequest() {
2385 // The stored data cannot be used. Get rid of it and restart this request.
2386 // We need to also reset the |truncated_| flag as a new entry is created.
2387 DoomPartialEntry(!range_requested_);
2388 mode_ = WRITE;
2389 truncated_ = false;
2390 next_state_ = STATE_INIT_ENTRY;
2391 return OK;
2394 void HttpCache::Transaction::ResetNetworkTransaction() {
2395 DCHECK(!old_network_trans_load_timing_);
2396 DCHECK(network_trans_);
2397 LoadTimingInfo load_timing;
2398 if (network_trans_->GetLoadTimingInfo(&load_timing))
2399 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2400 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2401 network_trans_.reset();
2404 // Histogram data from the end of 2010 show the following distribution of
2405 // response headers:
2407 // Content-Length............... 87%
2408 // Date......................... 98%
2409 // Last-Modified................ 49%
2410 // Etag......................... 19%
2411 // Accept-Ranges: bytes......... 25%
2412 // Accept-Ranges: none.......... 0.4%
2413 // Strong Validator............. 50%
2414 // Strong Validator + ranges.... 24%
2415 // Strong Validator + CL........ 49%
2417 bool HttpCache::Transaction::CanResume(bool has_data) {
2418 // Double check that there is something worth keeping.
2419 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2420 return false;
2422 if (request_->method != "GET")
2423 return false;
2425 // Note that if this is a 206, content-length was already fixed after calling
2426 // PartialData::ResponseHeadersOK().
2427 if (response_.headers->GetContentLength() <= 0 ||
2428 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2429 !response_.headers->HasStrongValidators()) {
2430 return false;
2433 return true;
2436 void HttpCache::Transaction::UpdateTransactionPattern(
2437 TransactionPattern new_transaction_pattern) {
2438 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2439 return;
2440 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2441 new_transaction_pattern == PATTERN_NOT_COVERED);
2442 transaction_pattern_ = new_transaction_pattern;
2445 void HttpCache::Transaction::RecordHistograms() {
2446 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2447 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2448 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2449 cache_->mode() != NORMAL || request_->method != "GET") {
2450 return;
2452 UMA_HISTOGRAM_ENUMERATION(
2453 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2454 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2455 return;
2456 DCHECK(!range_requested_);
2457 DCHECK(!first_cache_access_since_.is_null());
2459 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2461 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2463 bool did_send_request = !send_request_since_.is_null();
2464 DCHECK(
2465 (did_send_request &&
2466 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2467 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2468 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2469 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2470 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2472 if (!did_send_request) {
2473 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2474 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2475 return;
2478 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2479 int before_send_percent =
2480 total_time.ToInternalValue() == 0 ? 0
2481 : before_send_time * 100 / total_time;
2482 DCHECK_LE(0, before_send_percent);
2483 DCHECK_GE(100, before_send_percent);
2485 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2486 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2487 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent);
2489 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2490 // below this comment after we have received initial data.
2491 switch (transaction_pattern_) {
2492 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2493 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2494 before_send_time);
2495 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2496 before_send_percent);
2497 break;
2499 case PATTERN_ENTRY_NOT_CACHED: {
2500 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2501 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2502 before_send_percent);
2503 break;
2505 case PATTERN_ENTRY_VALIDATED: {
2506 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2507 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2508 before_send_percent);
2509 break;
2511 case PATTERN_ENTRY_UPDATED: {
2512 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2513 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2514 before_send_percent);
2515 break;
2517 default:
2518 NOTREACHED();
2522 void HttpCache::Transaction::OnIOComplete(int result) {
2523 DoLoop(result);
2526 } // namespace net