Add handling of 307 and 308 redirects

Fixes #9
This commit is contained in:
Aidan Hobson Sayers
2016-11-17 22:38:19 +00:00
committed by Sean McArthur
parent 9e70497781
commit a54447c1d9

View File

@@ -198,57 +198,62 @@ impl<'a> RequestBuilder<'a> {
try!(req.send()) try!(req.send())
}; };
body.take();
match res.status { let should_redirect = match res.status {
StatusCode::MovedPermanently | StatusCode::MovedPermanently |
StatusCode::Found | StatusCode::Found |
StatusCode::SeeOther => { StatusCode::SeeOther => {
body = None;
//TODO: turn this into self.redirect_policy.check() match method {
if redirect_count > 10 { Method::Get | Method::Head => {},
return Err(::Error::TooManyRedirects); _ => {
method = Method::Get;
}
} }
redirect_count += 1; true
method = match method {
Method::Post | Method::Put => Method::Get,
m => m
};
headers.set(Referer(url.to_string()));
let loc = {
let loc = res.headers.get::<Location>().map(|loc| url.join(loc));
if let Some(loc) = loc {
loc
} else {
return Ok(Response {
inner: res
});
}
};
url = match loc {
Ok(u) => u,
Err(e) => {
debug!("Location header had invalid URI: {:?}", e);
return Ok(Response {
inner: res
})
}
};
debug!("redirecting to '{}'", url);
//TODO: removeSensitiveHeaders(&mut headers, &url);
}, },
_ => { StatusCode::TemporaryRedirect |
return Ok(Response { StatusCode::PermanentRedirect => true,
inner: res _ => false,
}); };
if should_redirect {
//TODO: turn this into self.redirect_policy.check()
if redirect_count > 10 {
return Err(::Error::TooManyRedirects);
} }
redirect_count += 1;
headers.set(Referer(url.to_string()));
let loc = {
let loc = res.headers.get::<Location>().map(|loc| url.join(loc));
if let Some(loc) = loc {
loc
} else {
return Ok(Response {
inner: res
});
}
};
url = match loc {
Ok(u) => u,
Err(e) => {
debug!("Location header had invalid URI: {:?}", e);
return Ok(Response {
inner: res
})
}
};
debug!("redirecting to {:?} '{}'", method, url);
//TODO: removeSensitiveHeaders(&mut headers, &url);
} else {
return Ok(Response {
inner: res
});
} }
} }
} }