Skip to content

Commit 2d11a4b

Browse files
committed
fix 307/308 redirects with GET requests
1 parent 8296c5e commit 2d11a4b

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/async_impl/client.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ impl Client {
328328
let uri = to_uri(&url);
329329
let mut req = ::hyper::Request::new(method.clone(), uri.clone());
330330
*req.headers_mut() = headers.clone();
331-
let body = body.and_then(|body| {
332-
let (resuable, body) = body::into_hyper(body);
331+
let body = body.map(|body| {
332+
let (reusable, body) = body::into_hyper(body);
333333
req.set_body(body);
334-
resuable
334+
reusable
335335
});
336336

337337
if proxy::is_proxied(&self.inner.proxies, &uri) {
@@ -386,7 +386,7 @@ pub struct Pending {
386386
method: Method,
387387
url: Url,
388388
headers: Headers,
389-
body: Option<Bytes>,
389+
body: Option<Option<Bytes>>,
390390

391391
urls: Vec<Url>,
392392

@@ -419,8 +419,9 @@ impl Future for Pending {
419419
true
420420
},
421421
StatusCode::TemporaryRedirect |
422-
StatusCode::PermanentRedirect => {
423-
self.body.is_some()
422+
StatusCode::PermanentRedirect => match self.body {
423+
Some(Some(_)) | None => true,
424+
Some(None) => false,
424425
},
425426
_ => false,
426427
};
@@ -449,7 +450,7 @@ impl Future for Pending {
449450
uri.clone()
450451
);
451452
*req.headers_mut() = self.headers.clone();
452-
if let Some(ref body) = self.body {
453+
if let Some(Some(ref body)) = self.body {
453454
req.set_body(body.clone());
454455
}
455456
if proxy::is_proxied(&self.client.proxies, &uri) {

tests/redirect.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,59 @@ fn test_redirect_301_and_302_and_303_changes_post_to_get() {
5858
}
5959
}
6060

61+
#[test]
62+
fn test_redirect_307_and_308_tries_to_get_again() {
63+
let client = reqwest::Client::new().unwrap();
64+
let codes = [307, 308];
65+
for code in codes.iter() {
66+
let redirect = server! {
67+
request: format!("\
68+
GET /{} HTTP/1.1\r\n\
69+
Host: $HOST\r\n\
70+
User-Agent: $USERAGENT\r\n\
71+
Accept: */*\r\n\
72+
Accept-Encoding: gzip\r\n\
73+
\r\n\
74+
", code),
75+
response: format!("\
76+
HTTP/1.1 {} reason\r\n\
77+
Server: test-redirect\r\n\
78+
Content-Length: 0\r\n\
79+
Location: /dst\r\n\
80+
Connection: close\r\n\
81+
\r\n\
82+
", code),
83+
84+
request: format!("\
85+
GET /dst HTTP/1.1\r\n\
86+
Host: $HOST\r\n\
87+
User-Agent: $USERAGENT\r\n\
88+
Accept: */*\r\n\
89+
Accept-Encoding: gzip\r\n\
90+
Referer: http://$HOST/{}\r\n\
91+
\r\n\
92+
", code),
93+
response: b"\
94+
HTTP/1.1 200 OK\r\n\
95+
Server: test-dst\r\n\
96+
Content-Length: 0\r\n\
97+
\r\n\
98+
"
99+
};
100+
101+
let url = format!("http://{}/{}", redirect.addr(), code);
102+
let dst = format!("http://{}/{}", redirect.addr(), "dst");
103+
let res = client.get(&url)
104+
.unwrap()
105+
.send()
106+
.unwrap();
107+
assert_eq!(res.url().as_str(), dst);
108+
assert_eq!(res.status(), reqwest::StatusCode::Ok);
109+
assert_eq!(res.headers().get(),
110+
Some(&reqwest::header::Server::new("test-dst".to_string())));
111+
}
112+
}
113+
61114
#[test]
62115
fn test_redirect_307_and_308_tries_to_post_again() {
63116
let client = reqwest::Client::new().unwrap();
@@ -116,7 +169,6 @@ fn test_redirect_307_and_308_tries_to_post_again() {
116169
}
117170
}
118171

119-
/*
120172
#[test]
121173
fn test_redirect_307_does_not_try_if_reader_cannot_reset() {
122174
let client = reqwest::Client::new().unwrap();
@@ -127,7 +179,7 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() {
127179
POST /{} HTTP/1.1\r\n\
128180
Host: $HOST\r\n\
129181
User-Agent: $USERAGENT\r\n\
130-
Accept: * / *\r\n\
182+
Accept: */*\r\n\
131183
Accept-Encoding: gzip\r\n\
132184
Transfer-Encoding: chunked\r\n\
133185
\r\n\
@@ -156,7 +208,8 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() {
156208
assert_eq!(res.status(), reqwest::StatusCode::try_from(code).unwrap());
157209
}
158210
}
159-
*/
211+
212+
160213

161214
#[test]
162215
fn test_redirect_removes_sensitive_headers() {

0 commit comments

Comments
 (0)