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

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

Issue 7468017: Http Cache: Keep track of whether we are doing byte range (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 5 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 | « net/http/http_cache_transaction.h ('k') | net/http/http_cache_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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>
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 new_entry_(NULL), 106 new_entry_(NULL),
107 network_trans_(NULL), 107 network_trans_(NULL),
108 callback_(NULL), 108 callback_(NULL),
109 new_response_(NULL), 109 new_response_(NULL),
110 mode_(NONE), 110 mode_(NONE),
111 target_state_(STATE_NONE), 111 target_state_(STATE_NONE),
112 reading_(false), 112 reading_(false),
113 invalid_range_(false), 113 invalid_range_(false),
114 truncated_(false), 114 truncated_(false),
115 is_sparse_(false), 115 is_sparse_(false),
116 range_requested_(false),
116 handling_206_(false), 117 handling_206_(false),
117 cache_pending_(false), 118 cache_pending_(false),
118 done_reading_(false), 119 done_reading_(false),
119 read_offset_(0), 120 read_offset_(0),
120 effective_load_flags_(0), 121 effective_load_flags_(0),
121 write_len_(0), 122 write_len_(0),
122 final_upload_progress_(0), 123 final_upload_progress_(0),
123 ALLOW_THIS_IN_INITIALIZER_LIST( 124 ALLOW_THIS_IN_INITIALIZER_LIST(
124 io_callback_(this, &Transaction::OnIOComplete)), 125 io_callback_(this, &Transaction::OnIOComplete)),
125 ALLOW_THIS_IN_INITIALIZER_LIST( 126 ALLOW_THIS_IN_INITIALIZER_LIST(
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 } 669 }
669 } 670 }
670 } 671 }
671 672
672 // If must use cache, then we must fail. This can happen for back/forward 673 // If must use cache, then we must fail. This can happen for back/forward
673 // navigations to a page generated via a form post. 674 // navigations to a page generated via a form post.
674 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE) 675 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
675 return ERR_CACHE_MISS; 676 return ERR_CACHE_MISS;
676 677
677 if (mode_ == NONE) { 678 if (mode_ == NONE) {
678 if (partial_.get()) 679 if (partial_.get()) {
679 partial_->RestoreHeaders(&custom_request_->extra_headers); 680 partial_->RestoreHeaders(&custom_request_->extra_headers);
681 partial_.reset();
682 }
680 next_state_ = STATE_SEND_REQUEST; 683 next_state_ = STATE_SEND_REQUEST;
681 } else { 684 } else {
682 next_state_ = STATE_INIT_ENTRY; 685 next_state_ = STATE_INIT_ENTRY;
683 } 686 }
684 687
688 // This is only set if we have something to do with the response.
689 range_requested_ = (partial_.get() != NULL);
690
685 return OK; 691 return OK;
686 } 692 }
687 693
688 int HttpCache::Transaction::DoSendRequest() { 694 int HttpCache::Transaction::DoSendRequest() {
689 DCHECK(mode_ & WRITE || mode_ == NONE); 695 DCHECK(mode_ & WRITE || mode_ == NONE);
690 DCHECK(!network_trans_.get()); 696 DCHECK(!network_trans_.get());
691 697
692 // Create a network transaction. 698 // Create a network transaction.
693 int rv = cache_->network_layer_->CreateTransaction(&network_trans_); 699 int rv = cache_->network_layer_->CreateTransaction(&network_trans_);
694 if (rv != OK) 700 if (rv != OK)
695 return rv; 701 return rv;
696 702
697 next_state_ = STATE_SEND_REQUEST_COMPLETE; 703 next_state_ = STATE_SEND_REQUEST_COMPLETE;
698 rv = network_trans_->Start(request_, &io_callback_, net_log_); 704 rv = network_trans_->Start(request_, &io_callback_, net_log_);
699 return rv; 705 return rv;
700 } 706 }
701 707
702 int HttpCache::Transaction::DoSendRequestComplete(int result) { 708 int HttpCache::Transaction::DoSendRequestComplete(int result) {
703 if (!cache_) 709 if (!cache_)
704 return ERR_UNEXPECTED; 710 return ERR_UNEXPECTED;
705 711
706 if (result == OK) { 712 if (result == OK) {
707 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST; 713 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
708 return OK; 714 return OK;
709 } 715 }
710 716
711 if (IsCertificateError(result)) { 717 if (IsCertificateError(result)) {
712 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); 718 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
713 // If we get a certificate error, then there is a certificate in ssl_info, 719 // If we get a certificate error, then there is a certificate in ssl_info,
714 // so GetResponseInfo() should never returns NULL here. 720 // so GetResponseInfo() should never return NULL here.
715 DCHECK(response); 721 DCHECK(response);
716 response_.ssl_info = response->ssl_info; 722 response_.ssl_info = response->ssl_info;
717 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { 723 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
718 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); 724 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
719 DCHECK(response); 725 DCHECK(response);
720 response_.cert_request_info = response->cert_request_info; 726 response_.cert_request_info = response->cert_request_info;
721 } 727 }
722 return result; 728 return result;
723 } 729 }
724 730
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after
1265 DLOG(ERROR) << "ReadData failed: " << result; 1271 DLOG(ERROR) << "ReadData failed: " << result;
1266 return ERR_CACHE_READ_FAILURE; 1272 return ERR_CACHE_READ_FAILURE;
1267 } 1273 }
1268 1274
1269 return OK; 1275 return OK;
1270 } 1276 }
1271 1277
1272 int HttpCache::Transaction::DoCacheQueryData() { 1278 int HttpCache::Transaction::DoCacheQueryData() {
1273 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE; 1279 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1274 1280
1275 // Balanced in ValidateEntryHeadersAndContinue. 1281 // Balanced in DoCacheQueryDataComplete.
gavinp 2011/07/21 22:11:23 So these were just plain wrong? Good catch. And
rvargas (doing something else) 2011/07/21 23:05:18 Left behind by some refactor.
1276 cache_callback_->AddRef(); 1282 cache_callback_->AddRef();
1277 return entry_->disk_entry->ReadyForSparseIO(cache_callback_); 1283 return entry_->disk_entry->ReadyForSparseIO(cache_callback_);
1278 } 1284 }
1279 1285
1280 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { 1286 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1281 DCHECK_EQ(OK, result); 1287 DCHECK_EQ(OK, result);
1282 // Balance the AddRef from BeginPartialCacheValidation. 1288 // Balance the AddRef from DoCacheQueryData.
1283 cache_callback_->Release(); 1289 cache_callback_->Release();
1284 if (!cache_) 1290 if (!cache_)
1285 return ERR_UNEXPECTED; 1291 return ERR_UNEXPECTED;
1286 1292
1287 return ValidateEntryHeadersAndContinue(true); 1293 return ValidateEntryHeadersAndContinue();
1288 } 1294 }
1289 1295
1290 int HttpCache::Transaction::DoCacheReadData() { 1296 int HttpCache::Transaction::DoCacheReadData() {
1291 DCHECK(entry_); 1297 DCHECK(entry_);
1292 next_state_ = STATE_CACHE_READ_DATA_COMPLETE; 1298 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1293 cache_callback_->AddRef(); // Balanced in DoCacheReadDataComplete. 1299 cache_callback_->AddRef(); // Balanced in DoCacheReadDataComplete.
1294 1300
1295 if (net_log_.IsLoggingAllEvents()) 1301 if (net_log_.IsLoggingAllEvents())
1296 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA, NULL); 1302 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA, NULL);
1297 if (partial_.get()) { 1303 if (partial_.get()) {
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
1558 return OK; 1564 return OK;
1559 } 1565 }
1560 1566
1561 int HttpCache::Transaction::BeginPartialCacheValidation() { 1567 int HttpCache::Transaction::BeginPartialCacheValidation() {
1562 DCHECK(mode_ == READ_WRITE); 1568 DCHECK(mode_ == READ_WRITE);
1563 1569
1564 if (response_.headers->response_code() != 206 && !partial_.get() && 1570 if (response_.headers->response_code() != 206 && !partial_.get() &&
1565 !truncated_) 1571 !truncated_)
1566 return BeginCacheValidation(); 1572 return BeginCacheValidation();
1567 1573
1568 bool byte_range_requested = partial_.get() != NULL; 1574 if (range_requested_) {
1569 if (byte_range_requested) {
1570 next_state_ = STATE_CACHE_QUERY_DATA; 1575 next_state_ = STATE_CACHE_QUERY_DATA;
1571 return OK; 1576 return OK;
1572 } 1577 }
1573 // The request is not for a range, but we have stored just ranges. 1578 // The request is not for a range, but we have stored just ranges.
1574 partial_.reset(new PartialData()); 1579 partial_.reset(new PartialData());
1575 partial_->SetHeaders(request_->extra_headers); 1580 partial_->SetHeaders(request_->extra_headers);
1576 if (!custom_request_.get()) { 1581 if (!custom_request_.get()) {
1577 custom_request_.reset(new HttpRequestInfo(*request_)); 1582 custom_request_.reset(new HttpRequestInfo(*request_));
1578 request_ = custom_request_.get(); 1583 request_ = custom_request_.get();
1579 } 1584 }
1580 1585
1581 return ValidateEntryHeadersAndContinue(false); 1586 return ValidateEntryHeadersAndContinue();
1582 } 1587 }
1583 1588
1584 // This should only be called once per request. 1589 // This should only be called once per request.
1585 int HttpCache::Transaction::ValidateEntryHeadersAndContinue( 1590 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
1586 bool byte_range_requested) {
1587 DCHECK(mode_ == READ_WRITE); 1591 DCHECK(mode_ == READ_WRITE);
1588 1592
1589 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry, 1593 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry,
1590 truncated_)) { 1594 truncated_)) {
1591 // The stored data cannot be used. Get rid of it and restart this request. 1595 // The stored data cannot be used. Get rid of it and restart this request.
1592 // We need to also reset the |truncated_| flag as a new entry is created. 1596 // We need to also reset the |truncated_| flag as a new entry is created.
1593 DoomPartialEntry(!byte_range_requested); 1597 DoomPartialEntry(!range_requested_);
1594 mode_ = WRITE; 1598 mode_ = WRITE;
1595 truncated_ = false; 1599 truncated_ = false;
1596 next_state_ = STATE_INIT_ENTRY; 1600 next_state_ = STATE_INIT_ENTRY;
1597 return OK; 1601 return OK;
1598 } 1602 }
1599 1603
1600 if (response_.headers->response_code() == 206) 1604 if (response_.headers->response_code() == 206)
1601 is_sparse_ = true; 1605 is_sparse_ = true;
1602 1606
1603 if (!partial_->IsRequestedRangeOK()) { 1607 if (!partial_->IsRequestedRangeOK()) {
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 1829
1826 if (response_code == 304 && partial_->ResponseHeadersOK(headers)) 1830 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
1827 return true; 1831 return true;
1828 } else { 1832 } else {
1829 // We asked for "If-Range: " so a 206 means just another range. 1833 // We asked for "If-Range: " so a 206 means just another range.
1830 if (partial_response && partial_->ResponseHeadersOK(headers)) { 1834 if (partial_response && partial_->ResponseHeadersOK(headers)) {
1831 handling_206_ = true; 1835 handling_206_ = true;
1832 return true; 1836 return true;
1833 } 1837 }
1834 1838
1839 if (response_code == 200 && !reading_ && !is_sparse_) {
1840 // The server is sending the whole resource, and we can save it.
1841 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
1842 partial_.reset();
1843 truncated_ = false;
1844 return true;
1845 }
1846
1835 // 304 is not expected here, but we'll spare the entry (unless it was 1847 // 304 is not expected here, but we'll spare the entry (unless it was
1836 // truncated). 1848 // truncated).
1837 if (truncated_) { 1849 if (truncated_)
1838 if (!reading_ && response_code == 200) {
1839 // The server is sending the whole resource, and we can save it.
1840 DCHECK(!partial_->IsLastRange());
1841 partial_.reset();
1842 truncated_ = false;
1843 return true;
1844 }
1845 failure = true; 1850 failure = true;
1846 }
1847 } 1851 }
1848 1852
1849 if (failure) { 1853 if (failure) {
1850 // We cannot truncate this entry, it has to be deleted. 1854 // We cannot truncate this entry, it has to be deleted.
1851 DoomPartialEntry(false); 1855 DoomPartialEntry(false);
1852 mode_ = NONE; 1856 mode_ = NONE;
1853 if (!reading_ && !partial_->IsLastRange()) { 1857 if (!reading_ && !partial_->IsLastRange()) {
1854 // We'll attempt to issue another network request, this time without us 1858 // We'll attempt to issue another network request, this time without us
1855 // messing up the headers. 1859 // messing up the headers.
1856 partial_->RestoreHeaders(&custom_request_->extra_headers); 1860 partial_->RestoreHeaders(&custom_request_->extra_headers);
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
2041 return false; 2045 return false;
2042 2046
2043 return true; 2047 return true;
2044 } 2048 }
2045 2049
2046 void HttpCache::Transaction::OnIOComplete(int result) { 2050 void HttpCache::Transaction::OnIOComplete(int result) {
2047 DoLoop(result); 2051 DoLoop(result);
2048 } 2052 }
2049 2053
2050 } // namespace net 2054 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.h ('k') | net/http/http_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698