mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Accept owned locations in Redirect constructors (#3635)
This commit is contained in:
@@ -379,7 +379,7 @@ where
|
|||||||
});
|
});
|
||||||
|
|
||||||
if let Some(new_uri) = new_uri {
|
if let Some(new_uri) = new_uri {
|
||||||
Redirect::permanent(&new_uri.to_string()).into_response()
|
Redirect::permanent(new_uri.to_string()).into_response()
|
||||||
} else {
|
} else {
|
||||||
StatusCode::BAD_REQUEST.into_response()
|
StatusCode::BAD_REQUEST.into_response()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
- **changed:** `serve` has an additional generic argument and can now work with any response body
|
||||||
type, not just `axum::body::Body` ([#3205])
|
type, not just `axum::body::Body` ([#3205])
|
||||||
- **changed:** Update minimum rust version to 1.80 ([#3620])
|
- **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
|
[#3158]: https://github.com/tokio-rs/axum/pull/3158
|
||||||
[#3261]: https://github.com/tokio-rs/axum/pull/3261
|
[#3261]: https://github.com/tokio-rs/axum/pull/3261
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ impl Redirect {
|
|||||||
/// [`Redirect::temporary`] should be used instead.
|
/// [`Redirect::temporary`] should be used instead.
|
||||||
///
|
///
|
||||||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
|
||||||
pub fn to(uri: &str) -> Self {
|
pub fn to(uri: impl Into<String>) -> Self {
|
||||||
Self::with_status_code(StatusCode::SEE_OTHER, uri)
|
Self::with_status_code(StatusCode::SEE_OTHER, uri.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code.
|
/// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code.
|
||||||
@@ -44,15 +44,15 @@ impl Redirect {
|
|||||||
/// method and body.
|
/// method and body.
|
||||||
///
|
///
|
||||||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
|
||||||
pub fn temporary(uri: &str) -> Self {
|
pub fn temporary(uri: impl Into<String>) -> Self {
|
||||||
Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri)
|
Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new [`Redirect`] that uses a [`308 Permanent Redirect`][mdn] status code.
|
/// 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
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
|
||||||
pub fn permanent(uri: &str) -> Self {
|
pub fn permanent(uri: impl Into<String>) -> Self {
|
||||||
Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri)
|
Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the HTTP status code of the `Redirect`.
|
/// Returns the HTTP status code of the `Redirect`.
|
||||||
@@ -71,7 +71,7 @@ impl Redirect {
|
|||||||
// use the `Location` header, namely `304 Not Modified`.
|
// use the `Location` header, namely `304 Not Modified`.
|
||||||
//
|
//
|
||||||
// We're open to adding more constructors upon request, if they make sense :)
|
// 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!(
|
assert!(
|
||||||
status_code.is_redirection(),
|
status_code.is_redirection(),
|
||||||
"not a redirection status code"
|
"not a redirection status code"
|
||||||
@@ -79,7 +79,7 @@ impl Redirect {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
status_code,
|
status_code,
|
||||||
location: uri.to_owned(),
|
location: uri,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ where
|
|||||||
|
|
||||||
let redirect = move |uri: Uri| async move {
|
let redirect = move |uri: Uri| async move {
|
||||||
match make_https(uri, ports.https) {
|
match make_https(uri, ports.https) {
|
||||||
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
|
Ok(uri) => Ok(Redirect::permanent(uri.to_string())),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
||||||
Err(StatusCode::BAD_REQUEST)
|
Err(StatusCode::BAD_REQUEST)
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ async fn redirect_http_to_https(ports: Ports) {
|
|||||||
|
|
||||||
let redirect = move |uri: Uri| async move {
|
let redirect = move |uri: Uri| async move {
|
||||||
match make_https(uri, ports.https) {
|
match make_https(uri, ports.https) {
|
||||||
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
|
Ok(uri) => Ok(Redirect::permanent(uri.to_string())),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
||||||
Err(StatusCode::BAD_REQUEST)
|
Err(StatusCode::BAD_REQUEST)
|
||||||
|
|||||||
Reference in New Issue
Block a user