1 // Copyright 2014 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 "url_request_peer.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "net/base/load_flags.h"
9 #include "net/http/http_status_code.h"
11 static const size_t kBufferSizeIncrement = 8192;
13 // Fragment automatically inserted in the User-Agent header to indicate
14 // that the request is coming from this network stack.
15 static const char kUserAgentFragment[] = "; ChromiumJNI/";
17 URLRequestPeer::URLRequestPeer(URLRequestContextPeer* context,
18 URLRequestPeerDelegate* delegate,
20 net::RequestPriority priority)
23 read_buffer_(new net::GrowableIOBuffer()),
30 streaming_upload_(false) {
37 URLRequestPeer::~URLRequestPeer() { CHECK(url_request_ == NULL); }
39 void URLRequestPeer::SetMethod(const std::string& method) { method_ = method; }
41 void URLRequestPeer::AddHeader(const std::string& name,
42 const std::string& value) {
43 headers_.SetHeader(name, value);
46 void URLRequestPeer::SetPostContent(const char* bytes, int bytes_len) {
47 if (!upload_data_stream_) {
48 upload_data_stream_.reset(
49 new net::UploadDataStream(net::UploadDataStream::CHUNKED, 0));
51 upload_data_stream_->AppendChunk(bytes, bytes_len, true /* is_last_chunk */);
54 void URLRequestPeer::EnableStreamingUpload() { streaming_upload_ = true; }
56 void URLRequestPeer::AppendChunk(const char* bytes,
59 VLOG(context_->logging_level()) << "AppendChunk, len: " << bytes_len
60 << ", last: " << is_last_chunk;
62 context_->GetNetworkTaskRunner()->PostTask(
64 base::Bind(&URLRequestPeer::OnAppendChunkWrapper,
71 void URLRequestPeer::Start() {
72 context_->GetNetworkTaskRunner()->PostTask(
74 base::Bind(&URLRequestPeer::OnInitiateConnectionWrapper, this));
78 void URLRequestPeer::OnAppendChunkWrapper(URLRequestPeer* self,
82 self->OnAppendChunk(bytes, bytes_len, is_last_chunk);
85 void URLRequestPeer::OnAppendChunk(const char* bytes,
88 if (url_request_ != NULL) {
89 url_request_->AppendChunkToUpload(bytes, bytes_len, is_last_chunk);
90 delegate_->OnAppendChunkCompleted(this);
95 void URLRequestPeer::OnInitiateConnectionWrapper(URLRequestPeer* self) {
96 self->OnInitiateConnection();
99 void URLRequestPeer::OnInitiateConnection() {
104 VLOG(context_->logging_level())
105 << "Starting chromium request: " << url_.possibly_invalid_spec().c_str()
106 << " priority: " << RequestPriorityToString(priority_);
107 url_request_ = new net::URLRequest(
108 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext());
109 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE |
110 net::LOAD_DO_NOT_SAVE_COOKIES |
111 net::LOAD_DO_NOT_SEND_COOKIES);
112 url_request_->set_method(method_);
113 url_request_->SetExtraRequestHeaders(headers_);
114 std::string user_agent;
115 if (headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) {
116 headers_.GetHeader(net::HttpRequestHeaders::kUserAgent, &user_agent);
118 user_agent = context_->GetUserAgent(url_);
120 size_t pos = user_agent.find(')');
121 if (pos != std::string::npos) {
122 user_agent.insert(pos, context_->version());
123 user_agent.insert(pos, kUserAgentFragment);
125 url_request_->SetExtraRequestHeaderByName(
126 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */);
128 VLOG(context_->logging_level()) << "User agent: " << user_agent;
130 if (upload_data_stream_) {
131 url_request_->set_upload(make_scoped_ptr(upload_data_stream_.release()));
132 } else if (streaming_upload_) {
133 url_request_->EnableChunkedUpload();
136 url_request_->SetPriority(priority_);
138 url_request_->Start();
141 void URLRequestPeer::Cancel() {
148 context_->GetNetworkTaskRunner()->PostTask(
149 FROM_HERE, base::Bind(&URLRequestPeer::OnCancelRequestWrapper, this));
153 void URLRequestPeer::OnCancelRequestWrapper(URLRequestPeer* self) {
154 self->OnCancelRequest();
157 void URLRequestPeer::OnCancelRequest() {
158 VLOG(context_->logging_level())
159 << "Canceling chromium request: " << url_.possibly_invalid_spec();
161 if (url_request_ != NULL) {
162 url_request_->Cancel();
168 void URLRequestPeer::Destroy() {
169 context_->GetNetworkTaskRunner()->PostTask(
170 FROM_HERE, base::Bind(&URLRequestPeer::OnDestroyRequest, this));
174 void URLRequestPeer::OnDestroyRequest(URLRequestPeer* self) {
175 VLOG(self->context_->logging_level())
176 << "Destroying chromium request: " << self->url_.possibly_invalid_spec();
180 void URLRequestPeer::OnResponseStarted(net::URLRequest* request) {
181 if (request->status().status() != net::URLRequestStatus::SUCCESS) {
186 http_status_code_ = request->GetResponseCode();
187 VLOG(context_->logging_level())
188 << "Response started with status: " << http_status_code_;
190 request->GetResponseHeaderByName("Content-Type", &content_type_);
191 expected_size_ = request->GetExpectedContentSize();
192 delegate_->OnResponseStarted(this);
197 // Reads all available data or starts an asynchronous read.
198 void URLRequestPeer::Read() {
200 if (read_buffer_->RemainingCapacity() == 0) {
201 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement;
202 read_buffer_->SetCapacity(new_capacity);
206 if (url_request_->Read(
207 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) {
208 if (bytes_read == 0) {
209 OnRequestSucceeded();
213 VLOG(context_->logging_level()) << "Synchronously read: " << bytes_read
215 OnBytesRead(bytes_read);
216 } else if (url_request_->status().status() ==
217 net::URLRequestStatus::IO_PENDING) {
218 if (bytes_read_ != 0) {
219 VLOG(context_->logging_level()) << "Flushing buffer: " << bytes_read_
222 delegate_->OnBytesRead(this);
223 read_buffer_->set_offset(0);
226 VLOG(context_->logging_level()) << "Started async read";
235 void URLRequestPeer::OnReadCompleted(net::URLRequest* request, int bytes_read) {
236 VLOG(context_->logging_level()) << "Asynchronously read: " << bytes_read
238 if (bytes_read < 0) {
241 } else if (bytes_read == 0) {
242 OnRequestSucceeded();
246 OnBytesRead(bytes_read);
250 void URLRequestPeer::OnBytesRead(int bytes_read) {
251 read_buffer_->set_offset(read_buffer_->offset() + bytes_read);
252 bytes_read_ += bytes_read;
253 total_bytes_read_ += bytes_read;
256 void URLRequestPeer::OnRequestSucceeded() {
261 VLOG(context_->logging_level())
262 << "Request completed with HTTP status: " << http_status_code_
263 << ". Total bytes read: " << total_bytes_read_;
265 OnRequestCompleted();
268 void URLRequestPeer::OnRequestFailed() {
273 error_code_ = url_request_->status().error();
274 VLOG(context_->logging_level())
275 << "Request failed with status: " << url_request_->status().status()
276 << " and error: " << net::ErrorToString(error_code_);
277 OnRequestCompleted();
280 void URLRequestPeer::OnRequestCanceled() { OnRequestCompleted(); }
282 void URLRequestPeer::OnRequestCompleted() {
283 VLOG(context_->logging_level())
284 << "Completed: " << url_.possibly_invalid_spec();
285 if (url_request_ != NULL) {
290 delegate_->OnBytesRead(this);
291 delegate_->OnRequestFinished(this);
294 unsigned char* URLRequestPeer::Data() const {
295 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer());