From acc17a50890748c4d7e95a612d44e0c1442daf82 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 5 May 2026 15:04:03 +0200 Subject: [PATCH] Revert "refactor: remove duplication between `IntoResponse` and `IntoResponseParts` for `Redirect` (#3743)" This reverts commit 2b38288f613b154d21835f88e8a04a9149e40823. --- axum/src/response/redirect.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/axum/src/response/redirect.rs b/axum/src/response/redirect.rs index 27e183e6..b408e8cd 100644 --- a/axum/src/response/redirect.rs +++ b/axum/src/response/redirect.rs @@ -86,7 +86,10 @@ impl Redirect { impl IntoResponse for Redirect { fn into_response(self) -> Response { - (self, ()).into_response() + match HeaderValue::try_from(self.location) { + Ok(location) => (self.status_code, [(LOCATION, location)]).into_response(), + Err(error) => (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()).into_response(), + } } } @@ -130,17 +133,17 @@ impl IntoResponseParts for Redirect { /// ); /// ``` fn into_response_parts(self, mut res: ResponseParts) -> Result { - match HeaderValue::try_from(self.location) { - Ok(location) => { - *res.status_mut() = self.status_code; - res.headers_mut().insert(LOCATION, location); - Ok(res) - } - Err(err) => Err(( + let location = HeaderValue::try_from(self.location).map_err(|err| { + ( StatusCode::INTERNAL_SERVER_ERROR, format!("invalid redirect location: {err}"), - )), - } + ) + })?; + + *res.status_mut() = self.status_code; + res.headers_mut().insert(LOCATION, location); + + Ok(res) } }