From 536b8ca4ec1a6ffbd2403610a8e4ccb93fb41d4c Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 24 Aug 2021 12:13:18 +0200 Subject: [PATCH] Add Redirect::to constructor (#255) The motivation for this is established in the issue it fixes. Resolves #248 --- CHANGELOG.md | 2 +- src/response/redirect.rs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6268cae9..2147c125 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- 👀 +- **added:** Add `Redirect::to` constructor ([#255](https://github.com/tokio-rs/axum/pull/255)) # 0.2.0 (23. August, 2021) diff --git a/src/response/redirect.rs b/src/response/redirect.rs index e6139893..088d6151 100644 --- a/src/response/redirect.rs +++ b/src/response/redirect.rs @@ -29,8 +29,27 @@ pub struct Redirect { } impl Redirect { + /// Create a new [`Redirect`] that uses a [`303 See Other`][mdn] status code. + /// + /// This redirect instructs the client to change the method to GET for the subsequent request + /// to the given `uri`, which is useful after successful form submission, file upload or when + /// you generally don't want the redirected-to page to observe the original request method and + /// body (if non-empty). + /// + /// # Panics + /// + /// If `uri` isn't a valid [`HeaderValue`]. + /// + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303 + pub fn to(uri: Uri) -> Self { + Self::with_status_code(StatusCode::SEE_OTHER, uri) + } + /// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code. /// + /// This has the same behavior as [`Redirect::to`], except it will preserve the original HTTP + /// method and body. + /// /// # Panics /// /// If `uri` isn't a valid [`HeaderValue`]. @@ -53,6 +72,11 @@ impl Redirect { /// Create a new [`Redirect`] that uses a [`302 Found`][mdn] status code. /// + /// This is the same as [`Redirect::temporary`], except the status code is older and thus + /// supported by some legacy applications that doesn't understand the newer one, but some of + /// those applications wrongly apply [`Redirect::to`] (`303 See Other`) semantics for this + /// status code. It should be avoided where possible. + /// /// # Panics /// /// If `uri` isn't a valid [`HeaderValue`].