Accept owned locations in Redirect constructors (#3635)

This commit is contained in:
Joey de Waal
2026-01-30 11:34:46 +00:00
committed by GitHub
parent b1c5ed99c5
commit 7961711fc7
5 changed files with 12 additions and 11 deletions
+1 -1
View File
@@ -379,7 +379,7 @@ where
});
if let Some(new_uri) = new_uri {
Redirect::permanent(&new_uri.to_string()).into_response()
Redirect::permanent(new_uri.to_string()).into_response()
} else {
StatusCode::BAD_REQUEST.into_response()
}
+1
View File
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **changed:** `serve` has an additional generic argument and can now work with any response body
type, not just `axum::body::Body` ([#3205])
- **changed:** Update minimum rust version to 1.80 ([#3620])
- **changed:** `Redirect` constructors now accept any `impl Into<String>` ([#3635])
[#3158]: https://github.com/tokio-rs/axum/pull/3158
[#3261]: https://github.com/tokio-rs/axum/pull/3261
+8 -8
View File
@@ -34,8 +34,8 @@ impl Redirect {
/// [`Redirect::temporary`] should be used instead.
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
pub fn to(uri: &str) -> Self {
Self::with_status_code(StatusCode::SEE_OTHER, uri)
pub fn to(uri: impl Into<String>) -> Self {
Self::with_status_code(StatusCode::SEE_OTHER, uri.into())
}
/// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code.
@@ -44,15 +44,15 @@ impl Redirect {
/// method and body.
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
pub fn temporary(uri: &str) -> Self {
Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri)
pub fn temporary(uri: impl Into<String>) -> Self {
Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri.into())
}
/// Create a new [`Redirect`] that uses a [`308 Permanent Redirect`][mdn] status code.
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
pub fn permanent(uri: &str) -> Self {
Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri)
pub fn permanent(uri: impl Into<String>) -> Self {
Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri.into())
}
/// Returns the HTTP status code of the `Redirect`.
@@ -71,7 +71,7 @@ impl Redirect {
// use the `Location` header, namely `304 Not Modified`.
//
// We're open to adding more constructors upon request, if they make sense :)
fn with_status_code(status_code: StatusCode, uri: &str) -> Self {
fn with_status_code(status_code: StatusCode, uri: String) -> Self {
assert!(
status_code.is_redirection(),
"not a redirection status code"
@@ -79,7 +79,7 @@ impl Redirect {
Self {
status_code,
location: uri.to_owned(),
location: uri,
}
}
}
+1 -1
View File
@@ -120,7 +120,7 @@ where
let redirect = move |uri: Uri| async move {
match make_https(uri, ports.https) {
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
Ok(uri) => Ok(Redirect::permanent(uri.to_string())),
Err(error) => {
tracing::warn!(%error, "failed to convert URI to HTTPS");
Err(StatusCode::BAD_REQUEST)
+1 -1
View File
@@ -86,7 +86,7 @@ async fn redirect_http_to_https(ports: Ports) {
let redirect = move |uri: Uri| async move {
match make_https(uri, ports.https) {
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
Ok(uri) => Ok(Redirect::permanent(uri.to_string())),
Err(error) => {
tracing::warn!(%error, "failed to convert URI to HTTPS");
Err(StatusCode::BAD_REQUEST)