From 714c46847e7118a2ec02f0b608f4d10d76f248f7 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Thu, 25 Mar 2021 01:49:19 +0800 Subject: [PATCH] fix parsing uri from Location header value --- awc/src/middleware/redirect.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/awc/src/middleware/redirect.rs b/awc/src/middleware/redirect.rs index 62ea1d0ac..91db3cda5 100644 --- a/awc/src/middleware/redirect.rs +++ b/awc/src/middleware/redirect.rs @@ -257,28 +257,31 @@ where } fn rebuild_uri(res: &ClientResponse, org_uri: Uri) -> Result { - let uri = res - .headers() + res.headers() .get(header::LOCATION) - .map(|value| { - // try to parse the location to a full uri - let uri = Uri::try_from(value.as_bytes()) - .map_err(|e| SendRequestError::Url(InvalidUrl::HttpError(e.into())))?; + // TODO: this error type is wrong. + .ok_or(SendRequestError::Url(InvalidUrl::MissingScheme)) + .and_then(|value| { + // Try to parse the location to a full uri and fall back to default when failed + let uri = Uri::try_from(value.as_bytes()).unwrap_or_else(|_| Uri::default()); + + // When scheme or authority missing treat the location value as path and query. if uri.scheme().is_none() || uri.authority().is_none() { - let uri = Uri::builder() + let builder = Uri::builder() .scheme(org_uri.scheme().cloned().unwrap()) - .authority(org_uri.authority().cloned().unwrap()) - .path_and_query(value.as_bytes()) - .build()?; - Ok::<_, SendRequestError>(uri) + .authority(org_uri.authority().cloned().unwrap()); + + let uri = if value.as_bytes().starts_with(&[b'/']) { + builder.path_and_query(value.as_bytes()).build()? + } else { + let path = [&[b'/'], value.as_bytes()].concat(); + builder.path_and_query(&path[..]).build()? + }; + Ok(uri) } else { Ok(uri) } }) - // TODO: this error type is wrong. - .ok_or(SendRequestError::Url(InvalidUrl::MissingScheme))??; - - Ok(uri) } #[cfg(test)]