1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix: Handle bad redirects more gracefully (#7342)

This commit is contained in:
Ryan Dahl 2020-09-04 06:43:20 -04:00 committed by GitHub
parent 2b43ce65ae
commit a10339cb20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 55 deletions

View file

@ -2,7 +2,6 @@
use crate::version;
use bytes::Bytes;
use deno_core::ErrBox;
use futures::future::FutureExt;
use reqwest::header::HeaderMap;
use reqwest::header::HeaderValue;
use reqwest::header::IF_NONE_MATCH;
@ -92,14 +91,13 @@ pub enum FetchOnceResult {
/// yields Code(ResultPayload).
/// If redirect occurs, does not follow and
/// yields Redirect(url).
pub fn fetch_once(
pub async fn fetch_once(
client: Client,
url: &Url,
cached_etag: Option<String>,
) -> impl Future<Output = Result<FetchOnceResult, ErrBox>> {
) -> Result<FetchOnceResult, ErrBox> {
let url = url.clone();
let fut = async move {
let mut request = client.get(url.clone());
if let Some(etag) = cached_etag {
@ -135,34 +133,29 @@ pub fn fetch_once(
}
if response.status().is_redirection() {
let location_string = response
.headers()
.get(LOCATION)
.expect("url redirection should provide 'location' header")
.to_str()
.unwrap();
if let Some(location) = response.headers().get(LOCATION) {
let location_string = location.to_str().unwrap();
debug!("Redirecting to {:?}...", &location_string);
let new_url = resolve_url_from_location(&url, location_string);
return Ok(FetchOnceResult::Redirect(new_url, headers_));
} else {
return Err(ErrBox::error(format!(
"Redirection from '{}' did not provide location header",
url
)));
}
}
if response.status().is_client_error()
|| response.status().is_server_error()
if response.status().is_client_error() || response.status().is_server_error()
{
let err = io::Error::new(
io::ErrorKind::Other,
format!("Import '{}' failed: {}", &url, response.status()),
);
return Err(err.into());
let err =
ErrBox::error(format!("Import '{}' failed: {}", &url, response.status()));
return Err(err);
}
let body = response.bytes().await?.to_vec();
return Ok(FetchOnceResult::Code(body, headers_));
};
fut.boxed()
Ok(FetchOnceResult::Code(body, headers_))
}
/// Wraps reqwest `Response` so that it can be exposed as an `AsyncRead` and integrated
@ -500,4 +493,17 @@ mod tests {
panic!();
}
}
#[tokio::test]
async fn bad_redirect() {
let _g = test_util::http_server();
let url_str = "http://127.0.0.1:4545/bad_redirect";
let url = Url::parse(url_str).unwrap();
let client = create_http_client(None).unwrap();
let result = fetch_once(client, &url, None).await;
assert!(result.is_err());
let err = result.unwrap_err();
// Check that the error message contains the original URL
assert!(err.to_string().contains(url_str));
}
}

View file

@ -264,6 +264,11 @@ pub async fn run_all_servers() {
);
Box::new(res)
});
let bad_redirect = warp::path("bad_redirect").map(|| -> Box<dyn Reply> {
let mut res = Response::new(Body::from(""));
*res.status_mut() = StatusCode::FOUND;
Box::new(res)
});
let etag_script = warp::path!("etag_script.ts")
.and(warp::header::optional::<String>("if-none-match"))
@ -404,7 +409,8 @@ pub async fn run_all_servers() {
.or(xtypescripttypes)
.or(echo_server)
.or(echo_multipart_file)
.or(multipart_form_data);
.or(multipart_form_data)
.or(bad_redirect);
let http_fut =
warp::serve(content_type_handler.clone()).bind(([127, 0, 0, 1], PORT));