Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

add support for POST payload and better error handling #29

Merged
merged 1 commit into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions atom/browser/api/atom_api_web_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,49 +68,64 @@ WebRequest::~WebRequest() {

void WebRequest::OnURLFetchComplete(
const net::URLFetcher* source) {
std::string response;
source->GetResponseAsString(&response);
mate::Dictionary response = mate::Dictionary::CreateEmpty(isolate());
int response_code = source->GetResponseCode();
net::HttpResponseHeaders* headers = source->GetResponseHeaders();
response.Set("statusCode", response_code);

std::string body;
v8::Local<v8::Value> err = v8::Null(isolate());
if (response_code == net::URLFetcher::ResponseCode::RESPONSE_CODE_INVALID) {
base::DictionaryValue dict;
dict.SetInteger("errorCode", source->GetStatus().error());
err = mate::ConvertToV8(isolate(), dict);
} else {
response.Set("headers", source->GetResponseHeaders());
source->GetResponseAsString(&body);
}

FetchCallback callback = fetchers_[source];
callback.Run(response_code, response, headers);
// error, response, body
callback.Run(err, response, body);
fetchers_.erase(source);
}

void WebRequest::Fetch(mate::Arguments* args) {
GURL url;
if (!args->GetNext(&url)) {
args->ThrowError();
return;
}

net::URLFetcher::RequestType request_type;
if (!args->GetNext(&request_type)) {
args->ThrowError();
return;
}

net::HttpRequestHeaders* headers = nullptr;
if (!args->GetNext(&headers)) {
args->ThrowError();
args->ThrowError("invalid url parameter");
return;
}

net::URLFetcher::RequestType request_type = net::URLFetcher::RequestType::GET;
net::HttpRequestHeaders headers;
base::FilePath path;
if (!args->GetNext(&path)) {
// ignore optional argument
std::string payload;
std::string payload_content_type;
mate::Dictionary dict;
if (args->GetNext(&dict)) {
dict.Get("method", &request_type);
dict.Get("headers", &headers);
dict.Get("path", &path);
if (dict.Get("payload", &payload)) {
if (!dict.Get("payload_content_type", &payload_content_type)) {
args->ThrowError("payload_content_type is required for payload");
}
}
}

FetchCallback callback;
if (!args->GetNext(&callback)) {
args->ThrowError();
args->ThrowError("invalid callback parameter");
return;
}

net::URLFetcher* fetcher = net::URLFetcher::Create(url, request_type, this)
.release();
fetcher->SetRequestContext(browser_context_->GetRequestContext());
fetcher->SetExtraRequestHeaders(headers->ToString());
if (!payload.empty())
fetcher->SetUploadData(payload, payload_content_type);
if (!headers.IsEmpty())
fetcher->SetExtraRequestHeaders(headers.ToString());
if (!path.empty())
fetcher->SaveResponseToFileAtPath(
path,
Expand Down
20 changes: 10 additions & 10 deletions atom/browser/api/atom_api_web_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ struct Converter<net::HttpResponseHeaders*> {
};

template<>
struct Converter<net::HttpRequestHeaders*> {
struct Converter<net::HttpRequestHeaders> {
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
net::HttpRequestHeaders** out) {
std::unique_ptr<net::HttpRequestHeaders>
request_headers(new net::HttpRequestHeaders);
*out = request_headers.release();

net::HttpRequestHeaders* out) {
net::HttpRequestHeaders request_headers;
*out = request_headers;
base::DictionaryValue headers;
if (!ConvertFromV8(isolate, val, &headers))
return false;
Expand All @@ -63,13 +61,16 @@ struct Converter<net::HttpRequestHeaders*> {
if (!it.value().GetAsString(&value))
continue;

(*out)->SetHeader(it.key(), value);
out->SetHeader(it.key(), value);
}
return true;
}
};

class Dictionary;

} // namespace mate

namespace atom {

class AtomBrowserContext;
Expand All @@ -88,9 +89,8 @@ class WebRequest : public mate::TrackableObject<WebRequest>,
WebRequest(v8::Isolate* isolate, AtomBrowserContext* browser_context);
~WebRequest() override;

typedef base::Callback<void(int response_code,
const std::string& response,
net::HttpResponseHeaders* headers)> FetchCallback;
typedef base::Callback<void(
v8::Local<v8::Value>, mate::Dictionary, std::string)> FetchCallback;
void Fetch(mate::Arguments* args);
void OnURLFetchComplete(const net::URLFetcher* source) override;

Expand Down