Skip to content

Commit a54447c

Browse files
aidanhsseanmonstar
authored andcommitted
Add handling of 307 and 308 redirects
Fixes #9
1 parent 9e70497 commit a54447c

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

src/client.rs

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -198,57 +198,62 @@ impl<'a> RequestBuilder<'a> {
198198

199199
try!(req.send())
200200
};
201-
body.take();
202201

203-
match res.status {
202+
let should_redirect = match res.status {
204203
StatusCode::MovedPermanently |
205204
StatusCode::Found |
206205
StatusCode::SeeOther => {
207-
208-
//TODO: turn this into self.redirect_policy.check()
209-
if redirect_count > 10 {
210-
return Err(::Error::TooManyRedirects);
211-
}
212-
redirect_count += 1;
213-
214-
method = match method {
215-
Method::Post | Method::Put => Method::Get,
216-
m => m
217-
};
218-
219-
headers.set(Referer(url.to_string()));
220-
221-
let loc = {
222-
let loc = res.headers.get::<Location>().map(|loc| url.join(loc));
223-
if let Some(loc) = loc {
224-
loc
225-
} else {
226-
return Ok(Response {
227-
inner: res
228-
});
229-
}
230-
};
231-
232-
url = match loc {
233-
Ok(u) => u,
234-
Err(e) => {
235-
debug!("Location header had invalid URI: {:?}", e);
236-
return Ok(Response {
237-
inner: res
238-
})
206+
body = None;
207+
match method {
208+
Method::Get | Method::Head => {},
209+
_ => {
210+
method = Method::Get;
239211
}
240-
};
212+
}
213+
true
214+
},
215+
StatusCode::TemporaryRedirect |
216+
StatusCode::PermanentRedirect => true,
217+
_ => false,
218+
};
241219

242-
debug!("redirecting to '{}'", url);
220+
if should_redirect {
221+
//TODO: turn this into self.redirect_policy.check()
222+
if redirect_count > 10 {
223+
return Err(::Error::TooManyRedirects);
224+
}
225+
redirect_count += 1;
226+
227+
headers.set(Referer(url.to_string()));
228+
229+
let loc = {
230+
let loc = res.headers.get::<Location>().map(|loc| url.join(loc));
231+
if let Some(loc) = loc {
232+
loc
233+
} else {
234+
return Ok(Response {
235+
inner: res
236+
});
237+
}
238+
};
239+
240+
url = match loc {
241+
Ok(u) => u,
242+
Err(e) => {
243+
debug!("Location header had invalid URI: {:?}", e);
244+
return Ok(Response {
245+
inner: res
246+
})
247+
}
248+
};
243249

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

246-
},
247-
_ => {
248-
return Ok(Response {
249-
inner: res
250-
});
251-
}
252+
//TODO: removeSensitiveHeaders(&mut headers, &url);
253+
} else {
254+
return Ok(Response {
255+
inner: res
256+
});
252257
}
253258
}
254259
}

0 commit comments

Comments
 (0)