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

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

Issue 174039: Http cache: Fix the code that handles 206s when revalidating... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | net/http/http_cache_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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.h" 5 #include "net/http/http_cache.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 10
(...skipping 1108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1119 } else { 1119 } else {
1120 custom_request_->extra_headers.append("If-Modified-Since: "); 1120 custom_request_->extra_headers.append("If-Modified-Since: ");
1121 } 1121 }
1122 custom_request_->extra_headers.append(last_modified_value); 1122 custom_request_->extra_headers.append(last_modified_value);
1123 custom_request_->extra_headers.append("\r\n"); 1123 custom_request_->extra_headers.append("\r\n");
1124 } 1124 }
1125 1125
1126 return true; 1126 return true;
1127 } 1127 }
1128 1128
1129 // We just received some headers from the server. We may have asked for a range,
1130 // in which case partial_ has an object. This could be the first network request
1131 // we make to fulfill the original request, or we may be already reading (from
1132 // the net and / or the cache). If we are not expecting a certain response, we
1133 // just bypass the cache for this request (but again, maybe we are reading), and
1134 // delete partial_ (so we are not able to "fix" the headers that we return to
1135 // the user). This results in either a weird response for the caller (we don't
1136 // expect it after all), or maybe a range that was not exactly what it was asked
1137 // for.
1138 //
1139 // For example, if the original request is for 30KB and we have the last 20KB,
1140 // we ask the server for the first 10KB. If the resourse has changed, we'll
1141 // end up forwarding the 200 back to the user (so far so good). However, if
1142 // we have instead the first 10KB, we end up sending back a byte range response
1143 // for the first 10KB, because we never asked the server for the last part. It's
1144 // just too complicated to restart the whole request from this point; and of
1145 // course, maybe we already returned the headers.
1129 bool HttpCache::Transaction::ValidatePartialResponse( 1146 bool HttpCache::Transaction::ValidatePartialResponse(
1130 const HttpResponseHeaders* headers) { 1147 const HttpResponseHeaders* headers) {
1131 int response_code = headers->response_code(); 1148 int response_code = headers->response_code();
1132 #ifdef ENABLE_RANGE_SUPPORT 1149 #ifdef ENABLE_RANGE_SUPPORT
1133 bool partial_content = response_code == 206; 1150 bool partial_content = response_code == 206;
1134 #else 1151 #else
1135 bool partial_content = false; 1152 bool partial_content = false;
1136 #endif 1153 #endif
1137 1154
1138 if (invalid_range_) { 1155 if (invalid_range_) {
1139 // We gave up trying to match this request with the stored data. If the 1156 // We gave up trying to match this request with the stored data. If the
1140 // server is ok with the request, delete the entry, otherwise just ignore 1157 // server is ok with the request, delete the entry, otherwise just ignore
1141 // this request 1158 // this request
1142 if (partial_content || response_code == 200 || response_code == 304) { 1159 if (partial_content || response_code == 200 || response_code == 304) {
1143 DoomPartialEntry(true); 1160 DoomPartialEntry(true);
1144 mode_ = NONE; 1161 mode_ = NONE;
1145 } else { 1162 } else {
1146 IgnoreRangeRequest(); 1163 IgnoreRangeRequest();
1147 } 1164 }
1148 return false; 1165 return false;
1149 } 1166 }
1150 1167
1151 // TODO(rvargas): Handle 206 when the current range is already cached. 1168 if (!partial_.get()) {
1152 bool failure = false; 1169 // We are not expecting 206 but we may have one.
1153 if (!partial_content) { 1170 if (partial_content)
1154 if (!partial_.get()) 1171 IgnoreRangeRequest();
1155 return false;
1156 1172
1157 // TODO(rvargas): Do we need to consider other results here?. 1173 return false;
1158 if (response_code == 200 || response_code == 416) 1174 }
1175
1176 // TODO(rvargas): Do we need to consider other results here?.
1177 bool failure = response_code == 200 || response_code == 416;
1178
1179 if (partial_->IsCurrentRangeCached()) {
1180 // We asked for "If-None-Match: " so a 206 means a new object.
1181 if (partial_content)
1159 failure = true; 1182 failure = true;
1160 1183
1161 if (!reading_ && failure) {
1162 // We are expecting 206 or 304 because we asked for a range. Given that
1163 // the server is refusing the request we'll remove the sparse entry.
1164 DoomPartialEntry(true);
1165 mode_ = NONE;
1166 return false;
1167 }
1168
1169 if (response_code == 304 && partial_->ResponseHeadersOK(headers)) 1184 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
1170 return false; 1185 return false;
1186 } else {
1187 // We asked for "If-Range: " so a 206 means just another range.
1188 if (partial_content && partial_->ResponseHeadersOK(headers))
1189 return true;
1190
1191 // 304 is not expected here, but we'll spare the entry.
1171 } 1192 }
1172 1193
1173 if (!failure && partial_.get() && partial_->ResponseHeadersOK(headers)) 1194 if (failure) {
1174 return true; 1195 // We cannot truncate this entry, it has to be deleted.
1196 DoomPartialEntry(true);
1197 mode_ = NONE;
1198 return false;
1199 }
1175 1200
1176 IgnoreRangeRequest(); 1201 IgnoreRangeRequest();
1177 return false; 1202 return false;
1178 } 1203 }
1179 1204
1180 void HttpCache::Transaction::IgnoreRangeRequest() { 1205 void HttpCache::Transaction::IgnoreRangeRequest() {
1181 // We have a problem. We may or may not be reading already (in which case we 1206 // We have a problem. We may or may not be reading already (in which case we
1182 // returned the headers), but we'll just pretend that this request is not 1207 // returned the headers), but we'll just pretend that this request is not
1183 // using the cache and see what happens. Most likely this is the first 1208 // using the cache and see what happens. Most likely this is the first
1184 // response from the server (it's not changing its mind midway, right?). 1209 // response from the server (it's not changing its mind midway, right?).
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1343 // Are we expecting a response to a conditional query? 1368 // Are we expecting a response to a conditional query?
1344 if (mode_ == READ_WRITE || mode_ == UPDATE) { 1369 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1345 if (new_response->headers->response_code() == 304 || partial_content) { 1370 if (new_response->headers->response_code() == 304 || partial_content) {
1346 // Update cached response based on headers in new_response. 1371 // Update cached response based on headers in new_response.
1347 // TODO(wtc): should we update cached certificate 1372 // TODO(wtc): should we update cached certificate
1348 // (response_.ssl_info), too? 1373 // (response_.ssl_info), too?
1349 response_.headers->Update(*new_response->headers); 1374 response_.headers->Update(*new_response->headers);
1350 if (response_.headers->HasHeaderValue("cache-control", "no-store")) { 1375 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1351 cache_->DoomEntry(cache_key_); 1376 cache_->DoomEntry(cache_key_);
1352 } else { 1377 } else {
1353 WriteResponseInfoToEntry(); 1378 // If we are already reading, we already updated the headers for
1379 // this request; doing it again will change Content-Length.
1380 if (!reading_)
1381 WriteResponseInfoToEntry();
1354 } 1382 }
1355 1383
1356 if (mode_ == UPDATE) { 1384 if (mode_ == UPDATE) {
1357 DCHECK(!partial_content); 1385 DCHECK(!partial_content);
1358 // We got a "not modified" response and already updated the 1386 // We got a "not modified" response and already updated the
1359 // corresponding cache entry above. 1387 // corresponding cache entry above.
1360 // 1388 //
1361 // By closing the cached entry now, we make sure that the 1389 // By closing the cached entry now, we make sure that the
1362 // 304 rather than the cached 200 response, is what will be 1390 // 304 rather than the cached 200 response, is what will be
1363 // returned to the user. 1391 // returned to the user.
(...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after
2075 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); 2103 static_cast<net::HttpNetworkLayer*>(network_layer_.get());
2076 HttpNetworkSession* session = network->GetSession(); 2104 HttpNetworkSession* session = network->GetSession();
2077 if (session) { 2105 if (session) {
2078 session->connection_pool()->CloseIdleSockets(); 2106 session->connection_pool()->CloseIdleSockets();
2079 } 2107 }
2080 } 2108 }
2081 2109
2082 //----------------------------------------------------------------------------- 2110 //-----------------------------------------------------------------------------
2083 2111
2084 } // namespace net 2112 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/http/http_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698