Index: storage/browser/blob/blob_url_request_job.cc |
diff --git a/storage/browser/blob/blob_url_request_job.cc b/storage/browser/blob/blob_url_request_job.cc |
index 8d7aaea7c2dae0f35deb53893f90d7013bda499b..35cc47456a57c620db60a5e05ee76d390eeb6259 100644 |
--- a/storage/browser/blob/blob_url_request_job.cc |
+++ b/storage/browser/blob/blob_url_request_job.cc |
@@ -143,6 +143,85 @@ void BlobURLRequestJob::SetExtraRequestHeaders( |
} |
} |
+scoped_refptr<net::HttpResponseHeaders> BlobURLRequestJob::GenerateHeaders( |
+ net::HttpStatusCode status_code, |
+ BlobDataHandle* blob_handle, |
+ BlobReader* blob_reader, |
+ net::HttpByteRange* byte_range, |
+ int64_t* content_size) { |
+ std::string status("HTTP/1.1 "); |
+ status.append(base::IntToString(status_code)); |
+ status.append(" "); |
+ status.append(net::GetHttpReasonPhrase(status_code)); |
+ status.append("\0\0", 2); |
+ scoped_refptr<net::HttpResponseHeaders> headers = |
+ new net::HttpResponseHeaders(status); |
+ |
+ if (status_code == net::HTTP_OK || status_code == net::HTTP_PARTIAL_CONTENT) { |
+ *content_size = blob_reader->remaining_bytes(); |
+ std::string content_length_header(net::HttpRequestHeaders::kContentLength); |
+ content_length_header.append(": "); |
+ content_length_header.append(base::Int64ToString(*content_size)); |
+ headers->AddHeader(content_length_header); |
+ if (status_code == net::HTTP_PARTIAL_CONTENT) { |
+ DCHECK(byte_range->IsValid()); |
+ std::string content_range_header(net::HttpResponseHeaders::kContentRange); |
+ content_range_header.append(": bytes "); |
+ content_range_header.append(base::StringPrintf( |
+ "%" PRId64 "-%" PRId64, byte_range->first_byte_position(), |
+ byte_range->last_byte_position())); |
+ content_range_header.append("/"); |
+ content_range_header.append( |
+ base::StringPrintf("%" PRId64, blob_reader->total_size())); |
+ headers->AddHeader(content_range_header); |
+ } |
+ if (!blob_handle->content_type().empty()) { |
+ std::string content_type_header(net::HttpRequestHeaders::kContentType); |
+ content_type_header.append(": "); |
+ content_type_header.append(blob_handle->content_type()); |
+ headers->AddHeader(content_type_header); |
+ } |
+ if (!blob_handle->content_disposition().empty()) { |
+ std::string content_disposition_header("Content-Disposition: "); |
+ content_disposition_header.append(blob_handle->content_disposition()); |
+ headers->AddHeader(content_disposition_header); |
+ } |
+ } |
+ |
+ return headers; |
+} |
+ |
+net::HttpStatusCode BlobURLRequestJob::NetErrorToHttpStatusCode( |
+ int error_code) { |
+ net::HttpStatusCode status_code = net::HTTP_INTERNAL_SERVER_ERROR; |
+ switch (error_code) { |
+ case net::ERR_ACCESS_DENIED: |
+ status_code = net::HTTP_FORBIDDEN; |
+ break; |
+ case net::ERR_FILE_NOT_FOUND: |
+ status_code = net::HTTP_NOT_FOUND; |
+ break; |
+ case net::ERR_METHOD_NOT_SUPPORTED: |
+ status_code = net::HTTP_METHOD_NOT_ALLOWED; |
+ break; |
+ case net::ERR_REQUEST_RANGE_NOT_SATISFIABLE: |
+ status_code = net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE; |
+ break; |
+ case net::ERR_INVALID_ARGUMENT: |
+ status_code = net::HTTP_BAD_REQUEST; |
+ break; |
+ case net::ERR_CACHE_READ_FAILURE: |
+ case net::ERR_CACHE_CHECKSUM_READ_FAILURE: |
+ case net::ERR_UNEXPECTED: |
+ case net::ERR_FAILED: |
+ break; |
+ default: |
+ DCHECK(false) << "Error code not supported: " << error_code; |
+ break; |
+ } |
+ return status_code; |
+} |
+ |
BlobURLRequestJob::~BlobURLRequestJob() { |
TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest", this, "uuid", |
blob_handle_ ? blob_handle_->uuid() : "NotFound"); |
@@ -244,80 +323,16 @@ void BlobURLRequestJob::NotifyFailure(int error_code) { |
// now. Instead, we just error out. |
DCHECK(!response_info_) << "Cannot NotifyFailure after headers."; |
- net::HttpStatusCode status_code = net::HTTP_INTERNAL_SERVER_ERROR; |
- switch (error_code) { |
- case net::ERR_ACCESS_DENIED: |
- status_code = net::HTTP_FORBIDDEN; |
- break; |
- case net::ERR_FILE_NOT_FOUND: |
- status_code = net::HTTP_NOT_FOUND; |
- break; |
- case net::ERR_METHOD_NOT_SUPPORTED: |
- status_code = net::HTTP_METHOD_NOT_ALLOWED; |
- break; |
- case net::ERR_REQUEST_RANGE_NOT_SATISFIABLE: |
- status_code = net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE; |
- break; |
- case net::ERR_INVALID_ARGUMENT: |
- status_code = net::HTTP_BAD_REQUEST; |
- break; |
- case net::ERR_CACHE_READ_FAILURE: |
- case net::ERR_CACHE_CHECKSUM_READ_FAILURE: |
- case net::ERR_UNEXPECTED: |
- case net::ERR_FAILED: |
- break; |
- default: |
- DCHECK(false) << "Error code not supported: " << error_code; |
- break; |
- } |
- HeadersCompleted(status_code); |
+ HeadersCompleted(NetErrorToHttpStatusCode(error_code)); |
} |
void BlobURLRequestJob::HeadersCompleted(net::HttpStatusCode status_code) { |
- std::string status("HTTP/1.1 "); |
- status.append(base::IntToString(status_code)); |
- status.append(" "); |
- status.append(net::GetHttpReasonPhrase(status_code)); |
- status.append("\0\0", 2); |
- net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status); |
- |
- set_expected_content_size(0); |
- |
- if (status_code == net::HTTP_OK || status_code == net::HTTP_PARTIAL_CONTENT) { |
- set_expected_content_size(blob_reader_->remaining_bytes()); |
- std::string content_length_header(net::HttpRequestHeaders::kContentLength); |
- content_length_header.append(": "); |
- content_length_header.append( |
- base::Int64ToString(blob_reader_->remaining_bytes())); |
- headers->AddHeader(content_length_header); |
- if (status_code == net::HTTP_PARTIAL_CONTENT) { |
- DCHECK(byte_range_set_); |
- DCHECK(byte_range_.IsValid()); |
- std::string content_range_header(net::HttpResponseHeaders::kContentRange); |
- content_range_header.append(": bytes "); |
- content_range_header.append(base::StringPrintf( |
- "%" PRId64 "-%" PRId64, byte_range_.first_byte_position(), |
- byte_range_.last_byte_position())); |
- content_range_header.append("/"); |
- content_range_header.append( |
- base::StringPrintf("%" PRId64, blob_reader_->total_size())); |
- headers->AddHeader(content_range_header); |
- } |
- if (!blob_handle_->content_type().empty()) { |
- std::string content_type_header(net::HttpRequestHeaders::kContentType); |
- content_type_header.append(": "); |
- content_type_header.append(blob_handle_->content_type()); |
- headers->AddHeader(content_type_header); |
- } |
- if (!blob_handle_->content_disposition().empty()) { |
- std::string content_disposition_header("Content-Disposition: "); |
- content_disposition_header.append(blob_handle_->content_disposition()); |
- headers->AddHeader(content_disposition_header); |
- } |
- } |
- |
+ int64_t content_size = 0; |
response_info_.reset(new net::HttpResponseInfo()); |
- response_info_->headers = headers; |
+ response_info_->headers = |
+ GenerateHeaders(status_code, blob_handle_.get(), blob_reader_.get(), |
+ &byte_range_, &content_size); |
+ set_expected_content_size(content_size); |
if (blob_reader_) |
response_info_->metadata = blob_reader_->side_data(); |