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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 invalid_range_(false), | 161 invalid_range_(false), |
162 truncated_(false), | 162 truncated_(false), |
163 is_sparse_(false), | 163 is_sparse_(false), |
164 range_requested_(false), | 164 range_requested_(false), |
165 handling_206_(false), | 165 handling_206_(false), |
166 cache_pending_(false), | 166 cache_pending_(false), |
167 done_reading_(false), | 167 done_reading_(false), |
168 vary_mismatch_(false), | 168 vary_mismatch_(false), |
169 couldnt_conditionalize_request_(false), | 169 couldnt_conditionalize_request_(false), |
170 bypass_lock_for_test_(false), | 170 bypass_lock_for_test_(false), |
171 bypass_lock_after_headers_for_test_(false), | |
171 fail_conditionalization_for_test_(false), | 172 fail_conditionalization_for_test_(false), |
172 io_buf_len_(0), | 173 io_buf_len_(0), |
173 read_offset_(0), | 174 read_offset_(0), |
174 effective_load_flags_(0), | 175 effective_load_flags_(0), |
175 write_len_(0), | 176 write_len_(0), |
176 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), | 177 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), |
177 validation_cause_(VALIDATION_CAUSE_UNDEFINED), | 178 validation_cause_(VALIDATION_CAUSE_UNDEFINED), |
178 total_received_bytes_(0), | 179 total_received_bytes_(0), |
179 total_sent_bytes_(0), | 180 total_sent_bytes_(0), |
180 websocket_handshake_stream_base_create_helper_(NULL), | 181 websocket_handshake_stream_base_create_helper_(NULL), |
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
846 rv = DoPartialHeadersReceived(); | 847 rv = DoPartialHeadersReceived(); |
847 break; | 848 break; |
848 case STATE_CACHE_READ_METADATA: | 849 case STATE_CACHE_READ_METADATA: |
849 DCHECK_EQ(OK, rv); | 850 DCHECK_EQ(OK, rv); |
850 rv = DoCacheReadMetadata(); | 851 rv = DoCacheReadMetadata(); |
851 break; | 852 break; |
852 case STATE_CACHE_READ_METADATA_COMPLETE: | 853 case STATE_CACHE_READ_METADATA_COMPLETE: |
853 rv = DoCacheReadMetadataComplete(rv); | 854 rv = DoCacheReadMetadataComplete(rv); |
854 break; | 855 break; |
855 case STATE_HEADERS_PHASE_CANNOT_PROCEED: | 856 case STATE_HEADERS_PHASE_CANNOT_PROCEED: |
856 rv = DoHeadersPhaseCannotProceed(); | 857 rv = DoHeadersPhaseCannotProceed(rv); |
857 break; | 858 break; |
858 case STATE_FINISH_HEADERS: | 859 case STATE_FINISH_HEADERS: |
859 rv = DoFinishHeaders(rv); | 860 rv = DoFinishHeaders(rv); |
860 break; | 861 break; |
861 case STATE_FINISH_HEADERS_COMPLETE: | 862 case STATE_FINISH_HEADERS_COMPLETE: |
862 rv = DoFinishHeadersComplete(rv); | 863 rv = DoFinishHeadersComplete(rv); |
863 break; | 864 break; |
864 case STATE_NETWORK_READ: | 865 case STATE_NETWORK_READ: |
865 DCHECK_EQ(OK, rv); | 866 DCHECK_EQ(OK, rv); |
866 rv = DoNetworkRead(); | 867 rv = DoNetworkRead(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
916 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); | 917 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); |
917 return cache_->GetBackendForTransaction(this); | 918 return cache_->GetBackendForTransaction(this); |
918 } | 919 } |
919 | 920 |
920 int HttpCache::Transaction::DoGetBackendComplete(int result) { | 921 int HttpCache::Transaction::DoGetBackendComplete(int result) { |
921 DCHECK(result == OK || result == ERR_FAILED); | 922 DCHECK(result == OK || result == ERR_FAILED); |
922 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND, | 923 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND, |
923 result); | 924 result); |
924 cache_pending_ = false; | 925 cache_pending_ = false; |
925 | 926 |
927 // Reset mode_ that might get set in this function. This is done because this | |
928 // function can be invoked multiple times for a transaction. | |
929 mode_ = NONE; | |
930 | |
926 if (!ShouldPassThrough()) { | 931 if (!ShouldPassThrough()) { |
927 cache_key_ = cache_->GenerateCacheKey(request_); | 932 cache_key_ = cache_->GenerateCacheKey(request_); |
928 | 933 |
929 // Requested cache access mode. | 934 // Requested cache access mode. |
930 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { | 935 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { |
931 if (effective_load_flags_ & LOAD_BYPASS_CACHE) { | 936 if (effective_load_flags_ & LOAD_BYPASS_CACHE) { |
932 // The client has asked for nonsense. | 937 // The client has asked for nonsense. |
933 TransitionToState(STATE_FINISH_HEADERS); | 938 TransitionToState(STATE_FINISH_HEADERS); |
934 return ERR_CACHE_MISS; | 939 return ERR_CACHE_MISS; |
935 } | 940 } |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1124 | 1129 |
1125 int HttpCache::Transaction::DoAddToEntry() { | 1130 int HttpCache::Transaction::DoAddToEntry() { |
1126 TRACE_EVENT0("io", "HttpCacheTransaction::DoAddToEntry"); | 1131 TRACE_EVENT0("io", "HttpCacheTransaction::DoAddToEntry"); |
1127 DCHECK(new_entry_); | 1132 DCHECK(new_entry_); |
1128 cache_pending_ = true; | 1133 cache_pending_ = true; |
1129 TransitionToState(STATE_ADD_TO_ENTRY_COMPLETE); | 1134 TransitionToState(STATE_ADD_TO_ENTRY_COMPLETE); |
1130 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY); | 1135 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY); |
1131 DCHECK(entry_lock_waiting_since_.is_null()); | 1136 DCHECK(entry_lock_waiting_since_.is_null()); |
1132 entry_lock_waiting_since_ = TimeTicks::Now(); | 1137 entry_lock_waiting_since_ = TimeTicks::Now(); |
1133 int rv = cache_->AddTransactionToEntry(new_entry_, this); | 1138 int rv = cache_->AddTransactionToEntry(new_entry_, this); |
1134 if (rv == ERR_IO_PENDING) { | 1139 if (rv == ERR_IO_PENDING) |
1135 if (bypass_lock_for_test_) { | 1140 AddCacheLockTimeoutHandler(new_entry_); |
1136 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
1137 FROM_HERE, | |
1138 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout, | |
1139 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_)); | |
1140 } else { | |
1141 int timeout_milliseconds = 20 * 1000; | |
1142 if (partial_ && new_entry_->writer && | |
1143 new_entry_->writer->range_requested_) { | |
1144 // Quickly timeout and bypass the cache if we're a range request and | |
1145 // we're blocked by the reader/writer lock. Doing so eliminates a long | |
1146 // running issue, http://crbug.com/31014, where two of the same media | |
1147 // resources could not be played back simultaneously due to one locking | |
1148 // the cache entry until the entire video was downloaded. | |
1149 // | |
1150 // Bypassing the cache is not ideal, as we are now ignoring the cache | |
1151 // entirely for all range requests to a resource beyond the first. This | |
1152 // is however a much more succinct solution than the alternatives, which | |
1153 // would require somewhat significant changes to the http caching logic. | |
1154 // | |
1155 // Allow some timeout slack for the entry addition to complete in case | |
1156 // the writer lock is imminently released; we want to avoid skipping | |
1157 // the cache if at all possible. See http://crbug.com/408765 | |
1158 timeout_milliseconds = 25; | |
1159 } | |
1160 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
1161 FROM_HERE, | |
1162 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout, | |
1163 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_), | |
1164 TimeDelta::FromMilliseconds(timeout_milliseconds)); | |
1165 } | |
1166 } | |
1167 return rv; | 1141 return rv; |
1168 } | 1142 } |
1169 | 1143 |
1144 void HttpCache::Transaction::AddCacheLockTimeoutHandler(ActiveEntry* entry) { | |
1145 if ((bypass_lock_for_test_ && next_state_ == STATE_ADD_TO_ENTRY_COMPLETE) || | |
Randy Smith (Not in Mondays)
2017/06/23 20:20:46
suggestion, no need for it to be in this CL if you
Randy Smith (Not in Mondays)
2017/06/23 20:20:47
suggestion: Add DCHECK that we're in one of those
| |
1146 (bypass_lock_after_headers_for_test_ && | |
1147 next_state_ == STATE_FINISH_HEADERS_COMPLETE)) { | |
1148 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
1149 FROM_HERE, | |
1150 base::Bind(&HttpCache::Transaction::OnCacheLockTimeout, | |
1151 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_)); | |
1152 } else { | |
1153 int timeout_milliseconds = 20 * 1000; | |
1154 if (partial_ && entry->writer && entry->writer->range_requested_) { | |
1155 // Quickly timeout and bypass the cache if we're a range request and | |
1156 // we're blocked by the reader/writer lock. Doing so eliminates a long | |
1157 // running issue, http://crbug.com/31014, where two of the same media | |
1158 // resources could not be played back simultaneously due to one locking | |
1159 // the cache entry until the entire video was downloaded. | |
1160 // | |
1161 // Bypassing the cache is not ideal, as we are now ignoring the cache | |
1162 // entirely for all range requests to a resource beyond the first. This | |
1163 // is however a much more succinct solution than the alternatives, which | |
1164 // would require somewhat significant changes to the http caching logic. | |
1165 // | |
1166 // Allow some timeout slack for the entry addition to complete in case | |
1167 // the writer lock is imminently released; we want to avoid skipping | |
1168 // the cache if at all possible. See http://crbug.com/408765 | |
1169 timeout_milliseconds = 25; | |
1170 } | |
1171 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
1172 FROM_HERE, | |
1173 base::Bind(&HttpCache::Transaction::OnCacheLockTimeout, | |
1174 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_), | |
1175 TimeDelta::FromMilliseconds(timeout_milliseconds)); | |
1176 } | |
1177 } | |
1178 | |
1170 int HttpCache::Transaction::DoAddToEntryComplete(int result) { | 1179 int HttpCache::Transaction::DoAddToEntryComplete(int result) { |
1171 TRACE_EVENT0("io", "HttpCacheTransaction::DoAddToEntryComplete"); | 1180 TRACE_EVENT0("io", "HttpCacheTransaction::DoAddToEntryComplete"); |
1172 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY, | 1181 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY, |
1173 result); | 1182 result); |
1174 const TimeDelta entry_lock_wait = | 1183 const TimeDelta entry_lock_wait = |
1175 TimeTicks::Now() - entry_lock_waiting_since_; | 1184 TimeTicks::Now() - entry_lock_waiting_since_; |
1176 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait); | 1185 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait); |
1177 | 1186 |
1178 entry_lock_waiting_since_ = TimeTicks(); | 1187 entry_lock_waiting_since_ = TimeTicks(); |
1179 DCHECK(new_entry_); | 1188 DCHECK(new_entry_); |
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1810 // We are about to return the headers for a byte-range request to the user, | 1819 // We are about to return the headers for a byte-range request to the user, |
1811 // so let's fix them. | 1820 // so let's fix them. |
1812 partial_->FixResponseHeaders(response_.headers.get(), true); | 1821 partial_->FixResponseHeaders(response_.headers.get(), true); |
1813 TransitionToState(STATE_FINISH_HEADERS); | 1822 TransitionToState(STATE_FINISH_HEADERS); |
1814 } else { | 1823 } else { |
1815 TransitionToState(STATE_FINISH_HEADERS); | 1824 TransitionToState(STATE_FINISH_HEADERS); |
1816 } | 1825 } |
1817 return OK; | 1826 return OK; |
1818 } | 1827 } |
1819 | 1828 |
1820 int HttpCache::Transaction::DoHeadersPhaseCannotProceed() { | 1829 int HttpCache::Transaction::DoHeadersPhaseCannotProceed(int result) { |
1821 // If its the Start state machine and it cannot proceed due to a cache | 1830 // If its the Start state machine and it cannot proceed due to a cache |
1822 // failure, restart this transaction. | 1831 // failure, restart this transaction. |
1823 DCHECK(!reading_); | 1832 DCHECK(!reading_); |
1824 | 1833 |
1825 // Reset before invoking SetRequest() which can reset the request info sent to | 1834 // Reset before invoking SetRequest() which can reset the request info sent to |
1826 // network transaction. | 1835 // network transaction. |
1827 if (network_trans_) | 1836 if (network_trans_) |
1828 network_trans_.reset(); | 1837 network_trans_.reset(); |
1829 | 1838 |
1830 SetRequest(net_log_); | 1839 SetRequest(net_log_); |
1831 | 1840 |
1832 entry_ = nullptr; | 1841 entry_ = nullptr; |
1833 | 1842 |
1843 // Bypass the cache for timeout scenario. | |
1844 if (result == ERR_CACHE_LOCK_TIMEOUT) | |
1845 effective_load_flags_ |= LOAD_DISABLE_CACHE; | |
1846 | |
1834 TransitionToState(STATE_GET_BACKEND); | 1847 TransitionToState(STATE_GET_BACKEND); |
1835 return OK; | 1848 return OK; |
1836 } | 1849 } |
1837 | 1850 |
1838 int HttpCache::Transaction::DoFinishHeaders(int result) { | 1851 int HttpCache::Transaction::DoFinishHeaders(int result) { |
1839 if (!cache_.get() || !entry_ || result != OK) { | 1852 if (!cache_.get() || !entry_ || result != OK) { |
1840 TransitionToState(STATE_NONE); | 1853 TransitionToState(STATE_NONE); |
1841 return result; | 1854 return result; |
1842 } | 1855 } |
1843 | 1856 |
1844 TransitionToState(STATE_FINISH_HEADERS_COMPLETE); | 1857 TransitionToState(STATE_FINISH_HEADERS_COMPLETE); |
1845 | 1858 |
1846 // If it was an auth failure or 416, this transaction should continue to be | 1859 // If it was an auth failure or 416, this transaction should continue to be |
1847 // headers_transaction till consumer takes an action, so no need to do | 1860 // headers_transaction till consumer takes an action, so no need to do |
1848 // anything now. | 1861 // anything now. |
1849 if (auth_response_.headers.get() || | 1862 if (auth_response_.headers.get() || |
1850 (new_response_ && new_response_->headers && | 1863 (new_response_ && new_response_->headers && |
1851 new_response_->headers->response_code() == 416)) | 1864 new_response_->headers->response_code() == 416)) |
1852 return OK; | 1865 return OK; |
1853 | 1866 |
1854 // If the transaction needs to wait because another transaction is still | 1867 // If the transaction needs to wait because another transaction is still |
1855 // writing the response body, it will return ERR_IO_PENDING now and the | 1868 // writing the response body, it will return ERR_IO_PENDING now and the |
1856 // io_callback_ will be invoked when the wait is done. | 1869 // io_callback_ will be invoked when the wait is done. |
1857 return cache_->DoneWithResponseHeaders(entry_, this, partial_ != nullptr); | 1870 int rv = cache_->DoneWithResponseHeaders(entry_, this, partial_ != nullptr); |
1871 if (rv == ERR_IO_PENDING) { | |
1872 DCHECK(entry_lock_waiting_since_.is_null()); | |
1873 entry_lock_waiting_since_ = TimeTicks::Now(); | |
1874 AddCacheLockTimeoutHandler(entry_); | |
Randy Smith (Not in Mondays)
2017/06/23 20:20:47
nit, suggestion, doesn't need to be in this CL if
| |
1875 } | |
1876 return rv; | |
1858 } | 1877 } |
1859 | 1878 |
1860 int HttpCache::Transaction::DoFinishHeadersComplete(int rv) { | 1879 int HttpCache::Transaction::DoFinishHeadersComplete(int rv) { |
1861 if (rv == ERR_CACHE_RACE) { | 1880 entry_lock_waiting_since_ = TimeTicks(); |
1881 if (rv == ERR_CACHE_RACE || rv == ERR_CACHE_LOCK_TIMEOUT) { | |
1862 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED); | 1882 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED); |
1863 return OK; | 1883 return rv; |
1864 } | 1884 } |
1865 | 1885 |
1866 TransitionToState(STATE_NONE); | 1886 TransitionToState(STATE_NONE); |
1867 return rv; | 1887 return rv; |
1868 } | 1888 } |
1869 | 1889 |
1870 int HttpCache::Transaction::DoCacheReadMetadata() { | 1890 int HttpCache::Transaction::DoCacheReadMetadata() { |
1871 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); | 1891 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); |
1872 DCHECK(entry_); | 1892 DCHECK(entry_); |
1873 DCHECK(!response_.metadata.get()); | 1893 DCHECK(!response_.metadata.get()); |
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2798 is_sparse_ = false; | 2818 is_sparse_ = false; |
2799 partial_.reset(); | 2819 partial_.reset(); |
2800 TransitionToState(STATE_GET_BACKEND); | 2820 TransitionToState(STATE_GET_BACKEND); |
2801 return OK; | 2821 return OK; |
2802 } | 2822 } |
2803 | 2823 |
2804 TransitionToState(STATE_NONE); | 2824 TransitionToState(STATE_NONE); |
2805 return ERR_CACHE_READ_FAILURE; | 2825 return ERR_CACHE_READ_FAILURE; |
2806 } | 2826 } |
2807 | 2827 |
2808 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) { | 2828 void HttpCache::Transaction::OnCacheLockTimeout(base::TimeTicks start_time) { |
2809 if (entry_lock_waiting_since_ != start_time) | 2829 if (entry_lock_waiting_since_ != start_time) |
2810 return; | 2830 return; |
2811 | 2831 |
2812 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE); | 2832 DCHECK(next_state_ == STATE_ADD_TO_ENTRY_COMPLETE || |
2833 next_state_ == STATE_FINISH_HEADERS_COMPLETE); | |
2813 | 2834 |
2814 if (!cache_) | 2835 if (!cache_) |
2815 return; | 2836 return; |
2816 | 2837 |
2817 cache_->RemovePendingTransaction(this); | 2838 if (next_state_ == STATE_ADD_TO_ENTRY_COMPLETE) |
2839 cache_->RemovePendingTransaction(this); | |
2840 else | |
2841 cache_->DoneWithEntry(entry_, this, false, partial_ != nullptr); | |
2818 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT); | 2842 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT); |
2819 } | 2843 } |
2820 | 2844 |
2821 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) { | 2845 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) { |
2822 DVLOG(2) << "DoomPartialEntry"; | 2846 DVLOG(2) << "DoomPartialEntry"; |
2823 int rv = cache_->DoomEntry(cache_key_, NULL); | 2847 int rv = cache_->DoomEntry(cache_key_, NULL); |
2824 DCHECK_EQ(OK, rv); | 2848 DCHECK_EQ(OK, rv); |
2825 cache_->DoneWithEntry(entry_, this, false /* process_cancel */, | 2849 cache_->DoneWithEntry(entry_, this, false /* process_cancel */, |
2826 partial_ != nullptr); | 2850 partial_ != nullptr); |
2827 entry_ = NULL; | 2851 entry_ = NULL; |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3137 } | 3161 } |
3138 | 3162 |
3139 void HttpCache::Transaction::TransitionToState(State state) { | 3163 void HttpCache::Transaction::TransitionToState(State state) { |
3140 // Ensure that the state is only set once per Do* state. | 3164 // Ensure that the state is only set once per Do* state. |
3141 DCHECK(in_do_loop_); | 3165 DCHECK(in_do_loop_); |
3142 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; | 3166 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; |
3143 next_state_ = state; | 3167 next_state_ = state; |
3144 } | 3168 } |
3145 | 3169 |
3146 } // namespace net | 3170 } // namespace net |
OLD | NEW |