Skip to content

Commit

Permalink
Add handling of 307 and 308 redirects
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
aidanhs authored and seanmonstar committed Nov 28, 2016
1 parent 9e70497 commit a54447c
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,57 +198,62 @@ impl<'a> RequestBuilder<'a> {

try!(req.send())
};
body.take();

match res.status {
let should_redirect = match res.status {
StatusCode::MovedPermanently |
StatusCode::Found |
StatusCode::SeeOther => {

//TODO: turn this into self.redirect_policy.check()
if redirect_count > 10 {
return Err(::Error::TooManyRedirects);
}
redirect_count += 1;

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
})
body = None;
match method {
Method::Get | Method::Head => {},
_ => {
method = Method::Get;
}
};
}
true
},
StatusCode::TemporaryRedirect |
StatusCode::PermanentRedirect => true,
_ => false,
};

debug!("redirecting to '{}'", url);
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
})
}
};

//TODO: removeSensitiveHeaders(&mut headers, &url);
debug!("redirecting to {:?} '{}'", method, url);

},
_ => {
return Ok(Response {
inner: res
});
}
//TODO: removeSensitiveHeaders(&mut headers, &url);
} else {
return Ok(Response {
inner: res
});
}
}
}
Expand Down

0 comments on commit a54447c

Please sign in to comment.