http-rs / http-rs/surf

Enabling redirect middleware always sends two requests

Open
#307 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.5k
Forks
128
PR merge metrics
No merged PRs in 30d

Description

Hey all, just started playing around with rust and ran into this issue. Even when there is no redirect code given in a response, I'm still seeing multiple requests being dispatched. Based on reading the code with my naive understanding, this might be expected currently? The middleware itself is generating a new request by cloning the original user defined request causing both to be dispatched.

Is this a limitation of the middleware and the ability to send partial requests as mentioned in the middleware? Is it assumed that the middleware itself is the only thing that should be dispatching requests and this is a bug? This happens regardless of there being a redirect code so I figure its related to the cloning of the request.

Just trying to understand a bit more about the machinery since it seems there needs to be a way to have response interception internally that isn't present in the middleware unless I'm still too naive.

Wishing you all well, thanks for all the good modules here in rust land.

Example code with some extra from my test code in case relevant:

use surf;
use cookie::{CookieJar,Cookie};

#[async_std::main]
async fn main() -> surf::Result<()> {
  let mut jar = CookieJar::new();
  let cook = Cookie::build("name", "value").finish();
  jar.add(cook);
  let cookies: String  = jar.iter().map(|cookie| cookie.to_string()).collect::<Vec<String>>().join("; ");

  // This currently sends multiple requests by default with the redirect
  // middleware enabled
  let req = surf::get("http://localhost:18000/auth/signin").header("cookie", cookies).build();
  let client = surf::client().with(surf::middleware::Redirect::default());
  let mut res = client.send(req).await?;

  dbg!(res.body_string().await?);
  Ok(())
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with surf::middleware::Redirect and the client.send(req) path shown in the example; inspect how the middleware clones and dispatches the request when no redirect status is present. Reproduce the behavior against the localhost endpoint and verify that a non-redirect response results in only one request.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.