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

Not found error translate to method not allowed when there is route with method is at the start of the route #1104

Open
rluvaton opened this issue Aug 4, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@rluvaton
Copy link

rluvaton commented Aug 4, 2024

Maybe related to #1017

Version
v0.3.7

Platform

Darwin MacBook-Pro.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64

Description
Reproduction Repo

When some route return NOT FOUND (404) I get METHOD NOT ALLOWED (405) when there is a route that have the method at the start of the paths instead of at the end:

When put is at the start:

let problematic_route = warp::put()
        .and(warp::path("api"))
        .map(move || {
            "Hello".to_string()
        });

VS when `put is at the end:

let problematic_route = warp::path("api")
    .and(warp::put())
    .map(move || {
        "Hello".to_string()
    });

I did not see anywhere that putting the method at the start will cause this which is why I'm considering it as a bug


The following is working:

let crates = warp::path("b")
      // Simulating .and(warp::fs::dir(<some-dir>)) for simplicity
      .and(warp::get())
      .and(custom_filter())

      .with(warp::trace::request());

  let problematic_route = warp::path("api")
      .and(warp::put())
      .map(move || {
          "Hello".to_string()
      });


  let routes = problematic_route
      .or(crates);

Almost complete code

The following does not work:

let crates = warp::path("b")
      // Simulating .and(warp::fs::dir(<some-dir>)) for simplicity
      .and(warp::get())
      .and(custom_filter())

      .with(warp::trace::request());

  // <-------- This does not work
  let problematic_route =
      warp::put()
          .and(warp::path("api"))
          .map(move || {
              "Hello".to_string()
          });

  let routes = problematic_route
      .or(crates);

Custom filter is:

fn custom_filter() -> impl Filter<Extract=(String,), Error=Rejection> + Copy {
    warp::path::tail()

		// I use `and_then` to simulate warp::fs::dir(<some-dir>) which does that as well
        .and_then(|s: Tail| async move {
            let route = s.as_str();

            if !route.ends_with("something") {
                return Err(reject::not_found());
            }

            Ok(route.to_string())
        })
}
@arampp-xitaso
Copy link

Thank you for explaining the workaround. I have the same problem, the workaround helps.
I'd be interested to know what the root-cause of this issue is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants