Chromium Code Reviews| 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> |
| (...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 724 next_state_ = STATE_NETWORK_READ_COMPLETE; | 724 next_state_ = STATE_NETWORK_READ_COMPLETE; |
| 725 return network_trans_->Read(read_buf_, io_buf_len_, &io_callback_); | 725 return network_trans_->Read(read_buf_, io_buf_len_, &io_callback_); |
| 726 } | 726 } |
| 727 | 727 |
| 728 int HttpCache::Transaction::DoNetworkReadComplete(int result) { | 728 int HttpCache::Transaction::DoNetworkReadComplete(int result) { |
| 729 DCHECK(mode_ & WRITE || mode_ == NONE); | 729 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 730 | 730 |
| 731 if (!cache_) | 731 if (!cache_) |
| 732 return ERR_UNEXPECTED; | 732 return ERR_UNEXPECTED; |
| 733 | 733 |
| 734 // If there is an error and we are saving the data, just tell the user about | |
| 735 // it and wait until the destructor runs to see if we can keep the data. | |
| 736 if (mode_ != NONE && result < 0) | |
| 737 return result; | |
| 738 | |
| 734 next_state_ = STATE_CACHE_WRITE_DATA; | 739 next_state_ = STATE_CACHE_WRITE_DATA; |
| 735 return result; | 740 return result; |
| 736 } | 741 } |
| 737 | 742 |
| 738 int HttpCache::Transaction::DoInitEntry() { | 743 int HttpCache::Transaction::DoInitEntry() { |
| 739 DCHECK(!new_entry_); | 744 DCHECK(!new_entry_); |
| 740 | 745 |
| 741 if (!cache_) | 746 if (!cache_) |
| 742 return ERR_UNEXPECTED; | 747 return ERR_UNEXPECTED; |
| 743 | 748 |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 975 DoneWritingToEntry(true); | 980 DoneWritingToEntry(true); |
| 976 } else if (entry_ && !server_responded_206_) { | 981 } else if (entry_ && !server_responded_206_) { |
| 977 DCHECK_EQ(READ_WRITE, mode_); | 982 DCHECK_EQ(READ_WRITE, mode_); |
| 978 if (!partial_.get() || partial_->IsLastRange()) { | 983 if (!partial_.get() || partial_->IsLastRange()) { |
| 979 cache_->ConvertWriterToReader(entry_); | 984 cache_->ConvertWriterToReader(entry_); |
| 980 mode_ = READ; | 985 mode_ = READ; |
| 981 } | 986 } |
| 982 // We no longer need the network transaction, so destroy it. | 987 // We no longer need the network transaction, so destroy it. |
| 983 final_upload_progress_ = network_trans_->GetUploadProgress(); | 988 final_upload_progress_ = network_trans_->GetUploadProgress(); |
| 984 network_trans_.reset(); | 989 network_trans_.reset(); |
| 990 } else if (entry_ && server_responded_206_ && truncated_ && | |
| 991 partial_->initial_validation()) { | |
| 992 // We just finishing the validation of a truncated entry, and the server | |
|
eroman
2011/03/02 02:06:04
wording nit: "finishing" --> "finished".
| |
| 993 // is willing to resume the operation. Now we go back and start serving | |
| 994 // the first part to the user. | |
| 995 network_trans_.reset(); | |
| 996 new_response_ = NULL; | |
| 997 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | |
| 998 partial_->SetRangeToStartDownload(); | |
| 999 return OK; | |
| 985 } | 1000 } |
| 986 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE; | 1001 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE; |
| 987 return OK; | 1002 return OK; |
| 988 } | 1003 } |
| 989 | 1004 |
| 990 int HttpCache::Transaction::DoOverwriteCachedResponse() { | 1005 int HttpCache::Transaction::DoOverwriteCachedResponse() { |
| 991 if (mode_ & READ) { | 1006 if (mode_ & READ) { |
| 992 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; | 1007 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; |
| 993 return OK; | 1008 return OK; |
| 994 } | 1009 } |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1435 | 1450 |
| 1436 return OK; | 1451 return OK; |
| 1437 } | 1452 } |
| 1438 | 1453 |
| 1439 int HttpCache::Transaction::BeginCacheValidation() { | 1454 int HttpCache::Transaction::BeginCacheValidation() { |
| 1440 DCHECK(mode_ == READ_WRITE); | 1455 DCHECK(mode_ == READ_WRITE); |
| 1441 | 1456 |
| 1442 bool skip_validation = effective_load_flags_ & LOAD_PREFERRING_CACHE || | 1457 bool skip_validation = effective_load_flags_ & LOAD_PREFERRING_CACHE || |
| 1443 !RequiresValidation(); | 1458 !RequiresValidation(); |
| 1444 | 1459 |
| 1460 if (truncated_) | |
| 1461 skip_validation = !partial_->initial_validation(); | |
| 1462 | |
| 1445 if ((partial_.get() && !partial_->IsCurrentRangeCached()) || invalid_range_) | 1463 if ((partial_.get() && !partial_->IsCurrentRangeCached()) || invalid_range_) |
| 1446 skip_validation = false; | 1464 skip_validation = false; |
| 1447 | 1465 |
| 1448 if (skip_validation) { | 1466 if (skip_validation) { |
| 1449 if (partial_.get()) { | 1467 if (partial_.get()) { |
| 1450 // We are going to return the saved response headers to the caller, so | 1468 // We are going to return the saved response headers to the caller, so |
| 1451 // we may need to adjust them first. | 1469 // we may need to adjust them first. |
| 1452 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; | 1470 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; |
| 1453 return OK; | 1471 return OK; |
| 1454 } | 1472 } |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1731 return true; | 1749 return true; |
| 1732 } else { | 1750 } else { |
| 1733 // We asked for "If-Range: " so a 206 means just another range. | 1751 // We asked for "If-Range: " so a 206 means just another range. |
| 1734 if (partial_response && partial_->ResponseHeadersOK(headers)) { | 1752 if (partial_response && partial_->ResponseHeadersOK(headers)) { |
| 1735 *partial_content = true; | 1753 *partial_content = true; |
| 1736 return true; | 1754 return true; |
| 1737 } | 1755 } |
| 1738 | 1756 |
| 1739 // 304 is not expected here, but we'll spare the entry (unless it was | 1757 // 304 is not expected here, but we'll spare the entry (unless it was |
| 1740 // truncated). | 1758 // truncated). |
| 1741 if (truncated_) | 1759 if (truncated_) { |
| 1760 if (!reading_ && response_code == 200) { | |
| 1761 // The server is sending the whole resource, and we can save it. | |
| 1762 DCHECK(!partial_->IsLastRange()); | |
| 1763 partial_.reset(); | |
| 1764 truncated_ = false; | |
| 1765 return true; | |
| 1766 } | |
| 1742 failure = true; | 1767 failure = true; |
| 1768 } | |
| 1743 } | 1769 } |
| 1744 | 1770 |
| 1745 if (failure) { | 1771 if (failure) { |
| 1746 // We cannot truncate this entry, it has to be deleted. | 1772 // We cannot truncate this entry, it has to be deleted. |
| 1747 DoomPartialEntry(false); | 1773 DoomPartialEntry(false); |
| 1748 mode_ = NONE; | 1774 mode_ = NONE; |
| 1749 if (!reading_ && !partial_->IsLastRange()) { | 1775 if (!reading_ && !partial_->IsLastRange()) { |
| 1750 // We'll attempt to issue another network request, this time without us | 1776 // We'll attempt to issue another network request, this time without us |
| 1751 // messing up the headers. | 1777 // messing up the headers. |
| 1752 partial_->RestoreHeaders(&custom_request_->extra_headers); | 1778 partial_->RestoreHeaders(&custom_request_->extra_headers); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1908 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 1934 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
| 1909 } | 1935 } |
| 1910 return result; | 1936 return result; |
| 1911 } | 1937 } |
| 1912 | 1938 |
| 1913 void HttpCache::Transaction::OnIOComplete(int result) { | 1939 void HttpCache::Transaction::OnIOComplete(int result) { |
| 1914 DoLoop(result); | 1940 DoLoop(result); |
| 1915 } | 1941 } |
| 1916 | 1942 |
| 1917 } // namespace net | 1943 } // namespace net |
| OLD | NEW |