Skip to content

Commit

Permalink
Merge pull request #137 from baskerville/final-url
Browse files Browse the repository at this point in the history
Allow retrieving the final URL from the response
  • Loading branch information
sbstp authored Feb 11, 2023
2 parents e0acf95 + 3a71bdd commit 3fe9967
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/parsing/compressed_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod tests {
let req = PreparedRequest::new(Method::GET, "http://google.ca");

let sock = BaseStream::mock(buf);
let response = parse_response(sock, &req).unwrap();
let response = parse_response(sock, &req, req.url()).unwrap();
assert_eq!(response.text().unwrap(), "Hello world!!!!!!!!");
}

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

let sock = BaseStream::mock(buf);
let response = parse_response(sock, &req).unwrap();
let response = parse_response(sock, &req, req.url()).unwrap();
assert_eq!(response.text().unwrap(), "Hello world!!!!!!!!");
}

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

let sock = BaseStream::mock(buf);
let response = parse_response(sock, &req).unwrap();
let response = parse_response(sock, &req, req.url()).unwrap();

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

#[test]
Expand All @@ -227,6 +227,6 @@ mod tests {

let req = PreparedRequest::new(Method::HEAD, "http://google.ca");
let sock = BaseStream::mock(buf.to_vec());
assert!(parse_response(sock, &req).is_ok());
assert!(parse_response(sock, &req, req.url()).is_ok());
}
}
11 changes: 10 additions & 1 deletion src/parsing/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use http::{
header::{HeaderName, HeaderValue, TRANSFER_ENCODING},
HeaderMap, StatusCode,
};
use url::Url;

use crate::error::{ErrorKind, InvalidResponseKind, Result};
use crate::parsing::buffers::{self, trim_byte};
Expand Down Expand Up @@ -74,7 +75,7 @@ where
Ok((status, headers))
}

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

Ok(Response {
url: url.clone(),
status,
headers,
reader: response_reader,
Expand All @@ -94,12 +96,19 @@ pub fn parse_response<B>(reader: BaseStream, request: &PreparedRequest<B>) -> Re
/// `Response` represents a response returned by a server.
#[derive(Debug)]
pub struct Response {
url: Url,
status: StatusCode,
headers: HeaderMap,
reader: ResponseReader,
}

impl Response {
/// Get the final URL of this `Response`.
#[inline]
pub fn url(&self) -> &Url {
&self.url
}

/// Get the status code of this `Response`.
#[inline]
pub fn status(&self) -> StatusCode {
Expand Down
2 changes: 1 addition & 1 deletion src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<B: Body> PreparedRequest<B> {
let mut stream = BaseStream::connect(&info)?;

self.write_request(&mut stream, &url, proxy.as_ref())?;
let resp = parse_response(stream, self)?;
let resp = parse_response(stream, self, &url)?;

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

Expand Down

0 comments on commit 3fe9967

Please sign in to comment.