[go: nahoru, domu]

blob: fcd7808e3f2fc5aeaa38701e453f36dd07c364eb [file] [log] [blame]
rvargas@google.com8bf26f49a2009-06-12 17:35:501// 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
7#include "base/logging.h"
8#include "base/string_util.h"
9#include "net/base/net_errors.h"
10#include "net/disk_cache/disk_cache.h"
rvargas@google.com95792eb12009-06-22 21:30:4011#include "net/http/http_response_headers.h"
rvargas@google.com8bf26f49a2009-06-12 17:35:5012#include "net/http/http_util.h"
rvargas@google.com95792eb12009-06-22 21:30:4013
14namespace {
15
16// The headers that we have to process.
17const char kLengthHeader[] = "Content-Length";
18const char kRangeHeader[] = "Content-Range";
rvargas@google.come5dad132009-08-18 00:53:4119const int kDataStream = 1;
rvargas@google.com95792eb12009-06-22 21:30:4020
21}
rvargas@google.com8bf26f49a2009-06-12 17:35:5022
23namespace net {
24
25bool PartialData::Init(const std::string& headers,
26 const std::string& new_headers) {
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
36 extra_headers_ = new_headers;
rvargas@google.com95792eb12009-06-22 21:30:4037 resource_size_ = 0;
rvargas@google.com8bf26f49a2009-06-12 17:35:5038
rvargas@google.com8bf26f49a2009-06-12 17:35:5039 current_range_start_ = byte_range_.first_byte_position();
40 return true;
41}
42
43void PartialData::RestoreHeaders(std::string* headers) const {
rvargas@google.com67fe45c2009-06-24 17:44:5744 DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange());
45 int64 end = byte_range_.IsSuffixByteRange() ?
46 byte_range_.suffix_length() : byte_range_.last_byte_position();
rvargas@google.com8bf26f49a2009-06-12 17:35:5047
rvargas@google.com67fe45c2009-06-24 17:44:5748 AddRangeHeader(current_range_start_, end, headers);
rvargas@google.com8bf26f49a2009-06-12 17:35:5049}
50
51int PartialData::PrepareCacheValidation(disk_cache::Entry* entry,
52 std::string* headers) {
53 DCHECK(current_range_start_ >= 0);
54
55 // Scan the disk cache for the first cached portion within this range.
56 int64 range_len = byte_range_.HasLastBytePosition() ?
rvargas@google.com44f873a62009-08-12 00:14:4857 byte_range_.last_byte_position() - current_range_start_ + 1 : kint32max;
rvargas@google.com8bf26f49a2009-06-12 17:35:5058 if (range_len > kint32max)
59 range_len = kint32max;
60 int len = static_cast<int32>(range_len);
61 if (!len)
62 return 0;
63 range_present_ = false;
64
rvargas@google.come5dad132009-08-18 00:53:4165 if (sparse_entry_) {
66 cached_min_len_ = entry->GetAvailableRange(current_range_start_, len,
67 &cached_start_);
rvargas@google.com28accfe2009-09-04 23:36:3368 } else if (truncated_) {
69 if (!current_range_start_) {
70 // Update the cached range only the first time.
71 cached_min_len_ = static_cast<int32>(byte_range_.first_byte_position());
72 cached_start_ = 0;
73 }
rvargas@google.come5dad132009-08-18 00:53:4174 } else {
75 cached_min_len_ = len;
76 cached_start_ = current_range_start_;
77 }
78
rvargas@google.com8bf26f49a2009-06-12 17:35:5079 if (cached_min_len_ < 0) {
80 DCHECK(cached_min_len_ != ERR_IO_PENDING);
81 return cached_min_len_;
82 }
83
84 headers->assign(extra_headers_);
85
86 if (!cached_min_len_) {
87 // We don't have anything else stored.
88 final_range_ = true;
rvargas@google.com28accfe2009-09-04 23:36:3389 cached_start_ =
90 byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0;
rvargas@google.com8bf26f49a2009-06-12 17:35:5091 }
92
93 if (current_range_start_ == cached_start_) {
94 // The data lives in the cache.
95 range_present_ = true;
96 if (len == cached_min_len_)
97 final_range_ = true;
98 AddRangeHeader(current_range_start_, cached_start_ + cached_min_len_ - 1,
99 headers);
100 } else {
101 // This range is not in the cache.
102 AddRangeHeader(current_range_start_, cached_start_ - 1, headers);
103 }
104
105 // Return a positive number to indicate success (versus error or finished).
106 return 1;
107}
108
109bool PartialData::IsCurrentRangeCached() const {
110 return range_present_;
111}
112
113bool PartialData::IsLastRange() const {
114 return final_range_;
115}
116
rvargas@google.com44f873a62009-08-12 00:14:48117bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers,
rvargas@google.com28accfe2009-09-04 23:36:33118 disk_cache::Entry* entry,
119 bool truncated) {
rvargas@google.com67fe45c2009-06-24 17:44:57120 resource_size_ = 0;
rvargas@google.com28accfe2009-09-04 23:36:33121 if (truncated) {
122 DCHECK_EQ(headers->response_code(), 200);
123 truncated_ = true;
124 sparse_entry_ = false;
125
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
131 byte_range_.set_first_byte_position(entry->GetDataSize(kDataStream));
132 current_range_start_ = 0;
133 return true;
134 }
135
rvargas@google.come5dad132009-08-18 00:53:41136 if (headers->response_code() == 200) {
137 DCHECK(byte_range_.IsValid());
138 sparse_entry_ = false;
139 resource_size_ = entry->GetDataSize(kDataStream);
140 return true;
141 }
142
rvargas@google.com28accfe2009-09-04 23:36:33143 std::string length_value;
rvargas@google.com67fe45c2009-06-24 17:44:57144 if (!headers->GetNormalizedHeader(kLengthHeader, &length_value))
145 return false; // We must have stored the resource length.
146
rvargas@google.com44f873a62009-08-12 00:14:48147 if (!StringToInt64(length_value, &resource_size_) || !resource_size_)
rvargas@google.com67fe45c2009-06-24 17:44:57148 return false;
149
rvargas@google.come5dad132009-08-18 00:53:41150 // Make sure that this is really a sparse entry.
151 int64 n;
152 if (ERR_CACHE_OPERATION_NOT_SUPPORTED == entry->GetAvailableRange(0, 5, &n))
153 return false;
154
155 return true;
156}
157
158bool PartialData::IsRequestedRangeOK() {
rvargas@google.com44f873a62009-08-12 00:14:48159 if (byte_range_.IsValid()) {
rvargas@google.com28accfe2009-09-04 23:36:33160 if (truncated_)
161 return true;
rvargas@google.com44f873a62009-08-12 00:14:48162 if (!byte_range_.ComputeBounds(resource_size_))
163 return false;
rvargas@google.com67fe45c2009-06-24 17:44:57164
rvargas@google.com44f873a62009-08-12 00:14:48165 if (current_range_start_ < 0)
166 current_range_start_ = byte_range_.first_byte_position();
167 } else {
168 // This is not a range request but we have partial data stored.
169 current_range_start_ = 0;
170 byte_range_.set_last_byte_position(resource_size_ - 1);
171 }
172
rvargas@google.come5dad132009-08-18 00:53:41173 bool rv = current_range_start_ >= 0;
174 if (!rv)
175 current_range_start_ = 0;
rvargas@google.com67fe45c2009-06-24 17:44:57176
rvargas@google.come5dad132009-08-18 00:53:41177 return rv;
rvargas@google.com95792eb12009-06-22 21:30:40178}
179
180bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) {
rvargas@google.come5dad132009-08-18 00:53:41181 if (headers->response_code() == 304) {
rvargas@google.com28accfe2009-09-04 23:36:33182 if (truncated_)
183 return true;
184
rvargas@google.come5dad132009-08-18 00:53:41185 // We must have a complete range here.
186 return byte_range_.HasFirstBytePosition() &&
187 byte_range_.HasLastBytePosition();
188 }
189
rvargas@google.com95792eb12009-06-22 21:30:40190 int64 start, end, total_length;
191 if (!headers->GetContentRange(&start, &end, &total_length))
192 return false;
193 if (total_length <= 0)
194 return false;
195
196 if (!resource_size_) {
197 // First response. Update our values with the ones provided by the server.
198 resource_size_ = total_length;
rvargas@google.com67fe45c2009-06-24 17:44:57199 if (!byte_range_.HasFirstBytePosition()) {
rvargas@google.com95792eb12009-06-22 21:30:40200 byte_range_.set_first_byte_position(start);
rvargas@google.com67fe45c2009-06-24 17:44:57201 current_range_start_ = start;
202 }
rvargas@google.com95792eb12009-06-22 21:30:40203 if (!byte_range_.HasLastBytePosition())
204 byte_range_.set_last_byte_position(end);
205 } else if (resource_size_ != total_length) {
206 return false;
207 }
208
209 if (start != current_range_start_)
210 return false;
211
rvargas@google.com44f873a62009-08-12 00:14:48212 if (byte_range_.IsValid() && end > byte_range_.last_byte_position())
rvargas@google.com95792eb12009-06-22 21:30:40213 return false;
214
215 return true;
216}
217
218// We are making multiple requests to complete the range requested by the user.
219// Just assume that everything is fine and say that we are returning what was
220// requested.
221void PartialData::FixResponseHeaders(HttpResponseHeaders* headers) {
rvargas@google.com28accfe2009-09-04 23:36:33222 if (truncated_)
223 return;
224
rvargas@google.com95792eb12009-06-22 21:30:40225 headers->RemoveHeader(kLengthHeader);
226 headers->RemoveHeader(kRangeHeader);
227
rvargas@google.com44f873a62009-08-12 00:14:48228 int64 range_len;
229 if (byte_range_.IsValid()) {
rvargas@google.come5dad132009-08-18 00:53:41230 if (!sparse_entry_)
231 headers->ReplaceStatusLine("HTTP/1.1 206 Partial Content");
232
rvargas@google.com44f873a62009-08-12 00:14:48233 DCHECK(byte_range_.HasFirstBytePosition());
234 DCHECK(byte_range_.HasLastBytePosition());
235 headers->AddHeader(StringPrintf("%s: bytes %lld-%lld/%lld", kRangeHeader,
236 byte_range_.first_byte_position(),
237 byte_range_.last_byte_position(),
238 resource_size_));
239 range_len = byte_range_.last_byte_position() -
240 byte_range_.first_byte_position() + 1;
241 } else {
242 // TODO(rvargas): Is it safe to change the protocol version?
243 headers->ReplaceStatusLine("HTTP/1.1 200 OK");
244 DCHECK_NE(resource_size_, 0);
245 range_len = resource_size_;
246 }
rvargas@google.com95792eb12009-06-22 21:30:40247
rvargas@google.com95792eb12009-06-22 21:30:40248 headers->AddHeader(StringPrintf("%s: %lld", kLengthHeader, range_len));
249}
250
251void PartialData::FixContentLength(HttpResponseHeaders* headers) {
252 headers->RemoveHeader(kLengthHeader);
253 headers->AddHeader(StringPrintf("%s: %lld", kLengthHeader, resource_size_));
254}
255
rvargas@google.com8bf26f49a2009-06-12 17:35:50256int PartialData::CacheRead(disk_cache::Entry* entry, IOBuffer* data,
257 int data_len, CompletionCallback* callback) {
258 int read_len = std::min(data_len, cached_min_len_);
rvargas@google.come5dad132009-08-18 00:53:41259 int rv = 0;
260 if (sparse_entry_) {
261 rv = entry->ReadSparseData(current_range_start_, data, read_len,
262 callback);
263 } else {
264 if (current_range_start_ > kint32max)
265 return ERR_INVALID_ARGUMENT;
266
267 rv = entry->ReadData(kDataStream, static_cast<int>(current_range_start_),
268 data, read_len, callback);
269 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50270 return rv;
271}
272
273int PartialData::CacheWrite(disk_cache::Entry* entry, IOBuffer* data,
274 int data_len, CompletionCallback* callback) {
rvargas@google.come5dad132009-08-18 00:53:41275 if (sparse_entry_) {
276 return entry->WriteSparseData(current_range_start_, data, data_len,
277 callback);
278 } else {
279 if (current_range_start_ > kint32max)
280 return ERR_INVALID_ARGUMENT;
281
282 return entry->WriteData(kDataStream, static_cast<int>(current_range_start_),
rvargas@google.com28accfe2009-09-04 23:36:33283 data, data_len, callback, true);
rvargas@google.come5dad132009-08-18 00:53:41284 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50285}
286
287void PartialData::OnCacheReadCompleted(int result) {
288 if (result > 0) {
289 current_range_start_ += result;
290 cached_min_len_ -= result;
291 DCHECK(cached_min_len_ >= 0);
rvargas@google.com8bf26f49a2009-06-12 17:35:50292 }
293}
294
295void PartialData::OnNetworkReadCompleted(int result) {
296 if (result > 0)
297 current_range_start_ += result;
298}
299
300// Static.
301void PartialData::AddRangeHeader(int64 start, int64 end, std::string* headers) {
rvargas@google.com67fe45c2009-06-24 17:44:57302 DCHECK(start >= 0 || end >= 0);
303 std::string my_start, my_end;
304 if (start >= 0)
305 my_start = Int64ToString(start);
306 if (end >= 0)
307 my_end = Int64ToString(end);
308
309 headers->append(StringPrintf("Range: bytes=%s-%s\r\n", my_start.c_str(),
310 my_end.c_str()));
rvargas@google.com8bf26f49a2009-06-12 17:35:50311}
rvargas@google.com8bf26f49a2009-06-12 17:35:50312
313} // namespace net