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