2016-09-15 13:59:40 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_url_request.h"
|
2016-09-19 09:21:09 +00:00
|
|
|
#include "atom/browser/api/atom_api_session.h"
|
|
|
|
|
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
#include "atom/browser/net/atom_url_request.h"
|
2016-09-21 07:23:00 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2016-09-21 15:35:03 +00:00
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
namespace {
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
const char* const kResponse = "response";
|
|
|
|
const char* const kData = "data";
|
|
|
|
const char* const kEnd = "end";
|
|
|
|
|
|
|
|
}
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<>
|
2016-09-21 15:35:03 +00:00
|
|
|
struct Converter<scoped_refptr<const net::HttpResponseHeaders>> {
|
2016-09-26 12:59:53 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
scoped_refptr<const net::HttpResponseHeaders> val) {
|
2016-09-21 07:23:00 +00:00
|
|
|
|
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
if (val) {
|
|
|
|
size_t iter = 0;
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
while (val->EnumerateHeaderLines(&iter, &name, &value)) {
|
|
|
|
dict.Set(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2016-09-21 15:35:03 +00:00
|
|
|
struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
|
2016-09-21 07:23:00 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(
|
2016-09-26 12:59:53 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer) {
|
|
|
|
return node::Buffer::Copy(isolate,
|
|
|
|
buffer->data(),
|
|
|
|
buffer->size()).ToLocalChecked();
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-26 12:03:49 +00:00
|
|
|
|
2016-09-26 12:59:53 +00:00
|
|
|
static bool FromV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
scoped_refptr<const net::IOBufferWithSize>* out) {
|
2016-09-26 12:03:49 +00:00
|
|
|
|
|
|
|
auto size = node::Buffer::Length(val);
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
// Support conversoin from empty buffer. A use case is
|
|
|
|
// a GET request without body.
|
|
|
|
// Since zero-sized IOBuffer(s) are not supported, we set the
|
|
|
|
// out pointer to null.
|
|
|
|
*out = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = node::Buffer::Data(val);
|
|
|
|
if (!data) {
|
|
|
|
// This is an error as size is positif but data is null.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto io_buffer = new net::IOBufferWithSize(size);
|
|
|
|
if (!io_buffer) {
|
|
|
|
// Assuming allocation failed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do a deep copy. We could have used Buffer's internal memory
|
|
|
|
// but that is much more complicated to be properly handled.
|
|
|
|
memcpy(io_buffer->data(), data, size);
|
|
|
|
*out = io_buffer;
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2016-09-15 13:59:40 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-09-26 12:59:53 +00:00
|
|
|
URLRequest::URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
|
2016-09-19 09:21:09 +00:00
|
|
|
: weak_ptr_factory_(this) {
|
2016-09-26 12:59:53 +00:00
|
|
|
InitWith(isolate, wrapper);
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
URLRequest::~URLRequest() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
|
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
v8::Local<v8::Object> options;
|
|
|
|
args->GetNext(&options);
|
|
|
|
mate::Dictionary dict(args->isolate(), options);
|
|
|
|
std::string method;
|
|
|
|
dict.Get("method", &method);
|
2016-09-27 10:33:51 +00:00
|
|
|
std::string url;
|
|
|
|
dict.Get("url", &url);
|
2016-09-19 09:21:09 +00:00
|
|
|
std::string session_name;
|
|
|
|
dict.Get("session", &session_name);
|
|
|
|
|
|
|
|
auto session = Session::FromPartition(args->isolate(), session_name);
|
|
|
|
|
|
|
|
auto browser_context = session->browser_context();
|
|
|
|
auto api_url_request = new URLRequest(args->isolate(), args->GetThis());
|
|
|
|
auto weak_ptr = api_url_request->weak_ptr_factory_.GetWeakPtr();
|
2016-09-21 15:35:03 +00:00
|
|
|
auto atom_url_request = AtomURLRequest::Create(
|
|
|
|
browser_context,
|
|
|
|
method,
|
|
|
|
url,
|
|
|
|
weak_ptr);
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
api_url_request->atom_request_ = atom_url_request;
|
2016-09-19 09:21:09 +00:00
|
|
|
|
|
|
|
return api_url_request;
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void URLRequest::BuildPrototype(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-09-19 09:21:09 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "URLRequest"));
|
2016-09-15 13:59:40 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2016-09-19 13:06:13 +00:00
|
|
|
// Request API
|
|
|
|
.MakeDestroyable()
|
2016-09-28 13:07:54 +00:00
|
|
|
.SetMethod("write", &URLRequest::Write)
|
2016-09-19 13:06:13 +00:00
|
|
|
.SetMethod("abort", &URLRequest::Abort)
|
2016-09-27 08:21:11 +00:00
|
|
|
.SetMethod("setExtraHeader", &URLRequest::SetExtraHeader)
|
|
|
|
.SetMethod("removeExtraHeader", &URLRequest::RemoveExtraHeader)
|
|
|
|
.SetMethod("setChunkedUpload", &URLRequest::SetChunkedUpload)
|
2016-09-19 13:06:13 +00:00
|
|
|
// Response APi
|
2016-09-27 08:21:11 +00:00
|
|
|
.SetProperty("statusCode", &URLRequest::StatusCode)
|
|
|
|
.SetProperty("statusMessage", &URLRequest::StatusMessage)
|
|
|
|
.SetProperty("rawResponseHeaders", &URLRequest::RawResponseHeaders)
|
|
|
|
.SetProperty("httpVersionMajor", &URLRequest::ResponseHttpVersionMajor)
|
|
|
|
.SetProperty("httpVersionMinor", &URLRequest::ResponseHttpVersionMinor);
|
2016-09-21 07:23:00 +00:00
|
|
|
|
|
|
|
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 10:33:51 +00:00
|
|
|
bool URLRequest::Write(
|
2016-09-26 12:59:53 +00:00
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer,
|
|
|
|
bool is_last) {
|
2016-09-27 10:33:51 +00:00
|
|
|
return atom_request_->Write(buffer, is_last);
|
2016-09-26 12:59:53 +00:00
|
|
|
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-19 13:06:13 +00:00
|
|
|
void URLRequest::Abort() {
|
2016-09-21 15:35:03 +00:00
|
|
|
atom_request_->Abort();
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
bool URLRequest::SetExtraHeader(const std::string& name,
|
2016-09-26 12:59:53 +00:00
|
|
|
const std::string& value) {
|
2016-09-26 12:03:49 +00:00
|
|
|
if (!net::HttpUtil::IsValidHeaderName(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!net::HttpUtil::IsValidHeaderValue(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
atom_request_->SetExtraHeader(name, value);
|
2016-09-26 12:03:49 +00:00
|
|
|
return true;
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
2016-09-27 08:21:11 +00:00
|
|
|
|
|
|
|
void URLRequest::RemoveExtraHeader(const std::string& name) {
|
|
|
|
atom_request_->RemoveExtraHeader(name);
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
2016-09-27 08:21:11 +00:00
|
|
|
|
|
|
|
void URLRequest::SetChunkedUpload(bool is_chunked_upload) {
|
|
|
|
atom_request_->SetChunkedUpload(is_chunked_upload);
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void URLRequest::OnAuthenticationRequired(
|
2016-09-26 12:59:53 +00:00
|
|
|
scoped_refptr<const net::AuthChallengeInfo> auth_info) {
|
2016-09-21 15:35:03 +00:00
|
|
|
EmitRequestEvent(
|
|
|
|
"login",
|
|
|
|
auth_info.get(),
|
|
|
|
base::Bind(&AtomURLRequest::PassLoginInformation, atom_request_));
|
|
|
|
}
|
|
|
|
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
void URLRequest::OnResponseStarted() {
|
2016-09-27 08:21:11 +00:00
|
|
|
Emit("response");
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void URLRequest::OnResponseData(
|
2016-09-26 12:59:53 +00:00
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer) {
|
2016-09-21 07:23:00 +00:00
|
|
|
if (!buffer || !buffer->data() || !buffer->size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitResponseEvent("data", buffer);
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
void URLRequest::OnResponseCompleted() {
|
|
|
|
EmitResponseEvent("end");
|
2016-09-26 12:03:49 +00:00
|
|
|
unpin();
|
|
|
|
atom_request_ = nullptr;
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 13:07:54 +00:00
|
|
|
void URLRequest::OnError(const std::string& error) {
|
|
|
|
auto error_object = v8::Exception::Error(mate::StringToV8(isolate(), error));
|
|
|
|
EmitRequestEvent("error", error_object);
|
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
int URLRequest::StatusCode() const {
|
|
|
|
if (auto response_headers = atom_request_->GetResponseHeaders()) {
|
2016-09-21 07:23:00 +00:00
|
|
|
return response_headers->response_code();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
std::string URLRequest::StatusMessage() const {
|
2016-09-21 07:23:00 +00:00
|
|
|
std::string result;
|
2016-09-21 15:35:03 +00:00
|
|
|
if (auto response_headers = atom_request_->GetResponseHeaders()) {
|
2016-09-21 07:23:00 +00:00
|
|
|
result = response_headers->GetStatusText();
|
|
|
|
}
|
|
|
|
return result;
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 12:59:53 +00:00
|
|
|
scoped_refptr<const net::HttpResponseHeaders>
|
|
|
|
URLRequest::RawResponseHeaders() const {
|
2016-09-21 15:35:03 +00:00
|
|
|
return atom_request_->GetResponseHeaders();
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
uint32_t URLRequest::ResponseHttpVersionMajor() const {
|
|
|
|
if (auto response_headers = atom_request_->GetResponseHeaders()) {
|
2016-09-21 07:23:00 +00:00
|
|
|
return response_headers->GetHttpVersion().major_value();
|
|
|
|
}
|
|
|
|
return 0;
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
uint32_t URLRequest::ResponseHttpVersionMinor() const {
|
|
|
|
if (auto response_headers = atom_request_->GetResponseHeaders()) {
|
2016-09-21 07:23:00 +00:00
|
|
|
return response_headers->GetHttpVersion().minor_value();
|
|
|
|
}
|
|
|
|
return 0;
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void URLRequest::pin() {
|
|
|
|
if (wrapper_.IsEmpty()) {
|
|
|
|
wrapper_.Reset(isolate(), GetWrapper());
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
void URLRequest::unpin() {
|
|
|
|
wrapper_.Reset();
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
} // namepsace mate
|