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

ServeDir does not redirect correctly for directories without trailing / when nested #413

Open
Pistonight opened this issue Sep 23, 2023 · 4 comments · May be fixed by #486
Open

ServeDir does not redirect correctly for directories without trailing / when nested #413

Pistonight opened this issue Sep 23, 2023 · 4 comments · May be fixed by #486

Comments

@Pistonight
Copy link

Bug Report

Version

tower-http v0.4.4

Platform

All

Description

This function redirects paths that don't end with / and is a directory to the URI with / appended

async fn maybe_redirect_or_append_path(

However it does not take into account where the service could be nested, therefore producing an invalid redirect.

Example directory:

src/
├ main.rs
└ hello/
  └ index.html

main.rs:

use axum::{Router, Server};
use tower_http::services::ServeDir;

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt::init();

    let serve_dir = ServeDir::new("src");
    let router = Router::new().nest_service("/src", serve_dir);

    let addr = "0.0.0.0:3000".parse().unwrap();

    Server::bind(&addr)
        .serve(router.into_make_service())
        .await
        .unwrap();

}

Going to localhost:3000/src/hello to see the issue
Expected: Redirects to localhost:3000/src/hello/ and returns content at src/hello/index.html
Actual: Redirects to localhost:3000/hello/ and returns Not Found

@jplatte
Copy link
Collaborator

jplatte commented Nov 8, 2023

Nesting is a concept of axum, not tower / tower-http. This is tracked at tokio-rs/axum#1731.

@jplatte jplatte closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2023
@palant
Copy link

palant commented Apr 10, 2024

Unfortunately, this goes beyond axum. ServeDir generally doesn’t support being mounted into a subdirectory which limits its usefulness considerably. This can only be fixed by messing with the redirects produced by ServeDir which is quite frankly a very ugly hack.

@jplatte jplatte reopened this Apr 11, 2024
@jplatte
Copy link
Collaborator

jplatte commented Apr 11, 2024

Yeah, I've thought a bunch about this in the meantime and I think it's probably much easier (and useful beyond axum) to add some prefix to ServeDir (maybe optionally stripping it from the input path, depending on whether you're already using sth. like nesting that does it up-front).

@maxi0604
Copy link

maxi0604 commented May 1, 2024

I have created a PR: #486

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants