1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "storage/browser/blob/blob_url_request_job.h"
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/compiler_specific.h"
15 #include "base/files/file_util_proxy.h"
16 #include "base/format_macros.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/message_loop/message_loop_proxy.h"
19 #include "base/numerics/safe_conversions.h"
20 #include "base/profiler/scoped_tracker.h"
21 #include "base/stl_util.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/stringprintf.h"
24 #include "base/trace_event/trace_event.h"
25 #include "net/base/io_buffer.h"
26 #include "net/base/net_errors.h"
27 #include "net/http/http_request_headers.h"
28 #include "net/http/http_response_headers.h"
29 #include "net/http/http_response_info.h"
30 #include "net/http/http_util.h"
31 #include "net/url_request/url_request.h"
32 #include "net/url_request/url_request_context.h"
33 #include "net/url_request/url_request_error_job.h"
34 #include "net/url_request/url_request_status.h"
35 #include "storage/browser/fileapi/file_stream_reader.h"
36 #include "storage/browser/fileapi/file_system_context.h"
37 #include "storage/browser/fileapi/file_system_url.h"
38 #include "storage/common/data_element.h"
44 bool IsFileType(DataElement::Type type
) {
46 case DataElement::TYPE_FILE
:
47 case DataElement::TYPE_FILE_FILESYSTEM
:
56 BlobURLRequestJob::BlobURLRequestJob(
57 net::URLRequest
* request
,
58 net::NetworkDelegate
* network_delegate
,
59 scoped_ptr
<BlobDataSnapshot
> blob_data
,
60 storage::FileSystemContext
* file_system_context
,
61 base::MessageLoopProxy
* file_thread_proxy
)
62 : net::URLRequestJob(request
, network_delegate
),
63 blob_data_(blob_data
.Pass()),
64 file_system_context_(file_system_context
),
65 file_thread_proxy_(file_thread_proxy
),
68 pending_get_file_info_count_(0),
69 current_item_index_(0),
70 current_item_offset_(0),
72 byte_range_set_(false),
74 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest", this, "uuid",
76 DCHECK(file_thread_proxy_
.get());
79 void BlobURLRequestJob::Start() {
80 // Continue asynchronously.
81 base::MessageLoop::current()->PostTask(
83 base::Bind(&BlobURLRequestJob::DidStart
, weak_factory_
.GetWeakPtr()));
86 void BlobURLRequestJob::Kill() {
87 DeleteCurrentFileReader();
89 net::URLRequestJob::Kill();
90 weak_factory_
.InvalidateWeakPtrs();
93 bool BlobURLRequestJob::ReadRawData(net::IOBuffer
* dest
,
96 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
97 tracked_objects::ScopedTracker
tracking_profile(
98 FROM_HERE_WITH_EXPLICIT_FUNCTION(
99 "423948 BlobURLRequestJob::ReadRawData"));
101 DCHECK_NE(dest_size
, 0);
103 DCHECK_GE(remaining_bytes_
, 0);
105 // Bail out immediately if we encounter an error.
111 if (remaining_bytes_
< dest_size
)
112 dest_size
= static_cast<int>(remaining_bytes_
);
114 // If we should copy zero bytes because |remaining_bytes_| is zero, short
121 // Keep track of the buffer.
122 DCHECK(!read_buf_
.get());
123 read_buf_
= new net::DrainableIOBuffer(dest
, dest_size
);
125 return ReadLoop(bytes_read
);
128 bool BlobURLRequestJob::GetMimeType(std::string
* mime_type
) const {
132 return response_info_
->headers
->GetMimeType(mime_type
);
135 void BlobURLRequestJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
137 *info
= *response_info_
;
140 int BlobURLRequestJob::GetResponseCode() const {
144 return response_info_
->headers
->response_code();
147 void BlobURLRequestJob::SetExtraRequestHeaders(
148 const net::HttpRequestHeaders
& headers
) {
149 std::string range_header
;
150 if (headers
.GetHeader(net::HttpRequestHeaders::kRange
, &range_header
)) {
151 // We only care about "Range" header here.
152 std::vector
<net::HttpByteRange
> ranges
;
153 if (net::HttpUtil::ParseRangeHeader(range_header
, &ranges
)) {
154 if (ranges
.size() == 1) {
155 byte_range_set_
= true;
156 byte_range_
= ranges
[0];
158 // We don't support multiple range requests in one single URL request,
159 // because we need to do multipart encoding here.
160 // TODO(jianli): Support multipart byte range requests.
161 NotifyFailure(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
167 BlobURLRequestJob::~BlobURLRequestJob() {
168 STLDeleteValues(&index_to_reader_
);
169 TRACE_EVENT_ASYNC_END1("Blob", "Request", this, "uuid", blob_data_
->uuid());
172 void BlobURLRequestJob::DidStart() {
173 current_file_chunk_number_
= 0;
176 // We only support GET request per the spec.
177 if (request()->method() != "GET") {
178 NotifyFailure(net::ERR_METHOD_NOT_SUPPORTED
);
182 // If the blob data is not present, bail out.
184 NotifyFailure(net::ERR_FILE_NOT_FOUND
);
191 bool BlobURLRequestJob::AddItemLength(size_t index
, int64 item_length
) {
192 if (item_length
> kint64max
- total_size_
) {
193 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::CountSize", this, "uuid",
195 NotifyFailure(net::ERR_FAILED
);
199 // Cache the size and add it to the total size.
200 DCHECK_LT(index
, item_length_list_
.size());
201 item_length_list_
[index
] = item_length
;
202 total_size_
+= item_length
;
206 void BlobURLRequestJob::CountSize() {
207 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::CountSize", this, "uuid",
209 pending_get_file_info_count_
= 0;
211 const auto& items
= blob_data_
->items();
212 item_length_list_
.resize(items
.size());
214 for (size_t i
= 0; i
< items
.size(); ++i
) {
215 const BlobDataItem
& item
= *items
.at(i
);
216 if (IsFileType(item
.type())) {
217 ++pending_get_file_info_count_
;
218 GetFileStreamReader(i
)->GetLength(
219 base::Bind(&BlobURLRequestJob::DidGetFileItemLength
,
220 weak_factory_
.GetWeakPtr(), i
));
224 if (!AddItemLength(i
, item
.length()))
228 if (pending_get_file_info_count_
== 0)
229 DidCountSize(net::OK
);
232 void BlobURLRequestJob::DidCountSize(int error
) {
234 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::CountSize", this, "uuid",
237 // If an error occured, bail out.
238 if (error
!= net::OK
) {
239 NotifyFailure(error
);
243 // Apply the range requirement.
244 if (!byte_range_
.ComputeBounds(total_size_
)) {
245 NotifyFailure(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
);
249 remaining_bytes_
= base::checked_cast
<int64
>(
250 byte_range_
.last_byte_position() - byte_range_
.first_byte_position() + 1);
251 DCHECK_GE(remaining_bytes_
, 0);
253 // Do the seek at the beginning of the request.
254 if (byte_range_
.first_byte_position())
255 Seek(byte_range_
.first_byte_position());
260 void BlobURLRequestJob::DidGetFileItemLength(size_t index
, int64 result
) {
261 // Do nothing if we have encountered an error.
265 if (result
== net::ERR_UPLOAD_FILE_CHANGED
) {
266 NotifyFailure(net::ERR_FILE_NOT_FOUND
);
268 } else if (result
< 0) {
269 NotifyFailure(result
);
273 const auto& items
= blob_data_
->items();
274 DCHECK_LT(index
, items
.size());
275 const BlobDataItem
& item
= *items
.at(index
);
276 DCHECK(IsFileType(item
.type()));
278 uint64 file_length
= result
;
279 uint64 item_offset
= item
.offset();
280 uint64 item_length
= item
.length();
282 if (item_offset
> file_length
) {
283 NotifyFailure(net::ERR_FILE_NOT_FOUND
);
287 uint64 max_length
= file_length
- item_offset
;
289 // If item length is undefined, then we need to use the file size being
290 // resolved in the real time.
291 if (item_length
== std::numeric_limits
<uint64
>::max()) {
292 item_length
= max_length
;
293 } else if (item_length
> max_length
) {
294 NotifyFailure(net::ERR_FILE_NOT_FOUND
);
298 if (!AddItemLength(index
, item_length
))
301 if (--pending_get_file_info_count_
== 0)
302 DidCountSize(net::OK
);
305 void BlobURLRequestJob::Seek(int64 offset
) {
306 // Skip the initial items that are not in the range.
307 const auto& items
= blob_data_
->items();
308 for (current_item_index_
= 0;
309 current_item_index_
< items
.size() &&
310 offset
>= item_length_list_
[current_item_index_
];
311 ++current_item_index_
) {
312 offset
-= item_length_list_
[current_item_index_
];
315 // Set the offset that need to jump to for the first item in the range.
316 current_item_offset_
= offset
;
321 // Adjust the offset of the first stream if it is of file type.
322 const BlobDataItem
& item
= *items
.at(current_item_index_
);
323 if (IsFileType(item
.type())) {
324 DeleteCurrentFileReader();
325 CreateFileStreamReader(current_item_index_
, offset
);
329 bool BlobURLRequestJob::ReadItem() {
330 // Are we done with reading all the blob data?
331 if (remaining_bytes_
== 0)
334 const auto& items
= blob_data_
->items();
335 // If we get to the last item but still expect something to read, bail out
336 // since something is wrong.
337 if (current_item_index_
>= items
.size()) {
338 NotifyFailure(net::ERR_FAILED
);
342 // Compute the bytes to read for current item.
343 int bytes_to_read
= ComputeBytesToRead();
345 // If nothing to read for current item, advance to next item.
346 if (bytes_to_read
== 0) {
352 const BlobDataItem
& item
= *items
.at(current_item_index_
);
353 if (item
.type() == DataElement::TYPE_BYTES
)
354 return ReadBytesItem(item
, bytes_to_read
);
355 if (IsFileType(item
.type())) {
356 return ReadFileItem(GetFileStreamReader(current_item_index_
),
363 void BlobURLRequestJob::AdvanceItem() {
364 // Close the file if the current item is a file.
365 DeleteCurrentFileReader();
367 // Advance to the next item.
368 current_item_index_
++;
369 current_item_offset_
= 0;
372 void BlobURLRequestJob::AdvanceBytesRead(int result
) {
373 DCHECK_GT(result
, 0);
375 // Do we finish reading the current item?
376 current_item_offset_
+= result
;
377 if (current_item_offset_
== item_length_list_
[current_item_index_
])
380 // Subtract the remaining bytes.
381 remaining_bytes_
-= result
;
382 DCHECK_GE(remaining_bytes_
, 0);
384 // Adjust the read buffer.
385 read_buf_
->DidConsume(result
);
386 DCHECK_GE(read_buf_
->BytesRemaining(), 0);
389 bool BlobURLRequestJob::ReadBytesItem(const BlobDataItem
& item
,
391 TRACE_EVENT1("Blob", "BlobRequest::ReadBytesItem", "uuid",
393 DCHECK_GE(read_buf_
->BytesRemaining(), bytes_to_read
);
395 memcpy(read_buf_
->data(),
396 item
.bytes() + item
.offset() + current_item_offset_
,
399 AdvanceBytesRead(bytes_to_read
);
403 bool BlobURLRequestJob::ReadFileItem(FileStreamReader
* reader
,
405 DCHECK_GE(read_buf_
->BytesRemaining(), bytes_to_read
);
407 int chunk_number
= current_file_chunk_number_
++;
408 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::ReadFileItem", this, "uuid",
411 reader
->Read(read_buf_
.get(), bytes_to_read
,
412 base::Bind(&BlobURLRequestJob::DidReadFile
,
413 base::Unretained(this), chunk_number
));
415 // Data is immediately available.
416 if (GetStatus().is_io_pending())
417 DidReadFile(chunk_number
, result
);
419 AdvanceBytesRead(result
);
422 if (result
== net::ERR_IO_PENDING
)
423 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING
, 0));
425 NotifyFailure(result
);
429 void BlobURLRequestJob::DidReadFile(int chunk_number
, int result
) {
430 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::ReadFileItem", this, "uuid",
433 NotifyFailure(net::ERR_FAILED
);
436 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status
438 AdvanceBytesRead(result
);
440 // If the read buffer is completely filled, we're done.
441 if (!read_buf_
->BytesRemaining()) {
442 int bytes_read
= BytesReadCompleted();
443 NotifyReadComplete(bytes_read
);
447 // Otherwise, continue the reading.
449 if (ReadLoop(&bytes_read
))
450 NotifyReadComplete(bytes_read
);
453 void BlobURLRequestJob::DeleteCurrentFileReader() {
454 IndexToReaderMap::iterator found
= index_to_reader_
.find(current_item_index_
);
455 if (found
!= index_to_reader_
.end() && found
->second
) {
456 delete found
->second
;
457 index_to_reader_
.erase(found
);
461 int BlobURLRequestJob::BytesReadCompleted() {
462 int bytes_read
= read_buf_
->BytesConsumed();
467 int BlobURLRequestJob::ComputeBytesToRead() const {
468 int64 current_item_length
= item_length_list_
[current_item_index_
];
470 int64 item_remaining
= current_item_length
- current_item_offset_
;
471 int64 buf_remaining
= read_buf_
->BytesRemaining();
472 int64 max_remaining
= std::numeric_limits
<int>::max();
474 int64 min
= std::min(std::min(std::min(item_remaining
,
479 return static_cast<int>(min
);
482 bool BlobURLRequestJob::ReadLoop(int* bytes_read
) {
483 // Read until we encounter an error or could not get the data immediately.
484 while (remaining_bytes_
> 0 && read_buf_
->BytesRemaining() > 0) {
489 *bytes_read
= BytesReadCompleted();
493 void BlobURLRequestJob::NotifySuccess() {
494 net::HttpStatusCode status_code
= net::HTTP_OK
;
495 if (byte_range_set_
&& byte_range_
.IsValid())
496 status_code
= net::HTTP_PARTIAL_CONTENT
;
497 HeadersCompleted(status_code
);
500 void BlobURLRequestJob::NotifyFailure(int error_code
) {
503 // If we already return the headers on success, we can't change the headers
504 // now. Instead, we just error out.
505 if (response_info_
) {
506 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED
,
511 net::HttpStatusCode status_code
= net::HTTP_INTERNAL_SERVER_ERROR
;
512 switch (error_code
) {
513 case net::ERR_ACCESS_DENIED
:
514 status_code
= net::HTTP_FORBIDDEN
;
516 case net::ERR_FILE_NOT_FOUND
:
517 status_code
= net::HTTP_NOT_FOUND
;
519 case net::ERR_METHOD_NOT_SUPPORTED
:
520 status_code
= net::HTTP_METHOD_NOT_ALLOWED
;
522 case net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
:
523 status_code
= net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
;
525 case net::ERR_FAILED
:
531 HeadersCompleted(status_code
);
534 void BlobURLRequestJob::HeadersCompleted(net::HttpStatusCode status_code
) {
535 std::string
status("HTTP/1.1 ");
536 status
.append(base::IntToString(status_code
));
538 status
.append(net::GetHttpReasonPhrase(status_code
));
539 status
.append("\0\0", 2);
540 net::HttpResponseHeaders
* headers
= new net::HttpResponseHeaders(status
);
542 if (status_code
== net::HTTP_OK
|| status_code
== net::HTTP_PARTIAL_CONTENT
) {
543 std::string
content_length_header(net::HttpRequestHeaders::kContentLength
);
544 content_length_header
.append(": ");
545 content_length_header
.append(base::Int64ToString(remaining_bytes_
));
546 headers
->AddHeader(content_length_header
);
547 if (status_code
== net::HTTP_PARTIAL_CONTENT
) {
548 DCHECK(byte_range_set_
);
549 DCHECK(byte_range_
.IsValid());
550 std::string
content_range_header(net::HttpResponseHeaders::kContentRange
);
551 content_range_header
.append(": bytes ");
552 content_range_header
.append(base::StringPrintf(
553 "%" PRId64
"-%" PRId64
,
554 byte_range_
.first_byte_position(), byte_range_
.last_byte_position()));
555 content_range_header
.append("/");
556 content_range_header
.append(base::StringPrintf("%" PRId64
, total_size_
));
557 headers
->AddHeader(content_range_header
);
559 if (!blob_data_
->content_type().empty()) {
560 std::string
content_type_header(net::HttpRequestHeaders::kContentType
);
561 content_type_header
.append(": ");
562 content_type_header
.append(blob_data_
->content_type());
563 headers
->AddHeader(content_type_header
);
565 if (!blob_data_
->content_disposition().empty()) {
566 std::string
content_disposition_header("Content-Disposition: ");
567 content_disposition_header
.append(blob_data_
->content_disposition());
568 headers
->AddHeader(content_disposition_header
);
572 response_info_
.reset(new net::HttpResponseInfo());
573 response_info_
->headers
= headers
;
575 set_expected_content_size(remaining_bytes_
);
577 NotifyHeadersComplete();
580 FileStreamReader
* BlobURLRequestJob::GetFileStreamReader(size_t index
) {
581 const auto& items
= blob_data_
->items();
582 DCHECK_LT(index
, items
.size());
583 const BlobDataItem
& item
= *items
.at(index
);
584 if (!IsFileType(item
.type()))
586 if (index_to_reader_
.find(index
) == index_to_reader_
.end())
587 CreateFileStreamReader(index
, 0);
588 DCHECK(index_to_reader_
[index
]);
589 return index_to_reader_
[index
];
592 void BlobURLRequestJob::CreateFileStreamReader(size_t index
,
593 int64 additional_offset
) {
594 const auto& items
= blob_data_
->items();
595 DCHECK_LT(index
, items
.size());
596 const BlobDataItem
& item
= *items
.at(index
);
597 DCHECK(IsFileType(item
.type()));
598 DCHECK_EQ(0U, index_to_reader_
.count(index
));
600 FileStreamReader
* reader
= NULL
;
601 switch (item
.type()) {
602 case DataElement::TYPE_FILE
:
603 reader
= FileStreamReader::CreateForLocalFile(
604 file_thread_proxy_
.get(),
606 item
.offset() + additional_offset
,
607 item
.expected_modification_time());
609 case DataElement::TYPE_FILE_FILESYSTEM
:
610 reader
= file_system_context_
611 ->CreateFileStreamReader(
612 storage::FileSystemURL(file_system_context_
->CrackURL(
613 item
.filesystem_url())),
614 item
.offset() + additional_offset
,
615 item
.length() == std::numeric_limits
<uint64
>::max()
616 ? storage::kMaximumLength
617 : item
.length() - additional_offset
,
618 item
.expected_modification_time())
625 index_to_reader_
[index
] = reader
;
628 } // namespace storage