http_loader: Don't tail-call for redirects

Serving suggestion: git show -w

Fixes #1325.

Some of the control-flow reorganization is for clarity rather than out of
necessity.
This commit is contained in:
Keegan McAllister 2013-12-03 18:54:53 -08:00
parent b26fe9a430
commit 5763937ecd

View file

@ -21,7 +21,9 @@ pub fn factory() -> LoaderTask {
f
}
fn load(url: Url, start_chan: Chan<LoadResponse>) {
fn load(mut url: Url, start_chan: Chan<LoadResponse>) {
// Loop to handle redirects.
loop {
assert!("http" == url.scheme);
info!("requesting {:s}", url.to_str());
@ -45,9 +47,10 @@ fn load(url: Url, start_chan: Chan<LoadResponse>) {
// FIXME: detect redirect loops
if 3 == (response.status.code() / 100) {
match response.headers.location {
Some(url) => {
info!("redirecting to {:s}", url.to_str());
return load(url, start_chan);
Some(new_url) => {
info!("redirecting to {:s}", new_url.to_str());
url = new_url;
continue;
}
None => ()
}
@ -64,12 +67,16 @@ fn load(url: Url, start_chan: Chan<LoadResponse>) {
match response.read(buf) {
Some(len) => {
unsafe { vec::raw::set_len(&mut buf, len) };
progress_chan.send(Payload(buf));
}
None => {
progress_chan.send(Done(Ok(())));
return;
break;
}
}
progress_chan.send(Payload(buf));
}
// We didn't get redirected.
break;
}
}