Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: net/http/http_cache_transaction.cc

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Josh's latest feedback addressed. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_cache_transaction.h ('k') | net/http/http_cache_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/http/http_cache_transaction.h" 5 #include "net/http/http_cache_transaction.h"
6 6
7 #include "build/build_config.h" // For OS_POSIX 7 #include "build/build_config.h" // For OS_POSIX
8 8
9 #if defined(OS_POSIX) 9 #if defined(OS_POSIX)
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 163
164 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache) 164 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
165 : next_state_(STATE_NONE), 165 : next_state_(STATE_NONE),
166 request_(NULL), 166 request_(NULL),
167 priority_(priority), 167 priority_(priority),
168 cache_(cache->GetWeakPtr()), 168 cache_(cache->GetWeakPtr()),
169 entry_(NULL), 169 entry_(NULL),
170 new_entry_(NULL), 170 new_entry_(NULL),
171 new_response_(NULL), 171 new_response_(NULL),
172 mode_(NONE), 172 mode_(NONE),
173 original_mode_(NONE),
173 reading_(false), 174 reading_(false),
174 invalid_range_(false), 175 invalid_range_(false),
175 truncated_(false), 176 truncated_(false),
176 is_sparse_(false), 177 is_sparse_(false),
177 range_requested_(false), 178 range_requested_(false),
178 handling_206_(false), 179 handling_206_(false),
179 cache_pending_(false), 180 cache_pending_(false),
180 done_reading_(false), 181 done_reading_(false),
181 vary_mismatch_(false), 182 vary_mismatch_(false),
182 couldnt_conditionalize_request_(false), 183 couldnt_conditionalize_request_(false),
183 bypass_lock_for_test_(false), 184 bypass_lock_for_test_(false),
184 fail_conditionalization_for_test_(false), 185 fail_conditionalization_for_test_(false),
186 validating_cannot_proceed_(false),
185 io_buf_len_(0), 187 io_buf_len_(0),
186 read_offset_(0), 188 read_offset_(0),
187 effective_load_flags_(0), 189 effective_load_flags_(0),
188 write_len_(0), 190 write_len_(0),
189 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), 191 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED),
190 validation_cause_(VALIDATION_CAUSE_UNDEFINED), 192 validation_cause_(VALIDATION_CAUSE_UNDEFINED),
191 total_received_bytes_(0), 193 total_received_bytes_(0),
192 total_sent_bytes_(0), 194 total_sent_bytes_(0),
193 websocket_handshake_stream_base_create_helper_(NULL), 195 websocket_handshake_stream_base_create_helper_(NULL),
194 in_do_loop_(false), 196 in_do_loop_(false),
195 weak_factory_(this) { 197 weak_factory_(this) {
196 TRACE_EVENT0("io", "HttpCacheTransaction::Transaction"); 198 TRACE_EVENT0("io", "HttpCacheTransaction::Transaction");
197 static_assert(HttpCache::Transaction::kNumValidationHeaders == 199 static_assert(HttpCache::Transaction::kNumValidationHeaders ==
198 arraysize(kValidationHeaders), 200 arraysize(kValidationHeaders),
199 "invalid number of validation headers"); 201 "invalid number of validation headers");
200 202
201 io_callback_ = base::Bind(&Transaction::OnIOComplete, 203 io_callback_ = base::Bind(&Transaction::OnIOComplete,
202 weak_factory_.GetWeakPtr()); 204 weak_factory_.GetWeakPtr());
203 } 205 }
204 206
205 HttpCache::Transaction::~Transaction() { 207 HttpCache::Transaction::~Transaction() {
206 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction"); 208 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction");
207 // We may have to issue another IO, but we should never invoke the callback_ 209 // We may have to issue another IO, but we should never invoke the callback_
208 // after this point. 210 // after this point.
209 callback_.Reset(); 211 callback_.Reset();
210 212
211 if (cache_) { 213 if (cache_) {
212 if (entry_) { 214 if (entry_) {
213 bool cancel_request = reading_ && response_.headers.get(); 215 bool cancel_request =
214 if (cancel_request) { 216 request_->method != "HEAD" &&
215 if (partial_) { 217 (this == entry_->writer ||
216 entry_->disk_entry->CancelSparseIO(); 218 (auth_response_.headers.get() ||
217 } else { 219 (new_response_ && new_response_->headers &&
218 cancel_request &= (response_.headers->response_code() == 200); 220 new_response_->headers->response_code() == 416)));
shivanisha 2017/03/29 12:37:54 This still has special case logic. Thinking about
219 } 221 if (cancel_request && partial_)
220 } 222 entry_->disk_entry->CancelSparseIO();
221 223
222 cache_->DoneWithEntry(entry_, this, cancel_request); 224 cache_->DoneWithEntry(entry_, this, cancel_request);
223 } else if (cache_pending_) { 225 } else if (cache_pending_) {
224 cache_->RemovePendingTransaction(this); 226 cache_->RemovePendingTransaction(this);
225 } 227 }
226 } 228 }
227 } 229 }
228 230
229 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, 231 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
230 const CompletionCallback& callback) { 232 const CompletionCallback& callback) {
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 369 }
368 370
369 bool HttpCache::Transaction::IsReadyToRestartForAuth() { 371 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
370 if (!network_trans_.get()) 372 if (!network_trans_.get())
371 return false; 373 return false;
372 return network_trans_->IsReadyToRestartForAuth(); 374 return network_trans_->IsReadyToRestartForAuth();
373 } 375 }
374 376
375 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len, 377 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
376 const CompletionCallback& callback) { 378 const CompletionCallback& callback) {
379 // TODO(shivanisha): A lot of tests invoke Read on a HEAD request which
380 // should not be allowed in the Read() method. Fix those tests.
377 DCHECK_EQ(next_state_, STATE_NONE); 381 DCHECK_EQ(next_state_, STATE_NONE);
378 DCHECK(buf); 382 DCHECK(buf);
379 DCHECK_GT(buf_len, 0); 383 DCHECK_GT(buf_len, 0);
380 DCHECK(!callback.is_null()); 384 DCHECK(!callback.is_null());
381 385
382 DCHECK(callback_.is_null()); 386 DCHECK(callback_.is_null());
383 387
384 if (!cache_.get()) 388 if (!cache_.get())
385 return ERR_UNEXPECTED; 389 return ERR_UNEXPECTED;
386 390
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 ConnectionAttempts* out) const { 576 ConnectionAttempts* out) const {
573 ConnectionAttempts new_connection_attempts; 577 ConnectionAttempts new_connection_attempts;
574 if (network_trans_) 578 if (network_trans_)
575 network_trans_->GetConnectionAttempts(&new_connection_attempts); 579 network_trans_->GetConnectionAttempts(&new_connection_attempts);
576 580
577 out->swap(new_connection_attempts); 581 out->swap(new_connection_attempts);
578 out->insert(out->begin(), old_connection_attempts_.begin(), 582 out->insert(out->begin(), old_connection_attempts_.begin(),
579 old_connection_attempts_.end()); 583 old_connection_attempts_.end());
580 } 584 }
581 585
586 void HttpCache::Transaction::SetValidatingCannotProceed() {
587 validating_cannot_proceed_ = true;
588 entry_ = nullptr;
589 }
590
582 size_t HttpCache::Transaction::EstimateMemoryUsage() const { 591 size_t HttpCache::Transaction::EstimateMemoryUsage() const {
583 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. 592 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108.
584 return 0; 593 return 0;
585 } 594 }
586 595
587 //----------------------------------------------------------------------------- 596 //-----------------------------------------------------------------------------
588 597
589 // A few common patterns: (Foo* means Foo -> FooComplete) 598 // A few common patterns: (Foo* means Foo -> FooComplete)
590 // 599 //
591 // 1. Not-cached entry: 600 // 1. Not-cached entry:
592 // Start(): 601 // Start():
593 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> 602 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
594 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> 603 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
595 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> 604 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
596 // PartialHeadersReceived 605 // PartialHeadersReceived
597 // 606 //
598 // Read(): 607 // Read():
599 // NetworkRead* -> CacheWriteData* 608 // NetworkRead* -> CacheWriteData*
600 // 609 //
601 // 2. Cached entry, no validation: 610 // 2. Cached entry, no validation:
602 // Start(): 611 // Start():
603 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 612 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
604 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 613 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
605 // BeginCacheValidation() -> SetupEntryForRead() 614 // BeginCacheValidation() -> SetupEntryForRead() -> WaitBeforeRead*
606 // 615 //
607 // Read(): 616 // Read():
608 // CacheReadData* 617 // CacheReadData*
609 // 618 //
610 // 3. Cached entry, validation (304): 619 // 3. Cached entry, validation (304):
611 // Start(): 620 // Start():
612 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 621 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
613 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 622 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
614 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 623 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
615 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> 624 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
616 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> 625 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
617 // PartialHeadersReceived 626 // PartialHeadersReceived -> WaitBeforeRead*
618 // 627 //
619 // Read(): 628 // Read():
620 // CacheReadData* 629 // CacheReadData*
621 // 630 //
622 // 4. Cached entry, validation and replace (200): 631 // 4. Cached entry, validation and replace (200):
623 // Start(): 632 // Start():
624 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 633 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
625 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 634 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
626 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 635 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
627 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> 636 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 // 721 //
713 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: 722 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
714 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between 723 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
715 // CacheReadResponse* and CacheDispatchValidation. 724 // CacheReadResponse* and CacheDispatchValidation.
716 int HttpCache::Transaction::DoLoop(int result) { 725 int HttpCache::Transaction::DoLoop(int result) {
717 DCHECK_NE(STATE_UNSET, next_state_); 726 DCHECK_NE(STATE_UNSET, next_state_);
718 DCHECK_NE(STATE_NONE, next_state_); 727 DCHECK_NE(STATE_NONE, next_state_);
719 DCHECK(!in_do_loop_); 728 DCHECK(!in_do_loop_);
720 729
721 int rv = result; 730 int rv = result;
731 State state = next_state_;
722 do { 732 do {
723 State state = next_state_; 733 state = next_state_;
724 next_state_ = STATE_UNSET; 734 next_state_ = STATE_UNSET;
725 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true); 735 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true);
726 736
727 switch (state) { 737 switch (state) {
728 case STATE_GET_BACKEND: 738 case STATE_GET_BACKEND:
729 DCHECK_EQ(OK, rv); 739 DCHECK_EQ(OK, rv);
730 rv = DoGetBackend(); 740 rv = DoGetBackend();
731 break; 741 break;
732 case STATE_GET_BACKEND_COMPLETE: 742 case STATE_GET_BACKEND_COMPLETE:
733 rv = DoGetBackendComplete(rv); 743 rv = DoGetBackendComplete(rv);
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 DCHECK_EQ(OK, rv); 860 DCHECK_EQ(OK, rv);
851 rv = DoPartialHeadersReceived(); 861 rv = DoPartialHeadersReceived();
852 break; 862 break;
853 case STATE_CACHE_READ_METADATA: 863 case STATE_CACHE_READ_METADATA:
854 DCHECK_EQ(OK, rv); 864 DCHECK_EQ(OK, rv);
855 rv = DoCacheReadMetadata(); 865 rv = DoCacheReadMetadata();
856 break; 866 break;
857 case STATE_CACHE_READ_METADATA_COMPLETE: 867 case STATE_CACHE_READ_METADATA_COMPLETE:
858 rv = DoCacheReadMetadataComplete(rv); 868 rv = DoCacheReadMetadataComplete(rv);
859 break; 869 break;
870 case STATE_WAIT_BEFORE_READ:
871 DCHECK_EQ(OK, rv);
872 rv = DoWaitBeforeRead();
873 break;
874 case STATE_WAIT_BEFORE_READ_COMPLETE:
875 rv = DoWaitBeforeReadComplete(rv);
876 break;
860 case STATE_NETWORK_READ: 877 case STATE_NETWORK_READ:
861 DCHECK_EQ(OK, rv); 878 DCHECK_EQ(OK, rv);
862 rv = DoNetworkRead(); 879 rv = DoNetworkRead();
863 break; 880 break;
864 case STATE_NETWORK_READ_COMPLETE: 881 case STATE_NETWORK_READ_COMPLETE:
865 rv = DoNetworkReadComplete(rv); 882 rv = DoNetworkReadComplete(rv);
866 break; 883 break;
867 case STATE_CACHE_READ_DATA: 884 case STATE_CACHE_READ_DATA:
868 DCHECK_EQ(OK, rv); 885 DCHECK_EQ(OK, rv);
869 rv = DoCacheReadData(); 886 rv = DoCacheReadData();
(...skipping 16 matching lines...) Expand all
886 break; 903 break;
887 default: 904 default:
888 NOTREACHED() << "bad state " << state; 905 NOTREACHED() << "bad state " << state;
889 rv = ERR_FAILED; 906 rv = ERR_FAILED;
890 break; 907 break;
891 } 908 }
892 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state; 909 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state;
893 910
894 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); 911 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
895 912
913 // Assert Start() state machine's allowed last state in successful cases when
914 // caching is happening.
915 DCHECK(reading_ || rv != OK || !entry_ ||
916 state == STATE_WAIT_BEFORE_READ_COMPLETE);
917
918 if (!reading_ && rv == OK) {
919 DCHECK(!entry_ || state == STATE_WAIT_BEFORE_READ_COMPLETE);
920 }
921
896 if (rv != ERR_IO_PENDING && !callback_.is_null()) { 922 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
897 read_buf_ = NULL; // Release the buffer before invoking the callback. 923 read_buf_ = nullptr; // Release the buffer before invoking the callback.
898 base::ResetAndReturn(&callback_).Run(rv); 924 base::ResetAndReturn(&callback_).Run(rv);
899 } 925 }
900 926
901 return rv; 927 return rv;
902 } 928 }
903 929
904 int HttpCache::Transaction::DoGetBackend() { 930 int HttpCache::Transaction::DoGetBackend() {
905 cache_pending_ = true; 931 cache_pending_ = true;
906 TransitionToState(STATE_GET_BACKEND_COMPLETE); 932 TransitionToState(STATE_GET_BACKEND_COMPLETE);
907 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); 933 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 partial_->RestoreHeaders(&custom_request_->extra_headers); 995 partial_->RestoreHeaders(&custom_request_->extra_headers);
970 partial_.reset(); 996 partial_.reset();
971 } 997 }
972 TransitionToState(STATE_SEND_REQUEST); 998 TransitionToState(STATE_SEND_REQUEST);
973 } else { 999 } else {
974 TransitionToState(STATE_INIT_ENTRY); 1000 TransitionToState(STATE_INIT_ENTRY);
975 } 1001 }
976 1002
977 // This is only set if we have something to do with the response. 1003 // This is only set if we have something to do with the response.
978 range_requested_ = (partial_.get() != NULL); 1004 range_requested_ = (partial_.get() != NULL);
979 1005 original_mode_ = mode_;
980 return OK; 1006 return OK;
981 } 1007 }
982 1008
983 int HttpCache::Transaction::DoInitEntry() { 1009 int HttpCache::Transaction::DoInitEntry() {
984 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); 1010 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry");
985 DCHECK(!new_entry_); 1011 DCHECK(!new_entry_);
986 1012
987 if (!cache_.get()) { 1013 if (!cache_.get()) {
988 TransitionToState(STATE_NONE); 1014 TransitionToState(STATE_NONE);
989 return ERR_UNEXPECTED; 1015 return ERR_UNEXPECTED;
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 TransitionToState(STATE_NONE); 1373 TransitionToState(STATE_NONE);
1348 return ERR_UNEXPECTED; 1374 return ERR_UNEXPECTED;
1349 } 1375 }
1350 1376
1351 return ValidateEntryHeadersAndContinue(); 1377 return ValidateEntryHeadersAndContinue();
1352 } 1378 }
1353 1379
1354 // We may end up here multiple times for a given request. 1380 // We may end up here multiple times for a given request.
1355 int HttpCache::Transaction::DoStartPartialCacheValidation() { 1381 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1356 if (mode_ == NONE) { 1382 if (mode_ == NONE) {
1357 TransitionToState(STATE_NONE); 1383 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
1358 return OK; 1384 return OK;
1359 } 1385 }
1360 1386
1361 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); 1387 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION);
1362 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); 1388 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1363 } 1389 }
1364 1390
1365 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { 1391 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1366 if (!result) { 1392 if (!result) {
1367 // This is the end of the request. 1393 // This is the end of the request.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 // We received the response headers and there is no error. 1488 // We received the response headers and there is no error.
1463 int HttpCache::Transaction::DoSuccessfulSendRequest() { 1489 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1464 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); 1490 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest");
1465 DCHECK(!new_response_); 1491 DCHECK(!new_response_);
1466 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); 1492 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1467 1493
1468 if (new_response->headers->response_code() == 401 || 1494 if (new_response->headers->response_code() == 401 ||
1469 new_response->headers->response_code() == 407) { 1495 new_response->headers->response_code() == 407) {
1470 SetAuthResponse(*new_response); 1496 SetAuthResponse(*new_response);
1471 if (!reading_) { 1497 if (!reading_) {
1472 TransitionToState(STATE_NONE); 1498 TransitionToState(STATE_WAIT_BEFORE_READ);
1473 return OK; 1499 return OK;
1474 } 1500 }
1475 1501
1476 // We initiated a second request the caller doesn't know about. We should be 1502 // We initiated a second request the caller doesn't know about. We should be
1477 // able to authenticate this request because we should have authenticated 1503 // able to authenticate this request because we should have authenticated
1478 // this URL moments ago. 1504 // this URL moments ago.
1479 if (IsReadyToRestartForAuth()) { 1505 if (IsReadyToRestartForAuth()) {
1480 DCHECK(!response_.auth_challenge.get()); 1506 DCHECK(!response_.auth_challenge.get());
1481 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1507 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1482 // In theory we should check to see if there are new cookies, but there 1508 // In theory we should check to see if there are new cookies, but there
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); 1551 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE);
1526 } 1552 }
1527 1553
1528 // Invalidate any cached GET with a successful PUT or DELETE. 1554 // Invalidate any cached GET with a successful PUT or DELETE.
1529 if (mode_ == WRITE && 1555 if (mode_ == WRITE &&
1530 (request_->method == "PUT" || request_->method == "DELETE")) { 1556 (request_->method == "PUT" || request_->method == "DELETE")) {
1531 if (NonErrorResponse(new_response->headers->response_code())) { 1557 if (NonErrorResponse(new_response->headers->response_code())) {
1532 int ret = cache_->DoomEntry(cache_key_, NULL); 1558 int ret = cache_->DoomEntry(cache_key_, NULL);
1533 DCHECK_EQ(OK, ret); 1559 DCHECK_EQ(OK, ret);
1534 } 1560 }
1535 cache_->DoneWritingToEntry(entry_, true); 1561 cache_->DoneWritingToEntry(entry_, true, this);
1536 entry_ = NULL; 1562 entry_ = NULL;
1537 mode_ = NONE; 1563 mode_ = NONE;
1538 } 1564 }
1539 1565
1540 // Invalidate any cached GET with a successful POST. 1566 // Invalidate any cached GET with a successful POST.
1541 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && 1567 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1542 request_->method == "POST" && 1568 request_->method == "POST" &&
1543 NonErrorResponse(new_response->headers->response_code())) { 1569 NonErrorResponse(new_response->headers->response_code())) {
1544 cache_->DoomMainEntryForUrl(request_->url); 1570 cache_->DoomMainEntryForUrl(request_->url);
1545 } 1571 }
1546 1572
1547 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); 1573 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1548 1574
1549 if (new_response_->headers->response_code() == 416 && 1575 if (new_response_->headers->response_code() == 416 &&
1550 (request_->method == "GET" || request_->method == "POST")) { 1576 (request_->method == "GET" || request_->method == "POST")) {
1551 // If there is an active entry it may be destroyed with this transaction. 1577 // If there is an active entry it may be destroyed with this transaction.
1552 SetResponse(*new_response_); 1578 SetResponse(*new_response_);
1553 TransitionToState(STATE_NONE); 1579 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
1554 return OK; 1580 return OK;
1555 } 1581 }
1556 1582
1557 // Are we expecting a response to a conditional query? 1583 // Are we expecting a response to a conditional query?
1558 if (mode_ == READ_WRITE || mode_ == UPDATE) { 1584 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1559 if (new_response->headers->response_code() == 304 || handling_206_) { 1585 if (new_response->headers->response_code() == 304 || handling_206_) {
1560 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); 1586 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED);
1561 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); 1587 TransitionToState(STATE_UPDATE_CACHED_RESPONSE);
1562 return OK; 1588 return OK;
1563 } 1589 }
1564 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); 1590 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED);
1565 mode_ = WRITE; 1591 mode_ = WRITE;
1566 } 1592 }
1567 1593
1568 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); 1594 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE);
1595
1596 if (!entry_)
1597 return OK;
1598
1599 // Invalidate any current entry with a successful response if this transaction
1600 // cannot write to this entry.
1601 if (new_response->headers->response_code() != 304 &&
1602 (entry_->writer || !entry_->readers.empty())) {
1603 DCHECK_EQ(entry_->headers_transaction, this);
1604 cache_->DoneResponseHeaders(entry_, this, false);
1605 entry_ = nullptr;
1606 mode_ = NONE;
1607 return OK;
1608 }
1609
1569 return OK; 1610 return OK;
1570 } 1611 }
1571 1612
1572 // We received 304 or 206 and we want to update the cached response headers. 1613 // We received 304 or 206 and we want to update the cached response headers.
1573 int HttpCache::Transaction::DoUpdateCachedResponse() { 1614 int HttpCache::Transaction::DoUpdateCachedResponse() {
1574 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); 1615 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse");
1575 int rv = OK; 1616 int rv = OK;
1576 // Update the cached response based on the headers and properties of 1617 // Update the cached response based on the headers and properties of
1577 // new_response_. 1618 // new_response_.
1578 response_.headers->Update(*new_response_->headers.get()); 1619 response_.headers->Update(*new_response_->headers.get());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 DCHECK(!handling_206_); 1672 DCHECK(!handling_206_);
1632 // We got a "not modified" response and already updated the corresponding 1673 // We got a "not modified" response and already updated the corresponding
1633 // cache entry above. 1674 // cache entry above.
1634 // 1675 //
1635 // By closing the cached entry now, we make sure that the 304 rather than 1676 // By closing the cached entry now, we make sure that the 304 rather than
1636 // the cached 200 response, is what will be returned to the user. 1677 // the cached 200 response, is what will be returned to the user.
1637 DoneWritingToEntry(true); 1678 DoneWritingToEntry(true);
1638 } else if (entry_ && !handling_206_) { 1679 } else if (entry_ && !handling_206_) {
1639 DCHECK_EQ(READ_WRITE, mode_); 1680 DCHECK_EQ(READ_WRITE, mode_);
1640 if (!partial_ || partial_->IsLastRange()) { 1681 if (!partial_ || partial_->IsLastRange()) {
1641 cache_->ConvertWriterToReader(entry_);
1642 mode_ = READ; 1682 mode_ = READ;
1643 } 1683 }
1644 // We no longer need the network transaction, so destroy it. 1684 // We no longer need the network transaction, so destroy it.
1645 ResetNetworkTransaction(); 1685 ResetNetworkTransaction();
1646 } else if (entry_ && handling_206_ && truncated_ && 1686 } else if (entry_ && handling_206_ && truncated_ &&
1647 partial_->initial_validation()) { 1687 partial_->initial_validation()) {
1648 // We just finished the validation of a truncated entry, and the server 1688 // We just finished the validation of a truncated entry, and the server
1649 // is willing to resume the operation. Now we go back and start serving 1689 // is willing to resume the operation. Now we go back and start serving
1650 // the first part to the user. 1690 // the first part to the user.
1651 ResetNetworkTransaction(); 1691 ResetNetworkTransaction();
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1751 } 1791 }
1752 1792
1753 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1793 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1754 return OK; 1794 return OK;
1755 } 1795 }
1756 1796
1757 int HttpCache::Transaction::DoPartialHeadersReceived() { 1797 int HttpCache::Transaction::DoPartialHeadersReceived() {
1758 new_response_ = NULL; 1798 new_response_ = NULL;
1759 1799
1760 if (!partial_) { 1800 if (!partial_) {
1761 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) 1801 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) {
1762 TransitionToState(STATE_CACHE_READ_METADATA); 1802 TransitionToState(STATE_CACHE_READ_METADATA);
1763 else 1803 } else {
1764 TransitionToState(STATE_NONE); 1804 DCHECK(!reading_);
1805 TransitionToState(STATE_WAIT_BEFORE_READ);
1806 }
1765 return OK; 1807 return OK;
1766 } 1808 }
1767 1809
1768 if (reading_) { 1810 if (reading_) {
1769 if (network_trans_.get()) { 1811 if (network_trans_.get()) {
1770 TransitionToState(STATE_NETWORK_READ); 1812 TransitionToState(STATE_NETWORK_READ);
1771 } else { 1813 } else {
1772 TransitionToState(STATE_CACHE_READ_DATA); 1814 TransitionToState(STATE_CACHE_READ_DATA);
1773 } 1815 }
1774 } else if (mode_ != NONE) { 1816 } else if (mode_ != NONE) {
1775 // We are about to return the headers for a byte-range request to the user, 1817 // We are about to return the headers for a byte-range request to the user,
1776 // so let's fix them. 1818 // so let's fix them.
1777 partial_->FixResponseHeaders(response_.headers.get(), true); 1819 partial_->FixResponseHeaders(response_.headers.get(), true);
1778 TransitionToState(STATE_NONE); 1820 TransitionToState(STATE_WAIT_BEFORE_READ);
1779 } else { 1821 } else {
1780 TransitionToState(STATE_NONE); 1822 TransitionToState(STATE_WAIT_BEFORE_READ);
1781 } 1823 }
1782 return OK; 1824 return OK;
1783 } 1825 }
1784 1826
1827 int HttpCache::Transaction::DoWaitBeforeRead() {
1828 if (!entry_) {
1829 TransitionToState(STATE_NONE);
1830 return OK;
1831 }
1832
1833 // A transaction comes here between the completion of headers phase and the
1834 // start of response body phase, thus it should never be the active writer at
1835 // this point.
1836 DCHECK_NE(entry_->writer, this);
1837
1838 TransitionToState(STATE_WAIT_BEFORE_READ_COMPLETE);
1839
1840 // If it was an auth failure or 416, this transaction should continue to be
1841 // headers_transaction till consumer takes an action, so no need to do
1842 // anything now.
1843 if (auth_response_.headers.get() ||
1844 (new_response_ && new_response_->headers &&
1845 new_response_->headers->response_code() == 416))
1846 return OK;
1847
1848 // If there is no response body to be written or read, it does not need to
1849 // wait.
1850 if (request_->method == "HEAD")
1851 return OK;
1852
1853 // Proceed only if |this| completed the headers phase. For read-only
1854 // transactions since they do not have a headers phase, they would have
1855 // already been added to entry_->readers, so do nothing.
1856 if (entry_->headers_transaction == this)
1857 return cache_->DoneResponseHeaders(entry_, this, true);
1858
1859 return OK;
1860 }
1861
1862 int HttpCache::Transaction::DoWaitBeforeReadComplete(int rv) {
1863 if (rv == ERR_CACHE_RACE) {
1864 RestartAfterValidationStarted();
1865 return OK;
1866 }
1867
1868 TransitionToState(STATE_NONE);
1869 if (rv != OK)
1870 return rv;
1871
1872 // If successful then this must be either be the writer or a reader as the
1873 // response must have completely been written to the cache.
1874 if (entry_->writer == this)
1875 return OK;
1876
1877 // If it was an auth failure or 416, this transaction should continue to be
1878 // headers_transaction, so no need to do anything now.
1879 if (auth_response_.headers.get() ||
1880 (new_response_ && new_response_->headers &&
1881 new_response_->headers->response_code() == 416)) {
1882 DCHECK_EQ(entry_->headers_transaction, this);
1883 return OK;
1884 }
1885
1886 mode_ = READ;
1887 if (network_trans_)
1888 ResetNetworkTransaction();
1889 return OK;
1890 }
1891
1892 void HttpCache::Transaction::RestartAfterValidationStarted() {
1893 DCHECK(!reading_);
1894 next_state_ = STATE_INIT_ENTRY;
1895 cache_entry_status_ = CacheEntryStatus::ENTRY_UNDEFINED;
1896 entry_ = nullptr;
1897 mode_ = original_mode_;
1898 if (network_trans_)
1899 network_trans_.reset();
1900 }
1901
1785 int HttpCache::Transaction::DoCacheReadMetadata() { 1902 int HttpCache::Transaction::DoCacheReadMetadata() {
1786 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); 1903 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata");
1787 DCHECK(entry_); 1904 DCHECK(entry_);
1788 DCHECK(!response_.metadata.get()); 1905 DCHECK(!response_.metadata.get());
1789 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); 1906 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE);
1790 1907
1791 response_.metadata = 1908 response_.metadata =
1792 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); 1909 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1793 1910
1794 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); 1911 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO);
1795 return entry_->disk_entry->ReadData(kMetadataIndex, 0, 1912 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1796 response_.metadata.get(), 1913 response_.metadata.get(),
1797 response_.metadata->size(), 1914 response_.metadata->size(),
1798 io_callback_); 1915 io_callback_);
1799 } 1916 }
1800 1917
1801 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { 1918 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1802 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); 1919 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete");
1803 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, 1920 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO,
1804 result); 1921 result);
1805 if (result != response_.metadata->size()) 1922 if (result != response_.metadata->size())
1806 return OnCacheReadError(result, false); 1923 return OnCacheReadError(result, false);
1807 TransitionToState(STATE_NONE); 1924
1925 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
1808 return OK; 1926 return OK;
1809 } 1927 }
1810 1928
1811 int HttpCache::Transaction::DoNetworkRead() { 1929 int HttpCache::Transaction::DoNetworkRead() {
1812 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); 1930 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead");
1813 TransitionToState(STATE_NETWORK_READ_COMPLETE); 1931 TransitionToState(STATE_NETWORK_READ_COMPLETE);
1814 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); 1932 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1815 } 1933 }
1816 1934
1817 int HttpCache::Transaction::DoNetworkReadComplete(int result) { 1935 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
2115 TransitionToState(STATE_NONE); 2233 TransitionToState(STATE_NONE);
2116 return ERR_CACHE_MISS; 2234 return ERR_CACHE_MISS;
2117 } 2235 }
2118 2236
2119 if (request_->method == "HEAD") 2237 if (request_->method == "HEAD")
2120 FixHeadersForHead(); 2238 FixHeadersForHead();
2121 2239
2122 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2240 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2123 TransitionToState(STATE_CACHE_READ_METADATA); 2241 TransitionToState(STATE_CACHE_READ_METADATA);
2124 else 2242 else
2125 TransitionToState(STATE_NONE); 2243 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
2126 2244
2127 return OK; 2245 return OK;
2128 } 2246 }
2129 2247
2130 int HttpCache::Transaction::BeginCacheValidation() { 2248 int HttpCache::Transaction::BeginCacheValidation() {
2131 DCHECK_EQ(mode_, READ_WRITE); 2249 DCHECK_EQ(mode_, READ_WRITE);
2132 2250
2133 ValidationType required_validation = RequiresValidation(); 2251 ValidationType required_validation = RequiresValidation();
2134 2252
2135 bool skip_validation = (required_validation == VALIDATION_NONE); 2253 bool skip_validation = (required_validation == VALIDATION_NONE);
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
2628 if (partial_) { 2746 if (partial_) {
2629 if (truncated_ || is_sparse_ || !invalid_range_) { 2747 if (truncated_ || is_sparse_ || !invalid_range_) {
2630 // We are going to return the saved response headers to the caller, so 2748 // We are going to return the saved response headers to the caller, so
2631 // we may need to adjust them first. 2749 // we may need to adjust them first.
2632 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 2750 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
2633 return OK; 2751 return OK;
2634 } else { 2752 } else {
2635 partial_.reset(); 2753 partial_.reset();
2636 } 2754 }
2637 } 2755 }
2638 cache_->ConvertWriterToReader(entry_); 2756
2639 mode_ = READ; 2757 mode_ = READ;
2640 2758
2641 if (request_->method == "HEAD") 2759 if (request_->method == "HEAD")
2642 FixHeadersForHead(); 2760 FixHeadersForHead();
2643 2761
2644 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2762 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2645 TransitionToState(STATE_CACHE_READ_METADATA); 2763 TransitionToState(STATE_CACHE_READ_METADATA);
2646 else 2764 else
2647 TransitionToState(STATE_NONE); 2765 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
2648 return OK; 2766 return OK;
2649 } 2767 }
2650 2768
2651 int HttpCache::Transaction::WriteToEntry(int index, int offset, 2769 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2652 IOBuffer* data, int data_len, 2770 IOBuffer* data, int data_len,
2653 const CompletionCallback& callback) { 2771 const CompletionCallback& callback) {
2654 if (!entry_) 2772 if (!entry_)
2655 return data_len; 2773 return data_len;
2656 2774
2657 int rv = 0; 2775 int rv = 0;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2716 } 2834 }
2717 return OK; 2835 return OK;
2718 } 2836 }
2719 2837
2720 void HttpCache::Transaction::DoneWritingToEntry(bool success) { 2838 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2721 if (!entry_) 2839 if (!entry_)
2722 return; 2840 return;
2723 2841
2724 RecordHistograms(); 2842 RecordHistograms();
2725 2843
2726 cache_->DoneWritingToEntry(entry_, success); 2844 cache_->DoneWritingToEntry(entry_, success, this);
2727 entry_ = NULL; 2845 entry_ = NULL;
2728 mode_ = NONE; // switch to 'pass through' mode 2846 mode_ = NONE; // switch to 'pass through' mode
2729 } 2847 }
2730 2848
2731 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { 2849 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2732 DLOG(ERROR) << "ReadData failed: " << result; 2850 DLOG(ERROR) << "ReadData failed: " << result;
2733 const int result_for_histogram = std::max(0, -result); 2851 const int result_for_histogram = std::max(0, -result);
2734 if (restart) { 2852 if (restart) {
2735 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", 2853 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2736 result_for_histogram); 2854 result_for_histogram);
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
3073 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated", 3191 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
3074 before_send_sample); 3192 before_send_sample);
3075 break; 3193 break;
3076 } 3194 }
3077 default: 3195 default:
3078 NOTREACHED(); 3196 NOTREACHED();
3079 } 3197 }
3080 } 3198 }
3081 3199
3082 void HttpCache::Transaction::OnIOComplete(int result) { 3200 void HttpCache::Transaction::OnIOComplete(int result) {
3201 // If its the Start state machine and it cannot proceed due to failure by
3202 // another transaction in writing the response, restart this transaction.
3203 if (!reading_ && validating_cannot_proceed_) {
3204 validating_cannot_proceed_ = false;
3205 RestartAfterValidationStarted();
3206 }
3207
3083 DoLoop(result); 3208 DoLoop(result);
3084 } 3209 }
3085 3210
3086 void HttpCache::Transaction::TransitionToState(State state) { 3211 void HttpCache::Transaction::TransitionToState(State state) {
3087 // Ensure that the state is only set once per Do* state. 3212 // Ensure that the state is only set once per Do* state.
3088 DCHECK(in_do_loop_); 3213 DCHECK(in_do_loop_);
3089 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; 3214 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state;
3090 next_state_ = state; 3215 next_state_ = state;
3091 } 3216 }
3092 3217
3093 } // namespace net 3218 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.h ('k') | net/http/http_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698