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"
|
2016-01-26 08:28:21 +00:00
|
|
|
#include "base/values.h"
|
2016-07-19 03:06:17 +00:00
|
|
|
#include "base/strings/string_number_conversions.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::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);
|
2016-08-10 21:47:05 +00:00
|
|
|
dict.Set("data", encoded_data);
|
2015-11-05 14:06:36 +00:00
|
|
|
dict.Set("issuerName", val->issuer().GetDisplayName());
|
2016-07-12 17:05:28 +00:00
|
|
|
dict.Set("subjectName", val->subject().GetDisplayName());
|
2016-07-19 03:06:17 +00:00
|
|
|
dict.Set("serialNumber", base::HexEncode(val->serial_number().data(),
|
|
|
|
val->serial_number().size()));
|
2016-07-12 17:05:28 +00:00
|
|
|
dict.Set("validStart", val->valid_start().ToDoubleT());
|
|
|
|
dict.Set("validExpiry", val->valid_expiry().ToDoubleT());
|
2016-07-14 11:09:11 +00:00
|
|
|
dict.Set("fingerprint",
|
|
|
|
net::HashValue(
|
|
|
|
val->CalculateFingerprint256(val->os_cert_handle())).ToString());
|
2016-07-12 17:05:28 +00:00
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
|
2015-10-31 13:39:07 +00:00
|
|
|
} // namespace mate
|
2016-01-26 08:28:21 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2016-06-08 13:52:21 +00:00
|
|
|
void FillRequestDetails(base::DictionaryValue* details,
|
|
|
|
const net::URLRequest* request) {
|
|
|
|
details->SetString("method", request->method());
|
|
|
|
std::string url;
|
|
|
|
if (!request->url_chain().empty()) url = request->url().spec();
|
|
|
|
details->SetStringWithoutPathExpansion("url", url);
|
|
|
|
details->SetString("referrer", request->referrer());
|
|
|
|
std::unique_ptr<base::ListValue> list(new base::ListValue);
|
|
|
|
GetUploadData(list.get(), request);
|
|
|
|
if (!list->empty())
|
|
|
|
details->Set("uploadData", std::move(list));
|
|
|
|
}
|
|
|
|
|
2016-01-26 08:28:21 +00:00
|
|
|
void GetUploadData(base::ListValue* upload_data_list,
|
|
|
|
const net::URLRequest* request) {
|
|
|
|
const net::UploadDataStream* upload_data = request->get_upload();
|
|
|
|
if (!upload_data)
|
|
|
|
return;
|
2016-05-23 01:59:39 +00:00
|
|
|
const std::vector<std::unique_ptr<net::UploadElementReader>>* readers =
|
2016-01-26 08:28:21 +00:00
|
|
|
upload_data->GetElementReaders();
|
|
|
|
for (const auto& reader : *readers) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> upload_data_dict(
|
2016-01-26 08:28:21 +00:00
|
|
|
new base::DictionaryValue);
|
|
|
|
if (reader->AsBytesReader()) {
|
|
|
|
const net::UploadBytesElementReader* bytes_reader =
|
|
|
|
reader->AsBytesReader();
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::Value> bytes(
|
2016-01-26 08:28:21 +00:00
|
|
|
base::BinaryValue::CreateWithCopiedBuffer(bytes_reader->bytes(),
|
|
|
|
bytes_reader->length()));
|
2016-03-08 14:28:53 +00:00
|
|
|
upload_data_dict->Set("bytes", std::move(bytes));
|
2016-01-26 08:28:21 +00:00
|
|
|
} else if (reader->AsFileReader()) {
|
|
|
|
const net::UploadFileElementReader* file_reader =
|
|
|
|
reader->AsFileReader();
|
|
|
|
auto file_path = file_reader->path().AsUTF8Unsafe();
|
|
|
|
upload_data_dict->SetStringWithoutPathExpansion("file", file_path);
|
|
|
|
}
|
2016-03-08 14:28:53 +00:00
|
|
|
upload_data_list->Append(std::move(upload_data_dict));
|
2016-01-26 08:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|