From 417ea7bb6d14ea18b9b4caefe1f4ca2b12216d3e Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 10 Oct 2018 13:35:10 -0400 Subject: src/http.rs -> src/http_util.rs So as not to conflict with http crate. --- src/deno_dir.rs | 4 +-- src/http.rs | 95 -------------------------------------------------------- src/http_util.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- 4 files changed, 98 insertions(+), 98 deletions(-) delete mode 100644 src/http.rs create mode 100644 src/http_util.rs (limited to 'src') diff --git a/src/deno_dir.rs b/src/deno_dir.rs index 1e369b40f..e8cc86b18 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -4,7 +4,7 @@ use errors::DenoError; use errors::DenoResult; use errors::ErrorKind; use fs as deno_fs; -use http; +use http_util; use ring; use std; use std::fmt::Write; @@ -114,7 +114,7 @@ impl DenoDir { let src = if self.reload || !p.exists() { println!("Downloading {}", module_name); - let source = http::fetch_sync_string(module_name)?; + let source = http_util::fetch_sync_string(module_name)?; match p.parent() { Some(ref parent) => fs::create_dir_all(parent), None => Ok(()), diff --git a/src/http.rs b/src/http.rs deleted file mode 100644 index 0d3189d27..000000000 --- a/src/http.rs +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 2018 the Deno authors. All rights reserved. MIT license. - -use errors; -use errors::{DenoError, DenoResult}; -use tokio_util; - -use futures::future::{loop_fn, Loop}; -use futures::{Future, Stream}; -use hyper; -use hyper::client::{Client, HttpConnector}; -use hyper::Uri; -use hyper_rustls; - -type Connector = hyper_rustls::HttpsConnector; - -lazy_static! { - static ref CONNECTOR: Connector = { - let num_dns_threads = 4; - Connector::new(num_dns_threads) - }; -} - -pub fn get_client() -> Client { - // TODO use Hyper's connection pool. - let c = CONNECTOR.clone(); - Client::builder().build(c) -} - -// The CodeFetch message is used to load HTTP javascript resources and expects a -// synchronous response, this utility method supports that. -pub fn fetch_sync_string(module_name: &str) -> DenoResult { - let url = module_name.parse::().unwrap(); - let client = get_client(); - // TODO(kevinkassimo): consider set a max redirection counter - // to avoid bouncing between 2 or more urls - let fetch_future = loop_fn((client, Some(url)), |(client, maybe_url)| { - let url = maybe_url.expect("target url should not be None"); - client - .get(url) - .map_err(|err| DenoError::from(err)) - .and_then(|response| { - if response.status().is_redirection() { - let new_url_string = response - .headers() - .get("location") - .expect("url redirection should provide 'location' header") - .to_str() - .unwrap() - .to_string(); - debug!("Redirecting to {}...", &new_url_string); - let maybe_new_url = Some( - new_url_string - .parse::() - .expect("provided redirect url should be a valid url"), - ); - return Ok(Loop::Continue((client, maybe_new_url))); - } - if !response.status().is_success() { - return Err(errors::new( - errors::ErrorKind::NotFound, - "module not found".to_string(), - )); - } - Ok(Loop::Break(response)) - }) - }).and_then(|response| { - response - .into_body() - .concat2() - .map(|body| String::from_utf8(body.to_vec()).unwrap()) - .map_err(|err| DenoError::from(err)) - }); - - tokio_util::block_on(fetch_future) -} - -#[test] -fn test_fetch_sync_string() { - // Relies on external http server. See tools/http_server.py - tokio_util::init(|| { - let p = fetch_sync_string("http://127.0.0.1:4545/package.json").unwrap(); - println!("package.json len {}", p.len()); - assert!(p.len() > 1); - }); -} - -#[test] -fn test_fetch_sync_string_with_redirect() { - // Relies on external http server. See tools/http_server.py - tokio_util::init(|| { - let p = fetch_sync_string("http://127.0.0.1:4546/package.json").unwrap(); - println!("package.json len {}", p.len()); - assert!(p.len() > 1); - }); -} diff --git a/src/http_util.rs b/src/http_util.rs new file mode 100644 index 000000000..0d3189d27 --- /dev/null +++ b/src/http_util.rs @@ -0,0 +1,95 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. + +use errors; +use errors::{DenoError, DenoResult}; +use tokio_util; + +use futures::future::{loop_fn, Loop}; +use futures::{Future, Stream}; +use hyper; +use hyper::client::{Client, HttpConnector}; +use hyper::Uri; +use hyper_rustls; + +type Connector = hyper_rustls::HttpsConnector; + +lazy_static! { + static ref CONNECTOR: Connector = { + let num_dns_threads = 4; + Connector::new(num_dns_threads) + }; +} + +pub fn get_client() -> Client { + // TODO use Hyper's connection pool. + let c = CONNECTOR.clone(); + Client::builder().build(c) +} + +// The CodeFetch message is used to load HTTP javascript resources and expects a +// synchronous response, this utility method supports that. +pub fn fetch_sync_string(module_name: &str) -> DenoResult { + let url = module_name.parse::().unwrap(); + let client = get_client(); + // TODO(kevinkassimo): consider set a max redirection counter + // to avoid bouncing between 2 or more urls + let fetch_future = loop_fn((client, Some(url)), |(client, maybe_url)| { + let url = maybe_url.expect("target url should not be None"); + client + .get(url) + .map_err(|err| DenoError::from(err)) + .and_then(|response| { + if response.status().is_redirection() { + let new_url_string = response + .headers() + .get("location") + .expect("url redirection should provide 'location' header") + .to_str() + .unwrap() + .to_string(); + debug!("Redirecting to {}...", &new_url_string); + let maybe_new_url = Some( + new_url_string + .parse::() + .expect("provided redirect url should be a valid url"), + ); + return Ok(Loop::Continue((client, maybe_new_url))); + } + if !response.status().is_success() { + return Err(errors::new( + errors::ErrorKind::NotFound, + "module not found".to_string(), + )); + } + Ok(Loop::Break(response)) + }) + }).and_then(|response| { + response + .into_body() + .concat2() + .map(|body| String::from_utf8(body.to_vec()).unwrap()) + .map_err(|err| DenoError::from(err)) + }); + + tokio_util::block_on(fetch_future) +} + +#[test] +fn test_fetch_sync_string() { + // Relies on external http server. See tools/http_server.py + tokio_util::init(|| { + let p = fetch_sync_string("http://127.0.0.1:4545/package.json").unwrap(); + println!("package.json len {}", p.len()); + assert!(p.len() > 1); + }); +} + +#[test] +fn test_fetch_sync_string_with_redirect() { + // Relies on external http server. See tools/http_server.py + tokio_util::init(|| { + let p = fetch_sync_string("http://127.0.0.1:4546/package.json").unwrap(); + println!("package.json len {}", p.len()); + assert!(p.len() > 1); + }); +} diff --git a/src/main.rs b/src/main.rs index f6c85ef83..b84ee78d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ mod deno_dir; mod errors; mod flags; mod fs; -mod http; +mod http_util; mod isolate; mod libdeno; pub mod ops; -- cgit v1.2.3