Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 1554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1565 } | 1565 } |
| 1566 | 1566 |
| 1567 int HttpCache::Transaction::BeginCacheValidation() { | 1567 int HttpCache::Transaction::BeginCacheValidation() { |
| 1568 DCHECK(mode_ == READ_WRITE); | 1568 DCHECK(mode_ == READ_WRITE); |
| 1569 | 1569 |
| 1570 bool skip_validation = effective_load_flags_ & LOAD_PREFERRING_CACHE || | 1570 bool skip_validation = effective_load_flags_ & LOAD_PREFERRING_CACHE || |
| 1571 !RequiresValidation(); | 1571 !RequiresValidation(); |
| 1572 | 1572 |
| 1573 if (truncated_) | 1573 if (truncated_) |
| 1574 skip_validation = !partial_->initial_validation(); | 1574 skip_validation = !partial_->initial_validation(); |
| 1575 | 1575 |
|
gavinp
2012/07/30 18:35:07
Is it worthwhile adding a DCHECK here that capture
| |
| 1576 if ((partial_.get() && !partial_->IsCurrentRangeCached()) || invalid_range_) | 1576 if (partial_.get() && (is_sparse_ || truncated_) && |
| 1577 (!partial_->IsCurrentRangeCached() || invalid_range_)) { | |
| 1578 // Only force revalidation for sparse or truncated entries. | |
|
gavinp
2012/07/30 18:35:07
I think the "Only" here is a bit confusing: skip_v
rvargas (doing something else)
2012/07/30 21:38:52
but this is not about skip_validation being false,
| |
| 1577 skip_validation = false; | 1579 skip_validation = false; |
| 1580 } | |
| 1578 | 1581 |
| 1579 if (skip_validation) { | 1582 if (skip_validation) { |
| 1580 if (partial_.get()) { | 1583 if (partial_.get()) { |
| 1581 // We are going to return the saved response headers to the caller, so | 1584 if (truncated_ || is_sparse_ || !invalid_range_) { |
| 1582 // we may need to adjust them first. | 1585 // We are going to return the saved response headers to the caller, so |
| 1583 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; | 1586 // we may need to adjust them first. |
| 1584 return OK; | 1587 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; |
| 1588 return OK; | |
| 1589 } else { | |
| 1590 partial_.reset(); | |
| 1591 } | |
| 1585 } | 1592 } |
| 1586 cache_->ConvertWriterToReader(entry_); | 1593 cache_->ConvertWriterToReader(entry_); |
| 1587 mode_ = READ; | 1594 mode_ = READ; |
| 1588 | 1595 |
| 1589 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) | 1596 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) |
| 1590 next_state_ = STATE_CACHE_READ_METADATA; | 1597 next_state_ = STATE_CACHE_READ_METADATA; |
| 1591 } else { | 1598 } else { |
| 1592 // Make the network request conditional, to see if we may reuse our cached | 1599 // Make the network request conditional, to see if we may reuse our cached |
| 1593 // response. If we cannot do so, then we just resort to a normal fetch. | 1600 // response. If we cannot do so, then we just resort to a normal fetch. |
| 1594 // Our mode remains READ_WRITE for a conditional request. We'll switch to | 1601 // Our mode remains READ_WRITE for a conditional request. We'll switch to |
| 1595 // either READ or WRITE mode once we hear back from the server. | 1602 // either READ or WRITE mode once we hear back from the server. |
| 1596 if (!ConditionalizeRequest()) { | 1603 if (!ConditionalizeRequest()) { |
| 1597 DCHECK(!partial_.get()); | 1604 if (partial_.get()) |
| 1605 return DoRestartPartialRequest(); | |
|
gavinp
2012/07/30 18:35:07
So this makes me sad because if we have a range ca
rvargas (doing something else)
2012/07/30 21:38:52
Not unless we are unable to conditionalize the req
| |
| 1606 | |
| 1598 DCHECK_NE(206, response_.headers->response_code()); | 1607 DCHECK_NE(206, response_.headers->response_code()); |
| 1599 mode_ = WRITE; | 1608 mode_ = WRITE; |
| 1600 } | 1609 } |
| 1601 next_state_ = STATE_SEND_REQUEST; | 1610 next_state_ = STATE_SEND_REQUEST; |
| 1602 } | 1611 } |
| 1603 return OK; | 1612 return OK; |
| 1604 } | 1613 } |
| 1605 | 1614 |
| 1606 int HttpCache::Transaction::BeginPartialCacheValidation() { | 1615 int HttpCache::Transaction::BeginPartialCacheValidation() { |
| 1607 DCHECK(mode_ == READ_WRITE); | 1616 DCHECK(mode_ == READ_WRITE); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 1624 | 1633 |
| 1625 return ValidateEntryHeadersAndContinue(); | 1634 return ValidateEntryHeadersAndContinue(); |
| 1626 } | 1635 } |
| 1627 | 1636 |
| 1628 // This should only be called once per request. | 1637 // This should only be called once per request. |
| 1629 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() { | 1638 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() { |
| 1630 DCHECK(mode_ == READ_WRITE); | 1639 DCHECK(mode_ == READ_WRITE); |
| 1631 | 1640 |
| 1632 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry, | 1641 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry, |
| 1633 truncated_)) { | 1642 truncated_)) { |
| 1634 // The stored data cannot be used. Get rid of it and restart this request. | 1643 return DoRestartPartialRequest(); |
| 1635 // We need to also reset the |truncated_| flag as a new entry is created. | |
| 1636 DoomPartialEntry(!range_requested_); | |
| 1637 mode_ = WRITE; | |
| 1638 truncated_ = false; | |
| 1639 next_state_ = STATE_INIT_ENTRY; | |
| 1640 return OK; | |
| 1641 } | 1644 } |
| 1642 | 1645 |
| 1643 if (response_.headers->response_code() == 206) | 1646 if (response_.headers->response_code() == 206) |
| 1644 is_sparse_ = true; | 1647 is_sparse_ = true; |
| 1645 | 1648 |
| 1646 if (!partial_->IsRequestedRangeOK()) { | 1649 if (!partial_->IsRequestedRangeOK()) { |
| 1647 // The stored data is fine, but the request may be invalid. | 1650 // The stored data is fine, but the request may be invalid. |
| 1648 invalid_range_ = true; | 1651 invalid_range_ = true; |
| 1649 } | 1652 } |
| 1650 | 1653 |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1870 | 1873 |
| 1871 if (response_code == 304 && partial_->ResponseHeadersOK(headers)) | 1874 if (response_code == 304 && partial_->ResponseHeadersOK(headers)) |
| 1872 return true; | 1875 return true; |
| 1873 } else { | 1876 } else { |
| 1874 // We asked for "If-Range: " so a 206 means just another range. | 1877 // We asked for "If-Range: " so a 206 means just another range. |
| 1875 if (partial_response && partial_->ResponseHeadersOK(headers)) { | 1878 if (partial_response && partial_->ResponseHeadersOK(headers)) { |
| 1876 handling_206_ = true; | 1879 handling_206_ = true; |
| 1877 return true; | 1880 return true; |
| 1878 } | 1881 } |
| 1879 | 1882 |
| 1880 if (response_code == 200 && !reading_ && !is_sparse_) { | 1883 if (!reading_ && !is_sparse_ && !partial_response) { |
| 1881 // The server is sending the whole resource, and we can save it. | 1884 // See if we can ignore the fact that we issued a byte range request. |
| 1882 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_); | 1885 // If the server sends 200, just store it. If it sends an error, redirect |
| 1883 partial_.reset(); | 1886 // or something else, we may store the response as long as we didn't have |
| 1884 truncated_ = false; | 1887 // anything already stored. |
| 1885 return true; | 1888 if (response_code == 200 || |
| 1889 (!truncated_ && response_code != 304 && response_code != 416)) { | |
| 1890 // The server is sending something else, and we can save it. | |
| 1891 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_); | |
| 1892 partial_.reset(); | |
| 1893 truncated_ = false; | |
| 1894 return true; | |
| 1895 } | |
| 1886 } | 1896 } |
| 1887 | 1897 |
| 1888 // 304 is not expected here, but we'll spare the entry (unless it was | 1898 // 304 is not expected here, but we'll spare the entry (unless it was |
| 1889 // truncated). | 1899 // truncated). |
| 1890 if (truncated_) | 1900 if (truncated_) |
| 1891 failure = true; | 1901 failure = true; |
| 1892 } | 1902 } |
| 1893 | 1903 |
| 1894 if (failure) { | 1904 if (failure) { |
| 1895 // We cannot truncate this entry, it has to be deleted. | 1905 // We cannot truncate this entry, it has to be deleted. |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2072 | 2082 |
| 2073 if (result == 0 && mode_ == READ_WRITE) { | 2083 if (result == 0 && mode_ == READ_WRITE) { |
| 2074 // We need to move on to the next range. | 2084 // We need to move on to the next range. |
| 2075 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 2085 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
| 2076 } else if (result < 0) { | 2086 } else if (result < 0) { |
| 2077 return OnCacheReadError(result, false); | 2087 return OnCacheReadError(result, false); |
| 2078 } | 2088 } |
| 2079 return result; | 2089 return result; |
| 2080 } | 2090 } |
| 2081 | 2091 |
| 2092 int HttpCache::Transaction::DoRestartPartialRequest() { | |
| 2093 // The stored data cannot be used. Get rid of it and restart this request. | |
| 2094 // We need to also reset the |truncated_| flag as a new entry is created. | |
| 2095 DoomPartialEntry(!range_requested_); | |
| 2096 mode_ = WRITE; | |
| 2097 truncated_ = false; | |
| 2098 next_state_ = STATE_INIT_ENTRY; | |
| 2099 return OK; | |
| 2100 } | |
| 2101 | |
| 2082 // Histogram data from the end of 2010 show the following distribution of | 2102 // Histogram data from the end of 2010 show the following distribution of |
| 2083 // response headers: | 2103 // response headers: |
| 2084 // | 2104 // |
| 2085 // Content-Length............... 87% | 2105 // Content-Length............... 87% |
| 2086 // Date......................... 98% | 2106 // Date......................... 98% |
| 2087 // Last-Modified................ 49% | 2107 // Last-Modified................ 49% |
| 2088 // Etag......................... 19% | 2108 // Etag......................... 19% |
| 2089 // Accept-Ranges: bytes......... 25% | 2109 // Accept-Ranges: bytes......... 25% |
| 2090 // Accept-Ranges: none.......... 0.4% | 2110 // Accept-Ranges: none.......... 0.4% |
| 2091 // Strong Validator............. 50% | 2111 // Strong Validator............. 50% |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 2106 return false; | 2126 return false; |
| 2107 | 2127 |
| 2108 return true; | 2128 return true; |
| 2109 } | 2129 } |
| 2110 | 2130 |
| 2111 void HttpCache::Transaction::OnIOComplete(int result) { | 2131 void HttpCache::Transaction::OnIOComplete(int result) { |
| 2112 DoLoop(result); | 2132 DoLoop(result); |
| 2113 } | 2133 } |
| 2114 | 2134 |
| 2115 } // namespace net | 2135 } // namespace net |
| OLD | NEW |