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-09-11 07:31:00 -04:00
|
|
|
use crate::version;
|
2019-07-10 18:53:48 -04:00
|
|
|
use deno::ErrBox;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future;
|
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::future::TryFutureExt;
|
2019-04-25 13:29:21 -04:00
|
|
|
use reqwest;
|
2019-09-11 07:31:00 -04:00
|
|
|
use reqwest::header::HeaderMap;
|
2019-04-25 13:29:21 -04:00
|
|
|
use reqwest::header::CONTENT_TYPE;
|
|
|
|
use reqwest::header::LOCATION;
|
2019-09-11 07:31:00 -04:00
|
|
|
use reqwest::header::USER_AGENT;
|
2019-04-25 13:29:21 -04:00
|
|
|
use reqwest::r#async::Client;
|
|
|
|
use reqwest::RedirectPolicy;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2019-04-25 13:29:21 -04:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
/// Create new instance of async reqwest::Client. This client supports
|
|
|
|
/// proxies and doesn't follow redirects.
|
|
|
|
pub fn get_client() -> Client {
|
2019-09-11 07:31:00 -04:00
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(
|
|
|
|
USER_AGENT,
|
|
|
|
format!("Deno/{}", version::DENO).parse().unwrap(),
|
|
|
|
);
|
2019-04-25 13:29:21 -04:00
|
|
|
Client::builder()
|
|
|
|
.redirect(RedirectPolicy::none())
|
2019-09-11 07:31:00 -04:00
|
|
|
.default_headers(headers)
|
2019-04-25 13:29:21 -04:00
|
|
|
.use_sys_proxy()
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
2018-09-24 19:51:37 -04:00
|
|
|
}
|
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>
|
2019-04-25 13:29:21 -04:00
|
|
|
fn resolve_url_from_location(base_url: &Url, location: &str) -> Url {
|
2018-11-29 22:01:01 -05:00
|
|
|
if location.starts_with("http://") || location.starts_with("https://") {
|
|
|
|
// absolute uri
|
2019-04-25 13:29:21 -04:00
|
|
|
Url::parse(location).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
|
2019-04-25 13:29:21 -04:00
|
|
|
Url::parse(&format!("{}:{}", base_url.scheme(), location))
|
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
|
2019-04-25 13:29:21 -04:00
|
|
|
base_url
|
|
|
|
.join(location)
|
|
|
|
.expect("provided redirect url should be a valid url")
|
2018-11-29 22:01:01 -05:00
|
|
|
} else {
|
|
|
|
// assuming path-noscheme | path-empty
|
2019-04-25 13:29:21 -04:00
|
|
|
let base_url_path_str = base_url.path().to_owned();
|
|
|
|
// Pop last part or url (after last slash)
|
|
|
|
let segs: Vec<&str> = base_url_path_str.rsplitn(2, '/').collect();
|
|
|
|
let new_path = format!("{}/{}", segs.last().unwrap_or(&""), location);
|
|
|
|
base_url
|
|
|
|
.join(&new_path)
|
|
|
|
.expect("provided redirect url should be a valid url")
|
2018-11-29 22:01:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[derive(Debug, PartialEq)]
|
2019-04-01 21:46:40 -04:00
|
|
|
pub enum FetchOnceResult {
|
|
|
|
// (code, maybe_content_type)
|
|
|
|
Code(String, Option<String>),
|
2019-04-25 13:29:21 -04:00
|
|
|
Redirect(Url),
|
2019-04-01 21:46:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(
|
2019-04-25 13:29:21 -04:00
|
|
|
url: &Url,
|
2019-11-16 19:17:47 -05:00
|
|
|
) -> impl Future<Output = Result<FetchOnceResult, ErrBox>> {
|
2019-04-01 21:46:40 -04:00
|
|
|
type FetchAttempt = (Option<String>, Option<String>, Option<FetchOnceResult>);
|
2019-04-25 13:29:21 -04:00
|
|
|
|
|
|
|
let url = url.clone();
|
2019-04-01 21:46:40 -04:00
|
|
|
let client = get_client();
|
2019-04-25 13:29:21 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::compat::Compat01As03::new(client.get(url.clone()).send())
|
2019-07-10 18:53:48 -04:00
|
|
|
.map_err(ErrBox::from)
|
2019-04-09 01:15:49 -04:00
|
|
|
.and_then(
|
2019-11-16 19:17:47 -05:00
|
|
|
move |mut response| -> Pin<
|
|
|
|
Box<dyn Future<Output = Result<FetchAttempt, ErrBox>> + Send>,
|
2019-04-09 01:15:49 -04:00
|
|
|
> {
|
|
|
|
if response.status().is_redirection() {
|
2019-11-16 19:17:47 -05:00
|
|
|
let location_string = response
|
|
|
|
.headers()
|
2019-04-25 13:29:21 -04:00
|
|
|
.get(LOCATION)
|
2019-04-09 01:15:49 -04:00
|
|
|
.expect("url redirection should provide 'location' header")
|
|
|
|
.to_str()
|
2019-04-25 13:29:21 -04:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
debug!("Redirecting to {:?}...", &location_string);
|
|
|
|
let new_url = resolve_url_from_location(&url, location_string);
|
2019-04-09 01:15:49 -04:00
|
|
|
// Boxed trait object turns out to be the savior for 2+ types yielding same results.
|
2019-11-16 19:17:47 -05:00
|
|
|
return futures::future::try_join3(
|
|
|
|
future::ok(None),
|
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-11-16 19:17:47 -05:00
|
|
|
)
|
|
|
|
.boxed();
|
2019-04-25 13:29:21 -04:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
if response.status().is_client_error()
|
|
|
|
|| response.status().is_server_error()
|
|
|
|
{
|
|
|
|
return future::err(
|
|
|
|
DenoError::new(
|
|
|
|
deno_error::ErrorKind::Other,
|
|
|
|
format!("Import '{}' failed: {}", &url, response.status()),
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
.boxed();
|
2019-04-09 01:15:49 -04:00
|
|
|
}
|
2019-04-25 13:29:21 -04:00
|
|
|
|
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());
|
2019-04-25 13:29:21 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let body = futures::compat::Compat01As03::new(response.text())
|
|
|
|
.map_ok(Some)
|
2019-07-10 18:53:48 -04:00
|
|
|
.map_err(ErrBox::from);
|
2019-04-25 13:29:21 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::future::try_join3(
|
|
|
|
body,
|
|
|
|
future::ok(content_type),
|
|
|
|
future::ok(None),
|
2019-04-25 13:29:21 -04:00
|
|
|
)
|
2019-11-16 19:17:47 -05:00
|
|
|
.boxed()
|
|
|
|
},
|
2019-04-09 01:15:49 -04:00
|
|
|
)
|
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-04-25 13:29:21 -04:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::tokio_util;
|
2018-08-14 16:50:53 -04:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_sync_string() {
|
2019-09-19 14:48:05 -04:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
2019-04-25 13:29:21 -04:00
|
|
|
// Relies on external http server. See tools/http_server.py
|
2019-10-31 22:33:27 -04:00
|
|
|
let url =
|
|
|
|
Url::parse("http://127.0.0.1:4545/cli/tests/fixture.json").unwrap();
|
2019-10-06 15:03:30 -04:00
|
|
|
|
|
|
|
let fut = fetch_string_once(&url).then(|result| match result {
|
|
|
|
Ok(FetchOnceResult::Code(code, maybe_content_type)) => {
|
2019-04-25 13:29:21 -04:00
|
|
|
assert!(!code.is_empty());
|
|
|
|
assert_eq!(maybe_content_type, Some("application/json".to_string()));
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::future::ok(())
|
2019-04-25 13:29:21 -04:00
|
|
|
}
|
2019-10-06 15:03:30 -04:00
|
|
|
_ => panic!(),
|
2019-04-25 13:29:21 -04:00
|
|
|
});
|
2019-10-06 15:03:30 -04:00
|
|
|
|
|
|
|
tokio_util::run(fut);
|
2019-09-19 14:48:05 -04:00
|
|
|
drop(http_server_guard);
|
2019-04-25 13:29:21 -04:00
|
|
|
}
|
2019-03-19 14:06:44 -04:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_string_once_with_redirect() {
|
2019-09-19 14:48:05 -04:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
2019-04-25 13:29:21 -04:00
|
|
|
// Relies on external http server. See tools/http_server.py
|
2019-10-31 22:33:27 -04:00
|
|
|
let url =
|
|
|
|
Url::parse("http://127.0.0.1:4546/cli/tests/fixture.json").unwrap();
|
2019-04-25 13:29:21 -04:00
|
|
|
// Dns resolver substitutes `127.0.0.1` with `localhost`
|
2019-10-31 22:33:27 -04:00
|
|
|
let target_url =
|
|
|
|
Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap();
|
2019-10-06 15:03:30 -04:00
|
|
|
let fut = fetch_string_once(&url).then(move |result| match result {
|
|
|
|
Ok(FetchOnceResult::Redirect(url)) => {
|
|
|
|
assert_eq!(url, target_url);
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::future::ok(())
|
2019-10-06 15:03:30 -04:00
|
|
|
}
|
|
|
|
_ => panic!(),
|
2019-04-25 13:29:21 -04:00
|
|
|
});
|
2019-10-06 15:03:30 -04:00
|
|
|
|
|
|
|
tokio_util::run(fut);
|
2019-09-19 14:48:05 -04:00
|
|
|
drop(http_server_guard);
|
2019-04-25 13:29:21 -04:00
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_full_1() {
|
|
|
|
let url = "http://deno.land".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "http://golang.org");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "golang.org");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_full_2() {
|
|
|
|
let url = "https://deno.land".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "https://golang.org");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "golang.org");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_relative_1() {
|
|
|
|
let url = "http://deno.land/x".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "//rust-lang.org/en-US");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "rust-lang.org");
|
|
|
|
assert_eq!(new_uri.path(), "/en-US");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_relative_2() {
|
|
|
|
let url = "http://deno.land/x".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "/y");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "deno.land");
|
|
|
|
assert_eq!(new_uri.path(), "/y");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_url_from_location_relative_3() {
|
|
|
|
let url = "http://deno.land/x".parse::<Url>().unwrap();
|
|
|
|
let new_uri = resolve_url_from_location(&url, "z");
|
|
|
|
assert_eq!(new_uri.host_str().unwrap(), "deno.land");
|
|
|
|
assert_eq!(new_uri.path(), "/z");
|
|
|
|
}
|
2018-11-29 22:01:01 -05:00
|
|
|
}
|