2015-05-29 15:54:00 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/net/url_request_fetch_job.h"
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
#include <algorithm>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2015-06-17 03:34:47 +00:00
|
|
|
#include <string>
|
2018-10-05 20:29:57 +00:00
|
|
|
#include <utility>
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2016-11-27 19:07:50 +00:00
|
|
|
#include "atom/browser/api/atom_api_session.h"
|
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2018-10-05 20:29:57 +00:00
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
2018-08-14 21:07:53 +00:00
|
|
|
#include "base/guid.h"
|
2016-07-04 06:08:55 +00:00
|
|
|
#include "base/memory/ptr_util.h"
|
2015-06-17 02:57:26 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "base/task/post_task.h"
|
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2018-10-05 20:29:57 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2016-03-10 05:39:40 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-05-29 15:54:00 +00:00
|
|
|
#include "net/base/io_buffer.h"
|
|
|
|
#include "net/base/net_errors.h"
|
2015-06-17 01:31:33 +00:00
|
|
|
#include "net/http/http_response_headers.h"
|
|
|
|
#include "net/url_request/url_fetcher.h"
|
|
|
|
#include "net/url_request/url_fetcher_response_writer.h"
|
2016-03-10 05:39:40 +00:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
2015-05-29 15:54:00 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-06-17 02:57:26 +00:00
|
|
|
// Convert string to RequestType.
|
|
|
|
net::URLFetcher::RequestType GetRequestType(const std::string& raw) {
|
2015-12-07 11:56:23 +00:00
|
|
|
std::string method = base::ToUpperASCII(raw);
|
2015-06-17 02:57:26 +00:00
|
|
|
if (method.empty() || method == "GET")
|
|
|
|
return net::URLFetcher::GET;
|
|
|
|
else if (method == "POST")
|
|
|
|
return net::URLFetcher::POST;
|
|
|
|
else if (method == "HEAD")
|
|
|
|
return net::URLFetcher::HEAD;
|
|
|
|
else if (method == "DELETE")
|
|
|
|
return net::URLFetcher::DELETE_REQUEST;
|
|
|
|
else if (method == "PUT")
|
|
|
|
return net::URLFetcher::PUT;
|
|
|
|
else if (method == "PATCH")
|
|
|
|
return net::URLFetcher::PATCH;
|
|
|
|
else // Use "GET" as fallback.
|
|
|
|
return net::URLFetcher::GET;
|
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
// Pipe the response writer back to URLRequestFetchJob.
|
|
|
|
class ResponsePiper : public net::URLFetcherResponseWriter {
|
|
|
|
public:
|
2018-05-21 22:18:38 +00:00
|
|
|
explicit ResponsePiper(URLRequestFetchJob* job) : job_(job) {}
|
2015-06-17 01:31:33 +00:00
|
|
|
|
|
|
|
// net::URLFetcherResponseWriter:
|
2018-10-25 19:33:23 +00:00
|
|
|
int Initialize(net::CompletionOnceCallback callback) override {
|
2015-06-17 01:31:33 +00:00
|
|
|
return net::OK;
|
|
|
|
}
|
|
|
|
int Write(net::IOBuffer* buffer,
|
|
|
|
int num_bytes,
|
2018-10-25 19:33:23 +00:00
|
|
|
net::CompletionOnceCallback callback) override {
|
2015-06-17 01:31:33 +00:00
|
|
|
if (first_write_) {
|
|
|
|
// The URLFetcherResponseWriter doesn't have an event when headers have
|
|
|
|
// been read, so we have to emulate by hooking to first write event.
|
|
|
|
job_->HeadersCompleted();
|
|
|
|
first_write_ = false;
|
|
|
|
}
|
2018-10-25 19:33:23 +00:00
|
|
|
return job_->DataAvailable(buffer, num_bytes, std::move(callback));
|
2015-06-17 01:31:33 +00:00
|
|
|
}
|
2018-10-25 19:33:23 +00:00
|
|
|
int Finish(int net_error, net::CompletionOnceCallback callback) override {
|
2015-06-17 01:31:33 +00:00
|
|
|
return net::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-05-21 22:18:38 +00:00
|
|
|
bool first_write_ = true;
|
2015-06-17 01:31:33 +00:00
|
|
|
URLRequestFetchJob* job_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ResponsePiper);
|
|
|
|
};
|
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
void BeforeStartInUI(base::WeakPtr<URLRequestFetchJob> job,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
// Pass whatever user passed to the actaul request job.
|
|
|
|
v8::Local<v8::Value> value;
|
2016-03-10 05:39:40 +00:00
|
|
|
mate::Dictionary options;
|
2018-10-05 20:29:57 +00:00
|
|
|
if (!args->GetNext(&value) ||
|
|
|
|
!mate::ConvertFromV8(args->isolate(), value, &options)) {
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO},
|
|
|
|
base::BindOnce(&URLRequestFetchJob::OnError, job,
|
|
|
|
net::ERR_NOT_IMPLEMENTED));
|
2016-03-10 05:39:40 +00:00
|
|
|
return;
|
2018-10-05 20:29:57 +00:00
|
|
|
}
|
2016-03-10 05:39:40 +00:00
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter;
|
|
|
|
scoped_refptr<AtomBrowserContext> custom_browser_context;
|
|
|
|
// When |session| is set to |null| we use a new request context for fetch
|
|
|
|
// job.
|
|
|
|
if (options.Get("session", &value)) {
|
|
|
|
if (value->IsNull()) {
|
2016-11-27 19:07:50 +00:00
|
|
|
// We have to create the URLRequestContextGetter on UI thread.
|
2018-10-05 20:29:57 +00:00
|
|
|
custom_browser_context =
|
2018-08-14 21:07:53 +00:00
|
|
|
AtomBrowserContext::From(base::GenerateGUID(), true);
|
2018-10-05 20:29:57 +00:00
|
|
|
url_request_context_getter = custom_browser_context->GetRequestContext();
|
2016-11-27 19:07:50 +00:00
|
|
|
} else {
|
2016-11-27 19:10:06 +00:00
|
|
|
mate::Handle<api::Session> session;
|
2018-10-05 20:29:57 +00:00
|
|
|
if (mate::ConvertFromV8(args->isolate(), value, &session) &&
|
|
|
|
!session.IsEmpty()) {
|
2016-11-27 19:10:06 +00:00
|
|
|
AtomBrowserContext* browser_context = session->browser_context();
|
2018-10-05 20:29:57 +00:00
|
|
|
url_request_context_getter = browser_context->GetRequestContext();
|
2016-11-27 19:10:06 +00:00
|
|
|
}
|
2016-11-27 19:07:50 +00:00
|
|
|
}
|
2016-03-10 05:39:40 +00:00
|
|
|
}
|
2018-10-05 20:29:57 +00:00
|
|
|
|
|
|
|
V8ValueConverter converter;
|
|
|
|
v8::Local<v8::Context> context = args->isolate()->GetCurrentContext();
|
|
|
|
std::unique_ptr<base::Value> request_options(
|
|
|
|
converter.FromV8Value(value, context));
|
|
|
|
|
|
|
|
int error = net::OK;
|
|
|
|
if (!request_options || !request_options->is_dict())
|
|
|
|
error = net::ERR_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
JsAsker::IsErrorOptions(request_options.get(), &error);
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraits(
|
|
|
|
FROM_HERE, {content::BrowserThread::IO},
|
2018-10-05 20:29:57 +00:00
|
|
|
base::BindOnce(&URLRequestFetchJob::StartAsync, job,
|
|
|
|
base::RetainedRef(url_request_context_getter),
|
|
|
|
base::RetainedRef(custom_browser_context),
|
|
|
|
std::move(request_options), error));
|
2016-03-10 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
URLRequestFetchJob::URLRequestFetchJob(net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate)
|
|
|
|
: net::URLRequestJob(request, network_delegate), weak_factory_(this) {}
|
|
|
|
|
|
|
|
URLRequestFetchJob::~URLRequestFetchJob() = default;
|
|
|
|
|
|
|
|
void URLRequestFetchJob::Start() {
|
|
|
|
auto request_details = std::make_unique<base::DictionaryValue>();
|
|
|
|
FillRequestDetails(request_details.get(), request());
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraits(
|
|
|
|
FROM_HERE, {content::BrowserThread::UI},
|
2019-05-03 19:08:41 +00:00
|
|
|
base::BindOnce(
|
|
|
|
&JsAsker::AskForOptions, base::Unretained(isolate()), handler(),
|
|
|
|
std::move(request_details),
|
|
|
|
base::BindOnce(&BeforeStartInUI, weak_factory_.GetWeakPtr())));
|
2018-10-05 20:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void URLRequestFetchJob::StartAsync(
|
|
|
|
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
|
|
|
|
scoped_refptr<AtomBrowserContext> browser_context,
|
|
|
|
std::unique_ptr<base::Value> options,
|
|
|
|
int error) {
|
|
|
|
if (error != net::OK) {
|
|
|
|
NotifyStartError(
|
|
|
|
net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
|
2015-08-12 13:09:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string url, method, referrer;
|
2015-12-06 20:27:02 +00:00
|
|
|
base::DictionaryValue* upload_data = nullptr;
|
2015-08-12 13:09:44 +00:00
|
|
|
base::DictionaryValue* dict =
|
|
|
|
static_cast<base::DictionaryValue*>(options.get());
|
|
|
|
dict->GetString("url", &url);
|
|
|
|
dict->GetString("method", &method);
|
|
|
|
dict->GetString("referrer", &referrer);
|
2015-12-06 20:27:02 +00:00
|
|
|
dict->GetDictionary("uploadData", &upload_data);
|
2015-08-12 13:09:44 +00:00
|
|
|
|
2015-08-13 13:21:23 +00:00
|
|
|
// Check if URL is valid.
|
|
|
|
GURL formated_url(url);
|
|
|
|
if (!formated_url.is_valid()) {
|
2018-04-18 01:55:30 +00:00
|
|
|
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
|
|
|
net::ERR_INVALID_URL));
|
2015-08-13 13:21:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-17 03:11:13 +00:00
|
|
|
// Use |request|'s method if |method| is not specified.
|
|
|
|
net::URLFetcher::RequestType request_type;
|
|
|
|
if (method.empty())
|
2015-08-12 13:09:44 +00:00
|
|
|
request_type = GetRequestType(request()->method());
|
2015-06-17 03:11:13 +00:00
|
|
|
else
|
|
|
|
request_type = GetRequestType(method);
|
|
|
|
|
2015-08-13 13:21:23 +00:00
|
|
|
fetcher_ = net::URLFetcher::Create(formated_url, request_type, this);
|
2016-07-04 06:08:55 +00:00
|
|
|
fetcher_->SaveResponseWithWriter(base::WrapUnique(new ResponsePiper(this)));
|
2015-06-17 03:20:09 +00:00
|
|
|
|
2016-03-10 05:39:40 +00:00
|
|
|
// A request context getter is passed by the user.
|
2018-10-05 20:29:57 +00:00
|
|
|
if (url_request_context_getter)
|
|
|
|
fetcher_->SetRequestContext(url_request_context_getter.get());
|
2015-08-12 14:57:25 +00:00
|
|
|
else
|
|
|
|
fetcher_->SetRequestContext(request_context_getter());
|
|
|
|
|
2015-06-17 03:20:09 +00:00
|
|
|
// Use |request|'s referrer if |referrer| is not specified.
|
2015-08-12 13:09:44 +00:00
|
|
|
if (referrer.empty())
|
|
|
|
fetcher_->SetReferrer(request()->referrer());
|
|
|
|
else
|
2015-06-17 03:20:09 +00:00
|
|
|
fetcher_->SetReferrer(referrer);
|
2015-06-17 03:30:31 +00:00
|
|
|
|
2015-12-06 20:27:02 +00:00
|
|
|
// Set the data needed for POSTs.
|
|
|
|
if (upload_data && request_type == net::URLFetcher::POST) {
|
|
|
|
std::string content_type, data;
|
|
|
|
upload_data->GetString("contentType", &content_type);
|
|
|
|
upload_data->GetString("data", &data);
|
|
|
|
fetcher_->SetUploadData(content_type, data);
|
|
|
|
}
|
|
|
|
|
2015-06-17 03:30:31 +00:00
|
|
|
// Use |request|'s headers.
|
2015-08-12 13:09:44 +00:00
|
|
|
fetcher_->SetExtraRequestHeaders(
|
|
|
|
request()->extra_request_headers().ToString());
|
|
|
|
|
2017-05-26 16:40:43 +00:00
|
|
|
fetcher_->Start();
|
2018-10-05 20:29:57 +00:00
|
|
|
|
|
|
|
if (browser_context)
|
|
|
|
custom_browser_context_ = browser_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLRequestFetchJob::OnError(int error) {
|
|
|
|
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
|
2015-06-17 02:57:26 +00:00
|
|
|
}
|
2015-06-17 01:31:33 +00:00
|
|
|
|
|
|
|
void URLRequestFetchJob::HeadersCompleted() {
|
|
|
|
response_info_.reset(new net::HttpResponseInfo);
|
|
|
|
response_info_->headers = fetcher_->GetResponseHeaders();
|
|
|
|
NotifyHeadersComplete();
|
|
|
|
}
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2017-05-26 16:40:43 +00:00
|
|
|
int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer,
|
|
|
|
int num_bytes,
|
2018-10-25 19:33:23 +00:00
|
|
|
net::CompletionOnceCallback callback) {
|
2017-05-26 16:40:43 +00:00
|
|
|
// When pending_buffer_ is empty, there's no ReadRawData() operation waiting
|
|
|
|
// for IO completion, we have to save the parameters until the request is
|
|
|
|
// ready to read data.
|
|
|
|
if (!pending_buffer_.get()) {
|
|
|
|
write_buffer_ = buffer;
|
|
|
|
write_num_bytes_ = num_bytes;
|
2018-10-25 19:33:23 +00:00
|
|
|
write_callback_ = std::move(callback);
|
2017-05-26 16:40:43 +00:00
|
|
|
return net::ERR_IO_PENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write data to the pending buffer and clear them after the writing.
|
2018-04-18 01:55:30 +00:00
|
|
|
int bytes_read = BufferCopy(buffer, num_bytes, pending_buffer_.get(),
|
|
|
|
pending_buffer_size_);
|
2017-05-26 16:40:43 +00:00
|
|
|
ClearPendingBuffer();
|
|
|
|
ReadRawDataComplete(bytes_read);
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
2015-05-29 15:54:00 +00:00
|
|
|
void URLRequestFetchJob::Kill() {
|
2018-10-05 20:29:57 +00:00
|
|
|
weak_factory_.InvalidateWeakPtrs();
|
|
|
|
net::URLRequestJob::Kill();
|
2015-06-17 01:31:33 +00:00
|
|
|
fetcher_.reset();
|
2018-10-04 18:08:56 +00:00
|
|
|
custom_browser_context_ = nullptr;
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 14:28:53 +00:00
|
|
|
int URLRequestFetchJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
|
2015-11-20 20:50:51 +00:00
|
|
|
if (GetResponseCode() == 204) {
|
|
|
|
request()->set_received_response_content_length(prefilter_bytes_read());
|
2016-03-27 20:39:56 +00:00
|
|
|
return net::OK;
|
2015-11-20 20:50:51 +00:00
|
|
|
}
|
2016-05-30 11:31:00 +00:00
|
|
|
|
2017-05-26 16:40:43 +00:00
|
|
|
// When write_buffer_ is empty, there is no data valable yet, we have to save
|
|
|
|
// the dest buffer util DataAvailable.
|
|
|
|
if (!write_buffer_.get()) {
|
|
|
|
pending_buffer_ = dest;
|
|
|
|
pending_buffer_size_ = dest_size;
|
|
|
|
return net::ERR_IO_PENDING;
|
2016-05-30 11:31:00 +00:00
|
|
|
}
|
2017-05-26 16:40:43 +00:00
|
|
|
|
|
|
|
// Read from the write buffer and clear them after reading.
|
2018-04-18 01:55:30 +00:00
|
|
|
int bytes_read =
|
|
|
|
BufferCopy(write_buffer_.get(), write_num_bytes_, dest, dest_size);
|
2017-05-26 16:40:43 +00:00
|
|
|
ClearWriteBuffer();
|
2018-10-25 19:33:23 +00:00
|
|
|
if (!write_callback_.is_null())
|
|
|
|
std::move(write_callback_).Run(bytes_read);
|
2017-05-26 16:40:43 +00:00
|
|
|
return bytes_read;
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
bool URLRequestFetchJob::GetMimeType(std::string* mime_type) const {
|
2016-01-06 13:07:07 +00:00
|
|
|
if (!response_info_ || !response_info_->headers)
|
2015-06-17 01:31:33 +00:00
|
|
|
return false;
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
return response_info_->headers->GetMimeType(mime_type);
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
void URLRequestFetchJob::GetResponseInfo(net::HttpResponseInfo* info) {
|
|
|
|
if (response_info_)
|
|
|
|
*info = *response_info_;
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
int URLRequestFetchJob::GetResponseCode() const {
|
2016-01-06 13:07:07 +00:00
|
|
|
if (!response_info_ || !response_info_->headers)
|
2015-06-17 01:31:33 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return response_info_->headers->response_code();
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 22:28:14 +00:00
|
|
|
void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) {
|
2017-05-26 16:40:43 +00:00
|
|
|
ClearPendingBuffer();
|
|
|
|
ClearWriteBuffer();
|
|
|
|
|
|
|
|
if (fetcher_->GetStatus().is_success()) {
|
2016-11-27 19:10:06 +00:00
|
|
|
if (!response_info_) {
|
|
|
|
// Since we notify header completion only after first write there will be
|
|
|
|
// no response object constructed for http respones with no content 204.
|
|
|
|
// We notify header completion here.
|
|
|
|
HeadersCompleted();
|
|
|
|
return;
|
|
|
|
}
|
2017-10-26 02:01:53 +00:00
|
|
|
if (request_->status().is_io_pending()) {
|
|
|
|
ReadRawDataComplete(0);
|
|
|
|
}
|
2016-11-27 19:10:06 +00:00
|
|
|
} else {
|
2017-05-26 16:40:43 +00:00
|
|
|
NotifyStartError(fetcher_->GetStatus());
|
2016-11-27 19:10:06 +00:00
|
|
|
}
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
int URLRequestFetchJob::BufferCopy(net::IOBuffer* source,
|
|
|
|
int num_bytes,
|
|
|
|
net::IOBuffer* target,
|
|
|
|
int target_size) {
|
2017-05-26 16:40:43 +00:00
|
|
|
int bytes_written = std::min(num_bytes, target_size);
|
|
|
|
memcpy(target->data(), source->data(), bytes_written);
|
|
|
|
return bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLRequestFetchJob::ClearPendingBuffer() {
|
|
|
|
pending_buffer_ = nullptr;
|
|
|
|
pending_buffer_size_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLRequestFetchJob::ClearWriteBuffer() {
|
|
|
|
write_buffer_ = nullptr;
|
|
|
|
write_num_bytes_ = 0;
|
2016-05-30 11:31:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 15:54:00 +00:00
|
|
|
} // namespace atom
|