fix 307/308 redirects with GET requests
This commit is contained in:
		| @@ -328,10 +328,10 @@ impl Client { | |||||||
|         let uri = to_uri(&url); |         let uri = to_uri(&url); | ||||||
|         let mut req = ::hyper::Request::new(method.clone(), uri.clone()); |         let mut req = ::hyper::Request::new(method.clone(), uri.clone()); | ||||||
|         *req.headers_mut() = headers.clone(); |         *req.headers_mut() = headers.clone(); | ||||||
|         let body = body.and_then(|body| { |         let body = body.map(|body| { | ||||||
|             let (resuable, body) = body::into_hyper(body); |             let (reusable, body) = body::into_hyper(body); | ||||||
|             req.set_body(body); |             req.set_body(body); | ||||||
|             resuable |             reusable | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         if proxy::is_proxied(&self.inner.proxies, &uri) { |         if proxy::is_proxied(&self.inner.proxies, &uri) { | ||||||
| @@ -386,7 +386,7 @@ pub struct Pending { | |||||||
|     method: Method, |     method: Method, | ||||||
|     url: Url, |     url: Url, | ||||||
|     headers: Headers, |     headers: Headers, | ||||||
|     body: Option<Bytes>, |     body: Option<Option<Bytes>>, | ||||||
|  |  | ||||||
|     urls: Vec<Url>, |     urls: Vec<Url>, | ||||||
|  |  | ||||||
| @@ -419,8 +419,9 @@ impl Future for Pending { | |||||||
|                     true |                     true | ||||||
|                 }, |                 }, | ||||||
|                 StatusCode::TemporaryRedirect | |                 StatusCode::TemporaryRedirect | | ||||||
|                 StatusCode::PermanentRedirect => { |                 StatusCode::PermanentRedirect => match self.body { | ||||||
|                     self.body.is_some() |                     Some(Some(_)) | None => true, | ||||||
|  |                     Some(None) => false, | ||||||
|                 }, |                 }, | ||||||
|                 _ => false, |                 _ => false, | ||||||
|             }; |             }; | ||||||
| @@ -449,7 +450,7 @@ impl Future for Pending { | |||||||
|                                 uri.clone() |                                 uri.clone() | ||||||
|                             ); |                             ); | ||||||
|                             *req.headers_mut() = self.headers.clone(); |                             *req.headers_mut() = self.headers.clone(); | ||||||
|                             if let Some(ref body) = self.body { |                             if let Some(Some(ref body)) = self.body { | ||||||
|                                 req.set_body(body.clone()); |                                 req.set_body(body.clone()); | ||||||
|                             } |                             } | ||||||
|                             if proxy::is_proxied(&self.client.proxies, &uri) { |                             if proxy::is_proxied(&self.client.proxies, &uri) { | ||||||
|   | |||||||
| @@ -58,6 +58,59 @@ fn test_redirect_301_and_302_and_303_changes_post_to_get() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #[test] | ||||||
|  | fn test_redirect_307_and_308_tries_to_get_again() { | ||||||
|  |     let client = reqwest::Client::new().unwrap(); | ||||||
|  |     let codes = [307, 308]; | ||||||
|  |     for code in codes.iter() { | ||||||
|  |         let redirect = server! { | ||||||
|  |             request: format!("\ | ||||||
|  |                 GET /{} HTTP/1.1\r\n\ | ||||||
|  |                 Host: $HOST\r\n\ | ||||||
|  |                 User-Agent: $USERAGENT\r\n\ | ||||||
|  |                 Accept: */*\r\n\ | ||||||
|  |                 Accept-Encoding: gzip\r\n\ | ||||||
|  |                 \r\n\ | ||||||
|  |                 ", code), | ||||||
|  |             response: format!("\ | ||||||
|  |                 HTTP/1.1 {} reason\r\n\ | ||||||
|  |                 Server: test-redirect\r\n\ | ||||||
|  |                 Content-Length: 0\r\n\ | ||||||
|  |                 Location: /dst\r\n\ | ||||||
|  |                 Connection: close\r\n\ | ||||||
|  |                 \r\n\ | ||||||
|  |                 ", code), | ||||||
|  |  | ||||||
|  |             request: format!("\ | ||||||
|  |                 GET /dst HTTP/1.1\r\n\ | ||||||
|  |                 Host: $HOST\r\n\ | ||||||
|  |                 User-Agent: $USERAGENT\r\n\ | ||||||
|  |                 Accept: */*\r\n\ | ||||||
|  |                 Accept-Encoding: gzip\r\n\ | ||||||
|  |                 Referer: http://$HOST/{}\r\n\ | ||||||
|  |                 \r\n\ | ||||||
|  |                 ", code), | ||||||
|  |             response: b"\ | ||||||
|  |                 HTTP/1.1 200 OK\r\n\ | ||||||
|  |                 Server: test-dst\r\n\ | ||||||
|  |                 Content-Length: 0\r\n\ | ||||||
|  |                 \r\n\ | ||||||
|  |                 " | ||||||
|  |         }; | ||||||
|  |  | ||||||
|  |         let url = format!("http://{}/{}", redirect.addr(), code); | ||||||
|  |         let dst = format!("http://{}/{}", redirect.addr(), "dst"); | ||||||
|  |         let res = client.get(&url) | ||||||
|  |             .unwrap() | ||||||
|  |             .send() | ||||||
|  |             .unwrap(); | ||||||
|  |         assert_eq!(res.url().as_str(), dst); | ||||||
|  |         assert_eq!(res.status(), reqwest::StatusCode::Ok); | ||||||
|  |         assert_eq!(res.headers().get(), | ||||||
|  |                    Some(&reqwest::header::Server::new("test-dst".to_string()))); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| fn test_redirect_307_and_308_tries_to_post_again() { | fn test_redirect_307_and_308_tries_to_post_again() { | ||||||
|     let client = reqwest::Client::new().unwrap(); |     let client = reqwest::Client::new().unwrap(); | ||||||
| @@ -116,7 +169,6 @@ fn test_redirect_307_and_308_tries_to_post_again() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| /* |  | ||||||
| #[test] | #[test] | ||||||
| fn test_redirect_307_does_not_try_if_reader_cannot_reset() { | fn test_redirect_307_does_not_try_if_reader_cannot_reset() { | ||||||
|     let client = reqwest::Client::new().unwrap(); |     let client = reqwest::Client::new().unwrap(); | ||||||
| @@ -127,7 +179,7 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() { | |||||||
|                 POST /{} HTTP/1.1\r\n\ |                 POST /{} HTTP/1.1\r\n\ | ||||||
|                 Host: $HOST\r\n\ |                 Host: $HOST\r\n\ | ||||||
|                 User-Agent: $USERAGENT\r\n\ |                 User-Agent: $USERAGENT\r\n\ | ||||||
|                 Accept: * / *\r\n\ |                 Accept: */*\r\n\ | ||||||
|                 Accept-Encoding: gzip\r\n\ |                 Accept-Encoding: gzip\r\n\ | ||||||
|                 Transfer-Encoding: chunked\r\n\ |                 Transfer-Encoding: chunked\r\n\ | ||||||
|                 \r\n\ |                 \r\n\ | ||||||
| @@ -156,7 +208,8 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() { | |||||||
|         assert_eq!(res.status(), reqwest::StatusCode::try_from(code).unwrap()); |         assert_eq!(res.status(), reqwest::StatusCode::try_from(code).unwrap()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| */ |  | ||||||
|  |  | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| fn test_redirect_removes_sensitive_headers() { | fn test_redirect_removes_sensitive_headers() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user