fix parsing uri from Location header value

This commit is contained in:
fakeshadow 2021-03-25 01:49:19 +08:00
parent 9704beddf8
commit 714c46847e
1 changed files with 18 additions and 15 deletions

View File

@ -257,28 +257,31 @@ where
} }
fn rebuild_uri(res: &ClientResponse, org_uri: Uri) -> Result<Uri, SendRequestError> { fn rebuild_uri(res: &ClientResponse, org_uri: Uri) -> Result<Uri, SendRequestError> {
let uri = res res.headers()
.headers()
.get(header::LOCATION) .get(header::LOCATION)
.map(|value| { // TODO: this error type is wrong.
// try to parse the location to a full uri .ok_or(SendRequestError::Url(InvalidUrl::MissingScheme))
let uri = Uri::try_from(value.as_bytes()) .and_then(|value| {
.map_err(|e| SendRequestError::Url(InvalidUrl::HttpError(e.into())))?; // 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() { if uri.scheme().is_none() || uri.authority().is_none() {
let uri = Uri::builder() let builder = Uri::builder()
.scheme(org_uri.scheme().cloned().unwrap()) .scheme(org_uri.scheme().cloned().unwrap())
.authority(org_uri.authority().cloned().unwrap()) .authority(org_uri.authority().cloned().unwrap());
.path_and_query(value.as_bytes())
.build()?; let uri = if value.as_bytes().starts_with(&[b'/']) {
Ok::<_, SendRequestError>(uri) 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 { } else {
Ok(uri) Ok(uri)
} }
}) })
// TODO: this error type is wrong.
.ok_or(SendRequestError::Url(InvalidUrl::MissingScheme))??;
Ok(uri)
} }
#[cfg(test)] #[cfg(test)]