Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow retrieving the final URL from the response #137

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
sbstp marked this conversation as resolved.
Show resolved Hide resolved
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