2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-06-19 22:07:01 -04:00
|
|
|
use crate::deno_error;
|
|
|
|
use crate::deno_error::DenoError;
|
2019-07-10 18:53:48 -04:00
|
|
|
use deno::ErrBox;
|
2019-04-01 21:46:40 -04:00
|
|
|
#[cfg(test)]
|
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
|
|
|
|
2018-11-29 22:01:01 -05:00
|
|
|
/// Construct the next uri based on base uri and location header fragment
|
2018-11-30 03:30:49 -05:00
|
|
|
/// See <https://tools.ietf.org/html/rfc3986#section-4.2>
|
2018-11-29 22:01:01 -05:00
|
|
|
fn resolve_uri_from_location(base_uri: &Uri, location: &str) -> Uri {
|
|
|
|
if location.starts_with("http://") || location.starts_with("https://") {
|
|
|
|
// absolute uri
|
2018-11-30 03:30:49 -05:00
|
|
|
location
|
2018-11-29 22:01:01 -05:00
|
|
|
.parse::<Uri>()
|
2018-11-30 03:30:49 -05:00
|
|
|
.expect("provided redirect url should be a valid url")
|
2018-11-29 22:01:01 -05:00
|
|
|
} else if location.starts_with("//") {
|
|
|
|
// "//" authority path-abempty
|
2018-11-30 03:30:49 -05:00
|
|
|
format!("{}:{}", base_uri.scheme_part().unwrap().as_str(), location)
|
2018-11-29 22:01:01 -05:00
|
|
|
.parse::<Uri>()
|
2018-11-30 03:30:49 -05:00
|
|
|
.expect("provided redirect url should be a valid url")
|
|
|
|
} else if location.starts_with('/') {
|
2018-11-29 22:01:01 -05:00
|
|
|
// path-absolute
|
|
|
|
let mut new_uri_parts = base_uri.clone().into_parts();
|
|
|
|
new_uri_parts.path_and_query = Some(location.parse().unwrap());
|
2018-11-30 03:30:49 -05:00
|
|
|
Uri::from_parts(new_uri_parts).unwrap()
|
2018-11-29 22:01:01 -05:00
|
|
|
} else {
|
|
|
|
// assuming path-noscheme | path-empty
|
|
|
|
let mut new_uri_parts = base_uri.clone().into_parts();
|
2019-04-01 21:46:40 -04:00
|
|
|
let base_uri_path_str = base_uri.path().to_owned();
|
2019-04-09 01:15:49 -04:00
|
|
|
let segs: Vec<&str> = base_uri_path_str.rsplitn(2, '/').collect();
|
2019-04-01 21:46:40 -04:00
|
|
|
new_uri_parts.path_and_query = Some(
|
|
|
|
format!("{}/{}", segs.last().unwrap_or(&""), location)
|
|
|
|
.parse()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2018-11-30 03:30:49 -05:00
|
|
|
Uri::from_parts(new_uri_parts).unwrap()
|
2018-11-29 22:01:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:45:39 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
use crate::tokio_util;
|
|
|
|
#[cfg(test)]
|
2019-03-19 14:06:44 -04:00
|
|
|
/// Synchronously fetchs the given HTTP URL. Returns (content, media_type).
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn fetch_sync_string(
|
|
|
|
module_name: &str,
|
|
|
|
) -> Result<(String, String), ErrBox> {
|
2019-03-19 14:06:44 -04:00
|
|
|
tokio_util::block_on(fetch_string(module_name))
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
pub enum FetchOnceResult {
|
|
|
|
// (code, maybe_content_type)
|
|
|
|
Code(String, Option<String>),
|
|
|
|
Redirect(http::uri::Uri),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronously fetchs the given HTTP URL one pass only.
|
|
|
|
/// If no redirect is present and no error occurs,
|
|
|
|
/// yields Code(code, maybe_content_type).
|
|
|
|
/// If redirect occurs, does not follow and
|
|
|
|
/// yields Redirect(url).
|
|
|
|
pub fn fetch_string_once(
|
|
|
|
url: http::uri::Uri,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> impl Future<Item = FetchOnceResult, Error = ErrBox> {
|
2019-04-01 21:46:40 -04:00
|
|
|
type FetchAttempt = (Option<String>, Option<String>, Option<FetchOnceResult>);
|
|
|
|
let client = get_client();
|
|
|
|
client
|
|
|
|
.get(url.clone())
|
2019-07-10 18:53:48 -04:00
|
|
|
.map_err(ErrBox::from)
|
2019-04-09 01:15:49 -04:00
|
|
|
.and_then(
|
|
|
|
move |response| -> Box<
|
2019-07-10 18:53:48 -04:00
|
|
|
dyn Future<Item = FetchAttempt, Error = ErrBox> + Send,
|
2019-04-09 01:15:49 -04:00
|
|
|
> {
|
|
|
|
if response.status().is_redirection() {
|
|
|
|
let location_string = response
|
|
|
|
.headers()
|
|
|
|
.get("location")
|
|
|
|
.expect("url redirection should provide 'location' header")
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
debug!("Redirecting to {}...", &location_string);
|
|
|
|
let new_url = resolve_uri_from_location(&url, &location_string);
|
|
|
|
// Boxed trait object turns out to be the savior for 2+ types yielding same results.
|
|
|
|
return Box::new(future::ok(None).join3(
|
2019-04-01 21:46:40 -04:00
|
|
|
future::ok(None),
|
2019-04-09 01:15:49 -04:00
|
|
|
future::ok(Some(FetchOnceResult::Redirect(new_url))),
|
2019-04-01 21:46:40 -04:00
|
|
|
));
|
2019-04-09 01:15:49 -04:00
|
|
|
} else if response.status().is_client_error()
|
|
|
|
|| response.status().is_server_error()
|
|
|
|
{
|
2019-07-10 18:53:48 -04:00
|
|
|
return Box::new(future::err(DenoError::new(
|
2019-06-19 22:07:01 -04:00
|
|
|
deno_error::ErrorKind::Other,
|
2019-04-09 01:15:49 -04:00
|
|
|
format!("Import '{}' failed: {}", &url, response.status()),
|
2019-07-10 18:53:48 -04:00
|
|
|
).into()));
|
2019-04-09 01:15:49 -04:00
|
|
|
}
|
|
|
|
let content_type = response
|
|
|
|
.headers()
|
|
|
|
.get(CONTENT_TYPE)
|
|
|
|
.map(|content_type| content_type.to_str().unwrap().to_owned());
|
|
|
|
let body = response
|
|
|
|
.into_body()
|
|
|
|
.concat2()
|
|
|
|
.map(|body| String::from_utf8(body.to_vec()).ok())
|
2019-07-10 18:53:48 -04:00
|
|
|
.map_err(ErrBox::from);
|
2019-04-09 01:15:49 -04:00
|
|
|
Box::new(body.join3(future::ok(content_type), future::ok(None)))
|
|
|
|
},
|
|
|
|
)
|
2019-04-01 21:46:40 -04:00
|
|
|
.and_then(move |(maybe_code, maybe_content_type, maybe_redirect)| {
|
|
|
|
if let Some(redirect) = maybe_redirect {
|
|
|
|
future::ok(redirect)
|
|
|
|
} else {
|
|
|
|
// maybe_code should always contain code here!
|
2019-04-09 01:15:49 -04:00
|
|
|
future::ok(FetchOnceResult::Code(
|
|
|
|
maybe_code.unwrap(),
|
|
|
|
maybe_content_type,
|
|
|
|
))
|
2019-04-01 21:46:40 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-03-19 14:06:44 -04:00
|
|
|
/// Asynchronously fetchs the given HTTP URL. Returns (content, media_type).
|
|
|
|
pub fn fetch_string(
|
|
|
|
module_name: &str,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> impl Future<Item = (String, String), Error = ErrBox> {
|
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
|
2019-03-19 14:06:44 -04:00
|
|
|
loop_fn((client, url), |(client, url)| {
|
2018-10-09 20:31:06 -04:00
|
|
|
client
|
2018-11-29 22:01:01 -05:00
|
|
|
.get(url.clone())
|
2019-07-10 18:53:48 -04:00
|
|
|
.map_err(ErrBox::from)
|
2018-11-29 22:01:01 -05:00
|
|
|
.and_then(move |response| {
|
2018-10-09 20:31:06 -04:00
|
|
|
if response.status().is_redirection() {
|
2018-11-29 22:01:01 -05:00
|
|
|
let location_string = response
|
2018-10-09 20:31:06 -04:00
|
|
|
.headers()
|
|
|
|
.get("location")
|
|
|
|
.expect("url redirection should provide 'location' header")
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
2018-11-29 22:01:01 -05:00
|
|
|
debug!("Redirecting to {}...", &location_string);
|
|
|
|
let new_url = resolve_uri_from_location(&url, &location_string);
|
|
|
|
return Ok(Loop::Continue((client, new_url)));
|
2018-10-09 20:31:06 -04:00
|
|
|
}
|
|
|
|
if !response.status().is_success() {
|
2019-07-10 18:53:48 -04:00
|
|
|
return Err(
|
|
|
|
DenoError::new(
|
|
|
|
deno_error::ErrorKind::NotFound,
|
|
|
|
"module not found".to_string(),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.into(),
|
2019-07-10 18:53:48 -04:00
|
|
|
);
|
2018-10-09 20:31:06 -04:00
|
|
|
}
|
|
|
|
Ok(Loop::Break(response))
|
|
|
|
})
|
2019-07-31 17:11:37 -04:00
|
|
|
})
|
|
|
|
.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()
|
2019-01-03 22:11:01 -05:00
|
|
|
.map(|body| String::from_utf8(body.to_vec()).unwrap())
|
2019-07-10 18:53:48 -04:00
|
|
|
.map_err(ErrBox::from);
|
2018-10-21 22:14:27 -04:00
|
|
|
body.join(future::ok(content_type))
|
2019-07-31 17:11:37 -04:00
|
|
|
})
|
|
|
|
.and_then(|(body_string, maybe_content_type)| {
|
2019-01-03 22:11:01 -05:00
|
|
|
future::ok((body_string, maybe_content_type.unwrap()))
|
2019-03-19 14:06:44 -04:00
|
|
|
})
|
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
|
|
|
|
2019-03-19 14:06:44 -04:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_string() {
|
|
|
|
// Relies on external http server. See tools/http_server.py
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let (p, m) = fetch_string("http://127.0.0.1:4545/package.json")
|
|
|
|
.wait()
|
|
|
|
.unwrap();
|
|
|
|
println!("package.json len {}", p.len());
|
|
|
|
assert!(p.len() > 1);
|
|
|
|
assert!(m == "application/json")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_uri_from_location_full_1() {
|
|
|
|
let url = "http://deno.land".parse::<Uri>().unwrap();
|
|
|
|
let new_uri = resolve_uri_from_location(&url, "http://golang.org");
|
|
|
|
assert_eq!(new_uri.host().unwrap(), "golang.org");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_uri_from_location_full_2() {
|
|
|
|
let url = "https://deno.land".parse::<Uri>().unwrap();
|
|
|
|
let new_uri = resolve_uri_from_location(&url, "https://golang.org");
|
|
|
|
assert_eq!(new_uri.host().unwrap(), "golang.org");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_uri_from_location_relative_1() {
|
|
|
|
let url = "http://deno.land/x".parse::<Uri>().unwrap();
|
|
|
|
let new_uri = resolve_uri_from_location(&url, "//rust-lang.org/en-US");
|
|
|
|
assert_eq!(new_uri.host().unwrap(), "rust-lang.org");
|
|
|
|
assert_eq!(new_uri.path(), "/en-US");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_uri_from_location_relative_2() {
|
|
|
|
let url = "http://deno.land/x".parse::<Uri>().unwrap();
|
|
|
|
let new_uri = resolve_uri_from_location(&url, "/y");
|
|
|
|
assert_eq!(new_uri.host().unwrap(), "deno.land");
|
|
|
|
assert_eq!(new_uri.path(), "/y");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_uri_from_location_relative_3() {
|
|
|
|
let url = "http://deno.land/x".parse::<Uri>().unwrap();
|
|
|
|
let new_uri = resolve_uri_from_location(&url, "z");
|
|
|
|
assert_eq!(new_uri.host().unwrap(), "deno.land");
|
2019-04-01 21:46:40 -04:00
|
|
|
assert_eq!(new_uri.path(), "/z");
|
2018-11-29 22:01:01 -05:00
|
|
|
}
|