mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-04 00:00:21 +02:00
Accept &str for Redirect (#889)
* Accept &str for Redirect * Fix doc example * fix more docs examples * update changelog * fix changelog label Co-authored-by: David Pedersen <[email protected]>
This commit is contained in:
co-authored by
David Pedersen
parent
79501afd10
commit
3084dc10ca
@@ -1,5 +1,5 @@
|
||||
use axum_core::response::{IntoResponse, Response};
|
||||
use http::{header::LOCATION, HeaderValue, StatusCode, Uri};
|
||||
use http::{header::LOCATION, HeaderValue, StatusCode};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
/// Response that redirects the request to another location.
|
||||
@@ -14,7 +14,7 @@ use std::convert::TryFrom;
|
||||
/// };
|
||||
///
|
||||
/// let app = Router::new()
|
||||
/// .route("/old", get(|| async { Redirect::permanent("/new".parse().unwrap()) }))
|
||||
/// .route("/old", get(|| async { Redirect::permanent("/new") }))
|
||||
/// .route("/new", get(|| async { "Hello!" }));
|
||||
/// # async {
|
||||
/// # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
||||
@@ -40,7 +40,7 @@ impl Redirect {
|
||||
/// 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 {
|
||||
pub fn to(uri: &str) -> Self {
|
||||
Self::with_status_code(StatusCode::SEE_OTHER, uri)
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ impl Redirect {
|
||||
/// If `uri` isn't a valid [`HeaderValue`].
|
||||
///
|
||||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
|
||||
pub fn temporary(uri: Uri) -> Self {
|
||||
pub fn temporary(uri: &str) -> Self {
|
||||
Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri)
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ impl Redirect {
|
||||
/// If `uri` isn't a valid [`HeaderValue`].
|
||||
///
|
||||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
|
||||
pub fn permanent(uri: Uri) -> Self {
|
||||
pub fn permanent(uri: &str) -> Self {
|
||||
Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri)
|
||||
}
|
||||
|
||||
@@ -73,7 +73,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: Uri) -> Self {
|
||||
fn with_status_code(status_code: StatusCode, uri: &str) -> Self {
|
||||
assert!(
|
||||
status_code.is_redirection(),
|
||||
"not a redirection status code"
|
||||
@@ -81,8 +81,7 @@ impl Redirect {
|
||||
|
||||
Self {
|
||||
status_code,
|
||||
location: HeaderValue::try_from(uri.to_string())
|
||||
.expect("URI isn't a valid header value"),
|
||||
location: HeaderValue::try_from(uri).expect("URI isn't a valid header value"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-32
@@ -9,7 +9,7 @@ use crate::{
|
||||
util::try_downcast,
|
||||
BoxError,
|
||||
};
|
||||
use http::{Request, Uri};
|
||||
use http::Request;
|
||||
use matchit::MatchError;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
@@ -503,10 +503,10 @@ where
|
||||
match self.node.at(&path) {
|
||||
Ok(match_) => self.call_route(match_, req),
|
||||
Err(MatchError::MissingTrailingSlash) => RouteFuture::from_response(
|
||||
Redirect::permanent(with_path(req.uri(), &format!("{}/", path))).into_response(),
|
||||
Redirect::permanent(&format!("{}/", req.uri().to_string())).into_response(),
|
||||
),
|
||||
Err(MatchError::ExtraTrailingSlash) => RouteFuture::from_response(
|
||||
Redirect::permanent(with_path(req.uri(), path.strip_suffix('/').unwrap()))
|
||||
Redirect::permanent(&req.uri().to_string().strip_suffix('/').unwrap())
|
||||
.into_response(),
|
||||
),
|
||||
Err(MatchError::NotFound) => match &self.fallback {
|
||||
@@ -517,35 +517,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn with_path(uri: &Uri, new_path: &str) -> Uri {
|
||||
let path_and_query = if let Some(path_and_query) = uri.path_and_query() {
|
||||
let new_path = if new_path.starts_with('/') {
|
||||
Cow::Borrowed(new_path)
|
||||
} else {
|
||||
Cow::Owned(format!("/{}", new_path))
|
||||
};
|
||||
|
||||
if let Some(query) = path_and_query.query() {
|
||||
Some(
|
||||
format!("{}?{}", new_path, query)
|
||||
.parse::<http::uri::PathAndQuery>()
|
||||
.unwrap(),
|
||||
)
|
||||
} else {
|
||||
Some(new_path.parse().unwrap())
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut parts = http::uri::Parts::default();
|
||||
parts.scheme = uri.scheme().cloned();
|
||||
parts.authority = uri.authority().cloned();
|
||||
parts.path_and_query = path_and_query;
|
||||
|
||||
Uri::from_parts(parts).unwrap()
|
||||
}
|
||||
|
||||
/// Wrapper around `matchit::Router` that supports merging two `Router`s.
|
||||
#[derive(Clone, Default)]
|
||||
struct Node {
|
||||
|
||||
Reference in New Issue
Block a user