mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-27 04:50:31 +00:00
Remove unneeded bounds on S in FromRequest(Parts) implementations
This doesn't actually allow using non-Send or non-Sync state types, but it makes the docs simpler.
This commit is contained in:
parent
da058db509
commit
05767b986e
@ -84,8 +84,9 @@ pub trait FromRequestParts<S>: Sized {
|
||||
/// #[async_trait]
|
||||
/// impl<S, B> FromRequest<S, B> for MyExtractor
|
||||
/// where
|
||||
/// // these bounds are required by `async_trait`
|
||||
/// // this bound is required by `async_trait`
|
||||
/// B: Send + 'static,
|
||||
/// // this bound is also required if the state parameter is not discarded with `_: &S`
|
||||
/// S: Send + Sync,
|
||||
/// {
|
||||
/// type Rejection = http::StatusCode;
|
||||
|
@ -9,7 +9,6 @@ use std::convert::Infallible;
|
||||
impl<S, B> FromRequest<S, B> for Request<B>
|
||||
where
|
||||
B: Send,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
@ -19,10 +18,7 @@ where
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Method
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for Method {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
@ -31,10 +27,7 @@ where
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Uri
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for Uri {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
@ -43,10 +36,7 @@ where
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Version
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for Version {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
@ -60,10 +50,7 @@ where
|
||||
///
|
||||
/// [`TypedHeader`]: https://docs.rs/axum/latest/axum/extract/struct.TypedHeader.html
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for HeaderMap
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for HeaderMap {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
@ -77,7 +64,6 @@ where
|
||||
B: http_body::Body + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = BytesRejection;
|
||||
|
||||
@ -101,18 +87,13 @@ where
|
||||
B: http_body::Body + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = StringRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let bytes = Bytes::from_request(req, state)
|
||||
.await
|
||||
.map_err(|err| match err {
|
||||
BytesRejection::FailedToBufferBody(inner) => {
|
||||
StringRejection::FailedToBufferBody(inner)
|
||||
}
|
||||
})?;
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let bytes: Bytes = req.extract().await.map_err(|err| match err {
|
||||
BytesRejection::FailedToBufferBody(inner) => StringRejection::FailedToBufferBody(inner),
|
||||
})?;
|
||||
|
||||
let string = std::str::from_utf8(&bytes)
|
||||
.map_err(InvalidUtf8::from_err)?
|
||||
@ -126,7 +107,6 @@ where
|
||||
impl<S, B> FromRequest<S, B> for Parts
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
|
@ -5,10 +5,7 @@ use http::request::{Parts, Request};
|
||||
use std::convert::Infallible;
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for ()
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for () {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(_: &mut Parts, _: &S) -> Result<(), Self::Rejection> {
|
||||
|
@ -141,15 +141,12 @@ mod tests {
|
||||
struct Extractor(Instant);
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Extractor
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for Extractor {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(
|
||||
_parts: &mut Parts,
|
||||
_state: &S,
|
||||
_: &S,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
COUNTER.fetch_add(1, Ordering::SeqCst);
|
||||
Ok(Self(Instant::now()))
|
||||
|
@ -89,13 +89,10 @@ pub struct CookieJar {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for CookieJar
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for CookieJar {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
Ok(Self::from_headers(&parts.headers))
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use axum::{
|
||||
rejection::{FailedToDeserializeQueryString, FormRejection, InvalidFormContentType},
|
||||
FromRequest,
|
||||
},
|
||||
BoxError,
|
||||
BoxError, RequestExt,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use http::{header, HeaderMap, Method, Request};
|
||||
@ -61,11 +61,10 @@ where
|
||||
B: HttpBody + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = FormRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
if req.method() == Method::GET {
|
||||
let query = req.uri().query().unwrap_or_default();
|
||||
let value = serde_html_form::from_str(query)
|
||||
@ -76,7 +75,7 @@ where
|
||||
return Err(InvalidFormContentType::default().into());
|
||||
}
|
||||
|
||||
let bytes = Bytes::from_request(req, state).await?;
|
||||
let bytes: Bytes = req.extract().await?;
|
||||
let value = serde_html_form::from_bytes(&bytes)
|
||||
.map_err(FailedToDeserializeQueryString::__private_new)?;
|
||||
|
||||
|
@ -62,11 +62,10 @@ pub struct Query<T>(pub T);
|
||||
impl<T, S> FromRequestParts<S> for Query<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = QueryRejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let query = parts.uri.query().unwrap_or_default();
|
||||
let value = serde_html_form::from_str(query)
|
||||
.map_err(FailedToDeserializeQueryString::__private_new)?;
|
||||
|
@ -154,15 +154,12 @@ mod tests {
|
||||
struct TestRejection;
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for TestExtractor
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for TestExtractor {
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request_parts(
|
||||
_parts: &mut Parts,
|
||||
_state: &S,
|
||||
_: &S,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
Err(())
|
||||
}
|
||||
|
@ -106,11 +106,10 @@ where
|
||||
B::Data: Into<Bytes>,
|
||||
B::Error: Into<BoxError>,
|
||||
T: DeserializeOwned,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
// `Stream::lines` isn't a thing so we have to convert it into an `AsyncRead`
|
||||
// so we can call `AsyncRead::lines` and then convert it back to a `Stream`
|
||||
let body = BodyStream {
|
||||
|
@ -5,7 +5,7 @@ use axum::{
|
||||
body::{Bytes, HttpBody},
|
||||
extract::{rejection::BytesRejection, FromRequest},
|
||||
response::{IntoResponse, Response},
|
||||
BoxError,
|
||||
BoxError, RequestExt,
|
||||
};
|
||||
use bytes::BytesMut;
|
||||
use http::{Request, StatusCode};
|
||||
@ -103,12 +103,11 @@ where
|
||||
B: HttpBody + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = ProtoBufRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let mut bytes = Bytes::from_request(req, state).await?;
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let mut bytes: Bytes = req.extract().await?;
|
||||
|
||||
match T::decode(&mut bytes) {
|
||||
Ok(value) => Ok(ProtoBuf(value)),
|
||||
|
@ -135,17 +135,14 @@ fn expand_named_fields(
|
||||
let from_request_impl = quote! {
|
||||
#[::axum::async_trait]
|
||||
#[automatically_derived]
|
||||
impl<S> ::axum::extract::FromRequestParts<S> for #ident
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> ::axum::extract::FromRequestParts<S> for #ident {
|
||||
type Rejection = #rejection_assoc_type;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut ::axum::http::request::Parts,
|
||||
state: &S,
|
||||
_: &S,
|
||||
) -> ::std::result::Result<Self, Self::Rejection> {
|
||||
::axum::extract::Path::from_request_parts(parts, state)
|
||||
::axum::extract::Path::from_request_parts(parts, &())
|
||||
.await
|
||||
.map(|path| path.0)
|
||||
#map_err_rejection
|
||||
@ -240,17 +237,14 @@ fn expand_unnamed_fields(
|
||||
let from_request_impl = quote! {
|
||||
#[::axum::async_trait]
|
||||
#[automatically_derived]
|
||||
impl<S> ::axum::extract::FromRequestParts<S> for #ident
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> ::axum::extract::FromRequestParts<S> for #ident {
|
||||
type Rejection = #rejection_assoc_type;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut ::axum::http::request::Parts,
|
||||
state: &S,
|
||||
_: &S,
|
||||
) -> ::std::result::Result<Self, Self::Rejection> {
|
||||
::axum::extract::Path::from_request_parts(parts, state)
|
||||
::axum::extract::Path::from_request_parts(parts, &())
|
||||
.await
|
||||
.map(|path| path.0)
|
||||
#map_err_rejection
|
||||
@ -324,15 +318,12 @@ fn expand_unit_fields(
|
||||
let from_request_impl = quote! {
|
||||
#[::axum::async_trait]
|
||||
#[automatically_derived]
|
||||
impl<S> ::axum::extract::FromRequestParts<S> for #ident
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> ::axum::extract::FromRequestParts<S> for #ident {
|
||||
type Rejection = #rejection_assoc_type;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut ::axum::http::request::Parts,
|
||||
_state: &S,
|
||||
_: &S,
|
||||
) -> ::std::result::Result<Self, Self::Rejection> {
|
||||
if parts.uri.path() == <Self as ::axum_extra::routing::TypedPath>::PATH {
|
||||
Ok(Self)
|
||||
|
@ -1,8 +1,4 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::FromRequest,
|
||||
http::Request,
|
||||
};
|
||||
use axum::{async_trait, extract::FromRequest, http::Request};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
@ -11,11 +7,10 @@ struct A;
|
||||
impl<S, B> FromRequest<S, B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,4 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::FromRequest,
|
||||
http::Request,
|
||||
};
|
||||
use axum::{async_trait, extract::FromRequest, http::Request};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
@ -11,11 +7,10 @@ struct A;
|
||||
impl<S, B> FromRequest<S, B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
@ -116,13 +116,10 @@ impl A {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for A
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for A {
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request_parts(_parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,4 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::FromRequest,
|
||||
http::Request,
|
||||
};
|
||||
use axum::{async_trait, extract::FromRequest, http::Request};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
@ -11,11 +7,10 @@ struct A;
|
||||
impl<S, B> FromRequest<S, B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
@ -24,11 +19,10 @@ where
|
||||
impl<S, B> FromRequest<S, B> for Box<A>
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use axum_macros::debug_handler;
|
||||
use axum::extract::{FromRef, FromRequest};
|
||||
use axum::async_trait;
|
||||
use axum::extract::{FromRef, FromRequest};
|
||||
use axum::http::Request;
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler(state = AppState)]
|
||||
async fn handler(_: A) {}
|
||||
@ -15,12 +15,11 @@ struct A;
|
||||
impl<S, B> FromRequest<S, B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
AppState: FromRef<S>,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{rejection::ExtensionRejection, FromRequest},
|
||||
http::{StatusCode, Request},
|
||||
http::{Request, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
Extension, Router,
|
||||
@ -31,12 +31,11 @@ struct OtherExtractor;
|
||||
impl<S, B> FromRequest<S, B> for OtherExtractor
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
// this rejection doesn't implement `Display` and `Error`
|
||||
type Rejection = (StatusCode, String);
|
||||
|
||||
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
@ -28,14 +28,11 @@ struct MyExtractor {
|
||||
struct OtherExtractor;
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for OtherExtractor
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for OtherExtractor {
|
||||
// this rejection doesn't implement `Display` and `Error`
|
||||
type Rejection = (StatusCode, String);
|
||||
|
||||
async fn from_request_parts(_parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
@ -424,13 +424,10 @@ use axum::{
|
||||
struct ExtractUserAgent(HeaderValue);
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for ExtractUserAgent
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for ExtractUserAgent {
|
||||
type Rejection = (StatusCode, &'static str);
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
if let Some(user_agent) = parts.headers.get(USER_AGENT) {
|
||||
Ok(ExtractUserAgent(user_agent.clone()))
|
||||
} else {
|
||||
@ -456,16 +453,15 @@ If your extractor needs to consume the request body you must implement [`FromReq
|
||||
```rust,no_run
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::FromRequest,
|
||||
response::{Response, IntoResponse},
|
||||
body::Bytes,
|
||||
routing::get,
|
||||
Router,
|
||||
extract::FromRequest,
|
||||
http::{
|
||||
StatusCode,
|
||||
header::{HeaderValue, USER_AGENT},
|
||||
Request,
|
||||
Request, StatusCode,
|
||||
},
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
RequestExt, Router,
|
||||
};
|
||||
|
||||
struct ValidatedBody(Bytes);
|
||||
@ -475,14 +471,11 @@ impl<S, B> FromRequest<S, B> for ValidatedBody
|
||||
where
|
||||
Bytes: FromRequest<S, B>,
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let body = Bytes::from_request(req, state)
|
||||
.await
|
||||
.map_err(IntoResponse::into_response)?;
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let body: Bytes = req.extract().await.map_err(IntoResponse::into_response)?;
|
||||
|
||||
// do validation...
|
||||
|
||||
@ -523,12 +516,11 @@ struct MyExtractor;
|
||||
#[async_trait]
|
||||
impl<S, B> FromRequest<S, B> for MyExtractor
|
||||
where
|
||||
S: Send + Sync,
|
||||
B: Send + 'static,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
// ...
|
||||
# todo!()
|
||||
}
|
||||
@ -536,13 +528,10 @@ where
|
||||
|
||||
// and `FromRequestParts`
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for MyExtractor
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for MyExtractor {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
// ...
|
||||
# todo!()
|
||||
}
|
||||
@ -588,16 +577,13 @@ struct AuthenticatedUser {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for AuthenticatedUser
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for AuthenticatedUser {
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
// You can either call them directly...
|
||||
let TypedHeader(Authorization(token)) =
|
||||
TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state)
|
||||
TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, &())
|
||||
.await
|
||||
.map_err(|err| err.into_response())?;
|
||||
|
||||
|
@ -76,11 +76,10 @@ pub struct Extension<T>(pub T);
|
||||
impl<T, S> FromRequestParts<S> for Extension<T>
|
||||
where
|
||||
T: Clone + Send + Sync + 'static,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = ExtensionRejection;
|
||||
|
||||
async fn from_request_parts(req: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(req: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let value = req
|
||||
.extensions
|
||||
.get::<T>()
|
||||
|
@ -7,6 +7,7 @@
|
||||
use super::{Extension, FromRequestParts};
|
||||
use crate::middleware::AddExtension;
|
||||
use async_trait::async_trait;
|
||||
use axum_core::RequestPartsExt;
|
||||
use http::request::Parts;
|
||||
use hyper::server::conn::AddrStream;
|
||||
use std::{
|
||||
@ -131,13 +132,12 @@ pub struct ConnectInfo<T>(pub T);
|
||||
#[async_trait]
|
||||
impl<S, T> FromRequestParts<S> for ConnectInfo<T>
|
||||
where
|
||||
S: Send + Sync,
|
||||
T: Clone + Send + Sync + 'static,
|
||||
{
|
||||
type Rejection = <Extension<Self> as FromRequestParts<S>>::Rejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let Extension(connect_info) = Extension::<Self>::from_request_parts(parts, state).await?;
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let Extension(connect_info): Extension<Self> = parts.extract().await?;
|
||||
Ok(connect_info)
|
||||
}
|
||||
}
|
||||
|
@ -24,13 +24,10 @@ const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host";
|
||||
pub struct Host(pub String);
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Host
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for Host {
|
||||
type Rejection = HostRejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
if let Some(host) = parse_forwarded(&parts.headers) {
|
||||
return Ok(Host(host.to_owned()));
|
||||
}
|
||||
|
@ -65,13 +65,10 @@ impl MatchedPath {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for MatchedPath
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for MatchedPath {
|
||||
type Rejection = MatchedPathRejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let matched_path = parts
|
||||
.extensions
|
||||
.get::<Self>()
|
||||
|
@ -60,15 +60,14 @@ where
|
||||
B: HttpBody + Send + 'static,
|
||||
B::Data: Into<Bytes>,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = MultipartRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let boundary = parse_boundary(req.headers()).ok_or(InvalidBoundary)?;
|
||||
let stream_result = match req.with_limited_body() {
|
||||
Ok(limited) => BodyStream::from_request(limited, state).await,
|
||||
Err(unlimited) => BodyStream::from_request(unlimited, state).await,
|
||||
Ok(limited) => limited.extract::<BodyStream, _>().await,
|
||||
Err(unlimited) => unlimited.extract::<BodyStream, _>().await,
|
||||
};
|
||||
let stream = stream_result.unwrap_or_else(|err| match err {});
|
||||
let multipart = multer::Multipart::new(stream, boundary);
|
||||
|
@ -170,11 +170,10 @@ impl<T> DerefMut for Path<T> {
|
||||
impl<T, S> FromRequestParts<S> for Path<T>
|
||||
where
|
||||
T: DeserializeOwned + Send,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = PathRejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let params = match parts.extensions.get::<UrlParams>() {
|
||||
Some(UrlParams::Params(params)) => params,
|
||||
Some(UrlParams::InvalidUtf8InPathParam { key }) => {
|
||||
|
@ -53,11 +53,10 @@ pub struct Query<T>(pub T);
|
||||
impl<T, S> FromRequestParts<S> for Query<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = QueryRejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let query = parts.uri.query().unwrap_or_default();
|
||||
let value = serde_urlencoded::from_str(query)
|
||||
.map_err(FailedToDeserializeQueryString::__private_new)?;
|
||||
|
@ -1,5 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use axum_core::extract::FromRequest;
|
||||
use axum_core::{extract::FromRequest, RequestExt};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use http::{Method, Request};
|
||||
|
||||
@ -40,11 +40,10 @@ where
|
||||
B: HttpBody + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = RawFormRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
if req.method() == Method::GET {
|
||||
let mut bytes = BytesMut::new();
|
||||
|
||||
@ -58,7 +57,7 @@ where
|
||||
return Err(InvalidFormContentType.into());
|
||||
}
|
||||
|
||||
Ok(Self(Bytes::from_request(req, state).await?))
|
||||
Ok(Self(req.extract::<Bytes, _>().await?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,13 +28,10 @@ use std::convert::Infallible;
|
||||
pub struct RawQuery(pub Option<String>);
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for RawQuery
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for RawQuery {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let query = parts.uri.query().map(|query| query.to_owned());
|
||||
Ok(Self(query))
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ use crate::{
|
||||
BoxError, Error,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use axum_core::RequestPartsExt;
|
||||
use futures_util::stream::Stream;
|
||||
use http::{request::Parts, Request, Uri};
|
||||
use std::{
|
||||
@ -86,14 +87,12 @@ pub struct OriginalUri(pub Uri);
|
||||
|
||||
#[cfg(feature = "original-uri")]
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for OriginalUri
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for OriginalUri {
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let uri = Extension::<Self>::from_request_parts(parts, state)
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let uri = parts
|
||||
.extract::<Extension<Self>>()
|
||||
.await
|
||||
.unwrap_or_else(|_| Extension(OriginalUri(parts.uri.clone())))
|
||||
.0;
|
||||
@ -151,11 +150,10 @@ where
|
||||
B: HttpBody + Send + 'static,
|
||||
B::Data: Into<Bytes>,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let body = req
|
||||
.into_body()
|
||||
.map_data(Into::into)
|
||||
@ -212,11 +210,10 @@ pub struct RawBody<B = Body>(pub B);
|
||||
impl<S, B> FromRequest<S, B> for RawBody<B>
|
||||
where
|
||||
B: Send,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
Ok(Self(req.into_body()))
|
||||
}
|
||||
}
|
||||
|
@ -276,13 +276,10 @@ impl WebSocketUpgrade {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for WebSocketUpgrade
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for WebSocketUpgrade {
|
||||
type Rejection = WebSocketUpgradeRejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
if parts.method != Method::GET {
|
||||
return Err(MethodNotGet.into());
|
||||
}
|
||||
|
@ -68,11 +68,10 @@ where
|
||||
B: HttpBody + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = FormRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
match req.extract().await {
|
||||
Ok(RawForm(bytes)) => {
|
||||
let value = serde_urlencoded::from_bytes(&bytes)
|
||||
|
@ -4,7 +4,10 @@ use crate::{
|
||||
BoxError,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use axum_core::response::{IntoResponse, Response};
|
||||
use axum_core::{
|
||||
response::{IntoResponse, Response},
|
||||
RequestExt,
|
||||
};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use http::{
|
||||
header::{self, HeaderMap, HeaderValue},
|
||||
@ -106,13 +109,12 @@ where
|
||||
B: HttpBody + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = JsonRejection;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
if json_content_type(req.headers()) {
|
||||
let bytes = Bytes::from_request(req, state).await?;
|
||||
let bytes: Bytes = req.extract().await?;
|
||||
let deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
|
||||
|
||||
let value = match serde_path_to_error::deserialize(deserializer) {
|
||||
|
@ -45,13 +45,10 @@ use tower_service::Service;
|
||||
/// struct RequireAuth;
|
||||
///
|
||||
/// #[async_trait]
|
||||
/// impl<S> FromRequestParts<S> for RequireAuth
|
||||
/// where
|
||||
/// S: Send + Sync,
|
||||
/// {
|
||||
/// impl<S> FromRequestParts<S> for RequireAuth {
|
||||
/// type Rejection = StatusCode;
|
||||
///
|
||||
/// async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
/// let auth_header = parts
|
||||
/// .headers
|
||||
/// .get(header::AUTHORIZATION)
|
||||
@ -370,15 +367,12 @@ mod tests {
|
||||
struct MyExtractor;
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for MyExtractor
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for MyExtractor {
|
||||
type Rejection = std::convert::Infallible;
|
||||
|
||||
async fn from_request_parts(
|
||||
_parts: &mut Parts,
|
||||
_state: &S,
|
||||
_: &S,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
@ -56,11 +56,10 @@ pub struct TypedHeader<T>(pub T);
|
||||
impl<T, S> FromRequestParts<S> for TypedHeader<T>
|
||||
where
|
||||
T: headers::Header,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = TypedHeaderRejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
match parts.headers.typed_try_get::<T>() {
|
||||
Ok(Some(value)) => Ok(Self(value)),
|
||||
Ok(None) => Err(TypedHeaderRejection {
|
||||
|
@ -81,16 +81,11 @@ struct BufferRequestBody(Bytes);
|
||||
|
||||
// we must implement `FromRequest` (and not `FromRequestParts`) to consume the body
|
||||
#[async_trait]
|
||||
impl<S> FromRequest<S, BoxBody> for BufferRequestBody
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequest<S, BoxBody> for BufferRequestBody {
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request(req: Request<BoxBody>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let body = Bytes::from_request(req, state)
|
||||
.await
|
||||
.map_err(|err| err.into_response())?;
|
||||
async fn from_request(req: Request<BoxBody>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let body: Bytes = req.extract().await.map_err(|err| err.into_response())?;
|
||||
|
||||
do_thing_with_request_body(body.clone());
|
||||
|
||||
|
@ -122,13 +122,10 @@ impl AuthBody {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Claims
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for Claims {
|
||||
type Rejection = AuthError;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
// Extract the token from the authorization header
|
||||
let TypedHeader(Authorization(bearer)) = parts
|
||||
.extract::<TypedHeader<Authorization<Bearer>>>()
|
||||
|
@ -60,7 +60,6 @@ enum JsonOrForm<T, K = T> {
|
||||
impl<S, B, T, U> FromRequest<S, B> for JsonOrForm<T, U>
|
||||
where
|
||||
B: Send + 'static,
|
||||
S: Send + Sync,
|
||||
Json<T>: FromRequest<(), B>,
|
||||
Form<U>: FromRequest<(), B>,
|
||||
T: 'static,
|
||||
@ -68,7 +67,7 @@ where
|
||||
{
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let content_type_header = req.headers().get(CONTENT_TYPE);
|
||||
let content_type = content_type_header.and_then(|value| value.to_str().ok());
|
||||
|
||||
|
@ -70,7 +70,7 @@ where
|
||||
type Rejection = ServerError;
|
||||
|
||||
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let Form(value) = Form::<T>::from_request(req, state).await?;
|
||||
let Form(value): Form<T> = req.extract().await?;
|
||||
value.validate()?;
|
||||
Ok(ValidatedForm(value))
|
||||
}
|
||||
|
@ -48,13 +48,10 @@ enum Version {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Version
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
impl<S> FromRequestParts<S> for Version {
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let params: Path<HashMap<String, String>> =
|
||||
parts.extract().await.map_err(IntoResponse::into_response)?;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user