summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-08 23:51:23 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-08 23:51:23 +0000
commit4d91e2fea8f51b76679542893668e19fc274b287 (patch)
tree7ea8fd7c2a84b35fbd87ff95444357dae6b556a1 /net
parente9adf0702e30800cdfaebce8cfacff7b444b4e79 (diff)
downloadchromium_src-4d91e2fea8f51b76679542893668e19fc274b287.zip
chromium_src-4d91e2fea8f51b76679542893668e19fc274b287.tar.gz
chromium_src-4d91e2fea8f51b76679542893668e19fc274b287.tar.bz2
Http cache: Reorder the files after the previous mess-up.
No real code change. BUG=none TEST=none Review URL: http://codereview.chromium.org/669142 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40967 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/http/http_cache.cc124
-rw-r--r--net/http/http_cache_transaction.cc1132
-rw-r--r--net/http/http_cache_transaction.h3
3 files changed, 630 insertions, 629 deletions
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index 32e8e55..8b0811f 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -317,14 +317,6 @@ void HttpCache::Suspend(bool suspend) {
}
// static
-bool HttpCache::ParseResponseInfo(const char* data, int len,
- HttpResponseInfo* response_info,
- bool* response_truncated) {
- Pickle pickle(data, len);
- return response_info->InitFromPickle(pickle, response_truncated);
-}
-
-// static
bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry,
HttpResponseInfo* response_info,
bool* response_truncated) {
@@ -358,6 +350,14 @@ bool HttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry,
true) == len;
}
+// static
+bool HttpCache::ParseResponseInfo(const char* data, int len,
+ HttpResponseInfo* response_info,
+ bool* response_truncated) {
+ Pickle pickle(data, len);
+ return response_info->InitFromPickle(pickle, response_truncated);
+}
+
void HttpCache::WriteMetadata(const GURL& url,
base::Time expected_response_time, IOBuffer* buf,
int buf_len) {
@@ -373,6 +373,20 @@ void HttpCache::WriteMetadata(const GURL& url,
writer->Write(url, expected_response_time, buf, buf_len);
}
+void HttpCache::CloseCurrentConnections() {
+ net::HttpNetworkLayer* network =
+ static_cast<net::HttpNetworkLayer*>(network_layer_.get());
+ HttpNetworkSession* session = network->GetSession();
+ if (session) {
+ session->tcp_socket_pool()->CloseIdleSockets();
+ if (session->spdy_session_pool())
+ session->spdy_session_pool()->CloseAllSessions();
+ session->ReplaceTCPSocketPool();
+ }
+}
+
+//-----------------------------------------------------------------------------
+
// Generate a key that can be used inside the cache.
std::string HttpCache::GenerateCacheKey(const HttpRequestInfo* request) {
// Strip out the reference, username, and password sections of the URL.
@@ -477,6 +491,46 @@ HttpCache::ActiveEntry* HttpCache::FindActiveEntry(const std::string& key) {
return it != active_entries_.end() ? it->second : NULL;
}
+HttpCache::ActiveEntry* HttpCache::ActivateEntry(
+ const std::string& key,
+ disk_cache::Entry* disk_entry) {
+ DCHECK(!FindActiveEntry(key));
+ ActiveEntry* entry = new ActiveEntry(disk_entry);
+ active_entries_[key] = entry;
+ return entry;
+}
+
+void HttpCache::DeactivateEntry(ActiveEntry* entry) {
+ DCHECK(!entry->will_process_pending_queue);
+ DCHECK(!entry->doomed);
+ DCHECK(!entry->writer);
+ DCHECK(entry->readers.empty());
+ DCHECK(entry->pending_queue.empty());
+
+ std::string key = entry->disk_entry->GetKey();
+ if (key.empty())
+ return SlowDeactivateEntry(entry);
+
+ ActiveEntriesMap::iterator it = active_entries_.find(key);
+ DCHECK(it != active_entries_.end());
+ DCHECK(it->second == entry);
+
+ active_entries_.erase(it);
+ delete entry;
+}
+
+// We don't know this entry's key so we have to find it without it.
+void HttpCache::SlowDeactivateEntry(ActiveEntry* entry) {
+ for (ActiveEntriesMap::iterator it = active_entries_.begin();
+ it != active_entries_.end(); ++it) {
+ if (it->second == entry) {
+ active_entries_.erase(it);
+ delete entry;
+ break;
+ }
+ }
+}
+
HttpCache::NewEntry* HttpCache::GetNewEntry(const std::string& key) {
DCHECK(!FindActiveEntry(key));
@@ -573,46 +627,6 @@ void HttpCache::DestroyEntry(ActiveEntry* entry) {
}
}
-HttpCache::ActiveEntry* HttpCache::ActivateEntry(
- const std::string& key,
- disk_cache::Entry* disk_entry) {
- DCHECK(!FindActiveEntry(key));
- ActiveEntry* entry = new ActiveEntry(disk_entry);
- active_entries_[key] = entry;
- return entry;
-}
-
-void HttpCache::DeactivateEntry(ActiveEntry* entry) {
- DCHECK(!entry->will_process_pending_queue);
- DCHECK(!entry->doomed);
- DCHECK(!entry->writer);
- DCHECK(entry->readers.empty());
- DCHECK(entry->pending_queue.empty());
-
- std::string key = entry->disk_entry->GetKey();
- if (key.empty())
- return SlowDeactivateEntry(entry);
-
- ActiveEntriesMap::iterator it = active_entries_.find(key);
- DCHECK(it != active_entries_.end());
- DCHECK(it->second == entry);
-
- active_entries_.erase(it);
- delete entry;
-}
-
-// We don't know this entry's key so we have to find it without it.
-void HttpCache::SlowDeactivateEntry(ActiveEntry* entry) {
- for (ActiveEntriesMap::iterator it = active_entries_.begin();
- it != active_entries_.end(); ++it) {
- if (it->second == entry) {
- active_entries_.erase(it);
- delete entry;
- break;
- }
- }
-}
-
int HttpCache::AddTransactionToEntry(ActiveEntry* entry, Transaction* trans) {
DCHECK(entry);
@@ -900,18 +914,4 @@ void HttpCache::OnIOComplete(int result, NewEntry* new_entry) {
}
}
-void HttpCache::CloseCurrentConnections() {
- net::HttpNetworkLayer* network =
- static_cast<net::HttpNetworkLayer*>(network_layer_.get());
- HttpNetworkSession* session = network->GetSession();
- if (session) {
- session->tcp_socket_pool()->CloseIdleSockets();
- if (session->spdy_session_pool())
- session->spdy_session_pool()->CloseAllSessions();
- session->ReplaceTCPSocketPool();
- }
-}
-
-//-----------------------------------------------------------------------------
-
} // namespace net
diff --git a/net/http/http_cache_transaction.cc b/net/http/http_cache_transaction.cc
index 332a687..2192342 100644
--- a/net/http/http_cache_transaction.cc
+++ b/net/http/http_cache_transaction.cc
@@ -386,41 +386,299 @@ int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
callback, true);
}
-int HttpCache::Transaction::AddToEntry() {
- next_state_ = STATE_INIT_ENTRY;
- cache_pending_ = false;
- return DoLoop(OK);
+bool HttpCache::Transaction::AddTruncatedFlag() {
+ DCHECK(mode_ & WRITE);
+
+ // Don't set the flag for sparse entries.
+ if (partial_.get() && !truncated_)
+ return true;
+
+ // Double check that there is something worth keeping.
+ if (!entry_->disk_entry->GetDataSize(kResponseContentIndex))
+ return false;
+
+ if (response_.headers->GetContentLength() <= 0 ||
+ response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
+ !response_.headers->HasStrongValidators())
+ return false;
+
+ truncated_ = true;
+ target_state_ = STATE_NONE;
+ next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
+ DoLoop(OK);
+ return true;
}
-int HttpCache::Transaction::DoInitEntry() {
- DCHECK(!new_entry_);
+//-----------------------------------------------------------------------------
+
+void HttpCache::Transaction::DoCallback(int rv) {
+ DCHECK(rv != ERR_IO_PENDING);
+ DCHECK(callback_);
+ // Since Run may result in Read being called, clear callback_ up front.
+ CompletionCallback* c = callback_;
+ callback_ = NULL;
+ c->Run(rv);
+}
+
+int HttpCache::Transaction::HandleResult(int rv) {
+ DCHECK(rv != ERR_IO_PENDING);
+ if (callback_)
+ DoCallback(rv);
+ return rv;
+}
+
+int HttpCache::Transaction::DoLoop(int result) {
+ DCHECK(next_state_ != STATE_NONE);
+
+ int rv = result;
+ do {
+ State state = next_state_;
+ next_state_ = STATE_NONE;
+ switch (state) {
+ case STATE_SEND_REQUEST:
+ DCHECK_EQ(OK, rv);
+ rv = DoSendRequest();
+ break;
+ case STATE_SEND_REQUEST_COMPLETE:
+ rv = DoSendRequestComplete(rv);
+ break;
+ case STATE_SUCCESSFUL_SEND_REQUEST:
+ DCHECK_EQ(OK, rv);
+ rv = DoSuccessfulSendRequest();
+ break;
+ case STATE_NETWORK_READ:
+ DCHECK_EQ(OK, rv);
+ rv = DoNetworkRead();
+ break;
+ case STATE_NETWORK_READ_COMPLETE:
+ rv = DoNetworkReadComplete(rv);
+ break;
+ case STATE_INIT_ENTRY:
+ DCHECK_EQ(OK, rv);
+ rv = DoInitEntry();
+ break;
+ case STATE_OPEN_ENTRY:
+ DCHECK_EQ(OK, rv);
+ rv = DoOpenEntry();
+ break;
+ case STATE_OPEN_ENTRY_COMPLETE:
+ rv = DoOpenEntryComplete(rv);
+ break;
+ case STATE_CREATE_ENTRY:
+ DCHECK_EQ(OK, rv);
+ rv = DoCreateEntry();
+ break;
+ case STATE_CREATE_ENTRY_COMPLETE:
+ rv = DoCreateEntryComplete(rv);
+ break;
+ case STATE_DOOM_ENTRY:
+ DCHECK_EQ(OK, rv);
+ rv = DoDoomEntry();
+ break;
+ case STATE_DOOM_ENTRY_COMPLETE:
+ rv = DoDoomEntryComplete(rv);
+ break;
+ case STATE_ADD_TO_ENTRY:
+ DCHECK_EQ(OK, rv);
+ rv = DoAddToEntry();
+ break;
+ case STATE_ADD_TO_ENTRY_COMPLETE:
+ rv = DoAddToEntryComplete(rv);
+ break;
+ case STATE_PARTIAL_CACHE_VALIDATION:
+ DCHECK_EQ(OK, rv);
+ rv = DoPartialCacheValidation();
+ break;
+ case STATE_UPDATE_CACHED_RESPONSE:
+ DCHECK_EQ(OK, rv);
+ rv = DoUpdateCachedResponse();
+ break;
+ case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
+ rv = DoUpdateCachedResponseComplete(rv);
+ break;
+ case STATE_OVERWRITE_CACHED_RESPONSE:
+ DCHECK_EQ(OK, rv);
+ rv = DoOverwriteCachedResponse();
+ break;
+ case STATE_TRUNCATE_CACHED_DATA:
+ DCHECK_EQ(OK, rv);
+ rv = DoTruncateCachedData();
+ break;
+ case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
+ rv = DoTruncateCachedDataComplete(rv);
+ break;
+ case STATE_PARTIAL_HEADERS_RECEIVED:
+ DCHECK_EQ(OK, rv);
+ rv = DoPartialHeadersReceived();
+ break;
+ case STATE_CACHE_READ_RESPONSE:
+ DCHECK_EQ(OK, rv);
+ rv = DoCacheReadResponse();
+ break;
+ case STATE_CACHE_READ_RESPONSE_COMPLETE:
+ rv = DoCacheReadResponseComplete(rv);
+ break;
+ case STATE_CACHE_WRITE_RESPONSE:
+ DCHECK_EQ(OK, rv);
+ rv = DoCacheWriteResponse();
+ break;
+ case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
+ DCHECK_EQ(OK, rv);
+ rv = DoCacheWriteTruncatedResponse();
+ break;
+ case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
+ rv = DoCacheWriteResponseComplete(rv);
+ break;
+ case STATE_CACHE_READ_METADATA:
+ DCHECK_EQ(OK, rv);
+ rv = DoCacheReadMetadata();
+ break;
+ case STATE_CACHE_READ_METADATA_COMPLETE:
+ rv = DoCacheReadMetadataComplete(rv);
+ break;
+ case STATE_CACHE_QUERY_DATA:
+ DCHECK_EQ(OK, rv);
+ rv = DoCacheQueryData();
+ break;
+ case STATE_CACHE_QUERY_DATA_COMPLETE:
+ rv = DoCacheQueryDataComplete(rv);
+ break;
+ case STATE_CACHE_READ_DATA:
+ DCHECK_EQ(OK, rv);
+ rv = DoCacheReadData();
+ break;
+ case STATE_CACHE_READ_DATA_COMPLETE:
+ rv = DoCacheReadDataComplete(rv);
+ break;
+ case STATE_CACHE_WRITE_DATA:
+ rv = DoCacheWriteData(rv);
+ break;
+ case STATE_CACHE_WRITE_DATA_COMPLETE:
+ rv = DoCacheWriteDataComplete(rv);
+ break;
+ default:
+ NOTREACHED() << "bad state";
+ rv = ERR_FAILED;
+ break;
+ }
+ } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
+
+ if (rv != ERR_IO_PENDING)
+ HandleResult(rv);
+
+ return rv;
+}
+
+int HttpCache::Transaction::DoSendRequest() {
+ DCHECK(mode_ & WRITE || mode_ == NONE);
+ DCHECK(!network_trans_.get());
+
+ // Create a network transaction.
+ int rv = cache_->network_layer_->CreateTransaction(&network_trans_);
+ if (rv != OK)
+ return rv;
+
+ next_state_ = STATE_SEND_REQUEST_COMPLETE;
+ rv = network_trans_->Start(request_, &io_callback_, load_log_);
+ return rv;
+}
+
+int HttpCache::Transaction::DoSendRequestComplete(int result) {
if (!cache_)
return ERR_UNEXPECTED;
- if (mode_ == WRITE) {
- next_state_ = STATE_DOOM_ENTRY;
+ if (result == OK) {
+ next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
return OK;
}
- next_state_ = STATE_OPEN_ENTRY;
+ if (IsCertificateError(result)) {
+ const HttpResponseInfo* response = network_trans_->GetResponseInfo();
+ // If we get a certificate error, then there is a certificate in ssl_info,
+ // so GetResponseInfo() should never returns NULL here.
+ DCHECK(response);
+ response_.ssl_info = response->ssl_info;
+ } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
+ const HttpResponseInfo* response = network_trans_->GetResponseInfo();
+ DCHECK(response);
+ response_.cert_request_info = response->cert_request_info;
+ }
+ return result;
+}
+
+// We received the response headers and there is no error.
+int HttpCache::Transaction::DoSuccessfulSendRequest() {
+ DCHECK(!new_response_);
+ const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
+ if (new_response->headers->response_code() == 401 ||
+ new_response->headers->response_code() == 407) {
+ auth_response_ = *new_response;
+ return OK;
+ }
+
+ if (!ValidatePartialResponse(new_response->headers, &server_responded_206_) &&
+ !auth_response_.headers) {
+ // Something went wrong with this request and we have to restart it.
+ // If we have an authentication response, we are exposed to weird things
+ // hapenning if the user cancels the authentication before we receive
+ // the new response.
+ response_ = HttpResponseInfo();
+ network_trans_.reset();
+ next_state_ = STATE_SEND_REQUEST;
+ return OK;
+ }
+ if (server_responded_206_ && mode_ == READ_WRITE && !truncated_ &&
+ response_.headers->response_code() == 200) {
+ // We have stored the full entry, but it changed and the server is
+ // sending a range. We have to delete the old entry.
+ DoneWritingToEntry(false);
+ }
+
+ HistogramHeaders(new_response->headers);
+
+ new_response_ = new_response;
+ // Are we expecting a response to a conditional query?
+ if (mode_ == READ_WRITE || mode_ == UPDATE) {
+ if (new_response->headers->response_code() == 304 ||
+ server_responded_206_) {
+ next_state_ = STATE_UPDATE_CACHED_RESPONSE;
+ return OK;
+ }
+ mode_ = WRITE;
+ }
+
+ next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
return OK;
}
-int HttpCache::Transaction::DoDoomEntry() {
- next_state_ = STATE_DOOM_ENTRY_COMPLETE;
- cache_pending_ = true;
- LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
- return cache_->DoomEntry(cache_key_, this);
+int HttpCache::Transaction::DoNetworkRead() {
+ next_state_ = STATE_NETWORK_READ_COMPLETE;
+ return network_trans_->Read(read_buf_, io_buf_len_, &io_callback_);
}
-int HttpCache::Transaction::DoDoomEntryComplete(int result) {
- LoadLog::EndEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
- next_state_ = STATE_CREATE_ENTRY;
- cache_pending_ = false;
- if (result == ERR_CACHE_RACE)
- next_state_ = STATE_INIT_ENTRY;
+int HttpCache::Transaction::DoNetworkReadComplete(int result) {
+ DCHECK(mode_ & WRITE || mode_ == NONE);
+
+ if (!cache_)
+ return ERR_UNEXPECTED;
+
+ next_state_ = STATE_CACHE_WRITE_DATA;
+ return result;
+}
+
+int HttpCache::Transaction::DoInitEntry() {
+ DCHECK(!new_entry_);
+
+ if (!cache_)
+ return ERR_UNEXPECTED;
+ if (mode_ == WRITE) {
+ next_state_ = STATE_DOOM_ENTRY;
+ return OK;
+ }
+
+ next_state_ = STATE_OPEN_ENTRY;
return OK;
}
@@ -502,6 +760,23 @@ int HttpCache::Transaction::DoCreateEntryComplete(int result) {
return OK;
}
+int HttpCache::Transaction::DoDoomEntry() {
+ next_state_ = STATE_DOOM_ENTRY_COMPLETE;
+ cache_pending_ = true;
+ LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
+ return cache_->DoomEntry(cache_key_, this);
+}
+
+int HttpCache::Transaction::DoDoomEntryComplete(int result) {
+ LoadLog::EndEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
+ next_state_ = STATE_CREATE_ENTRY;
+ cache_pending_ = false;
+ if (result == ERR_CACHE_RACE)
+ next_state_ = STATE_INIT_ENTRY;
+
+ return OK;
+}
+
int HttpCache::Transaction::DoAddToEntry() {
DCHECK(new_entry_);
cache_pending_ = true;
@@ -543,6 +818,160 @@ int HttpCache::Transaction::DoAddToEntryComplete(int result) {
return OK;
}
+int HttpCache::Transaction::DoPartialCacheValidation() {
+ if (mode_ == NONE)
+ return OK;
+
+ int rv = partial_->PrepareCacheValidation(entry_->disk_entry,
+ &custom_request_->extra_headers);
+
+ if (!rv) {
+ // This is the end of the request.
+ if (mode_ & WRITE) {
+ DoneWritingToEntry(true);
+ } else {
+ cache_->DoneReadingFromEntry(entry_, this);
+ entry_ = NULL;
+ }
+ return rv;
+ }
+
+ if (rv < 0) {
+ DCHECK(rv != ERR_IO_PENDING);
+ return rv;
+ }
+
+ if (reading_ && partial_->IsCurrentRangeCached()) {
+ next_state_ = STATE_CACHE_READ_DATA;
+ return OK;
+ }
+
+ return BeginCacheValidation();
+}
+
+// We received 304 or 206 and we want to update the cached response headers.
+int HttpCache::Transaction::DoUpdateCachedResponse() {
+ next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
+ int rv = OK;
+ // Update cached response based on headers in new_response.
+ // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
+ response_.headers->Update(*new_response_->headers);
+ response_.response_time = new_response_->response_time;
+ response_.request_time = new_response_->request_time;
+
+ if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
+ int ret = cache_->DoomEntry(cache_key_, NULL);
+ DCHECK_EQ(OK, ret);
+ } else {
+ // If we are already reading, we already updated the headers for this
+ // request; doing it again will change Content-Length.
+ if (!reading_) {
+ target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
+ next_state_ = STATE_CACHE_WRITE_RESPONSE;
+ rv = OK;
+ }
+ }
+ return rv;
+}
+
+int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
+ if (mode_ == UPDATE) {
+ DCHECK(!server_responded_206_);
+ // We got a "not modified" response and already updated the corresponding
+ // cache entry above.
+ //
+ // By closing the cached entry now, we make sure that the 304 rather than
+ // the cached 200 response, is what will be returned to the user.
+ DoneWritingToEntry(true);
+ } else if (entry_ && !server_responded_206_) {
+ DCHECK_EQ(READ_WRITE, mode_);
+ if (!partial_.get() || partial_->IsLastRange()) {
+ cache_->ConvertWriterToReader(entry_);
+ mode_ = READ;
+ }
+ // We no longer need the network transaction, so destroy it.
+ final_upload_progress_ = network_trans_->GetUploadProgress();
+ network_trans_.reset();
+ }
+ next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
+ return OK;
+}
+
+int HttpCache::Transaction::DoOverwriteCachedResponse() {
+ if (mode_ & READ) {
+ next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
+ return OK;
+ }
+
+ // We change the value of Content-Length for partial content.
+ if (server_responded_206_ && partial_.get())
+ partial_->FixContentLength(new_response_->headers);
+
+ response_ = *new_response_;
+ target_state_ = STATE_TRUNCATE_CACHED_DATA;
+ next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
+ STATE_CACHE_WRITE_RESPONSE;
+ return OK;
+}
+
+int HttpCache::Transaction::DoTruncateCachedData() {
+ next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
+ if (!entry_)
+ return OK;
+
+ // Truncate the stream.
+ int rv = WriteToEntry(kResponseContentIndex, 0, NULL, 0, NULL);
+ DCHECK(rv != ERR_IO_PENDING);
+ rv = WriteToEntry(kMetadataIndex, 0, NULL, 0, NULL);
+ DCHECK(rv != ERR_IO_PENDING);
+ return OK;
+}
+
+int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
+ // If this response is a redirect, then we can stop writing now. (We don't
+ // need to cache the response body of a redirect.)
+ if (response_.headers->IsRedirect(NULL))
+ DoneWritingToEntry(true);
+ next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
+ return OK;
+}
+
+int HttpCache::Transaction::DoPartialHeadersReceived() {
+ new_response_ = NULL;
+ if (entry_ && !partial_.get() &&
+ entry_->disk_entry->GetDataSize(kMetadataIndex))
+ next_state_ = STATE_CACHE_READ_METADATA;
+
+ if (!partial_.get())
+ return OK;
+
+ if (reading_) {
+ if (network_trans_.get()) {
+ next_state_ = STATE_NETWORK_READ;
+ } else {
+ next_state_ = STATE_CACHE_READ_DATA;
+ }
+ } else if (mode_ != NONE) {
+ // We are about to return the headers for a byte-range request to the user,
+ // so let's fix them.
+ partial_->FixResponseHeaders(response_.headers);
+ }
+ return OK;
+}
+
+int HttpCache::Transaction::DoCacheReadResponse() {
+ DCHECK(entry_);
+ next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
+
+ io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
+ read_buf_ = new IOBuffer(io_buf_len_);
+
+ LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO);
+ cache_callback_->AddRef(); // Balanced in DoCacheReadResponseComplete.
+ return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_,
+ io_buf_len_, cache_callback_);
+}
+
int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
cache_callback_->Release(); // Balance the AddRef from DoCacheReadResponse.
LoadLog::EndEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO);
@@ -584,188 +1013,135 @@ int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
return result;
}
-bool HttpCache::Transaction::AddTruncatedFlag() {
- DCHECK(mode_ & WRITE);
+int HttpCache::Transaction::DoCacheWriteResponse() {
+ return WriteResponseInfoToEntry(false);
+}
- // Don't set the flag for sparse entries.
- if (partial_.get() && !truncated_)
- return true;
+int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
+ return WriteResponseInfoToEntry(true);
+}
- // Double check that there is something worth keeping.
- if (!entry_->disk_entry->GetDataSize(kResponseContentIndex))
- return false;
+int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
+ next_state_ = target_state_;
+ target_state_ = STATE_NONE;
+ if (!entry_)
+ return OK;
- if (response_.headers->GetContentLength() <= 0 ||
- response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
- !response_.headers->HasStrongValidators())
- return false;
+ // Balance the AddRef from WriteResponseInfoToEntry.
+ write_headers_callback_->Release();
+ if (result != io_buf_len_) {
+ DLOG(ERROR) << "failed to write response info to cache";
+ DoneWritingToEntry(false);
+ }
+ return OK;
+}
- truncated_ = true;
- target_state_ = STATE_NONE;
- next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
- DoLoop(OK);
- return true;
+int HttpCache::Transaction::DoCacheReadMetadata() {
+ DCHECK(entry_);
+ DCHECK(!response_.metadata);
+ next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
+
+ response_.metadata =
+ new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
+
+ LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO);
+ cache_callback_->AddRef(); // Balanced in DoCacheReadMetadataComplete.
+ return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata,
+ response_.metadata->size(),
+ cache_callback_);
}
-void HttpCache::Transaction::DoCallback(int rv) {
- DCHECK(rv != ERR_IO_PENDING);
- DCHECK(callback_);
+int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
+ cache_callback_->Release(); // Balance the AddRef from DoCacheReadMetadata.
+ LoadLog::EndEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO);
+ if (result != response_.metadata->size()) {
+ DLOG(ERROR) << "ReadData failed: " << result;
+ return ERR_CACHE_READ_FAILURE;
+ }
- // Since Run may result in Read being called, clear callback_ up front.
- CompletionCallback* c = callback_;
- callback_ = NULL;
- c->Run(rv);
+ return OK;
}
-int HttpCache::Transaction::HandleResult(int rv) {
- DCHECK(rv != ERR_IO_PENDING);
- if (callback_)
- DoCallback(rv);
- return rv;
+int HttpCache::Transaction::DoCacheQueryData() {
+ next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
+
+ // Balanced in ValidateEntryHeadersAndContinue.
+ cache_callback_->AddRef();
+ return entry_->disk_entry->ReadyForSparseIO(cache_callback_);
}
-int HttpCache::Transaction::DoLoop(int result) {
- DCHECK(next_state_ != STATE_NONE);
+int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
+ DCHECK_EQ(OK, result);
+ // Balance the AddRef from BeginPartialCacheValidation.
+ cache_callback_->Release();
+ if (!cache_)
+ return ERR_UNEXPECTED;
- int rv = result;
- do {
- State state = next_state_;
- next_state_ = STATE_NONE;
- switch (state) {
- case STATE_SEND_REQUEST:
- DCHECK_EQ(OK, rv);
- rv = DoSendRequest();
- break;
- case STATE_SEND_REQUEST_COMPLETE:
- rv = DoSendRequestComplete(rv);
- break;
- case STATE_SUCCESSFUL_SEND_REQUEST:
- DCHECK_EQ(OK, rv);
- rv = DoSuccessfulSendRequest();
- break;
- case STATE_NETWORK_READ:
- DCHECK_EQ(OK, rv);
- rv = DoNetworkRead();
- break;
- case STATE_NETWORK_READ_COMPLETE:
- rv = DoNetworkReadComplete(rv);
- break;
- case STATE_INIT_ENTRY:
- DCHECK_EQ(OK, rv);
- rv = DoInitEntry();
- break;
- case STATE_OPEN_ENTRY:
- DCHECK_EQ(OK, rv);
- rv = DoOpenEntry();
- break;
- case STATE_OPEN_ENTRY_COMPLETE:
- rv = DoOpenEntryComplete(rv);
- break;
- case STATE_CREATE_ENTRY:
- DCHECK_EQ(OK, rv);
- rv = DoCreateEntry();
- break;
- case STATE_CREATE_ENTRY_COMPLETE:
- rv = DoCreateEntryComplete(rv);
- break;
- case STATE_DOOM_ENTRY:
- DCHECK_EQ(OK, rv);
- rv = DoDoomEntry();
- break;
- case STATE_DOOM_ENTRY_COMPLETE:
- rv = DoDoomEntryComplete(rv);
- break;
- case STATE_ADD_TO_ENTRY:
- DCHECK_EQ(OK, rv);
- rv = DoAddToEntry();
- break;
- case STATE_ADD_TO_ENTRY_COMPLETE:
- rv = DoAddToEntryComplete(rv);
- break;
- case STATE_PARTIAL_CACHE_VALIDATION:
- DCHECK_EQ(OK, rv);
- rv = DoPartialCacheValidation();
- break;
- case STATE_UPDATE_CACHED_RESPONSE:
- DCHECK_EQ(OK, rv);
- rv = DoUpdateCachedResponse();
- break;
- case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
- rv = DoUpdateCachedResponseComplete(rv);
- break;
- case STATE_OVERWRITE_CACHED_RESPONSE:
- DCHECK_EQ(OK, rv);
- rv = DoOverwriteCachedResponse();
- break;
- case STATE_TRUNCATE_CACHED_DATA:
- DCHECK_EQ(OK, rv);
- rv = DoTruncateCachedData();
- break;
- case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
- rv = DoTruncateCachedDataComplete(rv);
- break;
- case STATE_PARTIAL_HEADERS_RECEIVED:
- DCHECK_EQ(OK, rv);
- rv = DoPartialHeadersReceived();
- break;
- case STATE_CACHE_READ_RESPONSE:
- DCHECK_EQ(OK, rv);
- rv = DoCacheReadResponse();
- break;
- case STATE_CACHE_READ_RESPONSE_COMPLETE:
- rv = DoCacheReadResponseComplete(rv);
- break;
- case STATE_CACHE_WRITE_RESPONSE:
- DCHECK_EQ(OK, rv);
- rv = DoCacheWriteResponse();
- break;
- case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
- DCHECK_EQ(OK, rv);
- rv = DoCacheWriteTruncatedResponse();
- break;
- case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
- rv = DoCacheWriteResponseComplete(rv);
- break;
- case STATE_CACHE_READ_METADATA:
- DCHECK_EQ(OK, rv);
- rv = DoCacheReadMetadata();
- break;
- case STATE_CACHE_READ_METADATA_COMPLETE:
- rv = DoCacheReadMetadataComplete(rv);
- break;
- case STATE_CACHE_QUERY_DATA:
- DCHECK_EQ(OK, rv);
- rv = DoCacheQueryData();
- break;
- case STATE_CACHE_QUERY_DATA_COMPLETE:
- rv = DoCacheQueryDataComplete(rv);
- break;
- case STATE_CACHE_READ_DATA:
- DCHECK_EQ(OK, rv);
- rv = DoCacheReadData();
- break;
- case STATE_CACHE_READ_DATA_COMPLETE:
- rv = DoCacheReadDataComplete(rv);
- break;
- case STATE_CACHE_WRITE_DATA:
- rv = DoCacheWriteData(rv);
- break;
- case STATE_CACHE_WRITE_DATA_COMPLETE:
- rv = DoCacheWriteDataComplete(rv);
- break;
- default:
- NOTREACHED() << "bad state";
- rv = ERR_FAILED;
- break;
- }
- } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
+ return ValidateEntryHeadersAndContinue(true);
+}
- if (rv != ERR_IO_PENDING)
- HandleResult(rv);
+int HttpCache::Transaction::DoCacheReadData() {
+ DCHECK(entry_);
+ next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
+ cache_callback_->AddRef(); // Balanced in DoCacheReadDataComplete.
+ if (partial_.get()) {
+ return partial_->CacheRead(entry_->disk_entry, read_buf_, io_buf_len_,
+ cache_callback_);
+ }
- return rv;
+ return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
+ read_buf_, io_buf_len_, cache_callback_);
}
+int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
+ cache_callback_->Release(); // Balance the AddRef from DoCacheReadData.
+
+ if (!cache_)
+ return ERR_UNEXPECTED;
+
+ if (partial_.get())
+ return DoPartialCacheReadCompleted(result);
+
+ if (result > 0) {
+ read_offset_ += result;
+ } else if (result == 0) { // End of file.
+ cache_->DoneReadingFromEntry(entry_, this);
+ entry_ = NULL;
+ }
+ return result;
+}
+
+int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
+ next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
+ cache_callback_->AddRef(); // Balanced in DoCacheWriteDataComplete.
+
+ return AppendResponseDataToEntry(read_buf_, num_bytes, cache_callback_);
+}
+
+int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
+ // Balance the AddRef from DoCacheWriteData.
+ cache_callback_->Release();
+ if (!cache_)
+ return ERR_UNEXPECTED;
+
+ if (result < 0)
+ return result;
+
+ if (partial_.get()) {
+ // This may be the last request.
+ if (!(result == 0 && !truncated_ &&
+ (partial_->IsLastRange() || mode_ == WRITE)))
+ return DoPartialNetworkReadCompleted(result);
+ }
+
+ if (result == 0) // End of file.
+ DoneWritingToEntry(true);
+
+ return result;
+}
+
+//-----------------------------------------------------------------------------
+
void HttpCache::Transaction::SetRequest(LoadLog* load_log,
const HttpRequestInfo* request) {
load_log_ = load_log;
@@ -908,6 +1284,12 @@ bool HttpCache::Transaction::ShouldPassThrough() {
return true;
}
+int HttpCache::Transaction::AddToEntry() {
+ next_state_ = STATE_INIT_ENTRY;
+ cache_pending_ = false;
+ return DoLoop(OK);
+}
+
int HttpCache::Transaction::BeginCacheRead() {
// We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
if (response_.headers->response_code() == 206 || partial_.get()) {
@@ -972,24 +1354,6 @@ int HttpCache::Transaction::BeginPartialCacheValidation() {
return ValidateEntryHeadersAndContinue(false);
}
-int HttpCache::Transaction::DoCacheQueryData() {
- next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
-
- // Balanced in ValidateEntryHeadersAndContinue.
- cache_callback_->AddRef();
- return entry_->disk_entry->ReadyForSparseIO(cache_callback_);
-}
-
-int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
- DCHECK_EQ(OK, result);
- // Balance the AddRef from BeginPartialCacheValidation.
- cache_callback_->Release();
- if (!cache_)
- return ERR_UNEXPECTED;
-
- return ValidateEntryHeadersAndContinue(true);
-}
-
int HttpCache::Transaction::ValidateEntryHeadersAndContinue(
bool byte_range_requested) {
DCHECK(mode_ == READ_WRITE);
@@ -1014,37 +1378,6 @@ int HttpCache::Transaction::ValidateEntryHeadersAndContinue(
return OK;
}
-int HttpCache::Transaction::DoPartialCacheValidation() {
- if (mode_ == NONE)
- return OK;
-
- int rv = partial_->PrepareCacheValidation(entry_->disk_entry,
- &custom_request_->extra_headers);
-
- if (!rv) {
- // This is the end of the request.
- if (mode_ & WRITE) {
- DoneWritingToEntry(true);
- } else {
- cache_->DoneReadingFromEntry(entry_, this);
- entry_ = NULL;
- }
- return rv;
- }
-
- if (rv < 0) {
- DCHECK(rv != ERR_IO_PENDING);
- return rv;
- }
-
- if (reading_ && partial_->IsCurrentRangeCached()) {
- next_state_ = STATE_CACHE_READ_DATA;
- return OK;
- }
-
- return BeginCacheValidation();
-}
-
int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
DCHECK_EQ(UPDATE, mode_);
DCHECK(external_validation_.initialized);
@@ -1076,20 +1409,6 @@ int HttpCache::Transaction::BeginNetworkRequest() {
return DoLoop(OK);
}
-int HttpCache::Transaction::DoSendRequest() {
- DCHECK(mode_ & WRITE || mode_ == NONE);
- DCHECK(!network_trans_.get());
-
- // Create a network transaction.
- int rv = cache_->network_layer_->CreateTransaction(&network_trans_);
- if (rv != OK)
- return rv;
-
- next_state_ = STATE_SEND_REQUEST_COMPLETE;
- rv = network_trans_->Start(request_, &io_callback_, load_log_);
- return rv;
-}
-
int HttpCache::Transaction::RestartNetworkRequest() {
DCHECK(mode_ & WRITE || mode_ == NONE);
DCHECK(network_trans_.get());
@@ -1332,11 +1651,6 @@ int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
return DoLoop(OK);
}
-int HttpCache::Transaction::DoNetworkRead() {
- next_state_ = STATE_NETWORK_READ_COMPLETE;
- return network_trans_->Read(read_buf_, io_buf_len_, &io_callback_);
-}
-
int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
read_buf_ = data;
io_buf_len_ = data_len;
@@ -1344,32 +1658,6 @@ int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
return DoLoop(OK);
}
-int HttpCache::Transaction::DoCacheReadData() {
- DCHECK(entry_);
- next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
- cache_callback_->AddRef(); // Balanced in DoCacheReadDataComplete.
- if (partial_.get()) {
- return partial_->CacheRead(entry_->disk_entry, read_buf_, io_buf_len_,
- cache_callback_);
- }
-
- return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
- read_buf_, io_buf_len_, cache_callback_);
-}
-
-int HttpCache::Transaction::DoCacheReadResponse() {
- DCHECK(entry_);
- next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
-
- io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
- read_buf_ = new IOBuffer(io_buf_len_);
-
- LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO);
- cache_callback_->AddRef(); // Balanced in DoCacheReadResponseComplete.
- return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_,
- io_buf_len_, cache_callback_);
-}
-
int HttpCache::Transaction::WriteToEntry(int index, int offset,
IOBuffer* data, int data_len,
CompletionCallback* callback) {
@@ -1395,14 +1683,6 @@ int HttpCache::Transaction::WriteToEntry(int index, int offset,
return rv;
}
-int HttpCache::Transaction::DoCacheWriteResponse() {
- return WriteResponseInfoToEntry(false);
-}
-
-int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
- return WriteResponseInfoToEntry(true);
-}
-
int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
if (!entry_)
@@ -1444,47 +1724,6 @@ int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
write_headers_callback_, true);
}
-int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
- next_state_ = target_state_;
- target_state_ = STATE_NONE;
- if (!entry_)
- return OK;
-
- // Balance the AddRef from WriteResponseInfoToEntry.
- write_headers_callback_->Release();
- if (result != io_buf_len_) {
- DLOG(ERROR) << "failed to write response info to cache";
- DoneWritingToEntry(false);
- }
- return OK;
-}
-
-int HttpCache::Transaction::DoCacheReadMetadata() {
- DCHECK(entry_);
- DCHECK(!response_.metadata);
- next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
-
- response_.metadata =
- new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
-
- LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO);
- cache_callback_->AddRef(); // Balanced in DoCacheReadMetadataComplete.
- return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata,
- response_.metadata->size(),
- cache_callback_);
-}
-
-int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
- cache_callback_->Release(); // Balance the AddRef from DoCacheReadMetadata.
- LoadLog::EndEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO);
- if (result != response_.metadata->size()) {
- DLOG(ERROR) << "ReadData failed: " << result;
- return ERR_CACHE_READ_FAILURE;
- }
-
- return OK;
-}
-
int HttpCache::Transaction::AppendResponseDataToEntry(
IOBuffer* data, int data_len, CompletionCallback* callback) {
if (!entry_ || !data_len)
@@ -1495,19 +1734,6 @@ int HttpCache::Transaction::AppendResponseDataToEntry(
callback);
}
-int HttpCache::Transaction::DoTruncateCachedData() {
- next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
- if (!entry_)
- return OK;
-
- // Truncate the stream.
- int rv = WriteToEntry(kResponseContentIndex, 0, NULL, 0, NULL);
- DCHECK(rv != ERR_IO_PENDING);
- rv = WriteToEntry(kMetadataIndex, 0, NULL, 0, NULL);
- DCHECK(rv != ERR_IO_PENDING);
- return OK;
-}
-
void HttpCache::Transaction::DoneWritingToEntry(bool success) {
if (!entry_)
return;
@@ -1530,23 +1756,6 @@ void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
partial_.reset(NULL);
}
-int HttpCache::Transaction::DoNetworkReadComplete(int result) {
- DCHECK(mode_ & WRITE || mode_ == NONE);
-
- if (!cache_)
- return ERR_UNEXPECTED;
-
- next_state_ = STATE_CACHE_WRITE_DATA;
- return result;
-}
-
-int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
- next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
- cache_callback_->AddRef(); // Balanced in DoCacheWriteDataComplete.
-
- return AppendResponseDataToEntry(read_buf_, num_bytes, cache_callback_);
-}
-
int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
partial_->OnNetworkReadCompleted(result);
@@ -1558,24 +1767,6 @@ int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
return result;
}
-int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
- cache_callback_->Release(); // Balance the AddRef from DoCacheReadData.
-
- if (!cache_)
- return ERR_UNEXPECTED;
-
- if (partial_.get())
- return DoPartialCacheReadCompleted(result);
-
- if (result > 0) {
- read_offset_ += result;
- } else if (result == 0) { // End of file.
- cache_->DoneReadingFromEntry(entry_, this);
- entry_ = NULL;
- }
- return result;
-}
-
int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
partial_->OnCacheReadCompleted(result);
@@ -1586,193 +1777,6 @@ int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
return result;
}
-int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
- // Balance the AddRef from DoCacheWriteData.
- cache_callback_->Release();
- if (!cache_)
- return ERR_UNEXPECTED;
-
- if (result < 0)
- return result;
-
- if (partial_.get()) {
- // This may be the last request.
- if (!(result == 0 && !truncated_ &&
- (partial_->IsLastRange() || mode_ == WRITE)))
- return DoPartialNetworkReadCompleted(result);
- }
-
- if (result == 0) // End of file.
- DoneWritingToEntry(true);
-
- return result;
-}
-
-// We received the response headers and there is no error.
-int HttpCache::Transaction::DoSuccessfulSendRequest() {
- DCHECK(!new_response_);
- const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
- if (new_response->headers->response_code() == 401 ||
- new_response->headers->response_code() == 407) {
- auth_response_ = *new_response;
- return OK;
- }
-
- if (!ValidatePartialResponse(new_response->headers, &server_responded_206_) &&
- !auth_response_.headers) {
- // Something went wrong with this request and we have to restart it.
- // If we have an authentication response, we are exposed to weird things
- // hapenning if the user cancels the authentication before we receive
- // the new response.
- response_ = HttpResponseInfo();
- network_trans_.reset();
- next_state_ = STATE_SEND_REQUEST;
- return OK;
- }
- if (server_responded_206_ && mode_ == READ_WRITE && !truncated_ &&
- response_.headers->response_code() == 200) {
- // We have stored the full entry, but it changed and the server is
- // sending a range. We have to delete the old entry.
- DoneWritingToEntry(false);
- }
-
- HistogramHeaders(new_response->headers);
-
- new_response_ = new_response;
- // Are we expecting a response to a conditional query?
- if (mode_ == READ_WRITE || mode_ == UPDATE) {
- if (new_response->headers->response_code() == 304 ||
- server_responded_206_) {
- next_state_ = STATE_UPDATE_CACHED_RESPONSE;
- return OK;
- }
- mode_ = WRITE;
- }
-
- next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
- return OK;
-}
-
-// We received 304 or 206 and we want to update the cached response headers.
-int HttpCache::Transaction::DoUpdateCachedResponse() {
- next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
- int rv = OK;
- // Update cached response based on headers in new_response.
- // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
- response_.headers->Update(*new_response_->headers);
- response_.response_time = new_response_->response_time;
- response_.request_time = new_response_->request_time;
-
- if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
- int ret = cache_->DoomEntry(cache_key_, NULL);
- DCHECK_EQ(OK, ret);
- } else {
- // If we are already reading, we already updated the headers for this
- // request; doing it again will change Content-Length.
- if (!reading_) {
- target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
- next_state_ = STATE_CACHE_WRITE_RESPONSE;
- rv = OK;
- }
- }
- return rv;
-}
-
-int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
- if (mode_ == UPDATE) {
- DCHECK(!server_responded_206_);
- // We got a "not modified" response and already updated the corresponding
- // cache entry above.
- //
- // By closing the cached entry now, we make sure that the 304 rather than
- // the cached 200 response, is what will be returned to the user.
- DoneWritingToEntry(true);
- } else if (entry_ && !server_responded_206_) {
- DCHECK_EQ(READ_WRITE, mode_);
- if (!partial_.get() || partial_->IsLastRange()) {
- cache_->ConvertWriterToReader(entry_);
- mode_ = READ;
- }
- // We no longer need the network transaction, so destroy it.
- final_upload_progress_ = network_trans_->GetUploadProgress();
- network_trans_.reset();
- }
- next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
- return OK;
-}
-
-int HttpCache::Transaction::DoOverwriteCachedResponse() {
- if (mode_ & READ) {
- next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
- return OK;
- }
-
- // We change the value of Content-Length for partial content.
- if (server_responded_206_ && partial_.get())
- partial_->FixContentLength(new_response_->headers);
-
- response_ = *new_response_;
- target_state_ = STATE_TRUNCATE_CACHED_DATA;
- next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
- STATE_CACHE_WRITE_RESPONSE;
- return OK;
-}
-
-int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
- // If this response is a redirect, then we can stop writing now. (We don't
- // need to cache the response body of a redirect.)
- if (response_.headers->IsRedirect(NULL))
- DoneWritingToEntry(true);
- next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
- return OK;
-}
-
-int HttpCache::Transaction::DoPartialHeadersReceived() {
- new_response_ = NULL;
- if (entry_ && !partial_.get() &&
- entry_->disk_entry->GetDataSize(kMetadataIndex))
- next_state_ = STATE_CACHE_READ_METADATA;
-
- if (!partial_.get())
- return OK;
-
- if (reading_) {
- if (network_trans_.get()) {
- next_state_ = STATE_NETWORK_READ;
- } else {
- next_state_ = STATE_CACHE_READ_DATA;
- }
- } else if (mode_ != NONE) {
- // We are about to return the headers for a byte-range request to the user,
- // so let's fix them.
- partial_->FixResponseHeaders(response_.headers);
- }
- return OK;
-}
-
-int HttpCache::Transaction::DoSendRequestComplete(int result) {
- if (!cache_)
- return ERR_UNEXPECTED;
-
- if (result == OK) {
- next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
- return OK;
- }
-
- if (IsCertificateError(result)) {
- const HttpResponseInfo* response = network_trans_->GetResponseInfo();
- // If we get a certificate error, then there is a certificate in ssl_info,
- // so GetResponseInfo() should never returns NULL here.
- DCHECK(response);
- response_.ssl_info = response->ssl_info;
- } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
- const HttpResponseInfo* response = network_trans_->GetResponseInfo();
- DCHECK(response);
- response_.cert_request_info = response->cert_request_info;
- }
- return result;
-}
-
// For a 200 response we'll add a histogram with one bit set per header:
// 0x01 Content-Length
// 0x02 Date
diff --git a/net/http/http_cache_transaction.h b/net/http/http_cache_transaction.h
index 0adb83a..936c2e3 100644
--- a/net/http/http_cache_transaction.h
+++ b/net/http/http_cache_transaction.h
@@ -291,9 +291,6 @@ class HttpCache::Transaction : public HttpTransaction {
// working with range requests.
int DoPartialCacheReadCompleted(int result);
- // Performs the needed work after writing data to the cache.
- int DoCacheWriteCompleted(int result);
-
// Sends a histogram with info about the response headers.
void HistogramHeaders(const HttpResponseHeaders* headers);