net_converter.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/common/native_mate_converters/net_converter.h"
  5. #include <string>
  6. #include <vector>
  7. #include "atom/common/native_mate_converters/gurl_converter.h"
  8. #include "atom/common/native_mate_converters/value_converter.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/values.h"
  12. #include "native_mate/dictionary.h"
  13. #include "net/base/upload_bytes_element_reader.h"
  14. #include "net/base/upload_data_stream.h"
  15. #include "net/base/upload_element_reader.h"
  16. #include "net/base/upload_file_element_reader.h"
  17. #include "net/cert/x509_certificate.h"
  18. #include "net/http/http_response_headers.h"
  19. #include "net/url_request/url_request.h"
  20. #include "storage/browser/blob/upload_blob_element_reader.h"
  21. #include "atom/common/node_includes.h"
  22. namespace mate {
  23. namespace {
  24. bool CertFromData(const std::string& data,
  25. scoped_refptr<net::X509Certificate>* out) {
  26. auto cert_list = net::X509Certificate::CreateCertificateListFromBytes(
  27. data.c_str(), data.length(),
  28. net::X509Certificate::FORMAT_SINGLE_CERTIFICATE);
  29. if (cert_list.empty())
  30. return false;
  31. auto leaf_cert = cert_list.front();
  32. if (!leaf_cert)
  33. return false;
  34. *out = leaf_cert;
  35. return true;
  36. }
  37. } // namespace
  38. // static
  39. v8::Local<v8::Value> Converter<const net::AuthChallengeInfo*>::ToV8(
  40. v8::Isolate* isolate,
  41. const net::AuthChallengeInfo* val) {
  42. mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
  43. dict.Set("isProxy", val->is_proxy);
  44. dict.Set("scheme", val->scheme);
  45. dict.Set("host", val->challenger.host());
  46. dict.Set("port", static_cast<uint32_t>(val->challenger.port()));
  47. dict.Set("realm", val->realm);
  48. return mate::ConvertToV8(isolate, dict);
  49. }
  50. // static
  51. v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
  52. v8::Isolate* isolate,
  53. const scoped_refptr<net::X509Certificate>& val) {
  54. mate::Dictionary dict(isolate, v8::Object::New(isolate));
  55. std::string encoded_data;
  56. net::X509Certificate::GetPEMEncoded(val->os_cert_handle(), &encoded_data);
  57. dict.Set("data", encoded_data);
  58. dict.Set("issuer", val->issuer());
  59. dict.Set("issuerName", val->issuer().GetDisplayName());
  60. dict.Set("subject", val->subject());
  61. dict.Set("subjectName", val->subject().GetDisplayName());
  62. dict.Set("serialNumber", base::HexEncode(val->serial_number().data(),
  63. val->serial_number().size()));
  64. dict.Set("validStart", val->valid_start().ToDoubleT());
  65. dict.Set("validExpiry", val->valid_expiry().ToDoubleT());
  66. dict.Set("fingerprint",
  67. net::HashValue(val->CalculateFingerprint256(val->os_cert_handle()))
  68. .ToString());
  69. if (!val->GetIntermediateCertificates().empty()) {
  70. net::X509Certificate::OSCertHandles issuer_intermediates(
  71. val->GetIntermediateCertificates().begin() + 1,
  72. val->GetIntermediateCertificates().end());
  73. const scoped_refptr<net::X509Certificate>& issuer_cert =
  74. net::X509Certificate::CreateFromHandle(
  75. val->GetIntermediateCertificates().front(), issuer_intermediates);
  76. dict.Set("issuerCert", issuer_cert);
  77. }
  78. return dict.GetHandle();
  79. }
  80. bool Converter<scoped_refptr<net::X509Certificate>>::FromV8(
  81. v8::Isolate* isolate,
  82. v8::Local<v8::Value> val,
  83. scoped_refptr<net::X509Certificate>* out) {
  84. mate::Dictionary dict;
  85. if (!ConvertFromV8(isolate, val, &dict))
  86. return false;
  87. std::string data;
  88. dict.Get("data", &data);
  89. scoped_refptr<net::X509Certificate> leaf_cert;
  90. if (!CertFromData(data, &leaf_cert))
  91. return false;
  92. scoped_refptr<net::X509Certificate> parent;
  93. if (dict.Get("issuerCert", &parent)) {
  94. auto parents = std::vector<net::X509Certificate::OSCertHandle>(
  95. parent->GetIntermediateCertificates());
  96. parents.insert(parents.begin(), parent->os_cert_handle());
  97. auto cert = net::X509Certificate::CreateFromHandle(
  98. leaf_cert->os_cert_handle(), parents);
  99. if (!cert)
  100. return false;
  101. *out = cert;
  102. } else {
  103. *out = leaf_cert;
  104. }
  105. return true;
  106. }
  107. // static
  108. v8::Local<v8::Value> Converter<net::CertPrincipal>::ToV8(
  109. v8::Isolate* isolate,
  110. const net::CertPrincipal& val) {
  111. mate::Dictionary dict(isolate, v8::Object::New(isolate));
  112. dict.Set("commonName", val.common_name);
  113. dict.Set("organizations", val.organization_names);
  114. dict.Set("organizationUnits", val.organization_unit_names);
  115. dict.Set("locality", val.locality_name);
  116. dict.Set("state", val.state_or_province_name);
  117. dict.Set("country", val.country_name);
  118. return dict.GetHandle();
  119. }
  120. // static
  121. v8::Local<v8::Value> Converter<net::HttpResponseHeaders*>::ToV8(
  122. v8::Isolate* isolate,
  123. net::HttpResponseHeaders* headers) {
  124. base::DictionaryValue response_headers;
  125. if (headers) {
  126. size_t iter = 0;
  127. std::string key;
  128. std::string value;
  129. while (headers->EnumerateHeaderLines(&iter, &key, &value)) {
  130. key = base::ToLowerASCII(key);
  131. if (response_headers.HasKey(key)) {
  132. base::ListValue* values = nullptr;
  133. if (response_headers.GetList(key, &values))
  134. values->AppendString(value);
  135. } else {
  136. std::unique_ptr<base::ListValue> values(new base::ListValue());
  137. values->AppendString(value);
  138. response_headers.Set(key, std::move(values));
  139. }
  140. }
  141. }
  142. return ConvertToV8(isolate, response_headers);
  143. }
  144. bool Converter<net::HttpResponseHeaders*>::FromV8(
  145. v8::Isolate* isolate,
  146. v8::Local<v8::Value> val,
  147. net::HttpResponseHeaders* out) {
  148. if (!val->IsObject()) {
  149. return false;
  150. }
  151. auto context = isolate->GetCurrentContext();
  152. auto headers = v8::Local<v8::Object>::Cast(val);
  153. auto keys = headers->GetOwnPropertyNames();
  154. for (uint32_t i = 0; i < keys->Length(); i++) {
  155. v8::Local<v8::String> key, value;
  156. if (!keys->Get(i)->ToString(context).ToLocal(&key)) {
  157. return false;
  158. }
  159. if (!headers->Get(key)->ToString(context).ToLocal(&value)) {
  160. return false;
  161. }
  162. v8::String::Utf8Value key_utf8(key);
  163. v8::String::Utf8Value value_utf8(value);
  164. std::string k(*key_utf8, key_utf8.length());
  165. std::string v(*value_utf8, value_utf8.length());
  166. std::ostringstream tmp;
  167. tmp << k << ": " << v;
  168. out->AddHeader(tmp.str());
  169. }
  170. return true;
  171. }
  172. } // namespace mate
  173. namespace atom {
  174. void FillRequestDetails(base::DictionaryValue* details,
  175. const net::URLRequest* request) {
  176. details->SetString("method", request->method());
  177. std::string url;
  178. if (!request->url_chain().empty())
  179. url = request->url().spec();
  180. details->SetKey("url", base::Value(url));
  181. details->SetString("referrer", request->referrer());
  182. std::unique_ptr<base::ListValue> list(new base::ListValue);
  183. GetUploadData(list.get(), request);
  184. if (!list->empty())
  185. details->Set("uploadData", std::move(list));
  186. std::unique_ptr<base::DictionaryValue> headers_value(
  187. new base::DictionaryValue);
  188. for (net::HttpRequestHeaders::Iterator it(request->extra_request_headers());
  189. it.GetNext();) {
  190. headers_value->SetString(it.name(), it.value());
  191. }
  192. details->Set("headers", std::move(headers_value));
  193. }
  194. void GetUploadData(base::ListValue* upload_data_list,
  195. const net::URLRequest* request) {
  196. const net::UploadDataStream* upload_data = request->get_upload();
  197. if (!upload_data)
  198. return;
  199. const std::vector<std::unique_ptr<net::UploadElementReader>>* readers =
  200. upload_data->GetElementReaders();
  201. for (const auto& reader : *readers) {
  202. std::unique_ptr<base::DictionaryValue> upload_data_dict(
  203. new base::DictionaryValue);
  204. if (reader->AsBytesReader()) {
  205. const net::UploadBytesElementReader* bytes_reader =
  206. reader->AsBytesReader();
  207. std::unique_ptr<base::Value> bytes(base::Value::CreateWithCopiedBuffer(
  208. bytes_reader->bytes(), bytes_reader->length()));
  209. upload_data_dict->Set("bytes", std::move(bytes));
  210. } else if (reader->AsFileReader()) {
  211. const net::UploadFileElementReader* file_reader = reader->AsFileReader();
  212. auto file_path = file_reader->path().AsUTF8Unsafe();
  213. upload_data_dict->SetKey("file", base::Value(file_path));
  214. } else {
  215. const storage::UploadBlobElementReader* blob_reader =
  216. static_cast<storage::UploadBlobElementReader*>(reader.get());
  217. upload_data_dict->SetString("blobUUID", blob_reader->uuid());
  218. }
  219. upload_data_list->Append(std::move(upload_data_dict));
  220. }
  221. }
  222. } // namespace atom