Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_simple_job.h" | 5 #include "net/url_request/url_request_simple_job.h" |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 10 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/http/http_request_headers.h" | |
| 15 #include "net/http/http_util.h" | |
| 12 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
| 13 | 17 |
| 14 namespace net { | 18 namespace net { |
| 15 | 19 |
| 16 URLRequestSimpleJob::URLRequestSimpleJob( | 20 URLRequestSimpleJob::URLRequestSimpleJob( |
| 17 URLRequest* request, NetworkDelegate* network_delegate) | 21 URLRequest* request, NetworkDelegate* network_delegate) |
| 18 : URLRequestJob(request, network_delegate), | 22 : URLRequestJob(request, network_delegate), |
| 19 data_offset_(0), | 23 data_offset_(0), |
| 20 weak_factory_(this) {} | 24 weak_factory_(this) {} |
| 21 | 25 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 35 bool URLRequestSimpleJob::GetCharset(std::string* charset) { | 39 bool URLRequestSimpleJob::GetCharset(std::string* charset) { |
| 36 *charset = charset_; | 40 *charset = charset_; |
| 37 return true; | 41 return true; |
| 38 } | 42 } |
| 39 | 43 |
| 40 URLRequestSimpleJob::~URLRequestSimpleJob() {} | 44 URLRequestSimpleJob::~URLRequestSimpleJob() {} |
| 41 | 45 |
| 42 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, | 46 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, |
| 43 int* bytes_read) { | 47 int* bytes_read) { |
| 44 DCHECK(bytes_read); | 48 DCHECK(bytes_read); |
| 45 int remaining = static_cast<int>(data_.size()) - data_offset_; | 49 int remaining = byte_range_.last_byte_position() - data_offset_ + 1; |
| 46 if (buf_size > remaining) | 50 if (buf_size > remaining) |
| 47 buf_size = remaining; | 51 buf_size = remaining; |
| 48 memcpy(buf->data(), data_.data() + data_offset_, buf_size); | 52 memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
| 49 data_offset_ += buf_size; | 53 data_offset_ += buf_size; |
| 50 *bytes_read = buf_size; | 54 *bytes_read = buf_size; |
| 51 return true; | 55 return true; |
| 52 } | 56 } |
| 53 | 57 |
| 58 void URLRequestSimpleJob::SetExtraRequestHeaders( | |
| 59 const HttpRequestHeaders& headers) { | |
| 60 std::string range_header; | |
| 61 if (headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) { | |
| 62 std::vector<HttpByteRange> ranges; | |
| 63 if (HttpUtil::ParseRangeHeader(range_header, &ranges)) { | |
|
mmenke
2013/12/17 17:10:15
Should we fail if we have a range header we can't
| |
| 64 if (ranges.size() == 1) { | |
| 65 byte_range_ = ranges[0]; | |
| 66 } else { | |
| 67 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | |
| 68 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
|
mmenke
2013/12/17 17:10:15
Calling NotifyDone before Start() seems like a bad
kirr
2013/12/18 08:03:30
It seems you are right.
But all URLRequestJobs wit
mmenke
2013/12/18 15:40:31
Thanks for pointing that out!
I think conceptuall
| |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 54 void URLRequestSimpleJob::StartAsync() { | 74 void URLRequestSimpleJob::StartAsync() { |
| 55 if (!request_) | 75 if (!request_) |
| 56 return; | 76 return; |
| 57 | 77 |
| 58 int result = GetData(&mime_type_, &charset_, &data_, | 78 int result = GetData(&mime_type_, &charset_, &data_, |
| 59 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, | 79 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, |
| 60 weak_factory_.GetWeakPtr())); | 80 weak_factory_.GetWeakPtr())); |
| 61 if (result != ERR_IO_PENDING) | 81 if (result != ERR_IO_PENDING) |
| 62 OnGetDataCompleted(result); | 82 OnGetDataCompleted(result); |
| 63 } | 83 } |
| 64 | 84 |
| 65 void URLRequestSimpleJob::OnGetDataCompleted(int result) { | 85 void URLRequestSimpleJob::OnGetDataCompleted(int result) { |
| 66 if (result == OK) { | 86 if (result == OK) { |
| 67 // Notify that the headers are complete | 87 // Notify that the headers are complete |
| 88 if (!byte_range_.ComputeBounds(data_.size())) { | |
| 89 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | |
| 90 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
| 91 return; | |
| 92 } | |
| 93 | |
| 94 data_offset_ = byte_range_.first_byte_position(); | |
| 95 int remaining_bytes = byte_range_.last_byte_position() - | |
| 96 byte_range_.first_byte_position() + 1; | |
| 97 set_expected_content_size(remaining_bytes); | |
| 68 NotifyHeadersComplete(); | 98 NotifyHeadersComplete(); |
| 69 } else { | 99 } else { |
| 70 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | 100 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 71 } | 101 } |
| 72 } | 102 } |
| 73 | 103 |
| 74 } // namespace net | 104 } // namespace net |
| OLD | NEW |