Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/http/partial_data.h" |
| 6 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 7 | #include <limits> |
Bence Béky | 74838e5 | 2018-08-03 19:25:23 | [diff] [blame] | 8 | #include <utility> |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 9 | |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 10 | #include "base/format_macros.h" |
Avi Drissman | 41c4a41 | 2023-01-11 22:45:37 | [diff] [blame] | 11 | #include "base/functional/bind.h" |
| 12 | #include "base/functional/callback_helpers.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 13 | #include "base/logging.h" |
avi@chromium.org | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/strings/string_util.h" |
| 16 | #include "base/strings/stringprintf.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 17 | #include "net/base/net_errors.h" |
| 18 | #include "net/disk_cache/disk_cache.h" |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 19 | #include "net/http/http_response_headers.h" |
Feifei Wang | ed3689f | 2022-05-29 23:35:38 | [diff] [blame] | 20 | #include "net/http/http_status_code.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 21 | #include "net/http/http_util.h" |
| 22 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 23 | namespace net { |
| 24 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // The headers that we have to process. |
| 28 | const char kLengthHeader[] = "Content-Length"; |
| 29 | const char kRangeHeader[] = "Content-Range"; |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 30 | const int kDataStream = 1; |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 31 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 32 | } // namespace |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 33 | |
Tsuyoshi Horo | 11e321eb | 2022-06-07 07:56:40 | [diff] [blame] | 34 | PartialData::PartialData() = default; |
erg@google.com | b104b50 | 2010-10-18 20:21:31 | [diff] [blame] | 35 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 36 | PartialData::~PartialData() = default; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 37 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 38 | bool PartialData::Init(const HttpRequestHeaders& headers) { |
| 39 | std::string range_header; |
Maks Orlovich | 70039f1 | 2018-11-07 18:52:57 | [diff] [blame] | 40 | if (!headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) { |
| 41 | range_requested_ = false; |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 42 | return false; |
Maks Orlovich | 70039f1 | 2018-11-07 18:52:57 | [diff] [blame] | 43 | } |
| 44 | range_requested_ = true; |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 45 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 46 | std::vector<HttpByteRange> ranges; |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 47 | if (!HttpUtil::ParseRangeHeader(range_header, &ranges) || ranges.size() != 1) |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 48 | return false; |
| 49 | |
| 50 | // We can handle this range request. |
| 51 | byte_range_ = ranges[0]; |
Maks Orlovich | 8e48d24 | 2023-04-25 18:44:26 | [diff] [blame] | 52 | user_byte_range_ = byte_range_; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 53 | if (!byte_range_.IsValid()) |
| 54 | return false; |
| 55 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 56 | current_range_start_ = byte_range_.first_byte_position(); |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 57 | |
| 58 | DVLOG(1) << "Range start: " << current_range_start_ << " end: " << |
| 59 | byte_range_.last_byte_position(); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 63 | void PartialData::SetHeaders(const HttpRequestHeaders& headers) { |
| 64 | DCHECK(extra_headers_.IsEmpty()); |
Adam Rice | 6104cc5b | 2023-10-05 08:34:16 | [diff] [blame] | 65 | extra_headers_ = headers; |
rvargas@google.com | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 66 | } |
| 67 | |
willchan@chromium.org | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 68 | void PartialData::RestoreHeaders(HttpRequestHeaders* headers) const { |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 69 | DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange()); |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 70 | int64_t end = byte_range_.IsSuffixByteRange() |
| 71 | ? byte_range_.suffix_length() |
| 72 | : byte_range_.last_byte_position(); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 73 | |
Adam Rice | 6104cc5b | 2023-10-05 08:34:16 | [diff] [blame] | 74 | *headers = extra_headers_; |
tommycli@chromium.org | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 75 | if (truncated_ || !byte_range_.IsValid()) |
| 76 | return; |
| 77 | |
| 78 | if (current_range_start_ < 0) { |
| 79 | headers->SetHeader(HttpRequestHeaders::kRange, |
| 80 | HttpByteRange::Suffix(end).GetHeaderValue()); |
| 81 | } else { |
| 82 | headers->SetHeader(HttpRequestHeaders::kRange, |
| 83 | HttpByteRange::Bounded( |
| 84 | current_range_start_, end).GetHeaderValue()); |
| 85 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 86 | } |
| 87 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 88 | int PartialData::ShouldValidateCache(disk_cache::Entry* entry, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 89 | CompletionOnceCallback callback) { |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 90 | DCHECK_GE(current_range_start_, 0); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 91 | |
| 92 | // Scan the disk cache for the first cached portion within this range. |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 93 | int len = GetNextRangeLen(); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 94 | if (!len) |
| 95 | return 0; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 96 | |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 97 | DVLOG(3) << "ShouldValidateCache len: " << len; |
| 98 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 99 | if (sparse_entry_) { |
jhawkins@chromium.org | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 100 | DCHECK(callback_.is_null()); |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 101 | disk_cache::RangeResultCallback cb = base::BindOnce( |
| 102 | &PartialData::GetAvailableRangeCompleted, weak_factory_.GetWeakPtr()); |
| 103 | disk_cache::RangeResult range = |
| 104 | entry->GetAvailableRange(current_range_start_, len, std::move(cb)); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 105 | |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 106 | cached_min_len_ = |
| 107 | range.net_error == OK ? range.available_len : range.net_error; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 108 | if (cached_min_len_ == ERR_IO_PENDING) { |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 109 | callback_ = std::move(callback); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 110 | return ERR_IO_PENDING; |
hubbe | 6cad67c | 2015-07-09 19:01:02 | [diff] [blame] | 111 | } else { |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 112 | cached_start_ = range.start; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 113 | } |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 114 | } else if (!truncated_) { |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 115 | if (byte_range_.HasFirstBytePosition() && |
| 116 | byte_range_.first_byte_position() >= resource_size_) { |
| 117 | // The caller should take care of this condition because we should have |
| 118 | // failed IsRequestedRangeOK(), but it's better to be consistent here. |
| 119 | len = 0; |
| 120 | } |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 121 | cached_min_len_ = len; |
| 122 | cached_start_ = current_range_start_; |
| 123 | } |
| 124 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 125 | if (cached_min_len_ < 0) |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 126 | return cached_min_len_; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 127 | |
| 128 | // Return a positive number to indicate success (versus error or finished). |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | void PartialData::PrepareCacheValidation(disk_cache::Entry* entry, |
| 133 | HttpRequestHeaders* headers) { |
| 134 | DCHECK_GE(current_range_start_, 0); |
| 135 | DCHECK_GE(cached_min_len_, 0); |
| 136 | |
| 137 | int len = GetNextRangeLen(); |
Maks Orlovich | 8e48d24 | 2023-04-25 18:44:26 | [diff] [blame] | 138 | if (!len) { |
| 139 | // Stored body is empty, so just use the original range header. |
| 140 | headers->SetHeader(HttpRequestHeaders::kRange, |
| 141 | user_byte_range_.GetHeaderValue()); |
| 142 | return; |
| 143 | } |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 144 | range_present_ = false; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 145 | |
Adam Rice | 6104cc5b | 2023-10-05 08:34:16 | [diff] [blame] | 146 | *headers = extra_headers_; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 147 | |
| 148 | if (!cached_min_len_) { |
| 149 | // We don't have anything else stored. |
| 150 | final_range_ = true; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 151 | cached_start_ = |
| 152 | byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (current_range_start_ == cached_start_) { |
| 156 | // The data lives in the cache. |
| 157 | range_present_ = true; |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 158 | current_range_end_ = cached_start_ + cached_min_len_ - 1; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 159 | if (len == cached_min_len_) |
| 160 | final_range_ = true; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 161 | } else { |
| 162 | // This range is not in the cache. |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 163 | current_range_end_ = cached_start_ - 1; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 164 | } |
hubbe | 70fbde6a | 2015-07-08 19:24:14 | [diff] [blame] | 165 | headers->SetHeader( |
| 166 | HttpRequestHeaders::kRange, |
| 167 | HttpByteRange::Bounded(current_range_start_, current_range_end_) |
| 168 | .GetHeaderValue()); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool PartialData::IsCurrentRangeCached() const { |
| 172 | return range_present_; |
| 173 | } |
| 174 | |
| 175 | bool PartialData::IsLastRange() const { |
| 176 | return final_range_; |
| 177 | } |
| 178 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 179 | bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers, |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 180 | disk_cache::Entry* entry, |
Maks Orlovich | d19c827 | 2018-03-23 15:27:05 | [diff] [blame] | 181 | bool truncated, |
| 182 | bool writing_in_progress) { |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 183 | resource_size_ = 0; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 184 | if (truncated) { |
| 185 | DCHECK_EQ(headers->response_code(), 200); |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 186 | // We don't have the real length and the user may be trying to create a |
| 187 | // sparse entry so let's not write to this entry. |
| 188 | if (byte_range_.IsValid()) |
| 189 | return false; |
| 190 | |
rvargas@google.com | 9f03cb7a | 2012-07-30 23:15:20 | [diff] [blame] | 191 | if (!headers->HasStrongValidators()) |
| 192 | return false; |
| 193 | |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 194 | // Now we avoid resume if there is no content length, but that was not |
| 195 | // always the case so double check here. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 196 | int64_t total_length = headers->GetContentLength(); |
rvargas@google.com | bd069d7 | 2011-05-19 01:11:11 | [diff] [blame] | 197 | if (total_length <= 0) |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 198 | return false; |
| 199 | |
Maks Orlovich | 332bb11a | 2018-03-17 01:43:32 | [diff] [blame] | 200 | // In case we see a truncated entry, we first send a network request for |
| 201 | // 1 byte range with If-Range: to probe server support for resumption. |
| 202 | // The setting of |current_range_start_| and |cached_start_| below (with any |
| 203 | // positive value of |cached_min_len_|) results in that. |
| 204 | // |
| 205 | // Setting |initial_validation_| to true is how this communicates to |
| 206 | // HttpCache::Transaction that we're doing that (and that it's not the user |
| 207 | // asking for one byte), so if it sees a 206 with that flag set it will call |
| 208 | // SetRangeToStartDownload(), and then restart the process looking for the |
| 209 | // entire file (which is what the user wanted), with the cache handling |
| 210 | // the previous portion, and then a second network request for the entire |
| 211 | // rest of the range. A 200 in response to the probe request can be simply |
| 212 | // returned directly to the user. |
hclam@chromium.org | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 213 | truncated_ = true; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 214 | initial_validation_ = true; |
hclam@chromium.org | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 215 | sparse_entry_ = false; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 216 | int current_len = entry->GetDataSize(kDataStream); |
| 217 | byte_range_.set_first_byte_position(current_len); |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 218 | resource_size_ = total_length; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 219 | current_range_start_ = current_len; |
| 220 | cached_min_len_ = current_len; |
| 221 | cached_start_ = current_len + 1; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
Feifei Wang | ed3689f | 2022-05-29 23:35:38 | [diff] [blame] | 225 | sparse_entry_ = (headers->response_code() == net::HTTP_PARTIAL_CONTENT); |
Maks Orlovich | d19c827 | 2018-03-23 15:27:05 | [diff] [blame] | 226 | |
| 227 | if (writing_in_progress || sparse_entry_) { |
| 228 | // |writing_in_progress| means another Transaction is still fetching the |
| 229 | // body, so the only way we can see the length is if the server sent it |
| 230 | // in Content-Length -- GetDataSize would just return what got written |
| 231 | // thus far. |
| 232 | // |
| 233 | // |sparse_entry_| means a 206, and for those FixContentLength arranges it |
| 234 | // so that Content-Length written to the cache has the full length (on wire |
| 235 | // it's for a particular range only); while GetDataSize would be unusable |
| 236 | // since the data is stored using WriteSparseData, and not in the usual data |
| 237 | // stream. |
| 238 | resource_size_ = headers->GetContentLength(); |
| 239 | if (resource_size_ <= 0) |
| 240 | return false; |
| 241 | } else { |
| 242 | // If we can safely use GetDataSize, it's preferrable since it's usable for |
| 243 | // things w/o Content-Length, such as chunked content. |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 244 | resource_size_ = entry->GetDataSize(kDataStream); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 245 | } |
| 246 | |
Maks Orlovich | d19c827 | 2018-03-23 15:27:05 | [diff] [blame] | 247 | DVLOG(2) << "UpdateFromStoredHeaders size: " << resource_size_; |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 248 | |
Maks Orlovich | d19c827 | 2018-03-23 15:27:05 | [diff] [blame] | 249 | if (sparse_entry_) { |
| 250 | // If our previous is a 206, we need strong validators as we may be |
| 251 | // stiching the cached data and network data together. |
| 252 | if (!headers->HasStrongValidators()) |
| 253 | return false; |
| 254 | // Make sure that this is really a sparse entry. |
| 255 | return entry->CouldBeSparse(); |
| 256 | } |
| 257 | return true; |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 258 | } |
| 259 | |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 260 | void PartialData::SetRangeToStartDownload() { |
| 261 | DCHECK(truncated_); |
| 262 | DCHECK(!sparse_entry_); |
| 263 | current_range_start_ = 0; |
| 264 | cached_start_ = 0; |
| 265 | initial_validation_ = false; |
| 266 | } |
| 267 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 268 | bool PartialData::IsRequestedRangeOK() { |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 269 | if (byte_range_.IsValid()) { |
| 270 | if (!byte_range_.ComputeBounds(resource_size_)) |
| 271 | return false; |
rvargas@google.com | 634739b | 2011-03-02 18:08:25 | [diff] [blame] | 272 | if (truncated_) |
| 273 | return true; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 274 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 275 | if (current_range_start_ < 0) |
| 276 | current_range_start_ = byte_range_.first_byte_position(); |
| 277 | } else { |
| 278 | // This is not a range request but we have partial data stored. |
| 279 | current_range_start_ = 0; |
| 280 | byte_range_.set_last_byte_position(resource_size_ - 1); |
| 281 | } |
| 282 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 283 | bool rv = current_range_start_ >= 0; |
| 284 | if (!rv) |
| 285 | current_range_start_ = 0; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 286 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 287 | return rv; |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) { |
Feifei Wang | ed3689f | 2022-05-29 23:35:38 | [diff] [blame] | 291 | if (headers->response_code() == net::HTTP_NOT_MODIFIED) { |
hclam@chromium.org | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 292 | if (!byte_range_.IsValid() || truncated_) |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 293 | return true; |
| 294 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 295 | // We must have a complete range here. |
| 296 | return byte_range_.HasFirstBytePosition() && |
cbentzel@chromium.org | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 297 | byte_range_.HasLastBytePosition(); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 298 | } |
| 299 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 300 | int64_t start, end, total_length; |
sclittle | d43b2fb | 2016-12-17 03:20:47 | [diff] [blame] | 301 | if (!headers->GetContentRangeFor206(&start, &end, &total_length)) |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 302 | return false; |
| 303 | if (total_length <= 0) |
| 304 | return false; |
| 305 | |
rvargas@chromium.org | 9f10ec3 | 2013-11-01 00:51:53 | [diff] [blame] | 306 | DCHECK_EQ(headers->response_code(), 206); |
| 307 | |
| 308 | // A server should return a valid content length with a 206 (per the standard) |
| 309 | // but relax the requirement because some servers don't do that. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 310 | int64_t content_length = headers->GetContentLength(); |
rvargas@chromium.org | 9f10ec3 | 2013-11-01 00:51:53 | [diff] [blame] | 311 | if (content_length > 0 && content_length != end - start + 1) |
rvargas@google.com | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 312 | return false; |
| 313 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 314 | if (!resource_size_) { |
| 315 | // First response. Update our values with the ones provided by the server. |
| 316 | resource_size_ = total_length; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 317 | if (!byte_range_.HasFirstBytePosition()) { |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 318 | byte_range_.set_first_byte_position(start); |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 319 | current_range_start_ = start; |
| 320 | } |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 321 | if (!byte_range_.HasLastBytePosition()) |
| 322 | byte_range_.set_last_byte_position(end); |
| 323 | } else if (resource_size_ != total_length) { |
| 324 | return false; |
| 325 | } |
| 326 | |
rvargas@google.com | dbd39fb | 2010-01-08 01:13:36 | [diff] [blame] | 327 | if (truncated_) { |
| 328 | if (!byte_range_.HasLastBytePosition()) |
| 329 | byte_range_.set_last_byte_position(end); |
| 330 | } |
| 331 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 332 | if (start != current_range_start_) |
| 333 | return false; |
| 334 | |
rvargas | 3b57e37a | 2015-01-06 00:56:34 | [diff] [blame] | 335 | if (!current_range_end_) { |
| 336 | // There is nothing in the cache. |
| 337 | DCHECK(byte_range_.HasLastBytePosition()); |
| 338 | current_range_end_ = byte_range_.last_byte_position(); |
| 339 | if (current_range_end_ >= resource_size_) { |
| 340 | // We didn't know the real file size, and the server is saying that the |
| 341 | // requested range goes beyond the size. Fix it. |
| 342 | current_range_end_ = end; |
| 343 | byte_range_.set_last_byte_position(end); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // If we received a range, but it's not exactly the range we asked for, avoid |
| 348 | // trouble and signal an error. |
| 349 | if (end != current_range_end_) |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 350 | return false; |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | // We are making multiple requests to complete the range requested by the user. |
| 356 | // Just assume that everything is fine and say that we are returning what was |
| 357 | // requested. |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 358 | void PartialData::FixResponseHeaders(HttpResponseHeaders* headers, |
| 359 | bool success) { |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 360 | if (truncated_) |
| 361 | return; |
| 362 | |
Maks Orlovich | 4b3edfc | 2023-04-25 17:21:08 | [diff] [blame] | 363 | if (!success) { |
kinuko@chromium.org | 3d47dac962 | 2014-03-10 07:28:58 | [diff] [blame] | 364 | headers->ReplaceStatusLine("HTTP/1.1 416 Requested Range Not Satisfiable"); |
Matt Menke | 73ed38f | 2020-04-10 21:29:11 | [diff] [blame] | 365 | headers->SetHeader( |
| 366 | kRangeHeader, base::StringPrintf("bytes 0-0/%" PRId64, resource_size_)); |
| 367 | headers->SetHeader(kLengthHeader, "0"); |
Maks Orlovich | 4b3edfc | 2023-04-25 17:21:08 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
| 371 | if (byte_range_.IsValid() && resource_size_) { |
| 372 | headers->UpdateWithNewRange(byte_range_, resource_size_, !sparse_entry_); |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 373 | } else { |
Maks Orlovich | 4b3edfc | 2023-04-25 17:21:08 | [diff] [blame] | 374 | if (headers->response_code() == net::HTTP_PARTIAL_CONTENT) { |
| 375 | // TODO(rvargas): Is it safe to change the protocol version? |
| 376 | headers->ReplaceStatusLine("HTTP/1.1 200 OK"); |
| 377 | } |
Matt Menke | 73ed38f | 2020-04-10 21:29:11 | [diff] [blame] | 378 | headers->RemoveHeader(kRangeHeader); |
| 379 | headers->SetHeader(kLengthHeader, |
| 380 | base::StringPrintf("%" PRId64, resource_size_)); |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 381 | } |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void PartialData::FixContentLength(HttpResponseHeaders* headers) { |
Matt Menke | 73ed38f | 2020-04-10 21:29:11 | [diff] [blame] | 385 | headers->SetHeader(kLengthHeader, |
| 386 | base::StringPrintf("%" PRId64, resource_size_)); |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 387 | } |
| 388 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 389 | int PartialData::CacheRead(disk_cache::Entry* entry, |
| 390 | IOBuffer* data, |
| 391 | int data_len, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 392 | CompletionOnceCallback callback) { |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 393 | int read_len = std::min(data_len, cached_min_len_); |
hclam@chromium.org | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 394 | if (!read_len) |
| 395 | return 0; |
| 396 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 397 | int rv = 0; |
| 398 | if (sparse_entry_) { |
| 399 | rv = entry->ReadSparseData(current_range_start_, data, read_len, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 400 | std::move(callback)); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 401 | } else { |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 402 | if (current_range_start_ > std::numeric_limits<int32_t>::max()) |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 403 | return ERR_INVALID_ARGUMENT; |
| 404 | |
| 405 | rv = entry->ReadData(kDataStream, static_cast<int>(current_range_start_), |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 406 | data, read_len, std::move(callback)); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 407 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 408 | return rv; |
| 409 | } |
| 410 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 411 | int PartialData::CacheWrite(disk_cache::Entry* entry, |
| 412 | IOBuffer* data, |
| 413 | int data_len, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 414 | CompletionOnceCallback callback) { |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 415 | DVLOG(3) << "To write: " << data_len; |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 416 | if (sparse_entry_) { |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 417 | return entry->WriteSparseData(current_range_start_, data, data_len, |
| 418 | std::move(callback)); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 419 | } else { |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 420 | if (current_range_start_ > std::numeric_limits<int32_t>::max()) |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 421 | return ERR_INVALID_ARGUMENT; |
| 422 | |
| 423 | return entry->WriteData(kDataStream, static_cast<int>(current_range_start_), |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 424 | data, data_len, std::move(callback), true); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 425 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void PartialData::OnCacheReadCompleted(int result) { |
rvargas@google.com | a5c9d98 | 2010-10-12 20:48:02 | [diff] [blame] | 429 | DVLOG(3) << "Read: " << result; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 430 | if (result > 0) { |
| 431 | current_range_start_ += result; |
| 432 | cached_min_len_ -= result; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 433 | DCHECK_GE(cached_min_len_, 0); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | void PartialData::OnNetworkReadCompleted(int result) { |
| 438 | if (result > 0) |
| 439 | current_range_start_ += result; |
| 440 | } |
| 441 | |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 442 | int PartialData::GetNextRangeLen() { |
Maks Orlovich | 4b3edfc | 2023-04-25 17:21:08 | [diff] [blame] | 443 | if (!resource_size_) { |
| 444 | return 0; |
| 445 | } |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 446 | int64_t range_len = |
| 447 | byte_range_.HasLastBytePosition() |
| 448 | ? byte_range_.last_byte_position() - current_range_start_ + 1 |
| 449 | : std::numeric_limits<int32_t>::max(); |
| 450 | if (range_len > std::numeric_limits<int32_t>::max()) |
| 451 | range_len = std::numeric_limits<int32_t>::max(); |
| 452 | return static_cast<int32_t>(range_len); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 453 | } |
| 454 | |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 455 | void PartialData::GetAvailableRangeCompleted( |
| 456 | const disk_cache::RangeResult& result) { |
jhawkins@chromium.org | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 457 | DCHECK(!callback_.is_null()); |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 458 | DCHECK_NE(ERR_IO_PENDING, result.net_error); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 459 | |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 460 | int len_or_error = |
| 461 | result.net_error == OK ? result.available_len : result.net_error; |
| 462 | cached_start_ = result.start; |
| 463 | cached_min_len_ = len_or_error; |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 464 | |
Maks Orlovich | 04cd1ad | 2021-07-02 17:32:24 | [diff] [blame] | 465 | // ShouldValidateCache has an unusual convention where 0 denotes EOF, |
| 466 | // so convert end of range to success (since there may be things that need |
| 467 | // fetching from network or other ranges). |
| 468 | std::move(callback_).Run(len_or_error >= 0 ? 1 : len_or_error); |
rvargas@google.com | 034740a | 2010-06-11 17:16:48 | [diff] [blame] | 469 | } |
| 470 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 471 | } // namespace net |