rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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 | |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame^] | 7 | #include "base/format_macros.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 8 | #include "base/logging.h" |
| 9 | #include "base/string_util.h" |
| 10 | #include "net/base/net_errors.h" |
| 11 | #include "net/disk_cache/disk_cache.h" |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 12 | #include "net/http/http_response_headers.h" |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 13 | #include "net/http/http_util.h" |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // The headers that we have to process. |
| 18 | const char kLengthHeader[] = "Content-Length"; |
| 19 | const char kRangeHeader[] = "Content-Range"; |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 20 | const int kDataStream = 1; |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 21 | |
| 22 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 23 | |
| 24 | namespace net { |
| 25 | |
rvargas@google.com | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 26 | bool PartialData::Init(const std::string& headers) { |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 27 | std::vector<HttpByteRange> ranges; |
| 28 | if (!HttpUtil::ParseRanges(headers, &ranges) || ranges.size() != 1) |
| 29 | return false; |
| 30 | |
| 31 | // We can handle this range request. |
| 32 | byte_range_ = ranges[0]; |
| 33 | if (!byte_range_.IsValid()) |
| 34 | return false; |
| 35 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 36 | resource_size_ = 0; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 37 | current_range_start_ = byte_range_.first_byte_position(); |
| 38 | return true; |
| 39 | } |
| 40 | |
rvargas@google.com | e75e8af | 2009-11-03 00:04:20 | [diff] [blame] | 41 | void PartialData::SetHeaders(const std::string& headers) { |
| 42 | DCHECK(extra_headers_.empty()); |
| 43 | extra_headers_ = headers; |
| 44 | } |
| 45 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 46 | void PartialData::RestoreHeaders(std::string* headers) const { |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 47 | DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange()); |
| 48 | int64 end = byte_range_.IsSuffixByteRange() ? |
| 49 | byte_range_.suffix_length() : byte_range_.last_byte_position(); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 50 | |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 51 | AddRangeHeader(current_range_start_, end, headers); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int PartialData::PrepareCacheValidation(disk_cache::Entry* entry, |
| 55 | std::string* headers) { |
| 56 | DCHECK(current_range_start_ >= 0); |
| 57 | |
| 58 | // Scan the disk cache for the first cached portion within this range. |
| 59 | int64 range_len = byte_range_.HasLastBytePosition() ? |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 60 | byte_range_.last_byte_position() - current_range_start_ + 1 : kint32max; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 61 | if (range_len > kint32max) |
| 62 | range_len = kint32max; |
| 63 | int len = static_cast<int32>(range_len); |
| 64 | if (!len) |
| 65 | return 0; |
| 66 | range_present_ = false; |
| 67 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 68 | if (sparse_entry_) { |
| 69 | cached_min_len_ = entry->GetAvailableRange(current_range_start_, len, |
| 70 | &cached_start_); |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 71 | } else if (truncated_) { |
| 72 | if (!current_range_start_) { |
| 73 | // Update the cached range only the first time. |
| 74 | cached_min_len_ = static_cast<int32>(byte_range_.first_byte_position()); |
| 75 | cached_start_ = 0; |
| 76 | } |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 77 | } else { |
| 78 | cached_min_len_ = len; |
| 79 | cached_start_ = current_range_start_; |
| 80 | } |
| 81 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 82 | if (cached_min_len_ < 0) { |
| 83 | DCHECK(cached_min_len_ != ERR_IO_PENDING); |
| 84 | return cached_min_len_; |
| 85 | } |
| 86 | |
| 87 | headers->assign(extra_headers_); |
| 88 | |
| 89 | if (!cached_min_len_) { |
| 90 | // We don't have anything else stored. |
| 91 | final_range_ = true; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 92 | cached_start_ = |
| 93 | byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0; |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (current_range_start_ == cached_start_) { |
| 97 | // The data lives in the cache. |
| 98 | range_present_ = true; |
| 99 | if (len == cached_min_len_) |
| 100 | final_range_ = true; |
| 101 | AddRangeHeader(current_range_start_, cached_start_ + cached_min_len_ - 1, |
| 102 | headers); |
| 103 | } else { |
| 104 | // This range is not in the cache. |
| 105 | AddRangeHeader(current_range_start_, cached_start_ - 1, headers); |
| 106 | } |
| 107 | |
| 108 | // Return a positive number to indicate success (versus error or finished). |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | bool PartialData::IsCurrentRangeCached() const { |
| 113 | return range_present_; |
| 114 | } |
| 115 | |
| 116 | bool PartialData::IsLastRange() const { |
| 117 | return final_range_; |
| 118 | } |
| 119 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 120 | bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers, |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 121 | disk_cache::Entry* entry, |
| 122 | bool truncated) { |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 123 | resource_size_ = 0; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 124 | if (truncated) { |
| 125 | DCHECK_EQ(headers->response_code(), 200); |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 126 | // We don't have the real length and the user may be trying to create a |
| 127 | // sparse entry so let's not write to this entry. |
| 128 | if (byte_range_.IsValid()) |
| 129 | return false; |
| 130 | |
hclam@chromium.org | ecd8becb | 2009-10-02 17:57:45 | [diff] [blame] | 131 | truncated_ = true; |
| 132 | sparse_entry_ = false; |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 133 | byte_range_.set_first_byte_position(entry->GetDataSize(kDataStream)); |
| 134 | current_range_start_ = 0; |
| 135 | return true; |
| 136 | } |
| 137 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 138 | if (headers->response_code() == 200) { |
| 139 | DCHECK(byte_range_.IsValid()); |
| 140 | sparse_entry_ = false; |
| 141 | resource_size_ = entry->GetDataSize(kDataStream); |
| 142 | return true; |
| 143 | } |
| 144 | |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 145 | std::string length_value; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 146 | if (!headers->GetNormalizedHeader(kLengthHeader, &length_value)) |
| 147 | return false; // We must have stored the resource length. |
| 148 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 149 | if (!StringToInt64(length_value, &resource_size_) || !resource_size_) |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 150 | return false; |
| 151 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 152 | // Make sure that this is really a sparse entry. |
| 153 | int64 n; |
| 154 | if (ERR_CACHE_OPERATION_NOT_SUPPORTED == entry->GetAvailableRange(0, 5, &n)) |
| 155 | return false; |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool PartialData::IsRequestedRangeOK() { |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 161 | if (byte_range_.IsValid()) { |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 162 | if (truncated_) |
| 163 | return true; |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 164 | if (!byte_range_.ComputeBounds(resource_size_)) |
| 165 | return false; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 166 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 167 | if (current_range_start_ < 0) |
| 168 | current_range_start_ = byte_range_.first_byte_position(); |
| 169 | } else { |
| 170 | // This is not a range request but we have partial data stored. |
| 171 | current_range_start_ = 0; |
| 172 | byte_range_.set_last_byte_position(resource_size_ - 1); |
| 173 | } |
| 174 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 175 | bool rv = current_range_start_ >= 0; |
| 176 | if (!rv) |
| 177 | current_range_start_ = 0; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 178 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 179 | return rv; |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) { |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 183 | if (headers->response_code() == 304) { |
hclam@chromium.org | d9adff2c | 2009-09-05 01:15:45 | [diff] [blame] | 184 | if (!byte_range_.IsValid() || truncated_) |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 185 | return true; |
| 186 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 187 | // We must have a complete range here. |
| 188 | return byte_range_.HasFirstBytePosition() && |
| 189 | byte_range_.HasLastBytePosition(); |
| 190 | } |
| 191 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 192 | int64 start, end, total_length; |
| 193 | if (!headers->GetContentRange(&start, &end, &total_length)) |
| 194 | return false; |
| 195 | if (total_length <= 0) |
| 196 | return false; |
| 197 | |
rvargas@google.com | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 198 | int64 content_length = headers->GetContentLength(); |
cevans@chromium.org | c3695f5 | 2009-10-28 18:29:54 | [diff] [blame] | 199 | if (content_length < 0 || content_length != end - start + 1) |
rvargas@google.com | 7eab0d226 | 2009-10-14 22:05:54 | [diff] [blame] | 200 | return false; |
| 201 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 202 | if (!resource_size_) { |
| 203 | // First response. Update our values with the ones provided by the server. |
| 204 | resource_size_ = total_length; |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 205 | if (!byte_range_.HasFirstBytePosition()) { |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 206 | byte_range_.set_first_byte_position(start); |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 207 | current_range_start_ = start; |
| 208 | } |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 209 | if (!byte_range_.HasLastBytePosition()) |
| 210 | byte_range_.set_last_byte_position(end); |
| 211 | } else if (resource_size_ != total_length) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if (start != current_range_start_) |
| 216 | return false; |
| 217 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 218 | if (byte_range_.IsValid() && end > byte_range_.last_byte_position()) |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 219 | return false; |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | // We are making multiple requests to complete the range requested by the user. |
| 225 | // Just assume that everything is fine and say that we are returning what was |
| 226 | // requested. |
| 227 | void PartialData::FixResponseHeaders(HttpResponseHeaders* headers) { |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 228 | if (truncated_) |
| 229 | return; |
| 230 | |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 231 | headers->RemoveHeader(kLengthHeader); |
| 232 | headers->RemoveHeader(kRangeHeader); |
| 233 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 234 | int64 range_len; |
| 235 | if (byte_range_.IsValid()) { |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 236 | if (!sparse_entry_) |
| 237 | headers->ReplaceStatusLine("HTTP/1.1 206 Partial Content"); |
| 238 | |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 239 | DCHECK(byte_range_.HasFirstBytePosition()); |
| 240 | DCHECK(byte_range_.HasLastBytePosition()); |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame^] | 241 | headers->AddHeader( |
| 242 | StringPrintf("%s: bytes %" PRId64 "-%" PRId64 "/%" PRId64, |
| 243 | kRangeHeader, |
| 244 | byte_range_.first_byte_position(), |
| 245 | byte_range_.last_byte_position(), |
| 246 | resource_size_)); |
rvargas@google.com | 44f873a6 | 2009-08-12 00:14:48 | [diff] [blame] | 247 | range_len = byte_range_.last_byte_position() - |
| 248 | byte_range_.first_byte_position() + 1; |
| 249 | } else { |
| 250 | // TODO(rvargas): Is it safe to change the protocol version? |
| 251 | headers->ReplaceStatusLine("HTTP/1.1 200 OK"); |
| 252 | DCHECK_NE(resource_size_, 0); |
| 253 | range_len = resource_size_; |
| 254 | } |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 255 | |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame^] | 256 | headers->AddHeader(StringPrintf("%s: %" PRId64, kLengthHeader, range_len)); |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void PartialData::FixContentLength(HttpResponseHeaders* headers) { |
| 260 | headers->RemoveHeader(kLengthHeader); |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame^] | 261 | headers->AddHeader(StringPrintf("%s: %" PRId64, kLengthHeader, |
| 262 | resource_size_)); |
rvargas@google.com | 95792eb1 | 2009-06-22 21:30:40 | [diff] [blame] | 263 | } |
| 264 | |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 265 | int PartialData::CacheRead(disk_cache::Entry* entry, IOBuffer* data, |
| 266 | int data_len, CompletionCallback* callback) { |
| 267 | int read_len = std::min(data_len, cached_min_len_); |
hclam@chromium.org | 8f28d63 | 2009-10-01 22:09:21 | [diff] [blame] | 268 | if (!read_len) |
| 269 | return 0; |
| 270 | |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 271 | int rv = 0; |
| 272 | if (sparse_entry_) { |
| 273 | rv = entry->ReadSparseData(current_range_start_, data, read_len, |
| 274 | callback); |
| 275 | } else { |
| 276 | if (current_range_start_ > kint32max) |
| 277 | return ERR_INVALID_ARGUMENT; |
| 278 | |
| 279 | rv = entry->ReadData(kDataStream, static_cast<int>(current_range_start_), |
| 280 | data, read_len, callback); |
| 281 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 282 | return rv; |
| 283 | } |
| 284 | |
| 285 | int PartialData::CacheWrite(disk_cache::Entry* entry, IOBuffer* data, |
| 286 | int data_len, CompletionCallback* callback) { |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 287 | if (sparse_entry_) { |
| 288 | return entry->WriteSparseData(current_range_start_, data, data_len, |
| 289 | callback); |
| 290 | } else { |
| 291 | if (current_range_start_ > kint32max) |
| 292 | return ERR_INVALID_ARGUMENT; |
| 293 | |
| 294 | return entry->WriteData(kDataStream, static_cast<int>(current_range_start_), |
rvargas@google.com | 28accfe | 2009-09-04 23:36:33 | [diff] [blame] | 295 | data, data_len, callback, true); |
rvargas@google.com | e5dad13 | 2009-08-18 00:53:41 | [diff] [blame] | 296 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void PartialData::OnCacheReadCompleted(int result) { |
| 300 | if (result > 0) { |
| 301 | current_range_start_ += result; |
| 302 | cached_min_len_ -= result; |
| 303 | DCHECK(cached_min_len_ >= 0); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | void PartialData::OnNetworkReadCompleted(int result) { |
| 308 | if (result > 0) |
| 309 | current_range_start_ += result; |
| 310 | } |
| 311 | |
| 312 | // Static. |
| 313 | void PartialData::AddRangeHeader(int64 start, int64 end, std::string* headers) { |
rvargas@google.com | 67fe45c | 2009-06-24 17:44:57 | [diff] [blame] | 314 | DCHECK(start >= 0 || end >= 0); |
| 315 | std::string my_start, my_end; |
| 316 | if (start >= 0) |
| 317 | my_start = Int64ToString(start); |
| 318 | if (end >= 0) |
| 319 | my_end = Int64ToString(end); |
| 320 | |
| 321 | headers->append(StringPrintf("Range: bytes=%s-%s\r\n", my_start.c_str(), |
| 322 | my_end.c_str())); |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 323 | } |
rvargas@google.com | 8bf26f49a | 2009-06-12 17:35:50 | [diff] [blame] | 324 | |
| 325 | } // namespace net |