2016-10-25 10:41:01 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
2016-09-19 09:21:09 +00:00
|
|
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
#include "atom/browser/net/atom_url_request.h"
|
2016-09-29 09:31:08 +00:00
|
|
|
#include <string>
|
2016-09-19 09:21:09 +00:00
|
|
|
#include "atom/browser/api/atom_api_url_request.h"
|
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2018-02-01 11:26:05 +00:00
|
|
|
#include "atom/browser/net/atom_url_request_job_factory.h"
|
2016-09-19 09:21:09 +00:00
|
|
|
#include "base/callback.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2016-09-26 12:03:49 +00:00
|
|
|
#include "net/base/elements_upload_data_stream.h"
|
2016-09-29 09:31:08 +00:00
|
|
|
#include "net/base/io_buffer.h"
|
2016-12-21 06:24:48 +00:00
|
|
|
#include "net/base/load_flags.h"
|
2016-09-26 12:03:49 +00:00
|
|
|
#include "net/base/upload_bytes_element_reader.h"
|
2017-03-23 19:37:54 +00:00
|
|
|
#include "net/url_request/redirect_info.h"
|
2016-09-21 07:23:00 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const int kBufferSize = 4096;
|
2016-09-29 09:31:08 +00:00
|
|
|
} // namespace
|
2016-09-19 09:21:09 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class UploadOwnedIOBufferElementReader : public net::UploadBytesElementReader {
|
2016-09-29 09:31:08 +00:00
|
|
|
public:
|
2016-09-26 12:03:49 +00:00
|
|
|
explicit UploadOwnedIOBufferElementReader(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer)
|
|
|
|
: net::UploadBytesElementReader(buffer->data(), buffer->size()),
|
2016-10-14 09:50:47 +00:00
|
|
|
buffer_(buffer) {}
|
2016-09-26 12:59:53 +00:00
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
~UploadOwnedIOBufferElementReader() override {}
|
2016-09-26 12:03:49 +00:00
|
|
|
|
|
|
|
static UploadOwnedIOBufferElementReader* CreateWithBuffer(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer) {
|
2016-09-26 12:03:49 +00:00
|
|
|
return new UploadOwnedIOBufferElementReader(std::move(buffer));
|
|
|
|
}
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
private:
|
2016-09-26 12:03:49 +00:00
|
|
|
scoped_refptr<const net::IOBuffer> buffer_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(UploadOwnedIOBufferElementReader);
|
|
|
|
};
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
} // namespace internal
|
2016-09-26 12:03:49 +00:00
|
|
|
|
2016-10-25 13:02:35 +00:00
|
|
|
AtomURLRequest::AtomURLRequest(api::URLRequest* delegate)
|
2016-09-30 12:17:29 +00:00
|
|
|
: delegate_(delegate),
|
2016-10-12 17:41:13 +00:00
|
|
|
is_chunked_upload_(false),
|
2016-10-14 09:50:47 +00:00
|
|
|
response_read_buffer_(new net::IOBuffer(kBufferSize)) {}
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
AtomURLRequest::~AtomURLRequest() {
|
|
|
|
DCHECK(!request_context_getter_);
|
|
|
|
DCHECK(!request_);
|
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
scoped_refptr<AtomURLRequest> AtomURLRequest::Create(
|
2016-09-26 12:59:53 +00:00
|
|
|
AtomBrowserContext* browser_context,
|
|
|
|
const std::string& method,
|
|
|
|
const std::string& url,
|
2017-03-23 19:37:54 +00:00
|
|
|
const std::string& redirect_policy,
|
2016-10-25 13:02:35 +00:00
|
|
|
api::URLRequest* delegate) {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-10-13 15:14:23 +00:00
|
|
|
|
2016-09-19 13:06:13 +00:00
|
|
|
DCHECK(browser_context);
|
|
|
|
DCHECK(!url.empty());
|
2016-10-14 15:37:39 +00:00
|
|
|
DCHECK(delegate);
|
|
|
|
if (!browser_context || url.empty() || !delegate) {
|
2016-10-13 15:14:23 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-23 22:42:59 +00:00
|
|
|
scoped_refptr<brightray::URLRequestContextGetter> request_context_getter(
|
|
|
|
browser_context->url_request_context_getter());
|
2016-10-14 15:37:39 +00:00
|
|
|
DCHECK(request_context_getter);
|
|
|
|
if (!request_context_getter) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-10-13 15:14:23 +00:00
|
|
|
scoped_refptr<AtomURLRequest> atom_url_request(new AtomURLRequest(delegate));
|
|
|
|
if (content::BrowserThread::PostTask(
|
2016-10-14 09:50:47 +00:00
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoInitialize, atom_url_request,
|
2017-03-23 19:37:54 +00:00
|
|
|
request_context_getter, method, url, redirect_policy))) {
|
2016-10-13 15:14:23 +00:00
|
|
|
return atom_url_request;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void AtomURLRequest::Terminate() {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-10-25 13:02:35 +00:00
|
|
|
delegate_ = nullptr;
|
2016-10-14 15:37:39 +00:00
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoTerminate, this));
|
|
|
|
}
|
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
void AtomURLRequest::DoInitialize(
|
|
|
|
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
|
|
|
|
const std::string& method,
|
2017-03-23 19:37:54 +00:00
|
|
|
const std::string& url,
|
|
|
|
const std::string& redirect_policy) {
|
2016-10-13 15:14:23 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-09-19 13:06:13 +00:00
|
|
|
DCHECK(request_context_getter);
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
redirect_policy_ = redirect_policy;
|
2016-10-14 15:37:39 +00:00
|
|
|
request_context_getter_ = request_context_getter;
|
|
|
|
request_context_getter_->AddObserver(this);
|
|
|
|
auto context = request_context_getter_->GetURLRequestContext();
|
|
|
|
if (!context) {
|
|
|
|
// Called after shutdown.
|
|
|
|
DoCancelWithError("Cannot start a request after shutdown.", true);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-19 13:06:13 +00:00
|
|
|
DCHECK(context);
|
2016-10-14 09:50:47 +00:00
|
|
|
request_ = context->CreateRequest(
|
|
|
|
GURL(url), net::RequestPriority::DEFAULT_PRIORITY, this);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
DoCancelWithError("Failed to create a net::URLRequest.", true);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 15:14:23 +00:00
|
|
|
request_->set_method(method);
|
2016-12-21 06:24:48 +00:00
|
|
|
// Do not send cookies from the cookie store.
|
|
|
|
DoSetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES);
|
2018-02-01 11:26:05 +00:00
|
|
|
// Set a flag to stop custom protocol from intercepting this request.
|
|
|
|
request_->SetUserData(DisableProtocolInterceptFlagKey(),
|
|
|
|
base::WrapUnique(new base::SupportsUserData::Data()));
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void AtomURLRequest::DoTerminate() {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
|
|
|
request_.reset();
|
|
|
|
if (request_context_getter_) {
|
|
|
|
request_context_getter_->RemoveObserver(this);
|
|
|
|
request_context_getter_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
bool AtomURLRequest::Write(scoped_refptr<const net::IOBufferWithSize> buffer,
|
|
|
|
bool is_last) {
|
2016-09-29 09:31:08 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-09-26 12:59:53 +00:00
|
|
|
return content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoWriteBuffer, this, buffer, is_last));
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
void AtomURLRequest::SetChunkedUpload(bool is_chunked_upload) {
|
2016-09-26 12:03:49 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
|
|
|
|
// The method can be called only before switching to multi-threaded mode,
|
|
|
|
// i.e. before the first call to write.
|
|
|
|
// So it is safe to change the object in the UI thread.
|
2016-09-27 08:21:11 +00:00
|
|
|
is_chunked_upload_ = is_chunked_upload;
|
2016-09-26 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void AtomURLRequest::Cancel() {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-10-14 09:50:47 +00:00
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoCancel, this));
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
void AtomURLRequest::FollowRedirect() {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoFollowRedirect, this));
|
|
|
|
}
|
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
void AtomURLRequest::SetExtraHeader(const std::string& name,
|
2016-10-14 08:58:16 +00:00
|
|
|
const std::string& value) const {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::PostTask(
|
2016-10-14 09:50:47 +00:00
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
2016-10-13 15:14:23 +00:00
|
|
|
base::Bind(&AtomURLRequest::DoSetExtraHeader, this, name, value));
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
void AtomURLRequest::RemoveExtraHeader(const std::string& name) const {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::PostTask(
|
2016-10-14 09:50:47 +00:00
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
2016-10-13 15:14:23 +00:00
|
|
|
base::Bind(&AtomURLRequest::DoRemoveExtraHeader, this, name));
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 08:58:16 +00:00
|
|
|
void AtomURLRequest::PassLoginInformation(
|
|
|
|
const base::string16& username,
|
|
|
|
const base::string16& password) const {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-10-13 15:14:23 +00:00
|
|
|
if (username.empty() || password.empty()) {
|
2016-09-21 15:35:03 +00:00
|
|
|
content::BrowserThread::PostTask(
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoCancelAuth, this));
|
|
|
|
} else {
|
2016-09-21 15:35:03 +00:00
|
|
|
content::BrowserThread::PostTask(
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoSetAuth, this, username, password));
|
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-11-30 20:01:32 +00:00
|
|
|
void AtomURLRequest::SetLoadFlags(int flags) const {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::DoSetLoadFlags, this, flags));
|
|
|
|
}
|
|
|
|
|
2016-09-26 12:59:53 +00:00
|
|
|
void AtomURLRequest::DoWriteBuffer(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer,
|
|
|
|
bool is_last) {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
if (is_chunked_upload_) {
|
|
|
|
// Chunked encoding case.
|
|
|
|
|
|
|
|
bool first_call = false;
|
|
|
|
if (!chunked_stream_writer_) {
|
|
|
|
std::unique_ptr<net::ChunkedUploadDataStream> chunked_stream(
|
2016-10-13 15:14:23 +00:00
|
|
|
new net::ChunkedUploadDataStream(0));
|
2016-09-26 12:03:49 +00:00
|
|
|
chunked_stream_writer_ = chunked_stream->CreateWriter();
|
|
|
|
request_->set_upload(std::move(chunked_stream));
|
|
|
|
first_call = true;
|
|
|
|
}
|
|
|
|
|
2016-09-28 13:07:54 +00:00
|
|
|
if (buffer)
|
2016-09-26 12:03:49 +00:00
|
|
|
// Non-empty buffer.
|
2016-10-14 09:50:47 +00:00
|
|
|
chunked_stream_writer_->AppendData(buffer->data(), buffer->size(),
|
2016-10-13 15:14:23 +00:00
|
|
|
is_last);
|
2016-09-28 13:07:54 +00:00
|
|
|
else if (is_last)
|
2016-10-04 15:33:34 +00:00
|
|
|
// Empty buffer and last chunk, i.e. request.end().
|
2016-10-14 09:50:47 +00:00
|
|
|
chunked_stream_writer_->AppendData(nullptr, 0, true);
|
2016-09-26 12:03:49 +00:00
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
if (first_call) {
|
2016-09-26 12:03:49 +00:00
|
|
|
request_->Start();
|
2016-10-13 15:14:23 +00:00
|
|
|
}
|
2016-09-29 09:31:08 +00:00
|
|
|
} else {
|
2016-09-26 12:03:49 +00:00
|
|
|
if (buffer) {
|
|
|
|
// Handling potential empty buffers.
|
2016-09-26 12:59:53 +00:00
|
|
|
using internal::UploadOwnedIOBufferElementReader;
|
2016-10-14 09:50:47 +00:00
|
|
|
auto element_reader =
|
|
|
|
UploadOwnedIOBufferElementReader::CreateWithBuffer(std::move(buffer));
|
2016-09-26 12:59:53 +00:00
|
|
|
upload_element_readers_.push_back(
|
2016-10-13 15:14:23 +00:00
|
|
|
std::unique_ptr<net::UploadElementReader>(element_reader));
|
2016-09-26 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_last) {
|
2016-10-13 15:14:23 +00:00
|
|
|
auto elements_upload_data_stream = new net::ElementsUploadDataStream(
|
|
|
|
std::move(upload_element_readers_), 0);
|
2016-09-26 12:59:53 +00:00
|
|
|
request_->set_upload(
|
2016-10-13 15:14:23 +00:00
|
|
|
std::unique_ptr<net::UploadDataStream>(elements_upload_data_stream));
|
2016-09-26 12:03:49 +00:00
|
|
|
request_->Start();
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void AtomURLRequest::DoCancel() {
|
2016-09-28 13:07:54 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (request_) {
|
|
|
|
request_->Cancel();
|
|
|
|
}
|
|
|
|
DoTerminate();
|
2016-09-28 13:07:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
void AtomURLRequest::DoFollowRedirect() {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
|
|
|
if (request_ && request_->is_redirecting() && redirect_policy_ == "manual") {
|
|
|
|
request_->FollowDeferredRedirect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
void AtomURLRequest::DoSetExtraHeader(const std::string& name,
|
|
|
|
const std::string& value) const {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 15:14:23 +00:00
|
|
|
request_->SetExtraRequestHeaderByName(name, value, true);
|
|
|
|
}
|
2016-11-30 20:01:32 +00:00
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
void AtomURLRequest::DoRemoveExtraHeader(const std::string& name) const {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 15:14:23 +00:00
|
|
|
request_->RemoveRequestHeaderByName(name);
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
void AtomURLRequest::DoSetAuth(const base::string16& username,
|
|
|
|
const base::string16& password) const {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
request_->SetAuth(net::AuthCredentials(username, password));
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void AtomURLRequest::DoCancelAuth() const {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
request_->CancelAuth();
|
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void AtomURLRequest::DoCancelWithError(const std::string& error,
|
|
|
|
bool isRequestError) {
|
|
|
|
DoCancel();
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::InformDelegateErrorOccured, this, error,
|
|
|
|
isRequestError));
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:01:32 +00:00
|
|
|
void AtomURLRequest::DoSetLoadFlags(int flags) const {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request_->SetLoadFlags(request_->load_flags() | flags);
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
void AtomURLRequest::OnReceivedRedirect(net::URLRequest* request,
|
|
|
|
const net::RedirectInfo& info,
|
|
|
|
bool* defer_redirect) {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
|
|
|
if (!request_ || redirect_policy_ == "follow")
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (redirect_policy_ == "error") {
|
|
|
|
request->Cancel();
|
|
|
|
DoCancelWithError(
|
|
|
|
"Request cannot follow redirect with the current redirect mode", true);
|
|
|
|
} else if (redirect_policy_ == "manual") {
|
|
|
|
*defer_redirect = true;
|
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers =
|
|
|
|
request->response_headers();
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::InformDelegateReceivedRedirect, this,
|
|
|
|
info.status_code, info.new_method, info.new_url,
|
|
|
|
response_headers));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
void AtomURLRequest::OnAuthRequired(net::URLRequest* request,
|
|
|
|
net::AuthChallengeInfo* auth_info) {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-09-29 09:31:08 +00:00
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
content::BrowserThread::PostTask(
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::UI, FROM_HERE,
|
2016-10-14 09:50:47 +00:00
|
|
|
base::Bind(&AtomURLRequest::InformDelegateAuthenticationRequired, this,
|
2016-10-13 15:14:23 +00:00
|
|
|
scoped_refptr<net::AuthChallengeInfo>(auth_info)));
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
void AtomURLRequest::OnResponseStarted(net::URLRequest* request) {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_EQ(request, request_.get());
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-10-13 15:51:19 +00:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers =
|
2016-10-14 09:50:47 +00:00
|
|
|
request->response_headers();
|
2016-09-28 13:07:54 +00:00
|
|
|
const auto& status = request_->status();
|
|
|
|
if (status.is_success()) {
|
2016-10-04 15:12:17 +00:00
|
|
|
// Success or pending trigger a Read.
|
2016-09-28 13:07:54 +00:00
|
|
|
content::BrowserThread::PostTask(
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::UI, FROM_HERE,
|
2016-10-14 09:50:47 +00:00
|
|
|
base::Bind(&AtomURLRequest::InformDelegateResponseStarted, this,
|
2016-10-13 15:14:23 +00:00
|
|
|
response_headers));
|
2016-09-28 13:07:54 +00:00
|
|
|
ReadResponse();
|
2016-10-04 15:12:17 +00:00
|
|
|
} else if (status.status() == net::URLRequestStatus::Status::FAILED) {
|
|
|
|
// Report error on Start.
|
2016-10-14 15:37:39 +00:00
|
|
|
DoCancelWithError(net::ErrorToString(status.ToNetError()), true);
|
2016-09-28 13:07:54 +00:00
|
|
|
}
|
2016-10-04 15:12:17 +00:00
|
|
|
// We don't report an error is the request is canceled.
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
void AtomURLRequest::ReadResponse() {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
int bytes_read = -1;
|
|
|
|
if (request_->Read(response_read_buffer_.get(), kBufferSize, &bytes_read)) {
|
|
|
|
OnReadCompleted(request_.get(), bytes_read);
|
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
void AtomURLRequest::OnReadCompleted(net::URLRequest* request, int bytes_read) {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-10-14 15:37:39 +00:00
|
|
|
if (!request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_EQ(request, request_.get());
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-09-28 13:07:54 +00:00
|
|
|
const auto status = request_->status();
|
2017-03-23 18:06:05 +00:00
|
|
|
if (status.error() == bytes_read &&
|
|
|
|
bytes_read == net::ERR_CONTENT_DECODING_INIT_FAILED) {
|
|
|
|
// When the request job is unable to create a source stream for the
|
|
|
|
// content encoding, we fail the request.
|
|
|
|
DoCancelWithError(net::ErrorToString(net::ERR_CONTENT_DECODING_INIT_FAILED),
|
|
|
|
true);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-04 15:12:17 +00:00
|
|
|
|
|
|
|
bool response_error = false;
|
|
|
|
bool data_ended = false;
|
|
|
|
bool data_transfer_error = false;
|
2016-09-21 07:23:00 +00:00
|
|
|
do {
|
2016-10-04 15:12:17 +00:00
|
|
|
if (!status.is_success()) {
|
|
|
|
response_error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bytes_read == 0) {
|
|
|
|
data_ended = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bytes_read < 0 || !CopyAndPostBuffer(bytes_read)) {
|
|
|
|
data_transfer_error = true;
|
2016-09-21 07:23:00 +00:00
|
|
|
break;
|
2016-09-28 13:07:54 +00:00
|
|
|
}
|
2016-10-14 09:50:47 +00:00
|
|
|
} while (
|
|
|
|
request_->Read(response_read_buffer_.get(), kBufferSize, &bytes_read));
|
2016-10-04 15:12:17 +00:00
|
|
|
if (response_error) {
|
2016-10-14 15:37:39 +00:00
|
|
|
DoCancelWithError(net::ErrorToString(status.ToNetError()), false);
|
2016-10-04 15:12:17 +00:00
|
|
|
} else if (data_ended) {
|
2016-09-21 07:23:00 +00:00
|
|
|
content::BrowserThread::PostTask(
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequest::InformDelegateResponseCompleted, this));
|
2016-10-14 15:37:39 +00:00
|
|
|
DoTerminate();
|
2016-10-04 15:12:17 +00:00
|
|
|
} else if (data_transfer_error) {
|
|
|
|
// We abort the request on corrupted data transfer.
|
2016-10-14 15:37:39 +00:00
|
|
|
DoCancelWithError("Failed to transfer data from IO to UI thread.", false);
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void AtomURLRequest::OnContextShuttingDown() {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
|
|
|
DoCancel();
|
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
bool AtomURLRequest::CopyAndPostBuffer(int bytes_read) {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
// data is only a wrapper for the asynchronous response_read_buffer_.
|
2016-09-21 07:23:00 +00:00
|
|
|
// Make a deep copy of payload and transfer ownership to the UI thread.
|
2017-12-18 00:23:02 +00:00
|
|
|
auto buffer_copy = WrapRefCounted(new net::IOBufferWithSize(bytes_read));
|
2016-09-26 12:03:49 +00:00
|
|
|
memcpy(buffer_copy->data(), response_read_buffer_->data(), bytes_read);
|
2016-09-21 07:23:00 +00:00
|
|
|
|
|
|
|
return content::BrowserThread::PostTask(
|
2016-10-13 15:14:23 +00:00
|
|
|
content::BrowserThread::UI, FROM_HERE,
|
2016-10-14 09:50:47 +00:00
|
|
|
base::Bind(&AtomURLRequest::InformDelegateResponseData, this,
|
2016-10-13 15:14:23 +00:00
|
|
|
buffer_copy));
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
void AtomURLRequest::InformDelegateReceivedRedirect(
|
|
|
|
int status_code,
|
|
|
|
const std::string& method,
|
|
|
|
const GURL& url,
|
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers) const {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
if (delegate_)
|
|
|
|
delegate_->OnReceivedRedirect(status_code, method, url, response_headers);
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void AtomURLRequest::InformDelegateAuthenticationRequired(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<net::AuthChallengeInfo> auth_info) const {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-09-28 13:07:54 +00:00
|
|
|
if (delegate_)
|
2016-09-21 15:35:03 +00:00
|
|
|
delegate_->OnAuthenticationRequired(auth_info);
|
|
|
|
}
|
|
|
|
|
2016-10-04 15:33:34 +00:00
|
|
|
void AtomURLRequest::InformDelegateResponseStarted(
|
2016-10-13 15:51:19 +00:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers) const {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-09-28 13:07:54 +00:00
|
|
|
if (delegate_)
|
2016-10-04 15:33:34 +00:00
|
|
|
delegate_->OnResponseStarted(response_headers);
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void AtomURLRequest::InformDelegateResponseData(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<net::IOBufferWithSize> data) const {
|
2016-09-21 15:35:03 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2016-09-21 07:23:00 +00:00
|
|
|
|
|
|
|
// Transfer ownership of the data buffer, data will be released
|
|
|
|
// by the delegate's OnResponseData.
|
2016-09-28 13:07:54 +00:00
|
|
|
if (delegate_)
|
2016-09-21 07:23:00 +00:00
|
|
|
delegate_->OnResponseData(data);
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void AtomURLRequest::InformDelegateResponseCompleted() const {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
|
2016-09-28 13:07:54 +00:00
|
|
|
if (delegate_)
|
2016-09-21 07:23:00 +00:00
|
|
|
delegate_->OnResponseCompleted();
|
2016-09-28 13:07:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void AtomURLRequest::InformDelegateErrorOccured(const std::string& error,
|
|
|
|
bool isRequestError) const {
|
2016-10-04 15:12:17 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
|
|
|
|
if (delegate_)
|
2016-10-14 15:37:39 +00:00
|
|
|
delegate_->OnError(error, isRequestError);
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
} // namespace atom
|