Skip to content

Commit

Permalink
Merge pull request #293 from Fishrock123/fix-redirects
Browse files Browse the repository at this point in the history
Fix relative redirects
  • Loading branch information
Fishrock123 authored Mar 1, 2021
2 parents 5a5621b + c32ee64 commit d4943aa
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/middleware/redirect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,27 @@ impl Middleware for Redirect {
// and try sending it until we get some status back that is not a
// redirect.

let mut base_url = req.url().clone();

while redirect_count < self.attempts {
redirect_count += 1;
let r: Request = req.clone();
let res: Response = client.send(r).await?;
if REDIRECT_CODES.contains(&res.status()) {
if let Some(location) = res.header(headers::LOCATION) {
let http_req: &mut http::Request = req.as_mut();
*http_req.url_mut() = Url::parse(location.last().as_str())?;
*http_req.url_mut() = match Url::parse(location.last().as_str()) {
Ok(valid_url) => {
base_url = valid_url;
base_url.clone()
}
Err(e) => match e {
http::url::ParseError::RelativeUrlWithoutBase => {
base_url.join(location.last().as_str())?
}
e => return Err(e.into()),
},
};
}
} else {
break;
Expand Down

0 comments on commit d4943aa

Please sign in to comment.