summaryrefslogtreecommitdiff
path: root/runtime/http_util.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-12-13 19:45:53 +0100
committerGitHub <noreply@github.com>2020-12-13 19:45:53 +0100
commit2e74f164b6dcf0ecbf8dd38fba9fae550d784bd0 (patch)
tree61abe8e09d5331ace5d9de529f0e2737a8e05dbb /runtime/http_util.rs
parent84ef9bd21fb48fb6b5fbc8dafc3de9f361bade3b (diff)
refactor: deno_runtime crate (#8640)
This commit moves Deno JS runtime, ops, permissions and inspector implementation to new "deno_runtime" crate located in "runtime/" directory. Details in "runtime/README.md". Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'runtime/http_util.rs')
-rw-r--r--runtime/http_util.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/runtime/http_util.rs b/runtime/http_util.rs
new file mode 100644
index 000000000..67703c214
--- /dev/null
+++ b/runtime/http_util.rs
@@ -0,0 +1,46 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+use deno_core::error::generic_error;
+use deno_core::error::AnyError;
+use deno_fetch::reqwest;
+use deno_fetch::reqwest::header::HeaderMap;
+use deno_fetch::reqwest::header::USER_AGENT;
+use deno_fetch::reqwest::redirect::Policy;
+use deno_fetch::reqwest::Client;
+use std::fs::File;
+use std::io::Read;
+
+/// Create new instance of async reqwest::Client. This client supports
+/// proxies and doesn't follow redirects.
+pub fn create_http_client(
+ user_agent: String,
+ ca_file: Option<&str>,
+) -> Result<Client, AnyError> {
+ let mut headers = HeaderMap::new();
+ headers.insert(USER_AGENT, user_agent.parse().unwrap());
+ let mut builder = Client::builder()
+ .redirect(Policy::none())
+ .default_headers(headers)
+ .use_rustls_tls();
+
+ if let Some(ca_file) = ca_file {
+ let mut buf = Vec::new();
+ File::open(ca_file)?.read_to_end(&mut buf)?;
+ let cert = reqwest::Certificate::from_pem(&buf)?;
+ builder = builder.add_root_certificate(cert);
+ }
+
+ builder
+ .build()
+ .map_err(|_| generic_error("Unable to build http client"))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn create_test_client() {
+ create_http_client("test_client".to_string(), None).unwrap();
+ }
+}