OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
16 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/ref_counted.h" | 18 #include "base/ref_counted.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/time.h" | 20 #include "base/time.h" |
21 #include "net/base/cert_status_flags.h" | 21 #include "net/base/cert_status_flags.h" |
22 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
25 #include "net/base/net_log.h" | 25 #include "net/base/net_log.h" |
| 26 #include "net/base/network_delegate.h" |
26 #include "net/base/ssl_cert_request_info.h" | 27 #include "net/base/ssl_cert_request_info.h" |
27 #include "net/base/ssl_config_service.h" | 28 #include "net/base/ssl_config_service.h" |
28 #include "net/disk_cache/disk_cache.h" | 29 #include "net/disk_cache/disk_cache.h" |
29 #include "net/http/disk_cache_based_ssl_host_info.h" | 30 #include "net/http/disk_cache_based_ssl_host_info.h" |
| 31 #include "net/http/http_network_session.h" |
30 #include "net/http/http_request_info.h" | 32 #include "net/http/http_request_info.h" |
31 #include "net/http/http_response_headers.h" | 33 #include "net/http/http_response_headers.h" |
32 #include "net/http/http_transaction.h" | 34 #include "net/http/http_transaction.h" |
33 #include "net/http/http_util.h" | 35 #include "net/http/http_util.h" |
34 #include "net/http/partial_data.h" | 36 #include "net/http/partial_data.h" |
35 | 37 |
36 using base::Time; | 38 using base::Time; |
37 | 39 |
38 namespace net { | 40 namespace net { |
39 | 41 |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 case STATE_DOOM_ENTRY_COMPLETE: | 481 case STATE_DOOM_ENTRY_COMPLETE: |
480 rv = DoDoomEntryComplete(rv); | 482 rv = DoDoomEntryComplete(rv); |
481 break; | 483 break; |
482 case STATE_ADD_TO_ENTRY: | 484 case STATE_ADD_TO_ENTRY: |
483 DCHECK_EQ(OK, rv); | 485 DCHECK_EQ(OK, rv); |
484 rv = DoAddToEntry(); | 486 rv = DoAddToEntry(); |
485 break; | 487 break; |
486 case STATE_ADD_TO_ENTRY_COMPLETE: | 488 case STATE_ADD_TO_ENTRY_COMPLETE: |
487 rv = DoAddToEntryComplete(rv); | 489 rv = DoAddToEntryComplete(rv); |
488 break; | 490 break; |
| 491 case STATE_NOTIFY_BEFORE_SEND_HEADERS: |
| 492 DCHECK_EQ(OK, rv); |
| 493 rv = DoNotifyBeforeSendHeaders(); |
| 494 break; |
| 495 case STATE_NOTIFY_BEFORE_SEND_HEADERS_COMPLETE: |
| 496 rv = DoNotifyBeforeSendHeadersComplete(rv); |
| 497 break; |
489 case STATE_START_PARTIAL_CACHE_VALIDATION: | 498 case STATE_START_PARTIAL_CACHE_VALIDATION: |
490 DCHECK_EQ(OK, rv); | 499 DCHECK_EQ(OK, rv); |
491 rv = DoStartPartialCacheValidation(); | 500 rv = DoStartPartialCacheValidation(); |
492 break; | 501 break; |
493 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION: | 502 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION: |
494 rv = DoCompletePartialCacheValidation(rv); | 503 rv = DoCompletePartialCacheValidation(rv); |
495 break; | 504 break; |
496 case STATE_UPDATE_CACHED_RESPONSE: | 505 case STATE_UPDATE_CACHED_RESPONSE: |
497 DCHECK_EQ(OK, rv); | 506 DCHECK_EQ(OK, rv); |
498 rv = DoUpdateCachedResponse(); | 507 rv = DoUpdateCachedResponse(); |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 partial_->RestoreHeaders(&custom_request_->extra_headers); | 911 partial_->RestoreHeaders(&custom_request_->extra_headers); |
903 next_state_ = STATE_SEND_REQUEST; | 912 next_state_ = STATE_SEND_REQUEST; |
904 } else { | 913 } else { |
905 // We have to read the headers from the cached entry. | 914 // We have to read the headers from the cached entry. |
906 DCHECK(mode_ & READ_META); | 915 DCHECK(mode_ & READ_META); |
907 next_state_ = STATE_CACHE_READ_RESPONSE; | 916 next_state_ = STATE_CACHE_READ_RESPONSE; |
908 } | 917 } |
909 return OK; | 918 return OK; |
910 } | 919 } |
911 | 920 |
| 921 int HttpCache::Transaction::DoNotifyBeforeSendHeaders() { |
| 922 // Balanced in DoNotifyBeforeSendHeadersComplete. |
| 923 cache_callback_->AddRef(); |
| 924 next_state_ = STATE_NOTIFY_BEFORE_SEND_HEADERS_COMPLETE; |
| 925 |
| 926 if (cache_->GetSession() && cache_->GetSession()->network_delegate()) { |
| 927 // TODO(mpcomplete): need to be able to modify these headers. |
| 928 HttpRequestHeaders headers = request_->extra_headers; |
| 929 return cache_->GetSession()->network_delegate()->NotifyBeforeSendHeaders( |
| 930 request_->request_id, &headers, cache_callback_); |
| 931 } |
| 932 |
| 933 return OK; |
| 934 } |
| 935 |
| 936 int HttpCache::Transaction::DoNotifyBeforeSendHeadersComplete(int result) { |
| 937 cache_callback_->Release(); // Balanced in DoNotifyBeforeSendHeaders. |
| 938 |
| 939 // We now have access to the cache entry. |
| 940 // |
| 941 // o if we are a reader for the transaction, then we can start reading the |
| 942 // cache entry. |
| 943 // |
| 944 // o if we can read or write, then we should check if the cache entry needs |
| 945 // to be validated and then issue a network request if needed or just read |
| 946 // from the cache if the cache entry is already valid. |
| 947 // |
| 948 // o if we are set to UPDATE, then we are handling an externally |
| 949 // conditionalized request (if-modified-since / if-none-match). We check |
| 950 // if the request headers define a validation request. |
| 951 // |
| 952 if (result == net::OK) { |
| 953 switch (mode_) { |
| 954 case READ: |
| 955 result = BeginCacheRead(); |
| 956 break; |
| 957 case READ_WRITE: |
| 958 result = BeginPartialCacheValidation(); |
| 959 break; |
| 960 case UPDATE: |
| 961 result = BeginExternallyConditionalizedRequest(); |
| 962 break; |
| 963 case WRITE: |
| 964 default: |
| 965 NOTREACHED(); |
| 966 result = ERR_FAILED; |
| 967 } |
| 968 } |
| 969 return result; |
| 970 } |
| 971 |
912 // We may end up here multiple times for a given request. | 972 // We may end up here multiple times for a given request. |
913 int HttpCache::Transaction::DoStartPartialCacheValidation() { | 973 int HttpCache::Transaction::DoStartPartialCacheValidation() { |
914 if (mode_ == NONE) | 974 if (mode_ == NONE) |
915 return OK; | 975 return OK; |
916 | 976 |
917 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION; | 977 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION; |
918 return partial_->ShouldValidateCache(entry_->disk_entry, &io_callback_); | 978 return partial_->ShouldValidateCache(entry_->disk_entry, &io_callback_); |
919 } | 979 } |
920 | 980 |
921 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { | 981 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1102 read_buf_ = new IOBuffer(io_buf_len_); | 1162 read_buf_ = new IOBuffer(io_buf_len_); |
1103 | 1163 |
1104 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL); | 1164 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL); |
1105 cache_callback_->AddRef(); // Balanced in DoCacheReadResponseComplete. | 1165 cache_callback_->AddRef(); // Balanced in DoCacheReadResponseComplete. |
1106 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_, | 1166 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_, |
1107 io_buf_len_, cache_callback_); | 1167 io_buf_len_, cache_callback_); |
1108 } | 1168 } |
1109 | 1169 |
1110 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { | 1170 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
1111 cache_callback_->Release(); // Balance the AddRef from DoCacheReadResponse. | 1171 cache_callback_->Release(); // Balance the AddRef from DoCacheReadResponse. |
| 1172 next_state_ = STATE_NOTIFY_BEFORE_SEND_HEADERS; |
| 1173 |
1112 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); | 1174 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); |
1113 if (result != io_buf_len_ || | 1175 if (result != io_buf_len_ || |
1114 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, | 1176 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, |
1115 &response_, &truncated_)) { | 1177 &response_, &truncated_)) { |
1116 DLOG(ERROR) << "ReadData failed: " << result; | 1178 DLOG(ERROR) << "ReadData failed: " << result; |
1117 return ERR_CACHE_READ_FAILURE; | 1179 return ERR_CACHE_READ_FAILURE; |
1118 } | 1180 } |
1119 | 1181 |
1120 // We now have access to the cache entry. | 1182 return OK; |
1121 // | |
1122 // o if we are a reader for the transaction, then we can start reading the | |
1123 // cache entry. | |
1124 // | |
1125 // o if we can read or write, then we should check if the cache entry needs | |
1126 // to be validated and then issue a network request if needed or just read | |
1127 // from the cache if the cache entry is already valid. | |
1128 // | |
1129 // o if we are set to UPDATE, then we are handling an externally | |
1130 // conditionalized request (if-modified-since / if-none-match). We check | |
1131 // if the request headers define a validation request. | |
1132 // | |
1133 switch (mode_) { | |
1134 case READ: | |
1135 result = BeginCacheRead(); | |
1136 break; | |
1137 case READ_WRITE: | |
1138 result = BeginPartialCacheValidation(); | |
1139 break; | |
1140 case UPDATE: | |
1141 result = BeginExternallyConditionalizedRequest(); | |
1142 break; | |
1143 case WRITE: | |
1144 default: | |
1145 NOTREACHED(); | |
1146 result = ERR_FAILED; | |
1147 } | |
1148 return result; | |
1149 } | 1183 } |
1150 | 1184 |
1151 int HttpCache::Transaction::DoCacheWriteResponse() { | 1185 int HttpCache::Transaction::DoCacheWriteResponse() { |
1152 if (net_log_.IsLoggingAllEvents() && entry_) | 1186 if (net_log_.IsLoggingAllEvents() && entry_) |
1153 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO, NULL); | 1187 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO, NULL); |
1154 return WriteResponseInfoToEntry(false); | 1188 return WriteResponseInfoToEntry(false); |
1155 } | 1189 } |
1156 | 1190 |
1157 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() { | 1191 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() { |
1158 if (net_log_.IsLoggingAllEvents() && entry_) | 1192 if (net_log_.IsLoggingAllEvents() && entry_) |
(...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1934 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 1968 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
1935 } | 1969 } |
1936 return result; | 1970 return result; |
1937 } | 1971 } |
1938 | 1972 |
1939 void HttpCache::Transaction::OnIOComplete(int result) { | 1973 void HttpCache::Transaction::OnIOComplete(int result) { |
1940 DoLoop(result); | 1974 DoLoop(result); |
1941 } | 1975 } |
1942 | 1976 |
1943 } // namespace net | 1977 } // namespace net |
OLD | NEW |