mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-31 00:00:10 +02:00
Only allow last extractor to mutate the request (#1272)
* Only allow last extractor to mutate the request * Change `FromRequest` and add `FromRequestParts` trait (#1275) * Add `Once`/`Mut` type parameter for `FromRequest` and `RequestParts` * 🪄 * split traits * `FromRequest` for tuples * Remove `BodyAlreadyExtracted` * don't need fully qualified path * don't export `Once` and `Mut` * remove temp tests * depend on axum again Co-authored-by: Jonas Platte <[email protected]> * Port `Handler` and most extractors (#1277) * Port `Handler` and most extractors * Put `M` inside `Handler` impls, not trait itself * comment out tuples for now * fix lints * Reorder arguments to `Handler` (#1281) I think `Request<B>, Arc<S>` is better since its consistent with `FromRequest` and `FromRequestParts`. * Port most things in axum-extra (#1282) * Port `#[derive(TypedPath)]` and `#[debug_handler]` (#1283) * port #[derive(TypedPath)] * wip: #[debug_handler] * fix #[debug_handler] * don't need itertools * also require `Send` * update expected error * support fully qualified `self` * Implement FromRequest[Parts] for tuples (#1286) * Port docs for axum and axum-core (#1285) * Port axum-extra (#1287) * Port axum-extra * Update axum-core/Cargo.toml Co-authored-by: Jonas Platte <[email protected]> * remove `impl FromRequest for Either*` Co-authored-by: Jonas Platte <[email protected]> * New FromRequest[Parts] trait cleanup (#1288) * Make private module truly private again * Simplify tuple FromRequest implementation * Port `#[derive(FromRequest)]` (#1289) * fix tests * fix docs * revert examples * fix docs link * fix intra docs links * Port examples (#1291) * Document wrapping other extractors (#1292) * axum-extra doesn't need to depend on axum-core (#1294) Missed this in https://github.com/tokio-rs/axum/pull/1287 * Add `FromRequest` changes to changelogs (#1293) * Update changelog * Remove default type for `S` in `Handler` * Clarify which types have default types for `S` * Apply suggestions from code review Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Jonas Platte <[email protected]> * remove unused import * Rename `Mut` and `Once` (#1296) * fix trybuild expected output Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
parent
f1769e5134
commit
be624306f4
@@ -1,7 +1,7 @@
|
||||
//! Additional handler utilities.
|
||||
|
||||
use axum::{
|
||||
extract::{FromRequest, RequestParts},
|
||||
extract::FromRequest,
|
||||
handler::Handler,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
@@ -26,8 +26,8 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
|
||||
/// Call the handler with the extracted inputs.
|
||||
fn call(
|
||||
self,
|
||||
state: Arc<S>,
|
||||
extractors: T,
|
||||
state: Arc<S>,
|
||||
) -> <Self as HandlerCallWithExtractors<T, S, B>>::Future;
|
||||
|
||||
/// Conver this `HandlerCallWithExtractors` into [`Handler`].
|
||||
@@ -51,7 +51,7 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
|
||||
/// Router,
|
||||
/// async_trait,
|
||||
/// routing::get,
|
||||
/// extract::FromRequest,
|
||||
/// extract::FromRequestParts,
|
||||
/// };
|
||||
///
|
||||
/// // handlers for varying levels of access
|
||||
@@ -71,14 +71,13 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
|
||||
/// struct AdminPermissions {}
|
||||
///
|
||||
/// #[async_trait]
|
||||
/// impl<S, B> FromRequest<S, B> for AdminPermissions
|
||||
/// impl<S> FromRequestParts<S> for AdminPermissions
|
||||
/// where
|
||||
/// B: Send,
|
||||
/// S: Send + Sync,
|
||||
/// {
|
||||
/// // check for admin permissions...
|
||||
/// # type Rejection = ();
|
||||
/// # async fn from_request(req: &mut axum::extract::RequestParts<S, B>) -> Result<Self, Self::Rejection> {
|
||||
/// # async fn from_request_parts(parts: &mut http::request::Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// # todo!()
|
||||
/// # }
|
||||
/// }
|
||||
@@ -86,14 +85,13 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
|
||||
/// struct User {}
|
||||
///
|
||||
/// #[async_trait]
|
||||
/// impl<S, B> FromRequest<S, B> for User
|
||||
/// impl<S> FromRequestParts<S> for User
|
||||
/// where
|
||||
/// B: Send,
|
||||
/// S: Send + Sync,
|
||||
/// {
|
||||
/// // check for a logged in user...
|
||||
/// # type Rejection = ();
|
||||
/// # async fn from_request(req: &mut axum::extract::RequestParts<S, B>) -> Result<Self, Self::Rejection> {
|
||||
/// # async fn from_request_parts(parts: &mut http::request::Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// # todo!()
|
||||
/// # }
|
||||
/// }
|
||||
@@ -121,27 +119,27 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
|
||||
}
|
||||
|
||||
macro_rules! impl_handler_call_with {
|
||||
( $($ty:ident),* $(,)? ) => {
|
||||
#[allow(non_snake_case)]
|
||||
impl<F, Fut, S, B, $($ty,)*> HandlerCallWithExtractors<($($ty,)*), S, B> for F
|
||||
where
|
||||
F: FnOnce($($ty,)*) -> Fut,
|
||||
Fut: Future + Send + 'static,
|
||||
Fut::Output: IntoResponse,
|
||||
{
|
||||
// this puts `futures_util` in our public API but thats fine in axum-extra
|
||||
type Future = Map<Fut, fn(Fut::Output) -> Response>;
|
||||
( $($ty:ident),* $(,)? ) => {
|
||||
#[allow(non_snake_case)]
|
||||
impl<F, Fut, S, B, $($ty,)*> HandlerCallWithExtractors<($($ty,)*), S, B> for F
|
||||
where
|
||||
F: FnOnce($($ty,)*) -> Fut,
|
||||
Fut: Future + Send + 'static,
|
||||
Fut::Output: IntoResponse,
|
||||
{
|
||||
// this puts `futures_util` in our public API but thats fine in axum-extra
|
||||
type Future = Map<Fut, fn(Fut::Output) -> Response>;
|
||||
|
||||
fn call(
|
||||
self,
|
||||
_state: Arc<S>,
|
||||
($($ty,)*): ($($ty,)*),
|
||||
) -> <Self as HandlerCallWithExtractors<($($ty,)*), S, B>>::Future {
|
||||
self($($ty,)*).map(IntoResponse::into_response)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
fn call(
|
||||
self,
|
||||
($($ty,)*): ($($ty,)*),
|
||||
_state: Arc<S>,
|
||||
) -> <Self as HandlerCallWithExtractors<($($ty,)*), S, B>>::Future {
|
||||
self($($ty,)*).map(IntoResponse::into_response)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_handler_call_with!();
|
||||
impl_handler_call_with!(T1);
|
||||
@@ -180,11 +178,10 @@ where
|
||||
{
|
||||
type Future = BoxFuture<'static, Response>;
|
||||
|
||||
fn call(self, state: Arc<S>, req: http::Request<B>) -> Self::Future {
|
||||
fn call(self, req: http::Request<B>, state: Arc<S>) -> Self::Future {
|
||||
Box::pin(async move {
|
||||
let mut req = RequestParts::with_state_arc(Arc::clone(&state), req);
|
||||
match req.extract::<T>().await {
|
||||
Ok(t) => self.handler.call(state, t).await,
|
||||
match T::from_request(req, &state).await {
|
||||
Ok(t) => self.handler.call(t, state).await,
|
||||
Err(rejection) => rejection.into_response(),
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user