| 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> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 18 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
| 22 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
| 23 #include "base/metrics/sparse_histogram.h" | 23 #include "base/metrics/sparse_histogram.h" |
| 24 #include "base/profiler/scoped_tracker.h" | 24 #include "base/profiler/scoped_tracker.h" |
| 25 #include "base/rand_util.h" | 25 #include "base/rand_util.h" |
| 26 #include "base/strings/string_number_conversions.h" | 26 #include "base/strings/string_number_conversions.h" |
| 27 #include "base/strings/string_piece.h" | 27 #include "base/strings/string_piece.h" |
| 28 #include "base/strings/string_util.h" | 28 #include "base/strings/string_util.h" |
| 29 #include "base/strings/stringprintf.h" | 29 #include "base/strings/stringprintf.h" |
| 30 #include "base/time/clock.h" |
| 30 #include "base/time/time.h" | 31 #include "base/time/time.h" |
| 31 #include "base/values.h" | 32 #include "base/values.h" |
| 32 #include "net/base/completion_callback.h" | 33 #include "net/base/completion_callback.h" |
| 33 #include "net/base/io_buffer.h" | 34 #include "net/base/io_buffer.h" |
| 34 #include "net/base/load_flags.h" | 35 #include "net/base/load_flags.h" |
| 35 #include "net/base/load_timing_info.h" | 36 #include "net/base/load_timing_info.h" |
| 36 #include "net/base/net_errors.h" | 37 #include "net/base/net_errors.h" |
| 37 #include "net/base/net_log.h" | 38 #include "net/base/net_log.h" |
| 38 #include "net/base/upload_data_stream.h" | 39 #include "net/base/upload_data_stream.h" |
| 39 #include "net/cert/cert_status_flags.h" | 40 #include "net/cert/cert_status_flags.h" |
| (...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> | 749 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> |
| 749 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> | 750 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> |
| 750 // PartialHeadersReceived | 751 // PartialHeadersReceived |
| 751 // | 752 // |
| 752 // Read(): | 753 // Read(): |
| 753 // NetworkRead* -> CacheWriteData* | 754 // NetworkRead* -> CacheWriteData* |
| 754 // | 755 // |
| 755 // 2. Cached entry, no validation: | 756 // 2. Cached entry, no validation: |
| 756 // Start(): | 757 // Start(): |
| 757 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 758 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| 758 // -> BeginPartialCacheValidation() -> BeginCacheValidation() -> | 759 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
| 759 // SetupEntryForRead() | 760 // BeginCacheValidation() -> SetupEntryForRead() |
| 760 // | 761 // |
| 761 // Read(): | 762 // Read(): |
| 762 // CacheReadData* | 763 // CacheReadData* |
| 763 // | 764 // |
| 764 // 3. Cached entry, validation (304): | 765 // 3. Cached entry, validation (304): |
| 765 // Start(): | 766 // Start(): |
| 766 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 767 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| 767 // -> BeginPartialCacheValidation() -> BeginCacheValidation() -> | 768 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
| 768 // SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse -> | 769 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
| 769 // CacheWriteResponse* -> UpdateCachedResponseComplete -> | 770 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete |
| 770 // OverwriteCachedResponse -> PartialHeadersReceived | 771 // -> OverwriteCachedResponse -> PartialHeadersReceived |
| 771 // | 772 // |
| 772 // Read(): | 773 // Read(): |
| 773 // CacheReadData* | 774 // CacheReadData* |
| 774 // | 775 // |
| 775 // 4. Cached entry, validation and replace (200): | 776 // 4. Cached entry, validation and replace (200): |
| 776 // Start(): | 777 // Start(): |
| 777 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 778 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| 778 // -> BeginPartialCacheValidation() -> BeginCacheValidation() -> | 779 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
| 779 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> | 780 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
| 780 // CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* -> | 781 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> |
| 781 // PartialHeadersReceived | 782 // TruncateCachedMetadata* -> PartialHeadersReceived |
| 782 // | 783 // |
| 783 // Read(): | 784 // Read(): |
| 784 // NetworkRead* -> CacheWriteData* | 785 // NetworkRead* -> CacheWriteData* |
| 785 // | 786 // |
| 786 // 5. Sparse entry, partially cached, byte range request: | 787 // 5. Sparse entry, partially cached, byte range request: |
| 787 // Start(): | 788 // Start(): |
| 788 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 789 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| 789 // -> BeginPartialCacheValidation() -> CacheQueryData* -> | 790 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
| 790 // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation -> | 791 // CacheQueryData* -> ValidateEntryHeadersAndContinue() -> |
| 791 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* -> | 792 // StartPartialCacheValidation -> CompletePartialCacheValidation -> |
| 792 // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* -> | 793 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
| 793 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> | 794 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete |
| 794 // PartialHeadersReceived | 795 // -> OverwriteCachedResponse -> PartialHeadersReceived |
| 795 // | 796 // |
| 796 // Read() 1: | 797 // Read() 1: |
| 797 // NetworkRead* -> CacheWriteData* | 798 // NetworkRead* -> CacheWriteData* |
| 798 // | 799 // |
| 799 // Read() 2: | 800 // Read() 2: |
| 800 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation -> | 801 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation -> |
| 801 // CompletePartialCacheValidation -> CacheReadData* -> | 802 // CompletePartialCacheValidation -> CacheReadData* -> |
| 802 // | 803 // |
| 803 // Read() 3: | 804 // Read() 3: |
| 804 // CacheReadData* -> StartPartialCacheValidation -> | 805 // CacheReadData* -> StartPartialCacheValidation -> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 823 // Start(): Same as for a GET request (example #3) | 824 // Start(): Same as for a GET request (example #3) |
| 824 // | 825 // |
| 825 // Read(): | 826 // Read(): |
| 826 // CacheReadData (returns 0) | 827 // CacheReadData (returns 0) |
| 827 // | 828 // |
| 828 // 9. HEAD. Cached entry, validation and replace (200): | 829 // 9. HEAD. Cached entry, validation and replace (200): |
| 829 // Pass through. The request dooms the old entry, as a HEAD won't be stored by | 830 // Pass through. The request dooms the old entry, as a HEAD won't be stored by |
| 830 // itself. | 831 // itself. |
| 831 // Start(): | 832 // Start(): |
| 832 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 833 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| 833 // -> BeginPartialCacheValidation() -> BeginCacheValidation() -> | 834 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
| 834 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse | 835 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
| 836 // OverwriteCachedResponse |
| 835 // | 837 // |
| 836 // 10. HEAD. Sparse entry, partially cached: | 838 // 10. HEAD. Sparse entry, partially cached: |
| 837 // Serve the request from the cache, as long as it doesn't require | 839 // Serve the request from the cache, as long as it doesn't require |
| 838 // revalidation. Ignore missing ranges when deciding to revalidate. If the | 840 // revalidation. Ignore missing ranges when deciding to revalidate. If the |
| 839 // entry requires revalidation, ignore the whole request and go to full pass | 841 // entry requires revalidation, ignore the whole request and go to full pass |
| 840 // through (the result of the HEAD request will NOT update the entry). | 842 // through (the result of the HEAD request will NOT update the entry). |
| 841 // | 843 // |
| 842 // Start(): Basically the same as example 7, as we never create a partial_ | 844 // Start(): Basically the same as example 7, as we never create a partial_ |
| 843 // object for this request. | 845 // object for this request. |
| 844 // | 846 // |
| 847 // 11. Prefetch, not-cached entry: |
| 848 // The same as example 1. The "unused_since_prefetch" bit is stored as true in |
| 849 // UpdateCachedResponse. |
| 850 // |
| 851 // 12. Prefetch, cached entry: |
| 852 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between |
| 853 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch |
| 854 // bit is unset. |
| 855 // |
| 856 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true: |
| 857 // Skip validation, similar to example 2. |
| 858 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| 859 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation -> |
| 860 // BeginPartialCacheValidation() -> BeginCacheValidation() -> |
| 861 // SetupEntryForRead() |
| 862 // |
| 863 // Read(): |
| 864 // CacheReadData* |
| 865 // |
| 866 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: |
| 867 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between |
| 868 // CacheReadResponse* and CacheDispatchValidation. |
| 845 int HttpCache::Transaction::DoLoop(int result) { | 869 int HttpCache::Transaction::DoLoop(int result) { |
| 846 DCHECK(next_state_ != STATE_NONE); | 870 DCHECK(next_state_ != STATE_NONE); |
| 847 | 871 |
| 848 int rv = result; | 872 int rv = result; |
| 849 do { | 873 do { |
| 850 State state = next_state_; | 874 State state = next_state_; |
| 851 next_state_ = STATE_NONE; | 875 next_state_ = STATE_NONE; |
| 852 switch (state) { | 876 switch (state) { |
| 853 case STATE_GET_BACKEND: | 877 case STATE_GET_BACKEND: |
| 854 DCHECK_EQ(OK, rv); | 878 DCHECK_EQ(OK, rv); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 943 DCHECK_EQ(OK, rv); | 967 DCHECK_EQ(OK, rv); |
| 944 rv = DoPartialHeadersReceived(); | 968 rv = DoPartialHeadersReceived(); |
| 945 break; | 969 break; |
| 946 case STATE_CACHE_READ_RESPONSE: | 970 case STATE_CACHE_READ_RESPONSE: |
| 947 DCHECK_EQ(OK, rv); | 971 DCHECK_EQ(OK, rv); |
| 948 rv = DoCacheReadResponse(); | 972 rv = DoCacheReadResponse(); |
| 949 break; | 973 break; |
| 950 case STATE_CACHE_READ_RESPONSE_COMPLETE: | 974 case STATE_CACHE_READ_RESPONSE_COMPLETE: |
| 951 rv = DoCacheReadResponseComplete(rv); | 975 rv = DoCacheReadResponseComplete(rv); |
| 952 break; | 976 break; |
| 977 case STATE_CACHE_DISPATCH_VALIDATION: |
| 978 DCHECK_EQ(OK, rv); |
| 979 rv = DoCacheDispatchValidation(); |
| 980 break; |
| 981 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH: |
| 982 DCHECK_EQ(OK, rv); |
| 983 rv = DoCacheToggleUnusedSincePrefetch(); |
| 984 break; |
| 985 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE: |
| 986 rv = DoCacheToggleUnusedSincePrefetchComplete(rv); |
| 987 break; |
| 953 case STATE_CACHE_WRITE_RESPONSE: | 988 case STATE_CACHE_WRITE_RESPONSE: |
| 954 DCHECK_EQ(OK, rv); | 989 DCHECK_EQ(OK, rv); |
| 955 rv = DoCacheWriteResponse(); | 990 rv = DoCacheWriteResponse(); |
| 956 break; | 991 break; |
| 957 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE: | 992 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE: |
| 958 DCHECK_EQ(OK, rv); | 993 DCHECK_EQ(OK, rv); |
| 959 rv = DoCacheWriteTruncatedResponse(); | 994 rv = DoCacheWriteTruncatedResponse(); |
| 960 break; | 995 break; |
| 961 case STATE_CACHE_WRITE_RESPONSE_COMPLETE: | 996 case STATE_CACHE_WRITE_RESPONSE_COMPLETE: |
| 962 rv = DoCacheWriteResponseComplete(rv); | 997 rv = DoCacheWriteResponseComplete(rv); |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1622 "422516 HttpCache::Transaction::DoUpdateCachedResponse")); | 1657 "422516 HttpCache::Transaction::DoUpdateCachedResponse")); |
| 1623 | 1658 |
| 1624 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE; | 1659 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE; |
| 1625 int rv = OK; | 1660 int rv = OK; |
| 1626 // Update cached response based on headers in new_response. | 1661 // Update cached response based on headers in new_response. |
| 1627 // TODO(wtc): should we update cached certificate (response_.ssl_info), too? | 1662 // TODO(wtc): should we update cached certificate (response_.ssl_info), too? |
| 1628 response_.headers->Update(*new_response_->headers.get()); | 1663 response_.headers->Update(*new_response_->headers.get()); |
| 1629 response_.response_time = new_response_->response_time; | 1664 response_.response_time = new_response_->response_time; |
| 1630 response_.request_time = new_response_->request_time; | 1665 response_.request_time = new_response_->request_time; |
| 1631 response_.network_accessed = new_response_->network_accessed; | 1666 response_.network_accessed = new_response_->network_accessed; |
| 1667 response_.unused_since_prefetch = new_response_->unused_since_prefetch; |
| 1632 | 1668 |
| 1633 if (response_.headers->HasHeaderValue("cache-control", "no-store")) { | 1669 if (response_.headers->HasHeaderValue("cache-control", "no-store")) { |
| 1634 if (!entry_->doomed) { | 1670 if (!entry_->doomed) { |
| 1635 int ret = cache_->DoomEntry(cache_key_, NULL); | 1671 int ret = cache_->DoomEntry(cache_key_, NULL); |
| 1636 DCHECK_EQ(OK, ret); | 1672 DCHECK_EQ(OK, ret); |
| 1637 } | 1673 } |
| 1638 } else { | 1674 } else { |
| 1639 // If we are already reading, we already updated the headers for this | 1675 // If we are already reading, we already updated the headers for this |
| 1640 // request; doing it again will change Content-Length. | 1676 // request; doing it again will change Content-Length. |
| 1641 if (!reading_) { | 1677 if (!reading_) { |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1850 | 1886 |
| 1851 // cert_cache() will be null if the CertCacheTrial field trial is disabled. | 1887 // cert_cache() will be null if the CertCacheTrial field trial is disabled. |
| 1852 if (cache_->cert_cache() && response_.ssl_info.is_valid()) | 1888 if (cache_->cert_cache() && response_.ssl_info.is_valid()) |
| 1853 ReadCertChain(); | 1889 ReadCertChain(); |
| 1854 | 1890 |
| 1855 // Some resources may have slipped in as truncated when they're not. | 1891 // Some resources may have slipped in as truncated when they're not. |
| 1856 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 1892 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| 1857 if (response_.headers->GetContentLength() == current_size) | 1893 if (response_.headers->GetContentLength() == current_size) |
| 1858 truncated_ = false; | 1894 truncated_ = false; |
| 1859 | 1895 |
| 1896 if ((response_.unused_since_prefetch && |
| 1897 !(request_->load_flags & LOAD_PREFETCH)) || |
| 1898 (!response_.unused_since_prefetch && |
| 1899 (request_->load_flags & LOAD_PREFETCH))) { |
| 1900 // Either this is the first use of an entry since it was prefetched or |
| 1901 // this is a prefetch. The value of response.unused_since_prefetch is valid |
| 1902 // for this transaction but the bit needs to be flipped in storage. |
| 1903 next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH; |
| 1904 return OK; |
| 1905 } |
| 1906 |
| 1907 next_state_ = STATE_CACHE_DISPATCH_VALIDATION; |
| 1908 return OK; |
| 1909 } |
| 1910 |
| 1911 int HttpCache::Transaction::DoCacheDispatchValidation() { |
| 1860 // We now have access to the cache entry. | 1912 // We now have access to the cache entry. |
| 1861 // | 1913 // |
| 1862 // o if we are a reader for the transaction, then we can start reading the | 1914 // o if we are a reader for the transaction, then we can start reading the |
| 1863 // cache entry. | 1915 // cache entry. |
| 1864 // | 1916 // |
| 1865 // o if we can read or write, then we should check if the cache entry needs | 1917 // o if we can read or write, then we should check if the cache entry needs |
| 1866 // to be validated and then issue a network request if needed or just read | 1918 // to be validated and then issue a network request if needed or just read |
| 1867 // from the cache if the cache entry is already valid. | 1919 // from the cache if the cache entry is already valid. |
| 1868 // | 1920 // |
| 1869 // o if we are set to UPDATE, then we are handling an externally | 1921 // o if we are set to UPDATE, then we are handling an externally |
| 1870 // conditionalized request (if-modified-since / if-none-match). We check | 1922 // conditionalized request (if-modified-since / if-none-match). We check |
| 1871 // if the request headers define a validation request. | 1923 // if the request headers define a validation request. |
| 1872 // | 1924 // |
| 1925 int result = ERR_FAILED; |
| 1873 switch (mode_) { | 1926 switch (mode_) { |
| 1874 case READ: | 1927 case READ: |
| 1875 UpdateTransactionPattern(PATTERN_ENTRY_USED); | 1928 UpdateTransactionPattern(PATTERN_ENTRY_USED); |
| 1876 result = BeginCacheRead(); | 1929 result = BeginCacheRead(); |
| 1877 break; | 1930 break; |
| 1878 case READ_WRITE: | 1931 case READ_WRITE: |
| 1879 result = BeginPartialCacheValidation(); | 1932 result = BeginPartialCacheValidation(); |
| 1880 break; | 1933 break; |
| 1881 case UPDATE: | 1934 case UPDATE: |
| 1882 result = BeginExternallyConditionalizedRequest(); | 1935 result = BeginExternallyConditionalizedRequest(); |
| 1883 break; | 1936 break; |
| 1884 case WRITE: | 1937 case WRITE: |
| 1885 default: | 1938 default: |
| 1886 NOTREACHED(); | 1939 NOTREACHED(); |
| 1887 result = ERR_FAILED; | |
| 1888 } | 1940 } |
| 1889 return result; | 1941 return result; |
| 1890 } | 1942 } |
| 1891 | 1943 |
| 1944 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() { |
| 1945 // Write back the toggled value for the next use of this entry. |
| 1946 response_.unused_since_prefetch = !response_.unused_since_prefetch; |
| 1947 |
| 1948 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this |
| 1949 // transaction then metadata will be written to cache twice. If prefetching |
| 1950 // becomes more common, consider combining the writes. |
| 1951 target_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE; |
| 1952 next_state_ = STATE_CACHE_WRITE_RESPONSE; |
| 1953 return OK; |
| 1954 } |
| 1955 |
| 1956 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete( |
| 1957 int result) { |
| 1958 // Restore the original value for this transaction. |
| 1959 response_.unused_since_prefetch = !response_.unused_since_prefetch; |
| 1960 next_state_ = STATE_CACHE_DISPATCH_VALIDATION; |
| 1961 return OK; |
| 1962 } |
| 1963 |
| 1892 int HttpCache::Transaction::DoCacheWriteResponse() { | 1964 int HttpCache::Transaction::DoCacheWriteResponse() { |
| 1893 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. | 1965 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. |
| 1894 tracked_objects::ScopedTracker tracking_profile( | 1966 tracked_objects::ScopedTracker tracking_profile( |
| 1895 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1967 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1896 "422516 HttpCache::Transaction::DoCacheWriteResponse")); | 1968 "422516 HttpCache::Transaction::DoCacheWriteResponse")); |
| 1897 | 1969 |
| 1898 if (entry_) { | 1970 if (entry_) { |
| 1899 if (net_log_.IsLogging()) | 1971 if (net_log_.IsLogging()) |
| 1900 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); | 1972 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); |
| 1901 } | 1973 } |
| (...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2547 if (response_.vary_data.is_valid() && | 2619 if (response_.vary_data.is_valid() && |
| 2548 !response_.vary_data.MatchesRequest(*request_, | 2620 !response_.vary_data.MatchesRequest(*request_, |
| 2549 *response_.headers.get())) { | 2621 *response_.headers.get())) { |
| 2550 vary_mismatch_ = true; | 2622 vary_mismatch_ = true; |
| 2551 return VALIDATION_SYNCHRONOUS; | 2623 return VALIDATION_SYNCHRONOUS; |
| 2552 } | 2624 } |
| 2553 | 2625 |
| 2554 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) | 2626 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) |
| 2555 return VALIDATION_NONE; | 2627 return VALIDATION_NONE; |
| 2556 | 2628 |
| 2629 if (response_.unused_since_prefetch && |
| 2630 !(effective_load_flags_ & LOAD_PREFETCH) && |
| 2631 response_.headers->GetCurrentAge( |
| 2632 response_.request_time, response_.response_time, |
| 2633 cache_->clock_->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins)) { |
| 2634 // The first use of a resource after prefetch within a short window skips |
| 2635 // validation. |
| 2636 return VALIDATION_NONE; |
| 2637 } |
| 2638 |
| 2557 if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION)) | 2639 if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION)) |
| 2558 return VALIDATION_SYNCHRONOUS; | 2640 return VALIDATION_SYNCHRONOUS; |
| 2559 | 2641 |
| 2560 if (request_->method == "PUT" || request_->method == "DELETE") | 2642 if (request_->method == "PUT" || request_->method == "DELETE") |
| 2561 return VALIDATION_SYNCHRONOUS; | 2643 return VALIDATION_SYNCHRONOUS; |
| 2562 | 2644 |
| 2563 ValidationType validation_required_by_headers = | 2645 ValidationType validation_required_by_headers = |
| 2564 response_.headers->RequiresValidation( | 2646 response_.headers->RequiresValidation(response_.request_time, |
| 2565 response_.request_time, response_.response_time, Time::Now()); | 2647 response_.response_time, |
| 2648 cache_->clock_->Now()); |
| 2566 | 2649 |
| 2567 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) { | 2650 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) { |
| 2568 // Asynchronous revalidation is only supported for GET and HEAD methods. | 2651 // Asynchronous revalidation is only supported for GET and HEAD methods. |
| 2569 if (request_->method != "GET" && request_->method != "HEAD") | 2652 if (request_->method != "GET" && request_->method != "HEAD") |
| 2570 return VALIDATION_SYNCHRONOUS; | 2653 return VALIDATION_SYNCHRONOUS; |
| 2571 } | 2654 } |
| 2572 | 2655 |
| 2573 return validation_required_by_headers; | 2656 return validation_required_by_headers; |
| 2574 } | 2657 } |
| 2575 | 2658 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2617 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() && | 2700 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() && |
| 2618 !invalid_range_; | 2701 !invalid_range_; |
| 2619 | 2702 |
| 2620 if (!use_if_range) { | 2703 if (!use_if_range) { |
| 2621 // stale-while-revalidate is not useful when we only have a partial response | 2704 // stale-while-revalidate is not useful when we only have a partial response |
| 2622 // cached, so don't set the header in that case. | 2705 // cached, so don't set the header in that case. |
| 2623 HttpResponseHeaders::FreshnessLifetimes lifetimes = | 2706 HttpResponseHeaders::FreshnessLifetimes lifetimes = |
| 2624 response_.headers->GetFreshnessLifetimes(response_.response_time); | 2707 response_.headers->GetFreshnessLifetimes(response_.response_time); |
| 2625 if (lifetimes.staleness > TimeDelta()) { | 2708 if (lifetimes.staleness > TimeDelta()) { |
| 2626 TimeDelta current_age = response_.headers->GetCurrentAge( | 2709 TimeDelta current_age = response_.headers->GetCurrentAge( |
| 2627 response_.request_time, response_.response_time, Time::Now()); | 2710 response_.request_time, response_.response_time, |
| 2711 cache_->clock_->Now()); |
| 2628 | 2712 |
| 2629 custom_request_->extra_headers.SetHeader( | 2713 custom_request_->extra_headers.SetHeader( |
| 2630 kFreshnessHeader, | 2714 kFreshnessHeader, |
| 2631 base::StringPrintf("max-age=%" PRId64 | 2715 base::StringPrintf("max-age=%" PRId64 |
| 2632 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64, | 2716 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64, |
| 2633 lifetimes.freshness.InSeconds(), | 2717 lifetimes.freshness.InSeconds(), |
| 2634 lifetimes.staleness.InSeconds(), | 2718 lifetimes.staleness.InSeconds(), |
| 2635 current_age.InSeconds())); | 2719 current_age.InSeconds())); |
| 2636 } | 2720 } |
| 2637 } | 2721 } |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3187 | 3271 |
| 3188 void HttpCache::Transaction::OnIOComplete(int result) { | 3272 void HttpCache::Transaction::OnIOComplete(int result) { |
| 3189 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. | 3273 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. |
| 3190 tracked_objects::ScopedTracker tracking_profile( | 3274 tracked_objects::ScopedTracker tracking_profile( |
| 3191 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 Transaction::OnIOComplete")); | 3275 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 Transaction::OnIOComplete")); |
| 3192 | 3276 |
| 3193 DoLoop(result); | 3277 DoLoop(result); |
| 3194 } | 3278 } |
| 3195 | 3279 |
| 3196 } // namespace net | 3280 } // namespace net |
| OLD | NEW |