blob: f266e3b505d05ea37b7e237ce91e8dbc472c0937 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "net/url_request/url_request_simple_job.h"
6
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +00007#include <vector>
8
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009#include "base/bind.h"
10#include "base/compiler_specific.h"
Ben Murdoch9ab55632013-07-18 11:57:30 +010011#include "base/message_loop/message_loop.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#include "net/base/io_buffer.h"
13#include "net/base/net_errors.h"
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000014#include "net/http/http_request_headers.h"
15#include "net/http/http_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "net/url_request/url_request_status.h"
17
18namespace net {
19
20URLRequestSimpleJob::URLRequestSimpleJob(
21 URLRequest* request, NetworkDelegate* network_delegate)
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000022 : URLRangeRequestJob(request, network_delegate),
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023 data_offset_(0),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010024 weak_factory_(this) {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025
26void URLRequestSimpleJob::Start() {
27 // Start reading asynchronously so that all error reporting and data
28 // callbacks happen as they would for network requests.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010029 base::MessageLoop::current()->PostTask(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000030 FROM_HERE,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010031 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr()));
Torne (Richard Coles)58218062012-11-14 11:43:16 +000032}
33
34bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
35 *mime_type = mime_type_;
36 return true;
37}
38
39bool URLRequestSimpleJob::GetCharset(std::string* charset) {
40 *charset = charset_;
41 return true;
42}
43
44URLRequestSimpleJob::~URLRequestSimpleJob() {}
45
46bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
47 int* bytes_read) {
48 DCHECK(bytes_read);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000049 int remaining = byte_range_.last_byte_position() - data_offset_ + 1;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050 if (buf_size > remaining)
51 buf_size = remaining;
52 memcpy(buf->data(), data_.data() + data_offset_, buf_size);
53 data_offset_ += buf_size;
54 *bytes_read = buf_size;
55 return true;
56}
57
58void URLRequestSimpleJob::StartAsync() {
59 if (!request_)
60 return;
61
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000062 if (ranges().size() > 1) {
63 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
64 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
65 return;
66 }
67
68 if (!ranges().empty() && range_parse_result() == OK)
69 byte_range_ = ranges().front();
70
Torne (Richard Coles)58218062012-11-14 11:43:16 +000071 int result = GetData(&mime_type_, &charset_, &data_,
72 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
73 weak_factory_.GetWeakPtr()));
74 if (result != ERR_IO_PENDING)
75 OnGetDataCompleted(result);
76}
77
78void URLRequestSimpleJob::OnGetDataCompleted(int result) {
79 if (result == OK) {
80 // Notify that the headers are complete
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000081 if (!byte_range_.ComputeBounds(data_.size())) {
82 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
83 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
84 return;
85 }
86
87 data_offset_ = byte_range_.first_byte_position();
88 int remaining_bytes = byte_range_.last_byte_position() -
89 byte_range_.first_byte_position() + 1;
90 set_expected_content_size(remaining_bytes);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091 NotifyHeadersComplete();
92 } else {
93 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
94 }
95}
96
97} // namespace net