Skip to content

Commit

Permalink
don't consume Response in Middleware::print
Browse files Browse the repository at this point in the history
  • Loading branch information
ducaale committed Apr 5, 2022
1 parent 2709621 commit 5c78ddc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a> DigestAuthMiddleware<'a> {

impl<'a> Middleware for DigestAuthMiddleware<'a> {
fn handle(&mut self, mut ctx: Context, mut request: Request) -> Result<Response> {
let response = self.next(&mut ctx, clone_request(&mut request)?)?;
let mut response = self.next(&mut ctx, clone_request(&mut request)?)?;
match response.headers().get(WWW_AUTHENTICATE) {
Some(wwwauth) if response.status() == StatusCode::UNAUTHORIZED => {
let mut context = digest_auth::AuthContext::new(
Expand All @@ -99,7 +99,7 @@ impl<'a> Middleware for DigestAuthMiddleware<'a> {
request
.headers_mut()
.insert(AUTHORIZATION, HeaderValue::from_str(&answer)?);
self.print(&mut ctx, response, &mut request)?;
self.print(&mut ctx, &mut response, &mut request)?;
Ok(self.next(&mut ctx, request)?)
}
_ => Ok(response),
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,13 @@ fn run(args: Cli) -> Result<i32> {
let history_print = args.history_print.unwrap_or(print);
let mut client = ClientWithMiddleware::new(&client);
if args.all {
client = client.with_printer(|mut prev_response, next_request| {
client = client.with_printer(|prev_response, next_request| {
if history_print.response_headers {
printer.print_response_headers(&prev_response)?;
printer.print_response_headers(prev_response)?;
}
if history_print.response_body {
printer.print_response_body(
&mut prev_response,
prev_response,
response_charset,
response_mime,
)?;
Expand Down
15 changes: 10 additions & 5 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use reqwest::blocking::{Client, Request, Response};

pub struct Context<'a, 'b> {
client: &'a Client,
printer: Option<&'a mut (dyn FnMut(Response, &mut Request) -> Result<()> + 'b)>,
printer: Option<&'a mut (dyn FnMut(&mut Response, &mut Request) -> Result<()> + 'b)>,
middlewares: &'a mut [Box<dyn Middleware + 'b>],
}

impl<'a, 'b> Context<'a, 'b> {
fn new(
client: &'a Client,
printer: Option<&'a mut (dyn FnMut(Response, &mut Request) -> Result<()> + 'b)>,
printer: Option<&'a mut (dyn FnMut(&mut Response, &mut Request) -> Result<()> + 'b)>,
middlewares: &'a mut [Box<dyn Middleware + 'b>],
) -> Self {
Context {
Expand Down Expand Up @@ -39,7 +39,12 @@ pub trait Middleware {
ctx.execute(request)
}

fn print(&self, ctx: &mut Context, response: Response, request: &mut Request) -> Result<()> {
fn print(
&self,
ctx: &mut Context,
response: &mut Response,
request: &mut Request,
) -> Result<()> {
if let Some(ref mut printer) = ctx.printer {
printer(response, request)?;
}
Expand All @@ -50,7 +55,7 @@ pub trait Middleware {

pub struct ClientWithMiddleware<'a, T>
where
T: FnMut(Response, &mut Request) -> Result<()>,
T: FnMut(&mut Response, &mut Request) -> Result<()>,
{
client: &'a Client,
printer: Option<T>,
Expand All @@ -59,7 +64,7 @@ where

impl<'a, T> ClientWithMiddleware<'a, T>
where
T: FnMut(Response, &mut Request) -> Result<()> + 'a,
T: FnMut(&mut Response, &mut Request) -> Result<()> + 'a,
{
pub fn new(client: &'a Client) -> Self {
ClientWithMiddleware {
Expand Down
2 changes: 1 addition & 1 deletion src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl Printer {
let url = response.url().clone();
let content_type =
mime.map_or_else(|| get_content_type(response.headers()), ContentType::from);
let encoding = encoding.or_else(|| get_charset(&response));
let encoding = encoding.or_else(|| get_charset(response));
let compression_type = get_compression_type(response.headers());
let mut body = decompress_stream(response, compression_type);

Expand Down
2 changes: 1 addition & 1 deletion src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Middleware for RedirectFollower {
self.max_redirects
));
}
self.print(&mut ctx, response, &mut next_request)?;
self.print(&mut ctx, &mut response, &mut next_request)?;
request = clone_request(&mut next_request)?;
response = self.next(&mut ctx, next_request)?;
}
Expand Down

0 comments on commit 5c78ddc

Please sign in to comment.