-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
davidpdrsn
wants to merge
1
commit into
main
Choose a base branch
from
david/fix-redirect-middleware
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,351 @@ | ||
use std::convert::Infallible; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use axum_core::response::{IntoResponse, IntoResponseParts, Response, ResponseParts}; | ||
use http::header::LOCATION; | ||
use http::{HeaderValue, Request}; | ||
use pin_project_lite::pin_project; | ||
use tower_layer::Layer; | ||
use tower_service::Service; | ||
|
||
use crate::extract::NestedPath; | ||
|
||
/// Middleware for fixing redirects from nested service to include the path they're nested at. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use axum::{ | ||
/// Router, | ||
/// routing::get, | ||
/// middleware::FixNestedRedirectLayer, | ||
/// response::Redirect, | ||
/// }; | ||
/// | ||
/// let api = Router::new() | ||
/// // redirect from `/old` to `/new` | ||
/// .route("/old", get(|| async { Redirect::to("/new") })) | ||
/// .route("/new", get(|| async { /* ... */ })); | ||
/// | ||
/// let app = Router::new() | ||
/// .nest( | ||
/// "/api", | ||
/// // make sure the redirects include `/api`, i.e. `location: /api/new` | ||
/// api.layer(FixNestedRedirectLayer::default()), | ||
/// ); | ||
/// # let _: Router = app; | ||
/// ``` | ||
/// | ||
/// # Multiple levels of nesting | ||
/// | ||
/// If you're nesting multiple levels of routers make sure to add `FixNestedRedirectLayer` at the | ||
/// inner most level: | ||
/// | ||
/// ``` | ||
/// use axum::{ | ||
/// Router, | ||
/// routing::get, | ||
/// middleware::FixNestedRedirectLayer, | ||
/// response::Redirect, | ||
/// }; | ||
/// | ||
/// let users_api = Router::new() | ||
/// // redirect from `/old` to `/new` | ||
/// .route("/old", get(|| async { Redirect::to("/new") })) | ||
/// .route("/new", get(|| async { /* ... */ })); | ||
/// | ||
/// let api = Router::new() | ||
/// .nest( | ||
/// "/users", | ||
/// // add the middleware at the inner most level | ||
/// users_api.layer(FixNestedRedirectLayer::default()), | ||
/// ); | ||
/// | ||
/// let app = Router::new() | ||
/// // don't add the middleware here | ||
/// .nest("/api", api); | ||
/// # let _: Router = app; | ||
/// ``` | ||
/// | ||
/// # Opt-out | ||
/// | ||
/// Individual handlers can opt-out by including `FixNestedRedirectOptOut` in the response: | ||
/// | ||
/// ``` | ||
/// use axum::{ | ||
/// Router, | ||
/// routing::get, | ||
/// middleware::{FixNestedRedirectLayer, FixNestedRedirectOptOut}, | ||
/// response::Redirect, | ||
/// }; | ||
/// | ||
/// let api = Router::new() | ||
/// .route("/foo", get(|| async { | ||
/// // this redirect will go to `/somewhere` and not `/api/somewhere` | ||
/// (FixNestedRedirectOptOut, Redirect::to("/somewhere")) | ||
/// })); | ||
/// | ||
/// let app = Router::new() | ||
/// .nest( | ||
/// "/api", | ||
/// api.layer(FixNestedRedirectLayer::default()), | ||
/// ); | ||
/// # let _: Router = app; | ||
/// ``` | ||
/// | ||
/// # Using with `ServeDir` | ||
/// | ||
/// `FixNestedRedirectLayer` can also be used with tower-http's [`ServeDir`]: | ||
/// | ||
/// ``` | ||
/// use axum::{ | ||
/// Router, | ||
/// middleware::FixNestedRedirect, | ||
/// }; | ||
/// use tower_http::services::ServeDir; | ||
/// | ||
/// let app = Router::new().nest_service( | ||
/// "/assets", | ||
/// FixNestedRedirect::new(ServeDir::new("/assets")), | ||
/// ); | ||
/// # let _: Router = app; | ||
/// ``` | ||
/// | ||
/// [`ServeDir`]: tower_http::services::ServeDir | ||
#[derive(Clone, Debug, Default)] | ||
#[non_exhaustive] | ||
pub struct FixNestedRedirectLayer; | ||
|
||
impl<S> Layer<S> for FixNestedRedirectLayer { | ||
type Service = FixNestedRedirect<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
FixNestedRedirect::new(inner) | ||
} | ||
} | ||
|
||
/// Service for fixing redirects from nested services. | ||
/// | ||
/// See [`FixNestedRedirectLayer`] for more details. | ||
#[derive(Clone, Debug)] | ||
pub struct FixNestedRedirect<S> { | ||
inner: S, | ||
} | ||
|
||
impl<S> FixNestedRedirect<S> { | ||
/// Create a new `FixNestedRedirect`. | ||
pub fn new(inner: S) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for FixNestedRedirect<S> | ||
where | ||
S: Service<Request<ReqBody>, Response = Response<ResBody>>, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = ResponseFuture<S::Future>; | ||
|
||
#[inline] | ||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, req: Request<ReqBody>) -> Self::Future { | ||
let (mut parts, body) = req.into_parts(); | ||
let nested_path = NestedPath::extract(&mut parts).ok(); | ||
let req = Request::from_parts(parts, body); | ||
ResponseFuture { | ||
future: self.inner.call(req), | ||
nested_path, | ||
} | ||
} | ||
} | ||
|
||
pin_project! { | ||
/// Response future for [`FixNestedRedirect`]. | ||
/// | ||
/// See [`FixNestedRedirectLayer`] for more details. | ||
pub struct ResponseFuture<F> { | ||
#[pin] | ||
future: F, | ||
nested_path: Option<NestedPath>, | ||
} | ||
} | ||
|
||
impl<F, B, E> Future for ResponseFuture<F> | ||
where | ||
F: Future<Output = Result<Response<B>, E>>, | ||
{ | ||
type Output = Result<Response<B>, E>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
match futures_util::ready!(this.future.poll(cx)) { | ||
Ok(res) => { | ||
let (mut parts, body) = res.into_parts(); | ||
if parts.extensions.get::<FixNestedRedirectOptOut>().is_none() { | ||
fix_nested_redirect(&mut parts, this.nested_path.take()); | ||
} | ||
let res = Response::from_parts(parts, body); | ||
Poll::Ready(Ok(res)) | ||
} | ||
Err(err) => Poll::Ready(Err(err)), | ||
} | ||
} | ||
} | ||
|
||
fn fix_nested_redirect( | ||
parts: &mut http::response::Parts, | ||
nested_path: Option<NestedPath>, | ||
) -> Option<()> { | ||
if !parts.status.is_redirection() { | ||
return Some(()); | ||
} | ||
|
||
let location = parts.headers.get(LOCATION)?.to_str().ok()?; | ||
|
||
// not sure if there is a more robust way to detect an absolute uri 🤔 | ||
if location.starts_with("https://") | ||
|| location.starts_with("http://") | ||
|| location.starts_with("//") | ||
{ | ||
return Some(()); | ||
} | ||
|
||
let nested_path = nested_path?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it could be done earlier. |
||
|
||
let new_location = format!("{}{}", nested_path.as_str().trim_end_matches('/'), location); | ||
let new_location = HeaderValue::from_str(&new_location).ok()?; | ||
parts.headers.insert(LOCATION, new_location); | ||
|
||
Some(()) | ||
} | ||
|
||
/// Response extension used to opt-out of [`FixNestedRedirectLayer`] changing the `Location` | ||
/// header. | ||
/// | ||
/// See [`FixNestedRedirectLayer`] for more details. | ||
#[derive(Copy, Clone, Debug)] | ||
pub struct FixNestedRedirectOptOut; | ||
|
||
impl IntoResponseParts for FixNestedRedirectOptOut { | ||
type Error = Infallible; | ||
|
||
fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> { | ||
res.extensions_mut().insert(self); | ||
Ok(res) | ||
} | ||
} | ||
|
||
impl IntoResponse for FixNestedRedirectOptOut { | ||
fn into_response(self) -> Response { | ||
(self, ()).into_response() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use http::StatusCode; | ||
use tower_http::services::ServeDir; | ||
|
||
use crate::{ | ||
middleware::{FixNestedRedirect, FixNestedRedirectLayer, FixNestedRedirectOptOut}, | ||
response::Redirect, | ||
routing::get, | ||
test_helpers::TestClient, | ||
Router, | ||
}; | ||
|
||
#[crate::test] | ||
async fn one_level() { | ||
let api = Router::new().route("/old", get(|| async { Redirect::to("/new") })); | ||
let app = Router::new().nest("/api", api.layer(FixNestedRedirectLayer)); | ||
|
||
let client = TestClient::new(app); | ||
|
||
let res = client.get("/api/old").send().await; | ||
assert_eq!(res.status(), StatusCode::SEE_OTHER); | ||
assert_eq!(res.headers()["location"], "/api/new"); | ||
} | ||
|
||
#[crate::test] | ||
async fn one_level_with_trailing_slash() { | ||
let api = Router::new().route("/old", get(|| async { Redirect::to("/new") })); | ||
let app = Router::new().nest("/api/", api.layer(FixNestedRedirectLayer)); | ||
|
||
let client = TestClient::new(app); | ||
|
||
let res = client.get("/api/old").send().await; | ||
assert_eq!(res.status(), StatusCode::SEE_OTHER); | ||
assert_eq!(res.headers()["location"], "/api/new"); | ||
} | ||
|
||
#[crate::test] | ||
async fn two_levels() { | ||
let users = Router::new().route("/old", get(|| async { Redirect::to("/new") })); | ||
let api = Router::new().nest("/users", users.layer(FixNestedRedirectLayer)); | ||
let app = Router::new().nest("/api", api); | ||
|
||
let client = TestClient::new(app); | ||
|
||
let res = client.get("/api/users/old").send().await; | ||
assert_eq!(res.status(), StatusCode::SEE_OTHER); | ||
assert_eq!(res.headers()["location"], "/api/users/new"); | ||
} | ||
|
||
#[crate::test] | ||
async fn opt_out() { | ||
let api = Router::new().route( | ||
"/old", | ||
get(|| async { | ||
( | ||
FixNestedRedirectOptOut, | ||
Redirect::to("/other/non/api/route"), | ||
) | ||
}), | ||
); | ||
let app = Router::new().nest("/api", api.layer(FixNestedRedirectLayer)); | ||
|
||
let client = TestClient::new(app); | ||
|
||
let res = client.get("/api/old").send().await; | ||
assert_eq!(res.status(), StatusCode::SEE_OTHER); | ||
assert_eq!(res.headers()["location"], "/other/non/api/route"); | ||
} | ||
|
||
#[crate::test] | ||
async fn absolute_uri() { | ||
let api = Router::new() | ||
.route("/old", get(|| async { Redirect::to("http://example.com") })) | ||
.route("/old2", get(|| async { Redirect::to("//example.com") })); | ||
let app = Router::new().nest("/api", api.layer(FixNestedRedirectLayer)); | ||
|
||
let client = TestClient::new(app); | ||
|
||
let res = client.get("/api/old").send().await; | ||
assert_eq!(res.status(), StatusCode::SEE_OTHER); | ||
assert_eq!(res.headers()["location"], "http://example.com"); | ||
|
||
let res = client.get("/api/old2").send().await; | ||
assert_eq!(res.status(), StatusCode::SEE_OTHER); | ||
assert_eq!(res.headers()["location"], "//example.com"); | ||
} | ||
|
||
#[crate::test] | ||
async fn using_serve_dir() { | ||
let app = Router::new().nest_service( | ||
"/public", | ||
FixNestedRedirect::new(ServeDir::new(std::env::var("CARGO_MANIFEST_DIR").unwrap())), | ||
); | ||
|
||
let client = TestClient::new(app); | ||
|
||
let res = client.get("/public/src").send().await; | ||
assert!(res.status().is_redirection()); | ||
assert_eq!(res.headers()["location"], "/public/src/"); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.