diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-12-30 14:57:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-30 14:57:17 +0100 |
commit | 46d76a7562025374600a7f866dfc68c1b7e268e9 (patch) | |
tree | 0681d383781d8a28ac7ea23d75f22b1faeea0208 /cli/http_util.rs | |
parent | df1665a8fc92168c3eb115a768ecfeccbe575e18 (diff) |
upgrade: Tokio 0.2 (#3418)
Diffstat (limited to 'cli/http_util.rs')
-rw-r--r-- | cli/http_util.rs | 115 |
1 files changed, 40 insertions, 75 deletions
diff --git a/cli/http_util.rs b/cli/http_util.rs index 41a01f1e3..1be9ad60f 100644 --- a/cli/http_util.rs +++ b/cli/http_util.rs @@ -3,18 +3,15 @@ use crate::deno_error; use crate::deno_error::DenoError; use crate::version; use deno::ErrBox; -use futures::future; use futures::future::FutureExt; -use futures::future::TryFutureExt; use reqwest; use reqwest::header::HeaderMap; use reqwest::header::CONTENT_TYPE; use reqwest::header::LOCATION; use reqwest::header::USER_AGENT; -use reqwest::r#async::Client; -use reqwest::RedirectPolicy; +use reqwest::redirect::Policy; +use reqwest::Client; use std::future::Future; -use std::pin::Pin; use url::Url; /// Create new instance of async reqwest::Client. This client supports @@ -26,9 +23,9 @@ pub fn get_client() -> Client { format!("Deno/{}", version::DENO).parse().unwrap(), ); Client::builder() - .redirect(RedirectPolicy::none()) + .redirect(Policy::none()) .default_headers(headers) - .use_sys_proxy() + .use_rustls_tls() .build() .unwrap() } @@ -75,77 +72,45 @@ pub enum FetchOnceResult { pub fn fetch_string_once( url: &Url, ) -> impl Future<Output = Result<FetchOnceResult, ErrBox>> { - type FetchAttempt = (Option<String>, Option<String>, Option<FetchOnceResult>); - let url = url.clone(); let client = get_client(); - futures::compat::Compat01As03::new(client.get(url.clone()).send()) - .map_err(ErrBox::from) - .and_then( - move |mut response| -> Pin< - Box<dyn Future<Output = Result<FetchAttempt, ErrBox>> + Send>, - > { - if response.status().is_redirection() { - let location_string = response - .headers() - .get(LOCATION) - .expect("url redirection should provide 'location' header") - .to_str() - .unwrap(); - - debug!("Redirecting to {:?}...", &location_string); - let new_url = resolve_url_from_location(&url, location_string); - // Boxed trait object turns out to be the savior for 2+ types yielding same results. - return futures::future::try_join3( - future::ok(None), - future::ok(None), - future::ok(Some(FetchOnceResult::Redirect(new_url))), - ) - .boxed(); - } - - 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(); - } - - let content_type = response - .headers() - .get(CONTENT_TYPE) - .map(|content_type| content_type.to_str().unwrap().to_owned()); - - let body = futures::compat::Compat01As03::new(response.text()) - .map_ok(Some) - .map_err(ErrBox::from); - - futures::future::try_join3( - body, - future::ok(content_type), - future::ok(None), - ) - .boxed() - }, - ) - .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! - future::ok(FetchOnceResult::Code( - maybe_code.unwrap(), - maybe_content_type, - )) - } - }) + let fut = async move { + let response = client.get(url.clone()).send().await?; + + if response.status().is_redirection() { + let location_string = response + .headers() + .get(LOCATION) + .expect("url redirection should provide 'location' header") + .to_str() + .unwrap(); + + debug!("Redirecting to {:?}...", &location_string); + let new_url = resolve_url_from_location(&url, location_string); + return Ok(FetchOnceResult::Redirect(new_url)); + } + + if response.status().is_client_error() + || response.status().is_server_error() + { + let err = DenoError::new( + deno_error::ErrorKind::Other, + format!("Import '{}' failed: {}", &url, response.status()), + ); + return Err(err.into()); + } + + let content_type = response + .headers() + .get(CONTENT_TYPE) + .map(|content_type| content_type.to_str().unwrap().to_owned()); + + let body = response.text().await?; + return Ok(FetchOnceResult::Code(body, content_type)); + }; + + fut.boxed() } #[cfg(test)] |