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

Add FixNestedRedirect middleware #2230

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

davidpdrsn
Copy link
Member

This adds FixNestedRedirectLayer which uses NestedPath to change redirects from nested services to include the path they're nested at.

Fixes #1731

This adds `FixNestedRedirectLayer` which uses `NestedPath` to change
redirects from nested services to include the path they're nested at.

Fixes #1731
@georgmu
Copy link

georgmu commented Sep 17, 2023

Wouldn't it be better to fix this in tower-http?
The current implementation in tower-http doesn't allow scenarios to use ServeDir with a prefix (e.g. /files/*).
If such a mechanism would exist, a thin wrapper around ServeDir on axum side would fix this automatically.

@davidpdrsn
Copy link
Member Author

davidpdrsn commented Sep 17, 2023

Wouldn't it be better to fix this in tower-http?

That would require moving axum's NestedPath to tower-http or adding something similar there. I'm not a big fan of that.

FixNestedRedirectLayer is also useful when not using ServeDir.

@davidpdrsn davidpdrsn marked this pull request as draft September 17, 2023 15:54
@davidpdrsn
Copy link
Member Author

Converting to draft because I just remember that this (and NestedPath) doesn't support path params in the nested path. axum supports .nest("/:foo", ...) which this should probably support.

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised this is in axum as opposed to axum-extra. There seem to be quite a few design choices to be made, for instance I would not have thought of including FixNestedRedirectOptOut, but I was wondering whether there could be problems with people applying the middleware multiple times redundantly (more / less nested); if we wanted to solve this having extract remove the NestedPath or insert some other extension do tell that the middleware ran already could work?

@@ -45,6 +45,13 @@ impl NestedPath {
pub fn as_str(&self) -> &str {
&self.0
}

pub(crate) fn extract(parts: &mut Parts) -> Result<Self, NestedPathRejection> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) fn extract(parts: &mut Parts) -> Result<Self, NestedPathRejection> {
pub(crate) fn extract(parts: &Parts) -> Result<Self, NestedPathRejection> {

return Some(());
}

let nested_path = nested_path?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it could be done earlier.

@georgmu
Copy link

georgmu commented Sep 18, 2023

Wouldn't it be better to fix this in tower-http?

That would require moving axum's NestedPath to tower-http or adding something similar there. I'm not a big fan of that.

I wouldn't add NestedPath to tower-http, but give ServeDir the option to specify a "mount point" in the URL tree (like "files", so that GET /files/foo.txt would search for foo.txt, not files/foo.txt).

If ServeDir could do this, a thin wrapper around ServeDir in axum (let's call it NestedServeDir) could use the NestedPath mechanism to apply to instanciate ServeDir with the correct settings.

@davidpdrsn
Copy link
Member Author

I'm a bit surprised this is in axum as opposed to axum-extra.

Yeah I considered putting it in axum-extra. That probably is the better path. I'll move it.

I wouldn't add NestedPath to tower-http, but give ServeDir the option to specify a "mount point" in the URL tree (like "files", so that GET /files/foo.txt would search for foo.txt, not files/foo.txt).

I think I'll move forward with the path in this PR and then see what happens as people use it and give feedback.

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

Successfully merging this pull request may close these issues.

ServeDir in nested route causes invalid redirects
3 participants