Skip to content

Follow 307/308 redirects #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,24 @@ impl AgentBuilder {
///
/// If the redirect count hits this limit (and it's > 0), TooManyRedirects is returned.
///
/// WARNING: for 307 and 308 redirects, this value is ignored for methods that have a body.
/// You must handle 307 redirects yourself when sending a PUT, POST, PATCH, or DELETE request.
///
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// let result = ureq::builder()
/// .redirects(1)
/// .build()
/// .get("http://httpbin.org/redirect/3")
/// .call();
/// .get("http://httpbin.org/status/301")
/// .error_on_non_2xx(false)
/// .call()?;
/// assert_ne!(result.status(), 301);
///
/// let result = ureq::post("http://httpbin.org/status/307")
/// .error_on_non_2xx(false)
/// .send_bytes(b"some data")?;
/// assert_eq!(result.status(), 307);
/// # Ok(())
/// # }
/// ```
Expand Down
8 changes: 6 additions & 2 deletions src/testserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ pub(crate) fn test_agent() -> Agent {
stream.write_all(b"HTTP/1.1 200 OK\r\n")?;
stream.write_all(b"\r\n")?;
stream.write_all(br#"{"hello": "world"}"#)?;
} else if headers.path() == "/redirect/3" {
stream.write_all(b"HTTP/1.1 302 Found\r\n")?;
} else if headers.path() == "/status/301" {
stream.write_all(b"HTTP/1.1 301 Found\r\n")?;
stream.write_all(b"Location: /redirect/3\r\n")?;
stream.write_all(b"\r\n")?;
} else if headers.path() == "/status/307" {
stream.write_all(b"HTTP/1.1 307 Found\r\n")?;
stream.write_all(b"Location: /redirect/3\r\n")?;
stream.write_all(b"\r\n")?;
} else {
Expand Down
10 changes: 8 additions & 2 deletions src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,15 @@ pub(crate) fn connect(
debug!("redirect {} {} -> {}", resp.status(), url, new_url);
return connect(new_unit, use_pooled, redirect_count + 1, empty, true);
}
// never change the method for 307/308
// only resend the request if it cannot have a body
// NOTE: DELETE is intentionally excluded: https://stackoverflow.com/questions/299628
307 | 308 if ["GET", "HEAD", "OPTIONS", "TRACE"].contains(&method.as_str()) => {
let empty = Payload::Empty.into_read();
debug!("redirect {} {} -> {}", resp.status(), url, new_url);
return connect(unit, use_pooled, redirect_count - 1, empty, true);
}
_ => (),
// reinstate this with expect-100
// 307 | 308 | _ => connect(unit, method, use_pooled, redirects - 1, body),
};
}
}
Expand Down