Skip to content

Commit 3fe9967

Browse files
authored
Merge pull request #137 from baskerville/final-url
Allow retrieving the final URL from the response
2 parents e0acf95 + 3a71bdd commit 3fe9967

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/parsing/compressed_reader.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ mod tests {
158158
let req = PreparedRequest::new(Method::GET, "http://google.ca");
159159

160160
let sock = BaseStream::mock(buf);
161-
let response = parse_response(sock, &req).unwrap();
161+
let response = parse_response(sock, &req, req.url()).unwrap();
162162
assert_eq!(response.text().unwrap(), "Hello world!!!!!!!!");
163163
}
164164

@@ -181,7 +181,7 @@ mod tests {
181181
let req = PreparedRequest::new(Method::GET, "http://google.ca");
182182

183183
let sock = BaseStream::mock(buf);
184-
let response = parse_response(sock, &req).unwrap();
184+
let response = parse_response(sock, &req, req.url()).unwrap();
185185
assert_eq!(response.text().unwrap(), "Hello world!!!!!!!!");
186186
}
187187

@@ -204,7 +204,7 @@ mod tests {
204204
let req = PreparedRequest::new(Method::GET, "http://google.ca");
205205

206206
let sock = BaseStream::mock(buf);
207-
let response = parse_response(sock, &req).unwrap();
207+
let response = parse_response(sock, &req, req.url()).unwrap();
208208

209209
assert_eq!(response.text().unwrap(), "Hello world!!!!!!!!");
210210
}
@@ -217,7 +217,7 @@ mod tests {
217217
let req = PreparedRequest::new(Method::GET, "http://google.ca");
218218
let sock = BaseStream::mock(buf.to_vec());
219219
// Fixed by the move from libflate to flate2
220-
assert!(parse_response(sock, &req).is_ok());
220+
assert!(parse_response(sock, &req, req.url()).is_ok());
221221
}
222222

223223
#[test]
@@ -227,6 +227,6 @@ mod tests {
227227

228228
let req = PreparedRequest::new(Method::HEAD, "http://google.ca");
229229
let sock = BaseStream::mock(buf.to_vec());
230-
assert!(parse_response(sock, &req).is_ok());
230+
assert!(parse_response(sock, &req, req.url()).is_ok());
231231
}
232232
}

src/parsing/response.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use http::{
55
header::{HeaderName, HeaderValue, TRANSFER_ENCODING},
66
HeaderMap, StatusCode,
77
};
8+
use url::Url;
89

910
use crate::error::{ErrorKind, InvalidResponseKind, Result};
1011
use crate::parsing::buffers::{self, trim_byte};
@@ -74,7 +75,7 @@ where
7475
Ok((status, headers))
7576
}
7677

77-
pub fn parse_response<B>(reader: BaseStream, request: &PreparedRequest<B>) -> Result<Response> {
78+
pub fn parse_response<B>(reader: BaseStream, request: &PreparedRequest<B>, url: &Url) -> Result<Response> {
7879
let mut reader = BufReader::new(reader);
7980
let (status, mut headers) = parse_response_head(&mut reader, request.base_settings.max_headers)?;
8081
let body_reader = BodyReader::new(&headers, reader)?;
@@ -85,6 +86,7 @@ pub fn parse_response<B>(reader: BaseStream, request: &PreparedRequest<B>) -> Re
8586
headers.remove(TRANSFER_ENCODING);
8687

8788
Ok(Response {
89+
url: url.clone(),
8890
status,
8991
headers,
9092
reader: response_reader,
@@ -94,12 +96,19 @@ pub fn parse_response<B>(reader: BaseStream, request: &PreparedRequest<B>) -> Re
9496
/// `Response` represents a response returned by a server.
9597
#[derive(Debug)]
9698
pub struct Response {
99+
url: Url,
97100
status: StatusCode,
98101
headers: HeaderMap,
99102
reader: ResponseReader,
100103
}
101104

102105
impl Response {
106+
/// Get the final URL of this `Response`.
107+
#[inline]
108+
pub fn url(&self) -> &Url {
109+
&self.url
110+
}
111+
103112
/// Get the status code of this `Response`.
104113
#[inline]
105114
pub fn status(&self) -> StatusCode {

src/request/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<B: Body> PreparedRequest<B> {
228228
let mut stream = BaseStream::connect(&info)?;
229229

230230
self.write_request(&mut stream, &url, proxy.as_ref())?;
231-
let resp = parse_response(stream, self)?;
231+
let resp = parse_response(stream, self, &url)?;
232232

233233
debug!("status code {}", resp.status().as_u16());
234234

0 commit comments

Comments
 (0)