2018-09-24 19:51:37 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-10-02 12:38:45 -04:00
|
|
|
use errors;
|
|
|
|
use errors::{DenoError, DenoResult};
|
2018-09-18 14:53:16 -04:00
|
|
|
use tokio_util;
|
2018-09-24 19:51:37 -04:00
|
|
|
|
2018-10-09 20:31:06 -04:00
|
|
|
use futures::future::{loop_fn, Loop};
|
2018-10-21 22:14:27 -04:00
|
|
|
use futures::{future, Future, Stream};
|
2018-08-28 13:49:19 -04:00
|
|
|
use hyper;
|
2018-10-09 20:31:06 -04:00
|
|
|
use hyper::client::{Client, HttpConnector};
|
2018-10-21 22:14:27 -04:00
|
|
|
use hyper::header::CONTENT_TYPE;
|
2018-09-24 19:51:37 -04:00
|
|
|
use hyper::Uri;
|
2018-08-28 13:49:19 -04:00
|
|
|
use hyper_rustls;
|
2018-09-24 19:51:37 -04:00
|
|
|
|
|
|
|
type Connector = hyper_rustls::HttpsConnector<HttpConnector>;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref CONNECTOR: Connector = {
|
|
|
|
let num_dns_threads = 4;
|
|
|
|
Connector::new(num_dns_threads)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_client() -> Client<Connector, hyper::Body> {
|
|
|
|
// TODO use Hyper's connection pool.
|
|
|
|
let c = CONNECTOR.clone();
|
|
|
|
Client::builder().build(c)
|
|
|
|
}
|
2018-08-14 16:50:53 -04:00
|
|
|
|
|
|
|
// The CodeFetch message is used to load HTTP javascript resources and expects a
|
|
|
|
// synchronous response, this utility method supports that.
|
2018-10-21 22:14:27 -04:00
|
|
|
pub fn fetch_sync_string(module_name: &str) -> DenoResult<(String, String)> {
|
2018-08-14 16:50:53 -04:00
|
|
|
let url = module_name.parse::<Uri>().unwrap();
|
2018-09-24 19:51:37 -04:00
|
|
|
let client = get_client();
|
2018-10-09 20:31:06 -04:00
|
|
|
// 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)
|
2018-11-04 09:04:24 -05:00
|
|
|
.map_err(DenoError::from)
|
2018-10-09 20:31:06 -04:00
|
|
|
.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::<Uri>()
|
|
|
|
.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| {
|
2018-10-21 22:14:27 -04:00
|
|
|
let content_type = response
|
|
|
|
.headers()
|
|
|
|
.get(CONTENT_TYPE)
|
|
|
|
.map(|content_type| content_type.to_str().unwrap().to_string());
|
|
|
|
let body = response
|
2018-10-09 20:31:06 -04:00
|
|
|
.into_body()
|
|
|
|
.concat2()
|
|
|
|
.map(|body| String::from_utf8(body.to_vec()).unwrap())
|
2018-11-04 09:04:24 -05:00
|
|
|
.map_err(DenoError::from);
|
2018-10-21 22:14:27 -04:00
|
|
|
body.join(future::ok(content_type))
|
|
|
|
}).and_then(|(body_string, maybe_content_type)| {
|
|
|
|
future::ok((body_string, maybe_content_type.unwrap()))
|
2018-10-09 20:31:06 -04:00
|
|
|
});
|
|
|
|
|
2018-10-02 12:38:45 -04:00
|
|
|
tokio_util::block_on(fetch_future)
|
2018-08-14 16:50:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_sync_string() {
|
|
|
|
// Relies on external http server. See tools/http_server.py
|
2018-09-18 14:53:16 -04:00
|
|
|
tokio_util::init(|| {
|
2018-10-21 22:14:27 -04:00
|
|
|
let (p, m) =
|
|
|
|
fetch_sync_string("http://127.0.0.1:4545/package.json").unwrap();
|
2018-10-09 13:11:10 -04:00
|
|
|
println!("package.json len {}", p.len());
|
|
|
|
assert!(p.len() > 1);
|
2018-10-21 22:14:27 -04:00
|
|
|
assert!(m == "application/json")
|
2018-09-18 14:53:16 -04:00
|
|
|
});
|
2018-08-14 16:50:53 -04:00
|
|
|
}
|
2018-10-09 20:31:06 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_sync_string_with_redirect() {
|
|
|
|
// Relies on external http server. See tools/http_server.py
|
|
|
|
tokio_util::init(|| {
|
2018-10-21 22:14:27 -04:00
|
|
|
let (p, m) =
|
|
|
|
fetch_sync_string("http://127.0.0.1:4546/package.json").unwrap();
|
2018-10-09 20:31:06 -04:00
|
|
|
println!("package.json len {}", p.len());
|
|
|
|
assert!(p.len() > 1);
|
2018-10-21 22:14:27 -04:00
|
|
|
assert!(m == "application/json")
|
2018-10-09 20:31:06 -04:00
|
|
|
});
|
|
|
|
}
|