2016-10-25 10:41:01 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
2016-09-15 13:59:40 +00:00
|
|
|
// 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/api/atom_api_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_session.h"
|
|
|
|
#include "atom/browser/net/atom_url_request.h"
|
2016-10-26 09:10:15 +00:00
|
|
|
#include "atom/common/api/event_emitter_caller.h"
|
2016-09-29 09:31:08 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.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"
|
2016-09-29 09:31:08 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
namespace mate {
|
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
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-10-13 15:14:23 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer) {
|
2016-10-14 09:50:47 +00:00
|
|
|
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-10-14 09:50:47 +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);
|
2016-09-29 09:31:08 +00:00
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
if (size == 0) {
|
2016-10-04 15:33:34 +00:00
|
|
|
// Support conversion from empty buffer. A use case is
|
2016-09-29 09:31:08 +00:00
|
|
|
// a GET request without body.
|
2016-09-26 12:03:49 +00:00
|
|
|
// 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) {
|
2016-10-04 15:33:34 +00:00
|
|
|
// This is an error as size is positive but data is null.
|
2016-09-26 12:03:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
*out = new net::IOBufferWithSize(size);
|
2016-09-26 12:03:49 +00:00
|
|
|
// We do a deep copy. We could have used Buffer's internal memory
|
|
|
|
// but that is much more complicated to be properly handled.
|
2016-10-25 10:41:01 +00:00
|
|
|
memcpy((*out)->data(), data, size);
|
2016-09-26 12:03:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
};
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
} // namespace mate
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
namespace atom {
|
2016-09-15 13:59:40 +00:00
|
|
|
namespace api {
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
template <typename Flags>
|
|
|
|
URLRequest::StateBase<Flags>::StateBase(Flags initialState)
|
2016-10-14 09:50:47 +00:00
|
|
|
: state_(initialState) {}
|
2016-10-04 15:12:17 +00:00
|
|
|
|
|
|
|
template <typename Flags>
|
|
|
|
void URLRequest::StateBase<Flags>::SetFlag(Flags flag) {
|
2016-10-14 09:50:47 +00:00
|
|
|
state_ =
|
|
|
|
static_cast<Flags>(static_cast<int>(state_) | static_cast<int>(flag));
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Flags>
|
|
|
|
bool URLRequest::StateBase<Flags>::operator==(Flags flag) const {
|
|
|
|
return state_ == flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Flags>
|
|
|
|
bool URLRequest::StateBase<Flags>::IsFlagSet(Flags flag) const {
|
|
|
|
return static_cast<int>(state_) & static_cast<int>(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
URLRequest::RequestState::RequestState()
|
2016-10-14 09:50:47 +00:00
|
|
|
: StateBase(RequestStateFlags::kNotStarted) {}
|
2016-10-04 15:12:17 +00:00
|
|
|
|
|
|
|
bool URLRequest::RequestState::NotStarted() const {
|
|
|
|
return *this == RequestStateFlags::kNotStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::RequestState::Started() const {
|
|
|
|
return IsFlagSet(RequestStateFlags::kStarted);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::RequestState::Finished() const {
|
|
|
|
return IsFlagSet(RequestStateFlags::kFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::RequestState::Canceled() const {
|
|
|
|
return IsFlagSet(RequestStateFlags::kCanceled);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::RequestState::Failed() const {
|
|
|
|
return IsFlagSet(RequestStateFlags::kFailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::RequestState::Closed() const {
|
|
|
|
return IsFlagSet(RequestStateFlags::kClosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
URLRequest::ResponseState::ResponseState()
|
2016-10-14 09:50:47 +00:00
|
|
|
: StateBase(ResponseStateFlags::kNotStarted) {}
|
2016-10-04 15:12:17 +00:00
|
|
|
|
|
|
|
bool URLRequest::ResponseState::NotStarted() const {
|
|
|
|
return *this == ResponseStateFlags::kNotStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::ResponseState::Started() const {
|
|
|
|
return IsFlagSet(ResponseStateFlags::kStarted);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::ResponseState::Ended() const {
|
|
|
|
return IsFlagSet(ResponseStateFlags::kEnded);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::ResponseState::Failed() const {
|
|
|
|
return IsFlagSet(ResponseStateFlags::kFailed);
|
|
|
|
}
|
|
|
|
|
2016-10-25 13:02:35 +00:00
|
|
|
URLRequest::URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
|
2016-09-30 12:17:29 +00:00
|
|
|
InitWith(isolate, wrapper);
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
URLRequest::~URLRequest() {
|
|
|
|
// A request has been created in JS, it was not used and then
|
|
|
|
// it got collected, no close event to cleanup, only destructor
|
|
|
|
// is called.
|
|
|
|
if (atom_request_) {
|
|
|
|
atom_request_->Terminate();
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 13:59:40 +00:00
|
|
|
|
|
|
|
// static
|
|
|
|
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
|
2016-10-25 14:19:26 +00:00
|
|
|
auto isolate = args->isolate();
|
2016-09-19 09:21:09 +00:00
|
|
|
v8::Local<v8::Object> options;
|
|
|
|
args->GetNext(&options);
|
2016-10-25 13:47:54 +00:00
|
|
|
mate::Dictionary dict(isolate, options);
|
2016-09-19 09:21:09 +00:00
|
|
|
std::string method;
|
|
|
|
dict.Get("method", &method);
|
2016-09-27 10:33:51 +00:00
|
|
|
std::string url;
|
|
|
|
dict.Get("url", &url);
|
2016-10-25 13:47:54 +00:00
|
|
|
std::string partition;
|
|
|
|
mate::Handle<api::Session> session;
|
|
|
|
if (dict.Get("session", &session)) {
|
|
|
|
} else if (dict.Get("partition", &partition)) {
|
|
|
|
session = Session::FromPartition(isolate, partition);
|
|
|
|
} else {
|
|
|
|
// Use the default session if not specified.
|
|
|
|
session = Session::FromPartition(isolate, "");
|
|
|
|
}
|
2016-09-19 09:21:09 +00:00
|
|
|
auto browser_context = session->browser_context();
|
|
|
|
auto api_url_request = new URLRequest(args->isolate(), args->GetThis());
|
2016-10-14 09:50:47 +00:00
|
|
|
auto atom_url_request =
|
2016-10-25 13:02:35 +00:00
|
|
|
AtomURLRequest::Create(browser_context, method, url, api_url_request);
|
2016-09-29 09:31:08 +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-10-14 09:50:47 +00:00
|
|
|
// Request API
|
|
|
|
.MakeDestroyable()
|
|
|
|
.SetMethod("write", &URLRequest::Write)
|
|
|
|
.SetMethod("cancel", &URLRequest::Cancel)
|
|
|
|
.SetMethod("setExtraHeader", &URLRequest::SetExtraHeader)
|
|
|
|
.SetMethod("removeExtraHeader", &URLRequest::RemoveExtraHeader)
|
|
|
|
.SetMethod("setChunkedUpload", &URLRequest::SetChunkedUpload)
|
2016-11-30 20:01:32 +00:00
|
|
|
.SetMethod("_setLoadFlags", &URLRequest::SetLoadFlags)
|
2016-10-14 09:50:47 +00:00
|
|
|
.SetProperty("notStarted", &URLRequest::NotStarted)
|
|
|
|
.SetProperty("finished", &URLRequest::Finished)
|
|
|
|
// Response APi
|
|
|
|
.SetProperty("statusCode", &URLRequest::StatusCode)
|
|
|
|
.SetProperty("statusMessage", &URLRequest::StatusMessage)
|
|
|
|
.SetProperty("rawResponseHeaders", &URLRequest::RawResponseHeaders)
|
|
|
|
.SetProperty("httpVersionMajor", &URLRequest::ResponseHttpVersionMajor)
|
|
|
|
.SetProperty("httpVersionMinor", &URLRequest::ResponseHttpVersionMinor);
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
bool URLRequest::NotStarted() const {
|
|
|
|
return request_state_.NotStarted();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::Finished() const {
|
|
|
|
return request_state_.Finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequest::Canceled() const {
|
|
|
|
return request_state_.Canceled();
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
bool URLRequest::Write(scoped_refptr<const net::IOBufferWithSize> buffer,
|
|
|
|
bool is_last) {
|
|
|
|
if (request_state_.Canceled() || request_state_.Failed() ||
|
|
|
|
request_state_.Finished() || request_state_.Closed()) {
|
2016-10-04 15:12:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request_state_.NotStarted()) {
|
|
|
|
request_state_.SetFlag(RequestStateFlags::kStarted);
|
|
|
|
// Pin on first write.
|
2016-10-25 10:41:01 +00:00
|
|
|
Pin();
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_last) {
|
|
|
|
request_state_.SetFlag(RequestStateFlags::kFinished);
|
2016-10-26 09:10:15 +00:00
|
|
|
EmitRequestEvent(true, "finish");
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(atom_request_);
|
|
|
|
if (atom_request_) {
|
|
|
|
return atom_request_->Write(buffer, is_last);
|
|
|
|
}
|
|
|
|
return false;
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
void URLRequest::Cancel() {
|
2016-10-14 09:50:47 +00:00
|
|
|
if (request_state_.Canceled() || request_state_.Closed()) {
|
2016-10-04 15:12:17 +00:00
|
|
|
// Cancel only once.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as canceled.
|
|
|
|
request_state_.SetFlag(RequestStateFlags::kCanceled);
|
|
|
|
|
2016-10-04 15:54:34 +00:00
|
|
|
DCHECK(atom_request_);
|
|
|
|
if (atom_request_ && request_state_.Started()) {
|
2016-10-04 15:12:17 +00:00
|
|
|
// Really cancel if it was started.
|
|
|
|
atom_request_->Cancel();
|
|
|
|
}
|
2016-10-26 09:10:15 +00:00
|
|
|
EmitRequestEvent(true, "abort");
|
2016-10-04 15:12:17 +00:00
|
|
|
|
2016-10-04 15:54:34 +00:00
|
|
|
if (response_state_.Started() && !response_state_.Ended()) {
|
2016-10-26 09:10:15 +00:00
|
|
|
EmitResponseEvent(true, "aborted");
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
|
|
|
Close();
|
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-10-14 08:58:16 +00:00
|
|
|
const std::string& value) {
|
2016-10-04 16:09:36 +00:00
|
|
|
// Request state must be in the initial non started state.
|
2016-10-04 15:12:17 +00:00
|
|
|
if (!request_state_.NotStarted()) {
|
|
|
|
// Cannot change headers after send.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
if (!net::HttpUtil::IsValidHeaderName(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!net::HttpUtil::IsValidHeaderValue(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:09:36 +00:00
|
|
|
DCHECK(atom_request_);
|
|
|
|
if (atom_request_) {
|
|
|
|
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) {
|
2016-10-04 15:12:17 +00:00
|
|
|
// State must be equal to not started.
|
|
|
|
if (!request_state_.NotStarted()) {
|
|
|
|
// Cannot change headers after send.
|
|
|
|
return;
|
|
|
|
}
|
2016-10-04 16:09:36 +00:00
|
|
|
DCHECK(atom_request_);
|
|
|
|
if (atom_request_) {
|
|
|
|
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) {
|
2016-10-04 15:12:17 +00:00
|
|
|
// State must be equal to not started.
|
|
|
|
if (!request_state_.NotStarted()) {
|
|
|
|
// Cannot change headers after send.
|
|
|
|
return;
|
|
|
|
}
|
2016-10-04 16:09:36 +00:00
|
|
|
DCHECK(atom_request_);
|
|
|
|
if (atom_request_) {
|
|
|
|
atom_request_->SetChunkedUpload(is_chunked_upload);
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 20:01:32 +00:00
|
|
|
void URLRequest::SetLoadFlags(int flags) {
|
|
|
|
// State must be equal to not started.
|
|
|
|
if (!request_state_.NotStarted()) {
|
|
|
|
// Cannot change load flags after start.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DCHECK(atom_request_);
|
|
|
|
if (atom_request_) {
|
|
|
|
atom_request_->SetLoadFlags(flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void URLRequest::OnAuthenticationRequired(
|
2016-10-14 09:50:47 +00:00
|
|
|
scoped_refptr<const net::AuthChallengeInfo> auth_info) {
|
|
|
|
if (request_state_.Canceled() || request_state_.Closed()) {
|
2016-10-04 16:09:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(atom_request_);
|
|
|
|
if (!atom_request_) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-12 10:29:25 +00:00
|
|
|
|
2016-10-20 09:43:26 +00:00
|
|
|
Emit("login", auth_info.get(),
|
|
|
|
base::Bind(&AtomURLRequest::PassLoginInformation, atom_request_));
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
2016-09-29 09:31:08 +00:00
|
|
|
|
2016-10-04 15:33:34 +00:00
|
|
|
void URLRequest::OnResponseStarted(
|
2016-10-14 09:50:47 +00:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers) {
|
|
|
|
if (request_state_.Canceled() || request_state_.Failed() ||
|
2016-10-04 15:12:17 +00:00
|
|
|
request_state_.Closed()) {
|
|
|
|
// Don't emit any event after request cancel.
|
|
|
|
return;
|
|
|
|
}
|
2016-10-04 15:33:34 +00:00
|
|
|
response_headers_ = response_headers;
|
2016-10-04 15:12:17 +00:00
|
|
|
response_state_.SetFlag(ResponseStateFlags::kStarted);
|
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-10-14 09:50:47 +00:00
|
|
|
scoped_refptr<const net::IOBufferWithSize> buffer) {
|
|
|
|
if (request_state_.Canceled() || request_state_.Closed() ||
|
|
|
|
request_state_.Failed() || response_state_.Failed()) {
|
2016-10-04 15:54:34 +00:00
|
|
|
// In case we received an unexpected event from Chromium net,
|
|
|
|
// don't emit any data event after request cancel/error/close.
|
2016-10-04 15:12:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
if (!buffer || !buffer->data() || !buffer->size()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-06 12:14:05 +00:00
|
|
|
Emit("data", buffer);
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
void URLRequest::OnResponseCompleted() {
|
2016-10-14 09:50:47 +00:00
|
|
|
if (request_state_.Canceled() || request_state_.Closed() ||
|
|
|
|
request_state_.Failed() || response_state_.Failed()) {
|
2016-10-04 15:54:34 +00:00
|
|
|
// In case we received an unexpected event from Chromium net,
|
|
|
|
// don't emit any data event after request cancel/error/close.
|
2016-10-04 15:12:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-04 15:54:34 +00:00
|
|
|
response_state_.SetFlag(ResponseStateFlags::kEnded);
|
2016-10-06 12:14:05 +00:00
|
|
|
Emit("end");
|
2016-10-04 15:12:17 +00:00
|
|
|
Close();
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
void URLRequest::OnError(const std::string& error, bool isRequestError) {
|
2016-10-04 15:12:17 +00:00
|
|
|
auto error_object = v8::Exception::Error(mate::StringToV8(isolate(), error));
|
2016-10-14 15:37:39 +00:00
|
|
|
if (isRequestError) {
|
|
|
|
request_state_.SetFlag(RequestStateFlags::kFailed);
|
2016-10-26 09:10:15 +00:00
|
|
|
EmitRequestEvent(false, "error", error_object);
|
2016-10-14 15:37:39 +00:00
|
|
|
} else {
|
|
|
|
response_state_.SetFlag(ResponseStateFlags::kFailed);
|
2016-10-26 09:10:15 +00:00
|
|
|
EmitResponseEvent(false, "error", error_object);
|
2016-10-14 15:37:39 +00:00
|
|
|
}
|
2016-10-04 15:12:17 +00:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
int URLRequest::StatusCode() const {
|
2016-10-04 15:33:34 +00:00
|
|
|
if (response_headers_) {
|
|
|
|
return response_headers_->response_code();
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
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-10-04 15:33:34 +00:00
|
|
|
if (response_headers_) {
|
|
|
|
result = response_headers_->GetStatusText();
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
net::HttpResponseHeaders* URLRequest::RawResponseHeaders() const {
|
|
|
|
return response_headers_.get();
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
uint32_t URLRequest::ResponseHttpVersionMajor() const {
|
2016-10-04 15:33:34 +00:00
|
|
|
if (response_headers_) {
|
2016-10-14 09:50:47 +00:00
|
|
|
return response_headers_->GetHttpVersion().major_value();
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
uint32_t URLRequest::ResponseHttpVersionMinor() const {
|
2016-10-04 15:33:34 +00:00
|
|
|
if (response_headers_) {
|
|
|
|
return response_headers_->GetHttpVersion().minor_value();
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2016-09-19 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
void URLRequest::Close() {
|
2016-10-04 15:54:34 +00:00
|
|
|
if (!request_state_.Closed()) {
|
|
|
|
request_state_.SetFlag(RequestStateFlags::kClosed);
|
2016-10-04 15:12:17 +00:00
|
|
|
if (response_state_.Started()) {
|
|
|
|
// Emit a close event if we really have a response object.
|
2016-10-26 09:10:15 +00:00
|
|
|
EmitResponseEvent(true, "close");
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2016-10-26 09:10:15 +00:00
|
|
|
EmitRequestEvent(true, "close");
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2016-10-25 10:41:01 +00:00
|
|
|
Unpin();
|
2016-10-14 15:37:39 +00:00
|
|
|
if (atom_request_) {
|
|
|
|
// A request has been created in JS, used and then it ended.
|
|
|
|
// We release unneeded net resources.
|
|
|
|
atom_request_->Terminate();
|
|
|
|
}
|
2016-10-04 15:12:17 +00:00
|
|
|
atom_request_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
void URLRequest::Pin() {
|
2016-09-19 09:21:09 +00:00
|
|
|
if (wrapper_.IsEmpty()) {
|
|
|
|
wrapper_.Reset(isolate(), GetWrapper());
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
void URLRequest::Unpin() {
|
2016-09-19 09:21:09 +00:00
|
|
|
wrapper_.Reset();
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 09:10:15 +00:00
|
|
|
template <typename... Args>
|
|
|
|
void URLRequest::EmitRequestEvent(Args... args) {
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
mate::CustomEmit(isolate(), GetWrapper(), "_emitRequestEvent", args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void URLRequest::EmitResponseEvent(Args... args) {
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
mate::CustomEmit(isolate(), GetWrapper(), "_emitResponseEvent", args...);
|
|
|
|
}
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
} // namespace api
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
} // namespace atom
|