OLD | NEW |
---|---|
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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 } | 189 } |
190 | 190 |
191 HttpCache::Transaction::~Transaction() { | 191 HttpCache::Transaction::~Transaction() { |
192 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction"); | 192 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction"); |
193 // We may have to issue another IO, but we should never invoke the callback_ | 193 // We may have to issue another IO, but we should never invoke the callback_ |
194 // after this point. | 194 // after this point. |
195 callback_.Reset(); | 195 callback_.Reset(); |
196 | 196 |
197 if (cache_) { | 197 if (cache_) { |
198 if (entry_) { | 198 if (entry_) { |
199 bool cancel_request = reading_ && response_.headers.get(); | 199 bool writing_incomplete = |
200 if (cancel_request) { | 200 cache_->IsTransactionWritingIncomplete(entry_, this, method_); |
201 if (partial_) { | 201 if (writing_incomplete && partial_) |
202 entry_->disk_entry->CancelSparseIO(); | 202 entry_->disk_entry->CancelSparseIO(); |
203 } else { | |
204 cancel_request &= (response_.headers->response_code() == 200); | |
205 } | |
206 } | |
207 | 203 |
208 cache_->DoneWithEntry(entry_, this, cancel_request); | 204 cache_->DoneWithEntry(entry_, this, writing_incomplete); |
209 } else if (cache_pending_) { | 205 } else if (cache_pending_) { |
210 cache_->RemovePendingTransaction(this); | 206 cache_->RemovePendingTransaction(this); |
211 } | 207 } |
212 } | 208 } |
213 } | 209 } |
214 | 210 |
215 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, | 211 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, |
216 const CompletionCallback& callback) { | 212 const CompletionCallback& callback) { |
217 DCHECK(buf); | 213 DCHECK(buf); |
218 DCHECK_GT(buf_len, 0); | 214 DCHECK_GT(buf_len, 0); |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
558 ConnectionAttempts* out) const { | 554 ConnectionAttempts* out) const { |
559 ConnectionAttempts new_connection_attempts; | 555 ConnectionAttempts new_connection_attempts; |
560 if (network_trans_) | 556 if (network_trans_) |
561 network_trans_->GetConnectionAttempts(&new_connection_attempts); | 557 network_trans_->GetConnectionAttempts(&new_connection_attempts); |
562 | 558 |
563 out->swap(new_connection_attempts); | 559 out->swap(new_connection_attempts); |
564 out->insert(out->begin(), old_connection_attempts_.begin(), | 560 out->insert(out->begin(), old_connection_attempts_.begin(), |
565 old_connection_attempts_.end()); | 561 old_connection_attempts_.end()); |
566 } | 562 } |
567 | 563 |
564 void HttpCache::Transaction::SetValidatingCannotProceed() { | |
565 DCHECK(!reading_); | |
566 next_state_ = STATE_HEADERS_PHASE_CANNOT_PROCEED; | |
567 entry_ = nullptr; | |
568 } | |
569 | |
568 size_t HttpCache::Transaction::EstimateMemoryUsage() const { | 570 size_t HttpCache::Transaction::EstimateMemoryUsage() const { |
569 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. | 571 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. |
570 return 0; | 572 return 0; |
571 } | 573 } |
572 | 574 |
573 //----------------------------------------------------------------------------- | 575 //----------------------------------------------------------------------------- |
574 | 576 |
575 // A few common patterns: (Foo* means Foo -> FooComplete) | 577 // A few common patterns: (Foo* means Foo -> FooComplete) |
576 // | 578 // |
577 // 1. Not-cached entry: | 579 // 1. Not-cached entry: |
578 // Start(): | 580 // Start(): |
579 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> | 581 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> |
580 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> | 582 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> |
581 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> | 583 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> |
582 // PartialHeadersReceived | 584 // PartialHeadersReceived -> FinishHeaders* |
583 // | 585 // |
584 // Read(): | 586 // Read(): |
585 // NetworkRead* -> CacheWriteData* | 587 // NetworkRead* -> CacheWriteData* |
586 // | 588 // |
587 // 2. Cached entry, no validation: | 589 // 2. Cached entry, no validation: |
588 // Start(): | 590 // Start(): |
589 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 591 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
590 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 592 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
591 // BeginCacheValidation() -> SetupEntryForRead() | 593 // BeginCacheValidation() -> SetupEntryForRead() -> FinishHeaders* |
592 // | 594 // |
593 // Read(): | 595 // Read(): |
594 // CacheReadData* | 596 // CacheReadData* |
595 // | 597 // |
596 // 3. Cached entry, validation (304): | 598 // 3. Cached entry, validation (304): |
597 // Start(): | 599 // Start(): |
598 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 600 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
599 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 601 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
600 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> | 602 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
601 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> | 603 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> |
602 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> | 604 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> |
603 // PartialHeadersReceived | 605 // PartialHeadersReceived -> FinishHeaders* |
604 // | 606 // |
605 // Read(): | 607 // Read(): |
606 // CacheReadData* | 608 // CacheReadData* |
607 // | 609 // |
608 // 4. Cached entry, validation and replace (200): | 610 // 4. Cached entry, validation and replace (200): |
609 // Start(): | 611 // Start(): |
610 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 612 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
611 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 613 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
612 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> | 614 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
613 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> | 615 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> |
614 // TruncateCachedMetadata* -> PartialHeadersReceived | 616 // TruncateCachedMetadata* -> PartialHeadersReceived -> FinishHeaders* |
615 // | 617 // |
616 // Read(): | 618 // Read(): |
617 // NetworkRead* -> CacheWriteData* | 619 // NetworkRead* -> CacheWriteData* |
618 // | 620 // |
619 // 5. Sparse entry, partially cached, byte range request: | 621 // 5. Sparse entry, partially cached, byte range request: |
620 // Start(): | 622 // Start(): |
621 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 623 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
622 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 624 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
623 // CacheQueryData* -> ValidateEntryHeadersAndContinue() -> | 625 // CacheQueryData* -> ValidateEntryHeadersAndContinue() -> |
624 // StartPartialCacheValidation -> CompletePartialCacheValidation -> | 626 // StartPartialCacheValidation -> CompletePartialCacheValidation -> |
625 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> | 627 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
626 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> | 628 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> |
627 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> | 629 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> |
628 // PartialHeadersReceived | 630 // PartialHeadersReceived -> FinishHeaders* |
629 // | 631 // |
630 // Read() 1: | 632 // Read() 1: |
631 // NetworkRead* -> CacheWriteData* | 633 // NetworkRead* -> CacheWriteData* |
632 // | 634 // |
633 // Read() 2: | 635 // Read() 2: |
634 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation -> | 636 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation -> |
635 // CompletePartialCacheValidation -> CacheReadData* -> | 637 // CompletePartialCacheValidation -> CacheReadData* -> |
636 // | 638 // |
637 // Read() 3: | 639 // Read() 3: |
638 // CacheReadData* -> StartPartialCacheValidation -> | 640 // CacheReadData* -> StartPartialCacheValidation -> |
(...skipping 20 matching lines...) Expand all Loading... | |
659 // Read(): | 661 // Read(): |
660 // CacheReadData (returns 0) | 662 // CacheReadData (returns 0) |
661 // | 663 // |
662 // 9. HEAD. Cached entry, validation and replace (200): | 664 // 9. HEAD. Cached entry, validation and replace (200): |
663 // Pass through. The request dooms the old entry, as a HEAD won't be stored by | 665 // Pass through. The request dooms the old entry, as a HEAD won't be stored by |
664 // itself. | 666 // itself. |
665 // Start(): | 667 // Start(): |
666 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 668 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
667 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 669 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
668 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> | 670 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
669 // OverwriteCachedResponse | 671 // OverwriteCachedResponse -> FinishHeaders* |
670 // | 672 // |
671 // 10. HEAD. Sparse entry, partially cached: | 673 // 10. HEAD. Sparse entry, partially cached: |
672 // Serve the request from the cache, as long as it doesn't require | 674 // Serve the request from the cache, as long as it doesn't require |
673 // revalidation. Ignore missing ranges when deciding to revalidate. If the | 675 // revalidation. Ignore missing ranges when deciding to revalidate. If the |
674 // entry requires revalidation, ignore the whole request and go to full pass | 676 // entry requires revalidation, ignore the whole request and go to full pass |
675 // through (the result of the HEAD request will NOT update the entry). | 677 // through (the result of the HEAD request will NOT update the entry). |
676 // | 678 // |
677 // Start(): Basically the same as example 7, as we never create a partial_ | 679 // Start(): Basically the same as example 7, as we never create a partial_ |
678 // object for this request. | 680 // object for this request. |
679 // | 681 // |
680 // 11. Prefetch, not-cached entry: | 682 // 11. Prefetch, not-cached entry: |
681 // The same as example 1. The "unused_since_prefetch" bit is stored as true in | 683 // The same as example 1. The "unused_since_prefetch" bit is stored as true in |
682 // UpdateCachedResponse. | 684 // UpdateCachedResponse. |
683 // | 685 // |
684 // 12. Prefetch, cached entry: | 686 // 12. Prefetch, cached entry: |
685 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between | 687 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between |
686 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch | 688 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch |
687 // bit is unset. | 689 // bit is unset. |
688 // | 690 // |
689 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true: | 691 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true: |
690 // Skip validation, similar to example 2. | 692 // Skip validation, similar to example 2. |
691 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 693 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
692 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation -> | 694 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation -> |
693 // BeginPartialCacheValidation() -> BeginCacheValidation() -> | 695 // BeginPartialCacheValidation() -> BeginCacheValidation() -> |
694 // SetupEntryForRead() | 696 // SetupEntryForRead() -> FinishHeaders* |
695 // | 697 // |
696 // Read(): | 698 // Read(): |
697 // CacheReadData* | 699 // CacheReadData* |
698 // | 700 // |
699 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: | 701 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: |
700 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between | 702 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between |
701 // CacheReadResponse* and CacheDispatchValidation. | 703 // CacheReadResponse* and CacheDispatchValidation. |
702 int HttpCache::Transaction::DoLoop(int result) { | 704 int HttpCache::Transaction::DoLoop(int result) { |
703 DCHECK_NE(STATE_UNSET, next_state_); | 705 DCHECK_NE(STATE_UNSET, next_state_); |
704 DCHECK_NE(STATE_NONE, next_state_); | 706 DCHECK_NE(STATE_NONE, next_state_); |
705 DCHECK(!in_do_loop_); | 707 DCHECK(!in_do_loop_); |
706 | 708 |
707 int rv = result; | 709 int rv = result; |
710 State state = next_state_; | |
708 do { | 711 do { |
709 State state = next_state_; | 712 state = next_state_; |
710 next_state_ = STATE_UNSET; | 713 next_state_ = STATE_UNSET; |
711 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true); | 714 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true); |
712 | 715 |
713 switch (state) { | 716 switch (state) { |
714 case STATE_GET_BACKEND: | 717 case STATE_GET_BACKEND: |
715 DCHECK_EQ(OK, rv); | 718 DCHECK_EQ(OK, rv); |
716 rv = DoGetBackend(); | 719 rv = DoGetBackend(); |
717 break; | 720 break; |
718 case STATE_GET_BACKEND_COMPLETE: | 721 case STATE_GET_BACKEND_COMPLETE: |
719 rv = DoGetBackendComplete(rv); | 722 rv = DoGetBackendComplete(rv); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
836 DCHECK_EQ(OK, rv); | 839 DCHECK_EQ(OK, rv); |
837 rv = DoPartialHeadersReceived(); | 840 rv = DoPartialHeadersReceived(); |
838 break; | 841 break; |
839 case STATE_CACHE_READ_METADATA: | 842 case STATE_CACHE_READ_METADATA: |
840 DCHECK_EQ(OK, rv); | 843 DCHECK_EQ(OK, rv); |
841 rv = DoCacheReadMetadata(); | 844 rv = DoCacheReadMetadata(); |
842 break; | 845 break; |
843 case STATE_CACHE_READ_METADATA_COMPLETE: | 846 case STATE_CACHE_READ_METADATA_COMPLETE: |
844 rv = DoCacheReadMetadataComplete(rv); | 847 rv = DoCacheReadMetadataComplete(rv); |
845 break; | 848 break; |
849 case STATE_HEADERS_PHASE_CANNOT_PROCEED: | |
850 rv = DoHeadersPhaseCannotProceed(); | |
851 break; | |
852 case STATE_FINISH_HEADERS: | |
853 rv = DoFinishHeaders(rv); | |
854 break; | |
855 case STATE_FINISH_HEADERS_COMPLETE: | |
856 rv = DoFinishHeadersComplete(rv); | |
857 break; | |
846 case STATE_NETWORK_READ: | 858 case STATE_NETWORK_READ: |
847 DCHECK_EQ(OK, rv); | 859 DCHECK_EQ(OK, rv); |
848 rv = DoNetworkRead(); | 860 rv = DoNetworkRead(); |
849 break; | 861 break; |
850 case STATE_NETWORK_READ_COMPLETE: | 862 case STATE_NETWORK_READ_COMPLETE: |
851 rv = DoNetworkReadComplete(rv); | 863 rv = DoNetworkReadComplete(rv); |
852 break; | 864 break; |
853 case STATE_CACHE_READ_DATA: | 865 case STATE_CACHE_READ_DATA: |
854 DCHECK_EQ(OK, rv); | 866 DCHECK_EQ(OK, rv); |
855 rv = DoCacheReadData(); | 867 rv = DoCacheReadData(); |
(...skipping 16 matching lines...) Expand all Loading... | |
872 break; | 884 break; |
873 default: | 885 default: |
874 NOTREACHED() << "bad state " << state; | 886 NOTREACHED() << "bad state " << state; |
875 rv = ERR_FAILED; | 887 rv = ERR_FAILED; |
876 break; | 888 break; |
877 } | 889 } |
878 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state; | 890 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state; |
879 | 891 |
880 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 892 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
881 | 893 |
894 // Assert Start() state machine's allowed last state in successful cases when | |
895 // caching is happening. | |
896 DCHECK(reading_ || rv != OK || !entry_ || | |
897 state == STATE_FINISH_HEADERS_COMPLETE); | |
898 | |
882 if (rv != ERR_IO_PENDING && !callback_.is_null()) { | 899 if (rv != ERR_IO_PENDING && !callback_.is_null()) { |
883 read_buf_ = NULL; // Release the buffer before invoking the callback. | 900 read_buf_ = nullptr; // Release the buffer before invoking the callback. |
884 base::ResetAndReturn(&callback_).Run(rv); | 901 base::ResetAndReturn(&callback_).Run(rv); |
885 } | 902 } |
886 | 903 |
887 return rv; | 904 return rv; |
888 } | 905 } |
889 | 906 |
890 int HttpCache::Transaction::DoGetBackend() { | 907 int HttpCache::Transaction::DoGetBackend() { |
891 cache_pending_ = true; | 908 cache_pending_ = true; |
892 TransitionToState(STATE_GET_BACKEND_COMPLETE); | 909 TransitionToState(STATE_GET_BACKEND_COMPLETE); |
893 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); | 910 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); |
894 return cache_->GetBackendForTransaction(this); | 911 return cache_->GetBackendForTransaction(this); |
895 } | 912 } |
896 | 913 |
897 int HttpCache::Transaction::DoGetBackendComplete(int result) { | 914 int HttpCache::Transaction::DoGetBackendComplete(int result) { |
898 DCHECK(result == OK || result == ERR_FAILED); | 915 DCHECK(result == OK || result == ERR_FAILED); |
899 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND, | 916 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND, |
900 result); | 917 result); |
901 cache_pending_ = false; | 918 cache_pending_ = false; |
902 | 919 |
903 if (!ShouldPassThrough()) { | 920 if (!ShouldPassThrough()) { |
904 cache_key_ = cache_->GenerateCacheKey(request_); | 921 cache_key_ = cache_->GenerateCacheKey(request_); |
905 | 922 |
906 // Requested cache access mode. | 923 // Requested cache access mode. |
907 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { | 924 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { |
908 if (effective_load_flags_ & LOAD_BYPASS_CACHE) { | 925 if (effective_load_flags_ & LOAD_BYPASS_CACHE) { |
909 // The client has asked for nonsense. | 926 // The client has asked for nonsense. |
910 TransitionToState(STATE_NONE); | 927 TransitionToState(STATE_FINISH_HEADERS); |
911 return ERR_CACHE_MISS; | 928 return ERR_CACHE_MISS; |
912 } | 929 } |
913 mode_ = READ; | 930 mode_ = READ; |
914 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) { | 931 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) { |
915 mode_ = WRITE; | 932 mode_ = WRITE; |
916 } else { | 933 } else { |
917 mode_ = READ_WRITE; | 934 mode_ = READ_WRITE; |
918 } | 935 } |
919 | 936 |
920 // Downgrade to UPDATE if the request has been externally conditionalized. | 937 // Downgrade to UPDATE if the request has been externally conditionalized. |
921 if (external_validation_.initialized) { | 938 if (external_validation_.initialized) { |
922 if (mode_ & WRITE) { | 939 if (mode_ & WRITE) { |
923 // Strip off the READ_DATA bit (and maybe add back a READ_META bit | 940 // Strip off the READ_DATA bit (and maybe add back a READ_META bit |
924 // in case READ was off). | 941 // in case READ was off). |
925 mode_ = UPDATE; | 942 mode_ = UPDATE; |
926 } else { | 943 } else { |
927 mode_ = NONE; | 944 mode_ = NONE; |
928 } | 945 } |
929 } | 946 } |
930 } | 947 } |
931 | 948 |
932 // Use PUT and DELETE only to invalidate existing stored entries. | 949 // Use PUT and DELETE only to invalidate existing stored entries. |
933 if ((request_->method == "PUT" || request_->method == "DELETE") && | 950 if ((method_ == "PUT" || method_ == "DELETE") && mode_ != READ_WRITE && |
934 mode_ != READ_WRITE && mode_ != WRITE) { | 951 mode_ != WRITE) { |
935 mode_ = NONE; | 952 mode_ = NONE; |
936 } | 953 } |
937 | 954 |
938 // Note that if mode_ == UPDATE (which is tied to external_validation_), the | 955 // Note that if mode_ == UPDATE (which is tied to external_validation_), the |
939 // transaction behaves the same for GET and HEAD requests at this point: if it | 956 // transaction behaves the same for GET and HEAD requests at this point: if it |
940 // was not modified, the entry is updated and a response is not returned from | 957 // was not modified, the entry is updated and a response is not returned from |
941 // the cache. If we receive 200, it doesn't matter if there was a validation | 958 // the cache. If we receive 200, it doesn't matter if there was a validation |
942 // header or not. | 959 // header or not. |
943 if (request_->method == "HEAD" && mode_ == WRITE) | 960 if (method_ == "HEAD" && mode_ == WRITE) |
944 mode_ = NONE; | 961 mode_ = NONE; |
945 | 962 |
946 // If must use cache, then we must fail. This can happen for back/forward | 963 // If must use cache, then we must fail. This can happen for back/forward |
947 // navigations to a page generated via a form post. | 964 // navigations to a page generated via a form post. |
948 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { | 965 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { |
949 TransitionToState(STATE_NONE); | 966 TransitionToState(STATE_FINISH_HEADERS); |
950 return ERR_CACHE_MISS; | 967 return ERR_CACHE_MISS; |
951 } | 968 } |
952 | 969 |
953 if (mode_ == NONE) { | 970 if (mode_ == NONE) { |
954 if (partial_) { | 971 if (partial_) { |
955 partial_->RestoreHeaders(&custom_request_->extra_headers); | 972 partial_->RestoreHeaders(&custom_request_->extra_headers); |
956 partial_.reset(); | 973 partial_.reset(); |
957 } | 974 } |
958 TransitionToState(STATE_SEND_REQUEST); | 975 TransitionToState(STATE_SEND_REQUEST); |
959 } else { | 976 } else { |
960 TransitionToState(STATE_INIT_ENTRY); | 977 TransitionToState(STATE_INIT_ENTRY); |
961 } | 978 } |
962 | 979 |
963 // This is only set if we have something to do with the response. | 980 // This is only set if we have something to do with the response. |
964 range_requested_ = (partial_.get() != NULL); | 981 range_requested_ = (partial_.get() != NULL); |
965 | 982 |
983 // mode_ may change later, save the initial mode in case we need to restart | |
984 // this request. | |
985 restart_info_.mode = mode_; | |
966 return OK; | 986 return OK; |
967 } | 987 } |
968 | 988 |
969 int HttpCache::Transaction::DoInitEntry() { | 989 int HttpCache::Transaction::DoInitEntry() { |
970 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); | 990 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); |
971 DCHECK(!new_entry_); | 991 DCHECK(!new_entry_); |
972 | 992 |
973 if (!cache_.get()) { | 993 if (!cache_.get()) { |
974 TransitionToState(STATE_NONE); | 994 TransitionToState(STATE_FINISH_HEADERS); |
975 return ERR_UNEXPECTED; | 995 return ERR_UNEXPECTED; |
976 } | 996 } |
977 | 997 |
978 if (mode_ == WRITE) { | 998 if (mode_ == WRITE) { |
979 TransitionToState(STATE_DOOM_ENTRY); | 999 TransitionToState(STATE_DOOM_ENTRY); |
980 return OK; | 1000 return OK; |
981 } | 1001 } |
982 | 1002 |
983 TransitionToState(STATE_OPEN_ENTRY); | 1003 TransitionToState(STATE_OPEN_ENTRY); |
984 return OK; | 1004 return OK; |
(...skipping 16 matching lines...) Expand all Loading... | |
1001 // transaction attached. | 1021 // transaction attached. |
1002 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_OPEN_ENTRY, | 1022 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_OPEN_ENTRY, |
1003 result); | 1023 result); |
1004 cache_pending_ = false; | 1024 cache_pending_ = false; |
1005 if (result == OK) { | 1025 if (result == OK) { |
1006 TransitionToState(STATE_ADD_TO_ENTRY); | 1026 TransitionToState(STATE_ADD_TO_ENTRY); |
1007 return OK; | 1027 return OK; |
1008 } | 1028 } |
1009 | 1029 |
1010 if (result == ERR_CACHE_RACE) { | 1030 if (result == ERR_CACHE_RACE) { |
1011 TransitionToState(STATE_INIT_ENTRY); | 1031 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED); |
1012 return OK; | 1032 return OK; |
1013 } | 1033 } |
1014 | 1034 |
1015 if (request_->method == "PUT" || request_->method == "DELETE" || | 1035 if (method_ == "PUT" || method_ == "DELETE" || |
1016 (request_->method == "HEAD" && mode_ == READ_WRITE)) { | 1036 (method_ == "HEAD" && mode_ == READ_WRITE)) { |
1017 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD"); | 1037 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || method_ == "HEAD"); |
1018 mode_ = NONE; | 1038 mode_ = NONE; |
1019 TransitionToState(STATE_SEND_REQUEST); | 1039 TransitionToState(STATE_SEND_REQUEST); |
1020 return OK; | 1040 return OK; |
1021 } | 1041 } |
1022 | 1042 |
1023 if (mode_ == READ_WRITE) { | 1043 if (mode_ == READ_WRITE) { |
1024 mode_ = WRITE; | 1044 mode_ = WRITE; |
1025 TransitionToState(STATE_CREATE_ENTRY); | 1045 TransitionToState(STATE_CREATE_ENTRY); |
1026 return OK; | 1046 return OK; |
1027 } | 1047 } |
1028 if (mode_ == UPDATE) { | 1048 if (mode_ == UPDATE) { |
1029 // There is no cache entry to update; proceed without caching. | 1049 // There is no cache entry to update; proceed without caching. |
1030 mode_ = NONE; | 1050 mode_ = NONE; |
1031 TransitionToState(STATE_SEND_REQUEST); | 1051 TransitionToState(STATE_SEND_REQUEST); |
1032 return OK; | 1052 return OK; |
1033 } | 1053 } |
1034 | 1054 |
1035 // The entry does not exist, and we are not permitted to create a new entry, | 1055 // The entry does not exist, and we are not permitted to create a new entry, |
1036 // so we must fail. | 1056 // so we must fail. |
1037 TransitionToState(STATE_NONE); | 1057 TransitionToState(STATE_FINISH_HEADERS); |
1038 return ERR_CACHE_MISS; | 1058 return ERR_CACHE_MISS; |
1039 } | 1059 } |
1040 | 1060 |
1041 int HttpCache::Transaction::DoDoomEntry() { | 1061 int HttpCache::Transaction::DoDoomEntry() { |
1042 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntry"); | 1062 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntry"); |
1043 TransitionToState(STATE_DOOM_ENTRY_COMPLETE); | 1063 TransitionToState(STATE_DOOM_ENTRY_COMPLETE); |
1044 cache_pending_ = true; | 1064 cache_pending_ = true; |
1045 if (first_cache_access_since_.is_null()) | 1065 if (first_cache_access_since_.is_null()) |
1046 first_cache_access_since_ = TimeTicks::Now(); | 1066 first_cache_access_since_ = TimeTicks::Now(); |
1047 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_DOOM_ENTRY); | 1067 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_DOOM_ENTRY); |
1048 return cache_->DoomEntry(cache_key_, this); | 1068 return cache_->DoomEntry(cache_key_, this); |
1049 } | 1069 } |
1050 | 1070 |
1051 int HttpCache::Transaction::DoDoomEntryComplete(int result) { | 1071 int HttpCache::Transaction::DoDoomEntryComplete(int result) { |
1052 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntryComplete"); | 1072 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntryComplete"); |
1053 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_DOOM_ENTRY, | 1073 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_DOOM_ENTRY, |
1054 result); | 1074 result); |
1055 cache_pending_ = false; | 1075 cache_pending_ = false; |
1056 TransitionToState(result == ERR_CACHE_RACE ? STATE_INIT_ENTRY | 1076 TransitionToState(result == ERR_CACHE_RACE |
1057 : STATE_CREATE_ENTRY); | 1077 ? STATE_HEADERS_PHASE_CANNOT_PROCEED |
1078 : STATE_CREATE_ENTRY); | |
1058 return OK; | 1079 return OK; |
1059 } | 1080 } |
1060 | 1081 |
1061 int HttpCache::Transaction::DoCreateEntry() { | 1082 int HttpCache::Transaction::DoCreateEntry() { |
1062 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntry"); | 1083 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntry"); |
1063 DCHECK(!new_entry_); | 1084 DCHECK(!new_entry_); |
1064 TransitionToState(STATE_CREATE_ENTRY_COMPLETE); | 1085 TransitionToState(STATE_CREATE_ENTRY_COMPLETE); |
1065 cache_pending_ = true; | 1086 cache_pending_ = true; |
1066 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_CREATE_ENTRY); | 1087 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_CREATE_ENTRY); |
1067 return cache_->CreateEntry(cache_key_, &new_entry_, this); | 1088 return cache_->CreateEntry(cache_key_, &new_entry_, this); |
1068 } | 1089 } |
1069 | 1090 |
1070 int HttpCache::Transaction::DoCreateEntryComplete(int result) { | 1091 int HttpCache::Transaction::DoCreateEntryComplete(int result) { |
1071 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete"); | 1092 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete"); |
1072 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is | 1093 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is |
1073 // OK, otherwise the cache will end up with an active entry without any | 1094 // OK, otherwise the cache will end up with an active entry without any |
1074 // transaction attached. | 1095 // transaction attached. |
1075 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY, | 1096 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY, |
1076 result); | 1097 result); |
1077 cache_pending_ = false; | 1098 cache_pending_ = false; |
1078 switch (result) { | 1099 switch (result) { |
1079 case OK: | 1100 case OK: |
1080 TransitionToState(STATE_ADD_TO_ENTRY); | 1101 TransitionToState(STATE_ADD_TO_ENTRY); |
1081 break; | 1102 break; |
1082 | 1103 |
1083 case ERR_CACHE_RACE: | 1104 case ERR_CACHE_RACE: |
1084 TransitionToState(STATE_INIT_ENTRY); | 1105 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED); |
1085 break; | 1106 break; |
1086 | 1107 |
1087 default: | 1108 default: |
1088 // We have a race here: Maybe we failed to open the entry and decided to | 1109 // We have a race here: Maybe we failed to open the entry and decided to |
1089 // create one, but by the time we called create, another transaction | 1110 // create one, but by the time we called create, another transaction |
1090 // already created the entry. If we want to eliminate this issue, we | 1111 // already created the entry. If we want to eliminate this issue, we |
1091 // need an atomic OpenOrCreate() method exposed by the disk cache. | 1112 // need an atomic OpenOrCreate() method exposed by the disk cache. |
1092 DLOG(WARNING) << "Unable to create cache entry"; | 1113 DLOG(WARNING) << "Unable to create cache entry"; |
1093 mode_ = NONE; | 1114 mode_ = NONE; |
1094 if (partial_) | 1115 if (partial_) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1155 DCHECK(new_entry_); | 1176 DCHECK(new_entry_); |
1156 cache_pending_ = false; | 1177 cache_pending_ = false; |
1157 | 1178 |
1158 if (result == OK) | 1179 if (result == OK) |
1159 entry_ = new_entry_; | 1180 entry_ = new_entry_; |
1160 | 1181 |
1161 // If there is a failure, the cache should have taken care of new_entry_. | 1182 // If there is a failure, the cache should have taken care of new_entry_. |
1162 new_entry_ = NULL; | 1183 new_entry_ = NULL; |
1163 | 1184 |
1164 if (result == ERR_CACHE_RACE) { | 1185 if (result == ERR_CACHE_RACE) { |
1165 TransitionToState(STATE_INIT_ENTRY); | 1186 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED); |
1166 return OK; | 1187 return OK; |
1167 } | 1188 } |
1168 | 1189 |
1169 if (result == ERR_CACHE_LOCK_TIMEOUT) { | 1190 if (result == ERR_CACHE_LOCK_TIMEOUT) { |
1170 if (mode_ == READ) { | 1191 if (mode_ == READ) { |
1171 TransitionToState(STATE_NONE); | 1192 TransitionToState(STATE_FINISH_HEADERS); |
1172 return ERR_CACHE_MISS; | 1193 return ERR_CACHE_MISS; |
1173 } | 1194 } |
1174 | 1195 |
1175 // The cache is busy, bypass it for this transaction. | 1196 // The cache is busy, bypass it for this transaction. |
1176 mode_ = NONE; | 1197 mode_ = NONE; |
1177 TransitionToState(STATE_SEND_REQUEST); | 1198 TransitionToState(STATE_SEND_REQUEST); |
1178 if (partial_) { | 1199 if (partial_) { |
1179 partial_->RestoreHeaders(&custom_request_->extra_headers); | 1200 partial_->RestoreHeaders(&custom_request_->extra_headers); |
1180 partial_.reset(); | 1201 partial_.reset(); |
1181 } | 1202 } |
1182 return OK; | 1203 return OK; |
1183 } | 1204 } |
1184 | 1205 |
1185 open_entry_last_used_ = entry_->disk_entry->GetLastUsed(); | 1206 // TODO(crbug.com/713354) Access timestamp for histograms only if entry is |
1207 // already written, to avoid data race since cache thread can also access | |
1208 // this. | |
1209 if (!cache_->IsWritingInProgress(entry_)) | |
1210 open_entry_last_used_ = entry_->disk_entry->GetLastUsed(); | |
1186 | 1211 |
1187 // TODO(jkarlin): We should either handle the case or DCHECK. | 1212 // TODO(jkarlin): We should either handle the case or DCHECK. |
1188 if (result != OK) { | 1213 if (result != OK) { |
1189 NOTREACHED(); | 1214 NOTREACHED(); |
1190 TransitionToState(STATE_NONE); | 1215 TransitionToState(STATE_FINISH_HEADERS); |
1191 return result; | 1216 return result; |
1192 } | 1217 } |
1193 | 1218 |
1194 if (mode_ == WRITE) { | 1219 if (mode_ == WRITE) { |
1195 if (partial_) | 1220 if (partial_) |
1196 partial_->RestoreHeaders(&custom_request_->extra_headers); | 1221 partial_->RestoreHeaders(&custom_request_->extra_headers); |
1197 TransitionToState(STATE_SEND_REQUEST); | 1222 TransitionToState(STATE_SEND_REQUEST); |
1198 } else { | 1223 } else { |
1199 // We have to read the headers from the cached entry. | 1224 // We have to read the headers from the cached entry. |
1200 DCHECK(mode_ & READ_META); | 1225 DCHECK(mode_ & READ_META); |
(...skipping 18 matching lines...) Expand all Loading... | |
1219 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { | 1244 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
1220 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadResponseComplete"); | 1245 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadResponseComplete"); |
1221 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, | 1246 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, |
1222 result); | 1247 result); |
1223 if (result != io_buf_len_ || | 1248 if (result != io_buf_len_ || |
1224 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, &response_, | 1249 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, &response_, |
1225 &truncated_)) { | 1250 &truncated_)) { |
1226 return OnCacheReadError(result, true); | 1251 return OnCacheReadError(result, true); |
1227 } | 1252 } |
1228 | 1253 |
1229 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 1254 // TODO(crbug.com/713354) Only get data size if there is no other transaction |
1230 int64_t full_response_length = response_.headers->GetContentLength(); | 1255 // currently writing the response body due to the data race mentioned in the |
1256 // associated bug. | |
1257 if (!cache_->IsWritingInProgress(entry_)) { | |
1258 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | |
1259 int64_t full_response_length = response_.headers->GetContentLength(); | |
1231 | 1260 |
1232 // Some resources may have slipped in as truncated when they're not. | 1261 // Some resources may have slipped in as truncated when they're not. |
1233 if (full_response_length == current_size) | 1262 if (full_response_length == current_size) |
1234 truncated_ = false; | 1263 truncated_ = false; |
1235 | 1264 |
1236 // The state machine's handling of StopCaching unfortunately doesn't deal well | 1265 // The state machine's handling of StopCaching unfortunately doesn't deal |
1237 // with resources that are larger than 2GB when there is a truncated or sparse | 1266 // well with resources that are larger than 2GB when there is a truncated or |
1238 // cache entry. While the state machine is reworked to resolve this, the | 1267 // sparse cache entry. While the state machine is reworked to resolve this, |
1239 // following logic is put in place to defer such requests to the network. The | 1268 // the following logic is put in place to defer such requests to the |
1240 // cache should not be storing multi gigabyte resources. See | 1269 // network. The cache should not be storing multi gigabyte resources. See |
1241 // http://crbug.com/89567. | 1270 // http://crbug.com/89567. |
1242 if ((truncated_ || response_.headers->response_code() == 206) && | 1271 if ((truncated_ || response_.headers->response_code() == 206) && |
1243 !range_requested_ && | 1272 !range_requested_ && |
1244 full_response_length > std::numeric_limits<int32_t>::max()) { | 1273 full_response_length > std::numeric_limits<int32_t>::max()) { |
1245 // Does not release the cache entry. If another transaction wants to use | 1274 DCHECK(!partial_); |
1246 // this cache entry while this transaction is active, the second transaction | 1275 |
1247 // will fall back to the network after the timeout. | 1276 // Doom the entry so that no other transaction gets added to this entry |
1248 DCHECK(!partial_); | 1277 // and avoid a race of not being able to check this condition because |
1249 mode_ = NONE; | 1278 // writing is in progress. |
1250 TransitionToState(STATE_SEND_REQUEST); | 1279 cache_->DoneWritingToEntry(entry_, false, this); |
1251 return OK; | 1280 entry_ = nullptr; |
1281 mode_ = NONE; | |
1282 TransitionToState(STATE_SEND_REQUEST); | |
1283 return OK; | |
1284 } | |
1252 } | 1285 } |
1253 | 1286 |
1254 if (response_.unused_since_prefetch != | 1287 if (response_.unused_since_prefetch != |
1255 !!(request_->load_flags & LOAD_PREFETCH)) { | 1288 !!(request_->load_flags & LOAD_PREFETCH)) { |
1256 // Either this is the first use of an entry since it was prefetched XOR | 1289 // Either this is the first use of an entry since it was prefetched XOR |
1257 // this is a prefetch. The value of response.unused_since_prefetch is | 1290 // this is a prefetch. The value of response.unused_since_prefetch is |
1258 // valid for this transaction but the bit needs to be flipped in storage. | 1291 // valid for this transaction but the bit needs to be flipped in storage. |
1259 TransitionToState(STATE_TOGGLE_UNUSED_SINCE_PREFETCH); | 1292 TransitionToState(STATE_TOGGLE_UNUSED_SINCE_PREFETCH); |
1260 return OK; | 1293 return OK; |
1261 } | 1294 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1323 } | 1356 } |
1324 | 1357 |
1325 int HttpCache::Transaction::DoCacheQueryData() { | 1358 int HttpCache::Transaction::DoCacheQueryData() { |
1326 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE); | 1359 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE); |
1327 return entry_->disk_entry->ReadyForSparseIO(io_callback_); | 1360 return entry_->disk_entry->ReadyForSparseIO(io_callback_); |
1328 } | 1361 } |
1329 | 1362 |
1330 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { | 1363 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { |
1331 DCHECK_EQ(OK, result); | 1364 DCHECK_EQ(OK, result); |
1332 if (!cache_.get()) { | 1365 if (!cache_.get()) { |
1333 TransitionToState(STATE_NONE); | 1366 TransitionToState(STATE_FINISH_HEADERS); |
1334 return ERR_UNEXPECTED; | 1367 return ERR_UNEXPECTED; |
1335 } | 1368 } |
1336 | 1369 |
1337 return ValidateEntryHeadersAndContinue(); | 1370 return ValidateEntryHeadersAndContinue(); |
1338 } | 1371 } |
1339 | 1372 |
1340 // We may end up here multiple times for a given request. | 1373 // We may end up here multiple times for a given request. |
1341 int HttpCache::Transaction::DoStartPartialCacheValidation() { | 1374 int HttpCache::Transaction::DoStartPartialCacheValidation() { |
1342 if (mode_ == NONE) { | 1375 if (mode_ == NONE) { |
1343 TransitionToState(STATE_NONE); | 1376 TransitionToState(STATE_FINISH_HEADERS); |
1344 return OK; | 1377 return OK; |
1345 } | 1378 } |
1346 | 1379 |
1347 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); | 1380 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); |
1348 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); | 1381 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); |
1349 } | 1382 } |
1350 | 1383 |
1351 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { | 1384 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { |
1352 if (!result) { | 1385 if (!result) { |
1353 // This is the end of the request. | 1386 // This is the end of the request. |
1354 if (mode_ & WRITE) { | 1387 if (mode_ & WRITE) { |
1355 DoneWritingToEntry(true); | 1388 DoneWritingToEntry(true); |
1356 } else { | 1389 } else { |
1357 cache_->DoneReadingFromEntry(entry_, this); | 1390 cache_->DoneReadingFromEntry(entry_, this); |
1358 entry_ = NULL; | 1391 entry_ = NULL; |
1359 } | 1392 } |
1360 TransitionToState(STATE_NONE); | 1393 TransitionToState(STATE_FINISH_HEADERS); |
1361 return result; | 1394 return result; |
1362 } | 1395 } |
1363 | 1396 |
1364 if (result < 0) { | 1397 if (result < 0) { |
1365 TransitionToState(STATE_NONE); | 1398 TransitionToState(STATE_FINISH_HEADERS); |
1366 return result; | 1399 return result; |
1367 } | 1400 } |
1368 | 1401 |
1369 partial_->PrepareCacheValidation(entry_->disk_entry, | 1402 partial_->PrepareCacheValidation(entry_->disk_entry, |
1370 &custom_request_->extra_headers); | 1403 &custom_request_->extra_headers); |
1371 | 1404 |
1372 if (reading_ && partial_->IsCurrentRangeCached()) { | 1405 if (reading_ && partial_->IsCurrentRangeCached()) { |
1373 TransitionToState(STATE_CACHE_READ_DATA); | 1406 TransitionToState(STATE_CACHE_READ_DATA); |
1374 return OK; | 1407 return OK; |
1375 } | 1408 } |
1376 | 1409 |
1377 return BeginCacheValidation(); | 1410 return BeginCacheValidation(); |
1378 } | 1411 } |
1379 | 1412 |
1380 int HttpCache::Transaction::DoSendRequest() { | 1413 int HttpCache::Transaction::DoSendRequest() { |
1381 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest"); | 1414 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest"); |
1382 DCHECK(mode_ & WRITE || mode_ == NONE); | 1415 DCHECK(mode_ & WRITE || mode_ == NONE); |
1383 DCHECK(!network_trans_.get()); | 1416 DCHECK(!network_trans_.get()); |
1384 | 1417 |
1385 send_request_since_ = TimeTicks::Now(); | 1418 send_request_since_ = TimeTicks::Now(); |
1386 | 1419 |
1387 // Create a network transaction. | 1420 // Create a network transaction. |
1388 int rv = | 1421 int rv = |
1389 cache_->network_layer_->CreateTransaction(priority_, &network_trans_); | 1422 cache_->network_layer_->CreateTransaction(priority_, &network_trans_); |
1390 if (rv != OK) { | 1423 if (rv != OK) { |
1391 TransitionToState(STATE_NONE); | 1424 TransitionToState(STATE_FINISH_HEADERS); |
1392 return rv; | 1425 return rv; |
1393 } | 1426 } |
1394 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_); | 1427 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_); |
1395 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_); | 1428 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_); |
1396 | 1429 |
1397 // Old load timing information, if any, is now obsolete. | 1430 // Old load timing information, if any, is now obsolete. |
1398 old_network_trans_load_timing_.reset(); | 1431 old_network_trans_load_timing_.reset(); |
1399 old_remote_endpoint_ = IPEndPoint(); | 1432 old_remote_endpoint_ = IPEndPoint(); |
1400 | 1433 |
1401 if (websocket_handshake_stream_base_create_helper_) | 1434 if (websocket_handshake_stream_base_create_helper_) |
1402 network_trans_->SetWebSocketHandshakeStreamCreateHelper( | 1435 network_trans_->SetWebSocketHandshakeStreamCreateHelper( |
1403 websocket_handshake_stream_base_create_helper_); | 1436 websocket_handshake_stream_base_create_helper_); |
1404 | 1437 |
1405 TransitionToState(STATE_SEND_REQUEST_COMPLETE); | 1438 TransitionToState(STATE_SEND_REQUEST_COMPLETE); |
1406 rv = network_trans_->Start(request_, io_callback_, net_log_); | 1439 rv = network_trans_->Start(request_, io_callback_, net_log_); |
1407 return rv; | 1440 return rv; |
1408 } | 1441 } |
1409 | 1442 |
1410 int HttpCache::Transaction::DoSendRequestComplete(int result) { | 1443 int HttpCache::Transaction::DoSendRequestComplete(int result) { |
1411 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete"); | 1444 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete"); |
1412 if (!cache_.get()) { | 1445 if (!cache_.get()) { |
1413 TransitionToState(STATE_NONE); | 1446 TransitionToState(STATE_FINISH_HEADERS); |
1414 return ERR_UNEXPECTED; | 1447 return ERR_UNEXPECTED; |
1415 } | 1448 } |
1416 | 1449 |
1417 // If we tried to conditionalize the request and failed, we know | 1450 // If we tried to conditionalize the request and failed, we know |
1418 // we won't be reading from the cache after this point. | 1451 // we won't be reading from the cache after this point. |
1419 if (couldnt_conditionalize_request_) | 1452 if (couldnt_conditionalize_request_) |
1420 mode_ = WRITE; | 1453 mode_ = WRITE; |
1421 | 1454 |
1422 if (result == OK) { | 1455 if (result == OK) { |
1423 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST); | 1456 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST); |
(...skipping 10 matching lines...) Expand all Loading... | |
1434 // so GetResponseInfo() should never return NULL here. | 1467 // so GetResponseInfo() should never return NULL here. |
1435 DCHECK(response); | 1468 DCHECK(response); |
1436 response_.ssl_info = response->ssl_info; | 1469 response_.ssl_info = response->ssl_info; |
1437 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { | 1470 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
1438 DCHECK(response); | 1471 DCHECK(response); |
1439 response_.cert_request_info = response->cert_request_info; | 1472 response_.cert_request_info = response->cert_request_info; |
1440 } else if (response_.was_cached) { | 1473 } else if (response_.was_cached) { |
1441 DoneWritingToEntry(true); | 1474 DoneWritingToEntry(true); |
1442 } | 1475 } |
1443 | 1476 |
1444 TransitionToState(STATE_NONE); | 1477 TransitionToState(STATE_FINISH_HEADERS); |
1445 return result; | 1478 return result; |
1446 } | 1479 } |
1447 | 1480 |
1448 // We received the response headers and there is no error. | 1481 // We received the response headers and there is no error. |
1449 int HttpCache::Transaction::DoSuccessfulSendRequest() { | 1482 int HttpCache::Transaction::DoSuccessfulSendRequest() { |
1450 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); | 1483 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); |
1451 DCHECK(!new_response_); | 1484 DCHECK(!new_response_); |
1452 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); | 1485 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); |
1453 | 1486 |
1454 if (new_response->headers->response_code() == 401 || | 1487 if (new_response->headers->response_code() == 401 || |
1455 new_response->headers->response_code() == 407) { | 1488 new_response->headers->response_code() == 407) { |
1456 SetAuthResponse(*new_response); | 1489 SetAuthResponse(*new_response); |
1457 if (!reading_) { | 1490 if (!reading_) { |
1458 TransitionToState(STATE_NONE); | 1491 TransitionToState(STATE_FINISH_HEADERS); |
1459 return OK; | 1492 return OK; |
1460 } | 1493 } |
1461 | 1494 |
1462 // We initiated a second request the caller doesn't know about. We should be | 1495 // We initiated a second request the caller doesn't know about. We should be |
1463 // able to authenticate this request because we should have authenticated | 1496 // able to authenticate this request because we should have authenticated |
1464 // this URL moments ago. | 1497 // this URL moments ago. |
1465 if (IsReadyToRestartForAuth()) { | 1498 if (IsReadyToRestartForAuth()) { |
1466 DCHECK(!response_.auth_challenge.get()); | 1499 DCHECK(!response_.auth_challenge.get()); |
1467 TransitionToState(STATE_SEND_REQUEST_COMPLETE); | 1500 TransitionToState(STATE_SEND_REQUEST_COMPLETE); |
1468 // In theory we should check to see if there are new cookies, but there | 1501 // In theory we should check to see if there are new cookies, but there |
1469 // is no way to do that from here. | 1502 // is no way to do that from here. |
1470 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_); | 1503 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_); |
1471 } | 1504 } |
1472 | 1505 |
1473 // We have to perform cleanup at this point so that at least the next | 1506 // We have to perform cleanup at this point so that at least the next |
1474 // request can succeed. We do not retry at this point, because data | 1507 // request can succeed. We do not retry at this point, because data |
1475 // has been read and we have no way to gather credentials. We would | 1508 // has been read and we have no way to gather credentials. We would |
1476 // fail again, and potentially loop. This can happen if the credentials | 1509 // fail again, and potentially loop. This can happen if the credentials |
1477 // expire while chrome is suspended. | 1510 // expire while chrome is suspended. |
1478 if (entry_) | 1511 if (entry_) |
1479 DoomPartialEntry(false); | 1512 DoomPartialEntry(false); |
1480 mode_ = NONE; | 1513 mode_ = NONE; |
1481 partial_.reset(); | 1514 partial_.reset(); |
1482 ResetNetworkTransaction(); | 1515 ResetNetworkTransaction(); |
1483 TransitionToState(STATE_NONE); | 1516 TransitionToState(STATE_FINISH_HEADERS); |
1484 return ERR_CACHE_AUTH_FAILURE_AFTER_READ; | 1517 return ERR_CACHE_AUTH_FAILURE_AFTER_READ; |
1485 } | 1518 } |
1486 | 1519 |
1487 new_response_ = new_response; | 1520 new_response_ = new_response; |
1488 if (!ValidatePartialResponse() && !auth_response_.headers.get()) { | 1521 if (!ValidatePartialResponse() && !auth_response_.headers.get()) { |
1489 // Something went wrong with this request and we have to restart it. | 1522 // Something went wrong with this request and we have to restart it. |
1490 // If we have an authentication response, we are exposed to weird things | 1523 // If we have an authentication response, we are exposed to weird things |
1491 // hapenning if the user cancels the authentication before we receive | 1524 // hapenning if the user cancels the authentication before we receive |
1492 // the new response. | 1525 // the new response. |
1493 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST); | 1526 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST); |
(...skipping 11 matching lines...) Expand all Loading... | |
1505 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); | 1538 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); |
1506 DoneWritingToEntry(false); | 1539 DoneWritingToEntry(false); |
1507 } | 1540 } |
1508 | 1541 |
1509 if (mode_ == WRITE && | 1542 if (mode_ == WRITE && |
1510 cache_entry_status_ != CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) { | 1543 cache_entry_status_ != CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) { |
1511 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); | 1544 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); |
1512 } | 1545 } |
1513 | 1546 |
1514 // Invalidate any cached GET with a successful PUT or DELETE. | 1547 // Invalidate any cached GET with a successful PUT or DELETE. |
1515 if (mode_ == WRITE && | 1548 if (mode_ == WRITE && (method_ == "PUT" || method_ == "DELETE")) { |
1516 (request_->method == "PUT" || request_->method == "DELETE")) { | |
1517 if (NonErrorResponse(new_response->headers->response_code())) { | 1549 if (NonErrorResponse(new_response->headers->response_code())) { |
1518 int ret = cache_->DoomEntry(cache_key_, NULL); | 1550 int ret = cache_->DoomEntry(cache_key_, NULL); |
1519 DCHECK_EQ(OK, ret); | 1551 DCHECK_EQ(OK, ret); |
1520 } | 1552 } |
1521 cache_->DoneWritingToEntry(entry_, true); | 1553 cache_->DoneWritingToEntry(entry_, true, this); |
1522 entry_ = NULL; | 1554 entry_ = NULL; |
1523 mode_ = NONE; | 1555 mode_ = NONE; |
1524 } | 1556 } |
1525 | 1557 |
1526 // Invalidate any cached GET with a successful POST. | 1558 // Invalidate any cached GET with a successful POST. |
1527 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && | 1559 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && method_ == "POST" && |
1528 request_->method == "POST" && | |
1529 NonErrorResponse(new_response->headers->response_code())) { | 1560 NonErrorResponse(new_response->headers->response_code())) { |
1530 cache_->DoomMainEntryForUrl(request_->url); | 1561 cache_->DoomMainEntryForUrl(request_->url); |
1531 } | 1562 } |
1532 | 1563 |
1533 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); | 1564 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); |
1534 | 1565 |
1535 if (new_response_->headers->response_code() == 416 && | 1566 if (new_response_->headers->response_code() == 416 && |
1536 (request_->method == "GET" || request_->method == "POST")) { | 1567 (method_ == "GET" || method_ == "POST")) { |
1537 // If there is an active entry it may be destroyed with this transaction. | 1568 // If there is an active entry it may be destroyed with this transaction. |
1538 SetResponse(*new_response_); | 1569 SetResponse(*new_response_); |
1539 TransitionToState(STATE_NONE); | 1570 TransitionToState(STATE_FINISH_HEADERS); |
1540 return OK; | 1571 return OK; |
1541 } | 1572 } |
1542 | 1573 |
1543 // Are we expecting a response to a conditional query? | 1574 // Are we expecting a response to a conditional query? |
1544 if (mode_ == READ_WRITE || mode_ == UPDATE) { | 1575 if (mode_ == READ_WRITE || mode_ == UPDATE) { |
1545 if (new_response->headers->response_code() == 304 || handling_206_) { | 1576 if (new_response->headers->response_code() == 304 || handling_206_) { |
1546 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); | 1577 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); |
1547 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); | 1578 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); |
1548 return OK; | 1579 return OK; |
1549 } | 1580 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1617 DCHECK(!handling_206_); | 1648 DCHECK(!handling_206_); |
1618 // We got a "not modified" response and already updated the corresponding | 1649 // We got a "not modified" response and already updated the corresponding |
1619 // cache entry above. | 1650 // cache entry above. |
1620 // | 1651 // |
1621 // By closing the cached entry now, we make sure that the 304 rather than | 1652 // By closing the cached entry now, we make sure that the 304 rather than |
1622 // the cached 200 response, is what will be returned to the user. | 1653 // the cached 200 response, is what will be returned to the user. |
1623 DoneWritingToEntry(true); | 1654 DoneWritingToEntry(true); |
1624 } else if (entry_ && !handling_206_) { | 1655 } else if (entry_ && !handling_206_) { |
1625 DCHECK_EQ(READ_WRITE, mode_); | 1656 DCHECK_EQ(READ_WRITE, mode_); |
1626 if (!partial_ || partial_->IsLastRange()) { | 1657 if (!partial_ || partial_->IsLastRange()) { |
1627 cache_->ConvertWriterToReader(entry_); | |
1628 mode_ = READ; | 1658 mode_ = READ; |
1629 } | 1659 } |
1630 // We no longer need the network transaction, so destroy it. | 1660 // We no longer need the network transaction, so destroy it. |
1631 ResetNetworkTransaction(); | 1661 ResetNetworkTransaction(); |
1632 } else if (entry_ && handling_206_ && truncated_ && | 1662 } else if (entry_ && handling_206_ && truncated_ && |
1633 partial_->initial_validation()) { | 1663 partial_->initial_validation()) { |
1634 // We just finished the validation of a truncated entry, and the server | 1664 // We just finished the validation of a truncated entry, and the server |
1635 // is willing to resume the operation. Now we go back and start serving | 1665 // is willing to resume the operation. Now we go back and start serving |
1636 // the first part to the user. | 1666 // the first part to the user. |
1637 ResetNetworkTransaction(); | 1667 ResetNetworkTransaction(); |
(...skipping 12 matching lines...) Expand all Loading... | |
1650 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); | 1680 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); |
1651 return OK; | 1681 return OK; |
1652 } | 1682 } |
1653 | 1683 |
1654 // We change the value of Content-Length for partial content. | 1684 // We change the value of Content-Length for partial content. |
1655 if (handling_206_ && partial_) | 1685 if (handling_206_ && partial_) |
1656 partial_->FixContentLength(new_response_->headers.get()); | 1686 partial_->FixContentLength(new_response_->headers.get()); |
1657 | 1687 |
1658 SetResponse(*new_response_); | 1688 SetResponse(*new_response_); |
1659 | 1689 |
1660 if (request_->method == "HEAD") { | 1690 if (method_ == "HEAD") { |
1661 // This response is replacing the cached one. | 1691 // This response is replacing the cached one. |
1662 DoneWritingToEntry(false); | 1692 DoneWritingToEntry(false); |
1663 mode_ = NONE; | 1693 mode_ = NONE; |
1664 new_response_ = NULL; | 1694 new_response_ = NULL; |
1665 TransitionToState(STATE_NONE); | 1695 TransitionToState(STATE_FINISH_HEADERS); |
1666 return OK; | 1696 return OK; |
1667 } | 1697 } |
1668 | 1698 |
1669 if (handling_206_ && !CanResume(false)) { | 1699 if (handling_206_ && !CanResume(false)) { |
1670 // There is no point in storing this resource because it will never be used. | 1700 // There is no point in storing this resource because it will never be used. |
1671 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries. | 1701 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries. |
1672 DoneWritingToEntry(false); | 1702 DoneWritingToEntry(false); |
1673 if (partial_) | 1703 if (partial_) |
1674 partial_->FixResponseHeaders(response_.headers.get(), true); | 1704 partial_->FixResponseHeaders(response_.headers.get(), true); |
1675 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); | 1705 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); |
1676 return OK; | 1706 return OK; |
1677 } | 1707 } |
1678 | 1708 |
1679 TransitionToState(STATE_CACHE_WRITE_RESPONSE); | 1709 TransitionToState(STATE_CACHE_WRITE_RESPONSE); |
1680 return OK; | 1710 return OK; |
1681 } | 1711 } |
1682 | 1712 |
1683 int HttpCache::Transaction::DoCacheWriteResponse() { | 1713 int HttpCache::Transaction::DoCacheWriteResponse() { |
1684 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse"); | 1714 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse"); |
1685 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE); | 1715 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE); |
1716 | |
1717 // Invalidate any current entry with a successful response if this transaction | |
1718 // cannot write to this entry. This transaction then continues to read from | |
1719 // the network without writing to the backend. | |
1720 bool is_match = response_.headers->response_code() == 304; | |
1721 if (entry_ && response_.headers && | |
1722 !cache_->CanTransactionWriteResponseHeaders(entry_, this, is_match)) { | |
1723 cache_->DoneWritingToEntry(entry_, false, this); | |
1724 entry_ = nullptr; | |
1725 mode_ = NONE; | |
1726 return OK; | |
1727 } | |
1728 | |
1686 return WriteResponseInfoToEntry(truncated_); | 1729 return WriteResponseInfoToEntry(truncated_); |
1687 } | 1730 } |
1688 | 1731 |
1689 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { | 1732 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { |
1690 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete"); | 1733 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete"); |
1691 TransitionToState(STATE_TRUNCATE_CACHED_DATA); | 1734 TransitionToState(STATE_TRUNCATE_CACHED_DATA); |
1692 return OnWriteResponseInfoToEntryComplete(result); | 1735 return OnWriteResponseInfoToEntryComplete(result); |
1693 } | 1736 } |
1694 | 1737 |
1695 int HttpCache::Transaction::DoTruncateCachedData() { | 1738 int HttpCache::Transaction::DoTruncateCachedData() { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1737 } | 1780 } |
1738 | 1781 |
1739 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); | 1782 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); |
1740 return OK; | 1783 return OK; |
1741 } | 1784 } |
1742 | 1785 |
1743 int HttpCache::Transaction::DoPartialHeadersReceived() { | 1786 int HttpCache::Transaction::DoPartialHeadersReceived() { |
1744 new_response_ = NULL; | 1787 new_response_ = NULL; |
1745 | 1788 |
1746 if (!partial_) { | 1789 if (!partial_) { |
1747 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) | 1790 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) { |
1748 TransitionToState(STATE_CACHE_READ_METADATA); | 1791 TransitionToState(STATE_CACHE_READ_METADATA); |
1749 else | 1792 } else { |
1750 TransitionToState(STATE_NONE); | 1793 DCHECK(!reading_); |
1794 TransitionToState(STATE_FINISH_HEADERS); | |
1795 } | |
1751 return OK; | 1796 return OK; |
1752 } | 1797 } |
1753 | 1798 |
1754 if (reading_) { | 1799 if (reading_) { |
1755 if (network_trans_.get()) { | 1800 if (network_trans_.get()) { |
1756 TransitionToState(STATE_NETWORK_READ); | 1801 TransitionToState(STATE_NETWORK_READ); |
1757 } else { | 1802 } else { |
1758 TransitionToState(STATE_CACHE_READ_DATA); | 1803 TransitionToState(STATE_CACHE_READ_DATA); |
1759 } | 1804 } |
1760 } else if (mode_ != NONE) { | 1805 } else if (mode_ != NONE) { |
1761 // We are about to return the headers for a byte-range request to the user, | 1806 // We are about to return the headers for a byte-range request to the user, |
1762 // so let's fix them. | 1807 // so let's fix them. |
1763 partial_->FixResponseHeaders(response_.headers.get(), true); | 1808 partial_->FixResponseHeaders(response_.headers.get(), true); |
1764 TransitionToState(STATE_NONE); | 1809 TransitionToState(STATE_FINISH_HEADERS); |
1765 } else { | 1810 } else { |
1766 TransitionToState(STATE_NONE); | 1811 TransitionToState(STATE_FINISH_HEADERS); |
1767 } | 1812 } |
1768 return OK; | 1813 return OK; |
1769 } | 1814 } |
1770 | 1815 |
1816 int HttpCache::Transaction::DoHeadersPhaseCannotProceed() { | |
1817 // If its the Start state machine and it cannot proceed due to a cache | |
1818 // failure, restart this transaction. | |
1819 DCHECK(!reading_); | |
1820 TransitionToState(STATE_INIT_ENTRY); | |
1821 cache_entry_status_ = restart_info_.cache_entry_status; | |
1822 entry_ = nullptr; | |
1823 mode_ = restart_info_.mode; | |
1824 if (network_trans_) | |
1825 network_trans_.reset(); | |
1826 | |
1827 return OK; | |
1828 } | |
1829 | |
1830 int HttpCache::Transaction::DoFinishHeaders(int result) { | |
1831 if (!entry_ || result != OK) { | |
1832 TransitionToState(STATE_NONE); | |
1833 return result; | |
1834 } | |
1835 | |
1836 TransitionToState(STATE_FINISH_HEADERS_COMPLETE); | |
1837 | |
1838 // If it was an auth failure or 416, this transaction should continue to be | |
1839 // headers_transaction till consumer takes an action, so no need to do | |
1840 // anything now. | |
1841 if (auth_response_.headers.get() || | |
1842 (new_response_ && new_response_->headers && | |
1843 new_response_->headers->response_code() == 416)) | |
1844 return OK; | |
1845 | |
1846 // If the transaction needs to wait because another transaction is still | |
1847 // writing the response body, it will return ERR_IO_PENDING now and the | |
1848 // io_callback_ will be invoked when the wait is done. | |
1849 return cache_->DoneWithResponseHeaders(entry_, this, partial_ != nullptr); | |
1850 } | |
1851 | |
1852 int HttpCache::Transaction::DoFinishHeadersComplete(int rv) { | |
1853 if (rv == ERR_CACHE_RACE) { | |
1854 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED); | |
1855 return OK; | |
1856 } | |
1857 | |
1858 TransitionToState(STATE_NONE); | |
1859 return rv; | |
1860 } | |
1861 | |
1771 int HttpCache::Transaction::DoCacheReadMetadata() { | 1862 int HttpCache::Transaction::DoCacheReadMetadata() { |
1772 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); | 1863 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); |
1773 DCHECK(entry_); | 1864 DCHECK(entry_); |
1774 DCHECK(!response_.metadata.get()); | 1865 DCHECK(!response_.metadata.get()); |
1775 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); | 1866 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); |
1776 | 1867 |
1777 response_.metadata = | 1868 response_.metadata = |
1778 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); | 1869 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); |
1779 | 1870 |
1780 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); | 1871 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); |
1781 return entry_->disk_entry->ReadData(kMetadataIndex, 0, | 1872 return entry_->disk_entry->ReadData(kMetadataIndex, 0, |
1782 response_.metadata.get(), | 1873 response_.metadata.get(), |
1783 response_.metadata->size(), | 1874 response_.metadata->size(), |
1784 io_callback_); | 1875 io_callback_); |
1785 } | 1876 } |
1786 | 1877 |
1787 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { | 1878 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { |
1788 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); | 1879 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); |
1789 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, | 1880 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, |
1790 result); | 1881 result); |
1791 if (result != response_.metadata->size()) | 1882 if (result != response_.metadata->size()) |
1792 return OnCacheReadError(result, false); | 1883 return OnCacheReadError(result, false); |
1793 TransitionToState(STATE_NONE); | 1884 |
1885 TransitionToState(STATE_FINISH_HEADERS); | |
1794 return OK; | 1886 return OK; |
1795 } | 1887 } |
1796 | 1888 |
1797 int HttpCache::Transaction::DoNetworkRead() { | 1889 int HttpCache::Transaction::DoNetworkRead() { |
1798 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); | 1890 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); |
1799 TransitionToState(STATE_NETWORK_READ_COMPLETE); | 1891 TransitionToState(STATE_NETWORK_READ_COMPLETE); |
1800 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); | 1892 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); |
1801 } | 1893 } |
1802 | 1894 |
1803 int HttpCache::Transaction::DoNetworkReadComplete(int result) { | 1895 int HttpCache::Transaction::DoNetworkReadComplete(int result) { |
(...skipping 12 matching lines...) Expand all Loading... | |
1816 return result; | 1908 return result; |
1817 } | 1909 } |
1818 | 1910 |
1819 TransitionToState(STATE_CACHE_WRITE_DATA); | 1911 TransitionToState(STATE_CACHE_WRITE_DATA); |
1820 return result; | 1912 return result; |
1821 } | 1913 } |
1822 | 1914 |
1823 int HttpCache::Transaction::DoCacheReadData() { | 1915 int HttpCache::Transaction::DoCacheReadData() { |
1824 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadData"); | 1916 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadData"); |
1825 | 1917 |
1826 if (request_->method == "HEAD") { | 1918 if (method_ == "HEAD") { |
1827 TransitionToState(STATE_NONE); | 1919 TransitionToState(STATE_NONE); |
1828 return 0; | 1920 return 0; |
1829 } | 1921 } |
1830 | 1922 |
1831 DCHECK(entry_); | 1923 DCHECK(entry_); |
1832 TransitionToState(STATE_CACHE_READ_DATA_COMPLETE); | 1924 TransitionToState(STATE_CACHE_READ_DATA_COMPLETE); |
1833 | 1925 |
1834 if (net_log_.IsCapturing()) | 1926 if (net_log_.IsCapturing()) |
1835 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_DATA); | 1927 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_DATA); |
1836 if (partial_) { | 1928 if (partial_) { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1954 return OnWriteResponseInfoToEntryComplete(result); | 2046 return OnWriteResponseInfoToEntryComplete(result); |
1955 } | 2047 } |
1956 | 2048 |
1957 //----------------------------------------------------------------------------- | 2049 //----------------------------------------------------------------------------- |
1958 | 2050 |
1959 void HttpCache::Transaction::SetRequest(const NetLogWithSource& net_log, | 2051 void HttpCache::Transaction::SetRequest(const NetLogWithSource& net_log, |
1960 const HttpRequestInfo* request) { | 2052 const HttpRequestInfo* request) { |
1961 net_log_ = net_log; | 2053 net_log_ = net_log; |
1962 request_ = request; | 2054 request_ = request; |
1963 effective_load_flags_ = request_->load_flags; | 2055 effective_load_flags_ = request_->load_flags; |
2056 method_ = request_->method; | |
1964 | 2057 |
1965 if (cache_->mode() == DISABLE) | 2058 if (cache_->mode() == DISABLE) |
1966 effective_load_flags_ |= LOAD_DISABLE_CACHE; | 2059 effective_load_flags_ |= LOAD_DISABLE_CACHE; |
1967 | 2060 |
1968 // Some headers imply load flags. The order here is significant. | 2061 // Some headers imply load flags. The order here is significant. |
1969 // | 2062 // |
1970 // LOAD_DISABLE_CACHE : no cache read or write | 2063 // LOAD_DISABLE_CACHE : no cache read or write |
1971 // LOAD_BYPASS_CACHE : no cache read | 2064 // LOAD_BYPASS_CACHE : no cache read |
1972 // LOAD_VALIDATE_CACHE : no cache read unless validation | 2065 // LOAD_VALIDATE_CACHE : no cache read unless validation |
1973 // | 2066 // |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2032 // If there is more than one validation header, we can't treat this request as | 2125 // If there is more than one validation header, we can't treat this request as |
2033 // a cache validation, since we don't know for sure which header the server | 2126 // a cache validation, since we don't know for sure which header the server |
2034 // will give us a response for (and they could be contradictory). | 2127 // will give us a response for (and they could be contradictory). |
2035 if (external_validation_error) { | 2128 if (external_validation_error) { |
2036 LOG(WARNING) << "Multiple or malformed validation headers found."; | 2129 LOG(WARNING) << "Multiple or malformed validation headers found."; |
2037 effective_load_flags_ |= LOAD_DISABLE_CACHE; | 2130 effective_load_flags_ |= LOAD_DISABLE_CACHE; |
2038 } | 2131 } |
2039 | 2132 |
2040 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) { | 2133 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) { |
2041 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); | 2134 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); |
2135 restart_info_.cache_entry_status = cache_entry_status_; | |
Randy Smith (Not in Mondays)
2017/05/09 23:20:32
So it looks like the only functional change in thi
shivanisha
2017/05/10 14:46:03
The idea was to set restart_info_.cache_entry_stat
| |
2042 partial_.reset(new PartialData); | 2136 partial_.reset(new PartialData); |
2043 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) { | 2137 if (method_ == "GET" && partial_->Init(request_->extra_headers)) { |
2044 // We will be modifying the actual range requested to the server, so | 2138 // We will be modifying the actual range requested to the server, so |
2045 // let's remove the header here. | 2139 // let's remove the header here. |
2046 custom_request_.reset(new HttpRequestInfo(*request_)); | 2140 custom_request_.reset(new HttpRequestInfo(*request_)); |
2047 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange); | 2141 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange); |
2048 request_ = custom_request_.get(); | 2142 request_ = custom_request_.get(); |
2049 partial_->SetHeaders(custom_request_->extra_headers); | 2143 partial_->SetHeaders(custom_request_->extra_headers); |
2050 } else { | 2144 } else { |
2051 // The range is invalid or we cannot handle it properly. | 2145 // The range is invalid or we cannot handle it properly. |
2052 VLOG(1) << "Invalid byte range found."; | 2146 VLOG(1) << "Invalid byte range found."; |
2053 effective_load_flags_ |= LOAD_DISABLE_CACHE; | 2147 effective_load_flags_ |= LOAD_DISABLE_CACHE; |
2054 partial_.reset(NULL); | 2148 partial_.reset(NULL); |
2055 } | 2149 } |
2056 } | 2150 } |
2057 } | 2151 } |
2058 | 2152 |
2059 bool HttpCache::Transaction::ShouldPassThrough() { | 2153 bool HttpCache::Transaction::ShouldPassThrough() { |
2060 // We may have a null disk_cache if there is an error we cannot recover from, | 2154 // We may have a null disk_cache if there is an error we cannot recover from, |
2061 // like not enough disk space, or sharing violations. | 2155 // like not enough disk space, or sharing violations. |
2062 if (!cache_->disk_cache_.get()) | 2156 if (!cache_->disk_cache_.get()) |
2063 return true; | 2157 return true; |
2064 | 2158 |
2065 if (effective_load_flags_ & LOAD_DISABLE_CACHE) | 2159 if (effective_load_flags_ & LOAD_DISABLE_CACHE) |
2066 return true; | 2160 return true; |
2067 | 2161 |
2068 if (request_->method == "GET" || request_->method == "HEAD") | 2162 if (method_ == "GET" || method_ == "HEAD") |
2069 return false; | 2163 return false; |
2070 | 2164 |
2071 if (request_->method == "POST" && request_->upload_data_stream && | 2165 if (method_ == "POST" && request_->upload_data_stream && |
2072 request_->upload_data_stream->identifier()) { | 2166 request_->upload_data_stream->identifier()) { |
2073 return false; | 2167 return false; |
2074 } | 2168 } |
2075 | 2169 |
2076 if (request_->method == "PUT" && request_->upload_data_stream) | 2170 if (method_ == "PUT" && request_->upload_data_stream) |
2077 return false; | 2171 return false; |
2078 | 2172 |
2079 if (request_->method == "DELETE") | 2173 if (method_ == "DELETE") |
2080 return false; | 2174 return false; |
2081 | 2175 |
2082 return true; | 2176 return true; |
2083 } | 2177 } |
2084 | 2178 |
2085 int HttpCache::Transaction::BeginCacheRead() { | 2179 int HttpCache::Transaction::BeginCacheRead() { |
2086 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges. | 2180 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges. |
2087 // TODO(jkarlin): Either handle this case or DCHECK. | 2181 // TODO(jkarlin): Either handle this case or DCHECK. |
2088 if (response_.headers->response_code() == 206 || partial_) { | 2182 if (response_.headers->response_code() == 206 || partial_) { |
2089 NOTREACHED(); | 2183 NOTREACHED(); |
2090 TransitionToState(STATE_NONE); | 2184 TransitionToState(STATE_FINISH_HEADERS); |
2091 return ERR_CACHE_MISS; | 2185 return ERR_CACHE_MISS; |
2092 } | 2186 } |
2093 | 2187 |
2094 // We don't have the whole resource. | 2188 // We don't have the whole resource. |
2095 if (truncated_) { | 2189 if (truncated_) { |
2096 TransitionToState(STATE_NONE); | 2190 TransitionToState(STATE_FINISH_HEADERS); |
2097 return ERR_CACHE_MISS; | 2191 return ERR_CACHE_MISS; |
2098 } | 2192 } |
2099 | 2193 |
2100 if (RequiresValidation()) { | 2194 if (RequiresValidation()) { |
2101 TransitionToState(STATE_NONE); | 2195 TransitionToState(STATE_FINISH_HEADERS); |
2102 return ERR_CACHE_MISS; | 2196 return ERR_CACHE_MISS; |
2103 } | 2197 } |
2104 | 2198 |
2105 if (request_->method == "HEAD") | 2199 if (method_ == "HEAD") |
2106 FixHeadersForHead(); | 2200 FixHeadersForHead(); |
2107 | 2201 |
2108 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) | 2202 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) |
2109 TransitionToState(STATE_CACHE_READ_METADATA); | 2203 TransitionToState(STATE_CACHE_READ_METADATA); |
2110 else | 2204 else |
2111 TransitionToState(STATE_NONE); | 2205 TransitionToState(STATE_FINISH_HEADERS); |
2112 | 2206 |
2113 return OK; | 2207 return OK; |
2114 } | 2208 } |
2115 | 2209 |
2116 int HttpCache::Transaction::BeginCacheValidation() { | 2210 int HttpCache::Transaction::BeginCacheValidation() { |
2117 DCHECK_EQ(mode_, READ_WRITE); | 2211 DCHECK_EQ(mode_, READ_WRITE); |
2118 | 2212 |
2119 bool skip_validation = !RequiresValidation(); | 2213 bool skip_validation = !RequiresValidation(); |
2120 | 2214 |
2121 if (request_->method == "HEAD" && | 2215 if (method_ == "HEAD" && |
2122 (truncated_ || response_.headers->response_code() == 206)) { | 2216 (truncated_ || response_.headers->response_code() == 206)) { |
2123 DCHECK(!partial_); | 2217 DCHECK(!partial_); |
2124 if (skip_validation) | 2218 if (skip_validation) |
2125 return SetupEntryForRead(); | 2219 return SetupEntryForRead(); |
2126 | 2220 |
2127 // Bail out! | 2221 // Bail out! |
2128 TransitionToState(STATE_SEND_REQUEST); | 2222 TransitionToState(STATE_SEND_REQUEST); |
2129 mode_ = NONE; | 2223 mode_ = NONE; |
2130 return OK; | 2224 return OK; |
2131 } | 2225 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2169 } | 2263 } |
2170 | 2264 |
2171 int HttpCache::Transaction::BeginPartialCacheValidation() { | 2265 int HttpCache::Transaction::BeginPartialCacheValidation() { |
2172 DCHECK_EQ(mode_, READ_WRITE); | 2266 DCHECK_EQ(mode_, READ_WRITE); |
2173 | 2267 |
2174 if (response_.headers->response_code() != 206 && !partial_ && !truncated_) | 2268 if (response_.headers->response_code() != 206 && !partial_ && !truncated_) |
2175 return BeginCacheValidation(); | 2269 return BeginCacheValidation(); |
2176 | 2270 |
2177 // Partial requests should not be recorded in histograms. | 2271 // Partial requests should not be recorded in histograms. |
2178 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); | 2272 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); |
2179 if (request_->method == "HEAD") | 2273 if (method_ == "HEAD") |
2180 return BeginCacheValidation(); | 2274 return BeginCacheValidation(); |
2181 | 2275 |
2182 if (!range_requested_) { | 2276 if (!range_requested_) { |
2183 // The request is not for a range, but we have stored just ranges. | 2277 // The request is not for a range, but we have stored just ranges. |
2184 | 2278 |
2185 partial_.reset(new PartialData()); | 2279 partial_.reset(new PartialData()); |
2186 partial_->SetHeaders(request_->extra_headers); | 2280 partial_->SetHeaders(request_->extra_headers); |
2187 if (!custom_request_.get()) { | 2281 if (!custom_request_.get()) { |
2188 custom_request_.reset(new HttpRequestInfo(*request_)); | 2282 custom_request_.reset(new HttpRequestInfo(*request_)); |
2189 request_ = custom_request_.get(); | 2283 request_ = custom_request_.get(); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2307 // The first use of a resource after prefetch within a short window skips | 2401 // The first use of a resource after prefetch within a short window skips |
2308 // validation. | 2402 // validation. |
2309 return false; | 2403 return false; |
2310 } | 2404 } |
2311 | 2405 |
2312 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) { | 2406 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) { |
2313 validation_cause_ = VALIDATION_CAUSE_VALIDATE_FLAG; | 2407 validation_cause_ = VALIDATION_CAUSE_VALIDATE_FLAG; |
2314 return true; | 2408 return true; |
2315 } | 2409 } |
2316 | 2410 |
2317 if (request_->method == "PUT" || request_->method == "DELETE") | 2411 if (method_ == "PUT" || method_ == "DELETE") |
2318 return true; | 2412 return true; |
2319 | 2413 |
2320 bool validation_required_by_headers = response_.headers->RequiresValidation( | 2414 bool validation_required_by_headers = response_.headers->RequiresValidation( |
2321 response_.request_time, response_.response_time, cache_->clock_->Now()); | 2415 response_.request_time, response_.response_time, cache_->clock_->Now()); |
2322 | 2416 |
2323 if (validation_required_by_headers) { | 2417 if (validation_required_by_headers) { |
2324 HttpResponseHeaders::FreshnessLifetimes lifetimes = | 2418 HttpResponseHeaders::FreshnessLifetimes lifetimes = |
2325 response_.headers->GetFreshnessLifetimes(response_.response_time); | 2419 response_.headers->GetFreshnessLifetimes(response_.response_time); |
2326 if (lifetimes.freshness == base::TimeDelta()) { | 2420 if (lifetimes.freshness == base::TimeDelta()) { |
2327 validation_cause_ = VALIDATION_CAUSE_ZERO_FRESHNESS; | 2421 validation_cause_ = VALIDATION_CAUSE_ZERO_FRESHNESS; |
2328 } else { | 2422 } else { |
2329 validation_cause_ = VALIDATION_CAUSE_STALE; | 2423 validation_cause_ = VALIDATION_CAUSE_STALE; |
2330 stale_entry_freshness_ = lifetimes.freshness; | 2424 stale_entry_freshness_ = lifetimes.freshness; |
2331 stale_entry_age_ = response_.headers->GetCurrentAge( | 2425 stale_entry_age_ = response_.headers->GetCurrentAge( |
2332 response_.request_time, response_.response_time, | 2426 response_.request_time, response_.response_time, |
2333 cache_->clock_->Now()); | 2427 cache_->clock_->Now()); |
2334 } | 2428 } |
2335 } | 2429 } |
2336 | 2430 |
2337 return validation_required_by_headers; | 2431 return validation_required_by_headers; |
2338 } | 2432 } |
2339 | 2433 |
2340 bool HttpCache::Transaction::ConditionalizeRequest() { | 2434 bool HttpCache::Transaction::ConditionalizeRequest() { |
2341 DCHECK(response_.headers.get()); | 2435 DCHECK(response_.headers.get()); |
2342 | 2436 |
2343 if (request_->method == "PUT" || request_->method == "DELETE") | 2437 if (method_ == "PUT" || method_ == "DELETE") |
2344 return false; | 2438 return false; |
2345 | 2439 |
2346 // This only makes sense for cached 200 or 206 responses. | 2440 // This only makes sense for cached 200 or 206 responses. |
2347 if (response_.headers->response_code() != 200 && | 2441 if (response_.headers->response_code() != 200 && |
2348 response_.headers->response_code() != 206) { | 2442 response_.headers->response_code() != 206) { |
2349 return false; | 2443 return false; |
2350 } | 2444 } |
2351 | 2445 |
2352 if (fail_conditionalization_for_test_) | 2446 if (fail_conditionalization_for_test_) |
2353 return false; | 2447 return false; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2427 // | 2521 // |
2428 // WARNING: Whenever this code returns false, it has to make sure that the next | 2522 // WARNING: Whenever this code returns false, it has to make sure that the next |
2429 // time it is called it will return true so that we don't keep retrying the | 2523 // time it is called it will return true so that we don't keep retrying the |
2430 // request. | 2524 // request. |
2431 bool HttpCache::Transaction::ValidatePartialResponse() { | 2525 bool HttpCache::Transaction::ValidatePartialResponse() { |
2432 const HttpResponseHeaders* headers = new_response_->headers.get(); | 2526 const HttpResponseHeaders* headers = new_response_->headers.get(); |
2433 int response_code = headers->response_code(); | 2527 int response_code = headers->response_code(); |
2434 bool partial_response = (response_code == 206); | 2528 bool partial_response = (response_code == 206); |
2435 handling_206_ = false; | 2529 handling_206_ = false; |
2436 | 2530 |
2437 if (!entry_ || request_->method != "GET") | 2531 if (!entry_ || method_ != "GET") |
2438 return true; | 2532 return true; |
2439 | 2533 |
2440 if (invalid_range_) { | 2534 if (invalid_range_) { |
2441 // We gave up trying to match this request with the stored data. If the | 2535 // We gave up trying to match this request with the stored data. If the |
2442 // server is ok with the request, delete the entry, otherwise just ignore | 2536 // server is ok with the request, delete the entry, otherwise just ignore |
2443 // this request | 2537 // this request |
2444 DCHECK(!reading_); | 2538 DCHECK(!reading_); |
2445 if (partial_response || response_code == 200) { | 2539 if (partial_response || response_code == 200) { |
2446 DoomPartialEntry(true); | 2540 DoomPartialEntry(true); |
2447 mode_ = NONE; | 2541 mode_ = NONE; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2561 if (partial_) { | 2655 if (partial_) { |
2562 if (truncated_ || is_sparse_ || !invalid_range_) { | 2656 if (truncated_ || is_sparse_ || !invalid_range_) { |
2563 // We are going to return the saved response headers to the caller, so | 2657 // We are going to return the saved response headers to the caller, so |
2564 // we may need to adjust them first. | 2658 // we may need to adjust them first. |
2565 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); | 2659 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); |
2566 return OK; | 2660 return OK; |
2567 } else { | 2661 } else { |
2568 partial_.reset(); | 2662 partial_.reset(); |
2569 } | 2663 } |
2570 } | 2664 } |
2571 cache_->ConvertWriterToReader(entry_); | 2665 |
2572 mode_ = READ; | 2666 mode_ = READ; |
2573 | 2667 |
2574 if (request_->method == "HEAD") | 2668 if (method_ == "HEAD") |
2575 FixHeadersForHead(); | 2669 FixHeadersForHead(); |
2576 | 2670 |
2577 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) | 2671 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) |
2578 TransitionToState(STATE_CACHE_READ_METADATA); | 2672 TransitionToState(STATE_CACHE_READ_METADATA); |
2579 else | 2673 else |
2580 TransitionToState(STATE_NONE); | 2674 TransitionToState(STATE_FINISH_HEADERS); |
2581 return OK; | 2675 return OK; |
2582 } | 2676 } |
2583 | 2677 |
2584 int HttpCache::Transaction::WriteToEntry(int index, int offset, | 2678 int HttpCache::Transaction::WriteToEntry(int index, int offset, |
2585 IOBuffer* data, int data_len, | 2679 IOBuffer* data, int data_len, |
2586 const CompletionCallback& callback) { | 2680 const CompletionCallback& callback) { |
2587 if (!entry_) | 2681 if (!entry_) |
2588 return data_len; | 2682 return data_len; |
2589 | 2683 |
2590 int rv = 0; | 2684 int rv = 0; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2649 } | 2743 } |
2650 return OK; | 2744 return OK; |
2651 } | 2745 } |
2652 | 2746 |
2653 void HttpCache::Transaction::DoneWritingToEntry(bool success) { | 2747 void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
2654 if (!entry_) | 2748 if (!entry_) |
2655 return; | 2749 return; |
2656 | 2750 |
2657 RecordHistograms(); | 2751 RecordHistograms(); |
2658 | 2752 |
2659 cache_->DoneWritingToEntry(entry_, success); | 2753 cache_->DoneWritingToEntry(entry_, success, this); |
2660 entry_ = NULL; | 2754 entry_ = NULL; |
2661 mode_ = NONE; // switch to 'pass through' mode | 2755 mode_ = NONE; // switch to 'pass through' mode |
2662 } | 2756 } |
2663 | 2757 |
2664 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { | 2758 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { |
2665 DLOG(ERROR) << "ReadData failed: " << result; | 2759 DLOG(ERROR) << "ReadData failed: " << result; |
2666 const int result_for_histogram = std::max(0, -result); | 2760 const int result_for_histogram = std::max(0, -result); |
2667 if (restart) { | 2761 if (restart) { |
2668 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", | 2762 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", |
2669 result_for_histogram); | 2763 result_for_histogram); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2797 // Accept-Ranges: none.......... 0.4% | 2891 // Accept-Ranges: none.......... 0.4% |
2798 // Strong Validator............. 50% | 2892 // Strong Validator............. 50% |
2799 // Strong Validator + ranges.... 24% | 2893 // Strong Validator + ranges.... 24% |
2800 // Strong Validator + CL........ 49% | 2894 // Strong Validator + CL........ 49% |
2801 // | 2895 // |
2802 bool HttpCache::Transaction::CanResume(bool has_data) { | 2896 bool HttpCache::Transaction::CanResume(bool has_data) { |
2803 // Double check that there is something worth keeping. | 2897 // Double check that there is something worth keeping. |
2804 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex)) | 2898 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex)) |
2805 return false; | 2899 return false; |
2806 | 2900 |
2807 if (request_->method != "GET") | 2901 if (method_ != "GET") |
2808 return false; | 2902 return false; |
2809 | 2903 |
2810 // Note that if this is a 206, content-length was already fixed after calling | 2904 // Note that if this is a 206, content-length was already fixed after calling |
2811 // PartialData::ResponseHeadersOK(). | 2905 // PartialData::ResponseHeadersOK(). |
2812 if (response_.headers->GetContentLength() <= 0 || | 2906 if (response_.headers->GetContentLength() <= 0 || |
2813 response_.headers->HasHeaderValue("Accept-Ranges", "none") || | 2907 response_.headers->HasHeaderValue("Accept-Ranges", "none") || |
2814 !response_.headers->HasStrongValidators()) { | 2908 !response_.headers->HasStrongValidators()) { |
2815 return false; | 2909 return false; |
2816 } | 2910 } |
2817 | 2911 |
(...skipping 28 matching lines...) Expand all Loading... | |
2846 response_.cache_entry_status = cache_entry_status_; | 2940 response_.cache_entry_status = cache_entry_status_; |
2847 if (auth_response_.headers.get()) { | 2941 if (auth_response_.headers.get()) { |
2848 auth_response_.cache_entry_status = cache_entry_status_; | 2942 auth_response_.cache_entry_status = cache_entry_status_; |
2849 } | 2943 } |
2850 } | 2944 } |
2851 | 2945 |
2852 void HttpCache::Transaction::RecordHistograms() { | 2946 void HttpCache::Transaction::RecordHistograms() { |
2853 DCHECK_NE(CacheEntryStatus::ENTRY_UNDEFINED, cache_entry_status_); | 2947 DCHECK_NE(CacheEntryStatus::ENTRY_UNDEFINED, cache_entry_status_); |
2854 if (!cache_.get() || !cache_->GetCurrentBackend() || | 2948 if (!cache_.get() || !cache_->GetCurrentBackend() || |
2855 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE || | 2949 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE || |
2856 cache_->mode() != NORMAL || request_->method != "GET") { | 2950 cache_->mode() != NORMAL || method_ != "GET") { |
2857 return; | 2951 return; |
2858 } | 2952 } |
2859 | 2953 |
2860 bool validation_request = | 2954 bool validation_request = |
2861 cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED || | 2955 cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED || |
2862 cache_entry_status_ == CacheEntryStatus::ENTRY_UPDATED; | 2956 cache_entry_status_ == CacheEntryStatus::ENTRY_UPDATED; |
2863 | 2957 |
2864 bool stale_request = | 2958 bool stale_request = |
2865 validation_cause_ == VALIDATION_CAUSE_STALE && | 2959 validation_cause_ == VALIDATION_CAUSE_STALE && |
2866 (validation_request || | 2960 (validation_request || |
2867 cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE); | 2961 cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE); |
2868 int64_t freshness_periods_since_last_used = 0; | 2962 int64_t freshness_periods_since_last_used = 0; |
2869 | 2963 |
2870 if (stale_request) { | 2964 if (stale_request && !open_entry_last_used_.is_null()) { |
2965 // Note that we are not able to capture those transactions' histograms which | |
2966 // when added to entry, the response was being written by another | |
2967 // transaction because getting the last used timestamp might lead to a data | |
2968 // race in that case. TODO(crbug.com/713354). | |
2969 | |
2871 // For stale entries, record how many freshness periods have elapsed since | 2970 // For stale entries, record how many freshness periods have elapsed since |
2872 // the entry was last used. | 2971 // the entry was last used. |
2873 DCHECK(!open_entry_last_used_.is_null()); | |
2874 DCHECK(!stale_entry_freshness_.is_zero()); | 2972 DCHECK(!stale_entry_freshness_.is_zero()); |
2875 base::TimeDelta time_since_use = base::Time::Now() - open_entry_last_used_; | 2973 base::TimeDelta time_since_use = base::Time::Now() - open_entry_last_used_; |
2876 freshness_periods_since_last_used = | 2974 freshness_periods_since_last_used = |
2877 (time_since_use * 1000) / stale_entry_freshness_; | 2975 (time_since_use * 1000) / stale_entry_freshness_; |
2878 | 2976 |
2879 if (validation_request) { | 2977 if (validation_request) { |
2880 int64_t age_in_freshness_periods = | 2978 int64_t age_in_freshness_periods = |
2881 (stale_entry_age_ * 100) / stale_entry_freshness_; | 2979 (stale_entry_age_ * 100) / stale_entry_freshness_; |
2882 if (cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED) { | 2980 if (cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED) { |
2883 UMA_HISTOGRAM_COUNTS("HttpCache.StaleEntry.Validated.Age", | 2981 UMA_HISTOGRAM_COUNTS("HttpCache.StaleEntry.Validated.Age", |
(...skipping 12 matching lines...) Expand all Loading... | |
2896 } | 2994 } |
2897 } | 2995 } |
2898 | 2996 |
2899 std::string mime_type; | 2997 std::string mime_type; |
2900 HttpResponseHeaders* response_headers = GetResponseInfo()->headers.get(); | 2998 HttpResponseHeaders* response_headers = GetResponseInfo()->headers.get(); |
2901 if (response_headers && response_headers->GetMimeType(&mime_type)) { | 2999 if (response_headers && response_headers->GetMimeType(&mime_type)) { |
2902 // Record the cache pattern by resource type. The type is inferred by | 3000 // Record the cache pattern by resource type. The type is inferred by |
2903 // response header mime type, which could be incorrect, so this is just an | 3001 // response header mime type, which could be incorrect, so this is just an |
2904 // estimate. | 3002 // estimate. |
2905 if (mime_type == "text/html" && | 3003 if (mime_type == "text/html" && |
2906 (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED)) { | 3004 (effective_load_flags_ & LOAD_MAIN_FRAME_DEPRECATED)) { |
2907 CACHE_STATUS_HISTOGRAMS(".MainFrameHTML"); | 3005 CACHE_STATUS_HISTOGRAMS(".MainFrameHTML"); |
2908 } else if (mime_type == "text/html") { | 3006 } else if (mime_type == "text/html") { |
2909 CACHE_STATUS_HISTOGRAMS(".NonMainFrameHTML"); | 3007 CACHE_STATUS_HISTOGRAMS(".NonMainFrameHTML"); |
2910 } else if (mime_type == "text/css") { | 3008 } else if (mime_type == "text/css") { |
2911 CACHE_STATUS_HISTOGRAMS(".CSS"); | 3009 CACHE_STATUS_HISTOGRAMS(".CSS"); |
2912 } else if (base::StartsWith(mime_type, "image/", | 3010 } else if (base::StartsWith(mime_type, "image/", |
2913 base::CompareCase::SENSITIVE)) { | 3011 base::CompareCase::SENSITIVE)) { |
2914 int64_t content_length = response_headers->GetContentLength(); | 3012 int64_t content_length = response_headers->GetContentLength(); |
2915 if (content_length >= 0 && content_length < 100) { | 3013 if (content_length >= 0 && content_length < 100) { |
2916 CACHE_STATUS_HISTOGRAMS(".TinyImage"); | 3014 CACHE_STATUS_HISTOGRAMS(".TinyImage"); |
(...skipping 19 matching lines...) Expand all Loading... | |
2936 | 3034 |
2937 CACHE_STATUS_HISTOGRAMS(""); | 3035 CACHE_STATUS_HISTOGRAMS(""); |
2938 | 3036 |
2939 if (cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) { | 3037 if (cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) { |
2940 UMA_HISTOGRAM_ENUMERATION("HttpCache.CantConditionalizeCause", | 3038 UMA_HISTOGRAM_ENUMERATION("HttpCache.CantConditionalizeCause", |
2941 validation_cause_, VALIDATION_CAUSE_MAX); | 3039 validation_cause_, VALIDATION_CAUSE_MAX); |
2942 } | 3040 } |
2943 | 3041 |
2944 if (cache_entry_status_ == CacheEntryStatus::ENTRY_OTHER) | 3042 if (cache_entry_status_ == CacheEntryStatus::ENTRY_OTHER) |
2945 return; | 3043 return; |
2946 DCHECK(!range_requested_); | 3044 |
3045 DCHECK(!range_requested_) << "Cache entry status " << cache_entry_status_; | |
2947 DCHECK(!first_cache_access_since_.is_null()); | 3046 DCHECK(!first_cache_access_since_.is_null()); |
2948 | 3047 |
2949 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_; | 3048 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_; |
2950 | 3049 |
2951 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time); | 3050 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time); |
2952 | 3051 |
2953 bool did_send_request = !send_request_since_.is_null(); | 3052 bool did_send_request = !send_request_since_.is_null(); |
2954 DCHECK( | 3053 DCHECK( |
2955 (did_send_request && | 3054 (did_send_request && |
2956 (cache_entry_status_ == CacheEntryStatus::ENTRY_NOT_IN_CACHE || | 3055 (cache_entry_status_ == CacheEntryStatus::ENTRY_NOT_IN_CACHE || |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3017 } | 3116 } |
3018 | 3117 |
3019 void HttpCache::Transaction::TransitionToState(State state) { | 3118 void HttpCache::Transaction::TransitionToState(State state) { |
3020 // Ensure that the state is only set once per Do* state. | 3119 // Ensure that the state is only set once per Do* state. |
3021 DCHECK(in_do_loop_); | 3120 DCHECK(in_do_loop_); |
3022 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; | 3121 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; |
3023 next_state_ = state; | 3122 next_state_ = state; |
3024 } | 3123 } |
3025 | 3124 |
3026 } // namespace net | 3125 } // namespace net |
OLD | NEW |