2015-10-31 13:39:07 +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/common/native_mate_converters/net_converter.h"
|
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
#include <string>
|
2015-12-05 01:48:19 +00:00
|
|
|
#include <vector>
|
2015-11-05 14:06:36 +00:00
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
2015-12-01 04:52:22 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
2015-10-31 13:39:07 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-12-05 01:48:19 +00:00
|
|
|
#include "net/base/upload_bytes_element_reader.h"
|
|
|
|
#include "net/base/upload_data_stream.h"
|
|
|
|
#include "net/base/upload_element_reader.h"
|
|
|
|
#include "net/base/upload_file_element_reader.h"
|
2015-11-05 14:06:36 +00:00
|
|
|
#include "net/cert/x509_certificate.h"
|
2015-12-01 04:52:22 +00:00
|
|
|
#include "net/http/http_response_headers.h"
|
2015-10-31 13:39:07 +00:00
|
|
|
#include "net/url_request/url_request.h"
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Local<v8::Value> Converter<const net::URLRequest*>::ToV8(
|
|
|
|
v8::Isolate* isolate, const net::URLRequest* val) {
|
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.Set("method", val->method());
|
|
|
|
dict.Set("url", val->url().spec());
|
|
|
|
dict.Set("referrer", val->referrer());
|
2015-12-05 01:48:19 +00:00
|
|
|
const net::UploadDataStream* upload_data = val->get_upload();
|
|
|
|
if (upload_data) {
|
|
|
|
const ScopedVector<net::UploadElementReader>* readers =
|
|
|
|
upload_data->GetElementReaders();
|
|
|
|
std::vector<mate::Dictionary> upload_data_list;
|
|
|
|
upload_data_list.reserve(readers->size());
|
|
|
|
for (const auto& reader : *readers) {
|
|
|
|
auto upload_data_dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
if (reader->AsBytesReader()) {
|
|
|
|
const net::UploadBytesElementReader* bytes_reader =
|
|
|
|
reader->AsBytesReader();
|
|
|
|
auto bytes =
|
|
|
|
node::Buffer::Copy(isolate, bytes_reader->bytes(),
|
|
|
|
bytes_reader->length()).ToLocalChecked();
|
|
|
|
upload_data_dict.Set("bytes", bytes);
|
|
|
|
} else if (reader->AsFileReader()) {
|
|
|
|
const net::UploadFileElementReader* file_reader =
|
|
|
|
reader->AsFileReader();
|
|
|
|
upload_data_dict.Set("file", file_reader->path().AsUTF8Unsafe());
|
|
|
|
}
|
|
|
|
upload_data_list.push_back(upload_data_dict);
|
|
|
|
}
|
|
|
|
dict.Set("uploadData", upload_data_list);
|
|
|
|
}
|
2015-10-31 13:39:07 +00:00
|
|
|
return mate::ConvertToV8(isolate, dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Local<v8::Value> Converter<const net::AuthChallengeInfo*>::ToV8(
|
|
|
|
v8::Isolate* isolate, const net::AuthChallengeInfo* val) {
|
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.Set("isProxy", val->is_proxy);
|
|
|
|
dict.Set("scheme", val->scheme);
|
|
|
|
dict.Set("host", val->challenger.host());
|
|
|
|
dict.Set("port", static_cast<uint32_t>(val->challenger.port()));
|
|
|
|
dict.Set("realm", val->realm);
|
|
|
|
return mate::ConvertToV8(isolate, dict);
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
// static
|
|
|
|
v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
|
|
|
|
v8::Isolate* isolate, const scoped_refptr<net::X509Certificate>& val) {
|
|
|
|
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
|
|
|
std::string encoded_data;
|
|
|
|
net::X509Certificate::GetPEMEncoded(
|
|
|
|
val->os_cert_handle(), &encoded_data);
|
|
|
|
auto buffer = node::Buffer::Copy(isolate,
|
|
|
|
encoded_data.data(),
|
|
|
|
encoded_data.size()).ToLocalChecked();
|
|
|
|
dict.Set("data", buffer);
|
|
|
|
dict.Set("issuerName", val->issuer().GetDisplayName());
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
// static
|
|
|
|
bool Converter<atom::AtomNetworkDelegate::BlockingResponse>::FromV8(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
atom::AtomNetworkDelegate::BlockingResponse* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
if (!dict.Get("cancel", &(out->cancel)))
|
|
|
|
return false;
|
2015-12-09 19:16:03 +00:00
|
|
|
dict.Get("redirectURL", &(out->redirect_url));
|
2015-12-01 04:52:22 +00:00
|
|
|
base::DictionaryValue request_headers;
|
|
|
|
if (dict.Get("requestHeaders", &request_headers)) {
|
|
|
|
for (base::DictionaryValue::Iterator it(request_headers);
|
|
|
|
!it.IsAtEnd();
|
|
|
|
it.Advance()) {
|
|
|
|
std::string value;
|
|
|
|
CHECK(it.value().GetAsString(&value));
|
2015-12-09 19:16:03 +00:00
|
|
|
out->request_headers.SetHeader(it.key(), value);
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
base::DictionaryValue response_headers;
|
|
|
|
if (dict.Get("responseHeaders", &response_headers)) {
|
2015-12-09 19:16:03 +00:00
|
|
|
out->response_headers = new net::HttpResponseHeaders("");
|
2015-12-01 04:52:22 +00:00
|
|
|
for (base::DictionaryValue::Iterator it(response_headers);
|
|
|
|
!it.IsAtEnd();
|
|
|
|
it.Advance()) {
|
|
|
|
std::string value;
|
|
|
|
CHECK(it.value().GetAsString(&value));
|
2015-12-09 19:16:03 +00:00
|
|
|
out->response_headers->AddHeader(it.key() + " : " + value);
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-31 13:39:07 +00:00
|
|
|
} // namespace mate
|