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

URL path with trailing slash does not match wildcard if a route without a trailing slash exists #1986

Closed
1 task done
ColonelThirtyTwo opened this issue May 4, 2023 · 4 comments
Labels
C-bug Category: This is a bug. S-blocked Status: marked as blocked ❌ on something else such as a PR or other implementation work.

Comments

@ColonelThirtyTwo
Copy link

ColonelThirtyTwo commented May 4, 2023

  • I have looked for existing issues (including closed) about this

Bug Report

Version

Axum 0.6.18

Platform

Arch Linux x86_64

Description

Example program:

use axum::{Router, routing::get};

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/path/foo", get(path_foo))
        .route("/path/*rest", get(wildcard))
        .fallback(fallback);

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn wildcard() -> &'static str {
    "Wildcard route!"
}

async fn path_foo() -> &'static str {
    "path/foo route!"
}

async fn fallback() -> &'static str {
    "Fallback route!"
}

Requesting /path/foo/ (note trailing slash that is not in the route) should match the wildcard route /path/*rest, but instead, the fallback route is ran. Commenting out the /path/foo route causes the wildcard route to be taken instead of the fallback.

@ColonelThirtyTwo
Copy link
Author

May be related - if there is a route with a normal parameter at the end of it, and the request path is any descendant of that route, the fallback will be taken, even if a wildcard matches:

use axum::{Router, routing::get, extract::Path};

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/path/foo/:arg", get(path_foo))
        .route("/path/*rest", get(wildcard))
        .fallback(fallback);

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn wildcard() -> &'static str {
    "Wildcard route!"
}

async fn path_foo(Path(arg): Path<String>) -> String {
    format!("path/arg route, arg: {}", arg)
}

async fn fallback() -> &'static str {
    "Fallback route!"
}
> curl localhost:3000/path/foo/arg
path/arg route, arg: arg⏎
> curl localhost:3000/path/foo/arg/
Fallback route!⏎
> curl localhost:3000/path/foo/arg/bar
Fallback route!⏎

Changing "/path/foo/:arg" to "/path/foo/:arg/" causes the wildcard route to be taken for descendant paths:

> curl localhost:3000/path/foo/arg/
path/arg route, arg: arg⏎
> curl localhost:3000/path/foo/arg/bar
Wildcard route!⏎

@jplatte jplatte added the C-bug Category: This is a bug. label May 7, 2023
@jplatte
Copy link
Member

jplatte commented May 7, 2023

This seems like it's probably a bug in matchit, which axum uses for routing. It would be nice if somebody could try reproducing with just that, and reporting a bug there if reproduction is successful. (and please link to the new issue here then)

@ColonelThirtyTwo
Copy link
Author

Filed ibraheemdev/matchit#31

@davidpdrsn
Copy link
Member

Thanks! In that case I'll close this issue since its not actionable from axum.

@davidpdrsn davidpdrsn closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2023
@davidpdrsn davidpdrsn added the S-blocked Status: marked as blocked ❌ on something else such as a PR or other implementation work. label May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. S-blocked Status: marked as blocked ❌ on something else such as a PR or other implementation work.
Projects
None yet
Development

No branches or pull requests

3 participants