mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-01 00:00:14 +02:00
Remove B type param: Follow ups (#1789)
Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Michael Scofield <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
Michael Scofield
parent
0d50d17304
commit
2ae0cdf7cf
+2
-14
@@ -1,6 +1,5 @@
|
||||
//! HTTP body utilities.
|
||||
|
||||
use crate::response::{IntoResponse, Response};
|
||||
use crate::{BoxError, Error};
|
||||
use bytes::Bytes;
|
||||
use bytes::{Buf, BufMut};
|
||||
@@ -13,14 +12,9 @@ use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use sync_wrapper::SyncWrapper;
|
||||
|
||||
/// A boxed [`Body`] trait object.
|
||||
///
|
||||
/// This is used in axum as the response body type for applications. It's
|
||||
/// necessary to unify multiple response bodies types into one.
|
||||
pub type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;
|
||||
type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;
|
||||
|
||||
/// Convert a [`http_body::Body`] into a [`BoxBody`].
|
||||
pub fn boxed<B>(body: B) -> BoxBody
|
||||
fn boxed<B>(body: B) -> BoxBody
|
||||
where
|
||||
B: http_body::Body<Data = Bytes> + Send + 'static,
|
||||
B::Error: Into<BoxError>,
|
||||
@@ -230,12 +224,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Body {
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_downcast() {
|
||||
assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::body::Body;
|
||||
use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts};
|
||||
use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts, Request};
|
||||
use futures_util::future::BoxFuture;
|
||||
use http::Request;
|
||||
use http_body::Limited;
|
||||
|
||||
mod sealed {
|
||||
@@ -23,9 +22,9 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// ```
|
||||
/// use axum::{
|
||||
/// async_trait,
|
||||
/// extract::FromRequest,
|
||||
/// extract::{Request, FromRequest},
|
||||
/// body::Body,
|
||||
/// http::{header::CONTENT_TYPE, Request, StatusCode},
|
||||
/// http::{header::CONTENT_TYPE, StatusCode},
|
||||
/// response::{IntoResponse, Response},
|
||||
/// Form, Json, RequestExt,
|
||||
/// };
|
||||
@@ -42,7 +41,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// {
|
||||
/// type Rejection = Response;
|
||||
///
|
||||
/// async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// let content_type = req
|
||||
/// .headers()
|
||||
/// .get(CONTENT_TYPE)
|
||||
@@ -87,8 +86,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// use axum::{
|
||||
/// async_trait,
|
||||
/// body::Body,
|
||||
/// extract::{FromRef, FromRequest},
|
||||
/// http::Request,
|
||||
/// extract::{Request, FromRef, FromRequest},
|
||||
/// RequestExt,
|
||||
/// };
|
||||
///
|
||||
@@ -104,7 +102,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// {
|
||||
/// type Rejection = std::convert::Infallible;
|
||||
///
|
||||
/// async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// let requires_state = req.extract_with_state::<RequiresState, _, _>(state).await?;
|
||||
///
|
||||
/// Ok(Self { requires_state })
|
||||
@@ -122,7 +120,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// {
|
||||
/// // ...
|
||||
/// # type Rejection = std::convert::Infallible;
|
||||
/// # async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// # async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// # todo!()
|
||||
/// # }
|
||||
/// }
|
||||
@@ -141,9 +139,8 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// ```
|
||||
/// use axum::{
|
||||
/// async_trait,
|
||||
/// extract::FromRequest,
|
||||
/// extract::{Request, FromRequest},
|
||||
/// headers::{authorization::Bearer, Authorization},
|
||||
/// http::Request,
|
||||
/// response::{IntoResponse, Response},
|
||||
/// body::Body,
|
||||
/// Json, RequestExt, TypedHeader,
|
||||
@@ -163,7 +160,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// {
|
||||
/// type Rejection = Response;
|
||||
///
|
||||
/// async fn from_request(mut req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// async fn from_request(mut req: Request, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// let TypedHeader(auth_header) = req
|
||||
/// .extract_parts::<TypedHeader<Authorization<Bearer>>>()
|
||||
/// .await
|
||||
@@ -194,8 +191,8 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// ```
|
||||
/// use axum::{
|
||||
/// async_trait,
|
||||
/// extract::{FromRef, FromRequest, FromRequestParts},
|
||||
/// http::{request::Parts, Request},
|
||||
/// extract::{Request, FromRef, FromRequest, FromRequestParts},
|
||||
/// http::request::Parts,
|
||||
/// response::{IntoResponse, Response},
|
||||
/// body::Body,
|
||||
/// Json, RequestExt,
|
||||
@@ -216,7 +213,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// {
|
||||
/// type Rejection = Response;
|
||||
///
|
||||
/// async fn from_request(mut req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// async fn from_request(mut req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
/// let requires_state = req
|
||||
/// .extract_parts_with_state::<RequiresState, _>(state)
|
||||
/// .await
|
||||
@@ -260,7 +257,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
/// Apply the [default body limit](crate::extract::DefaultBodyLimit).
|
||||
///
|
||||
/// If it is disabled, return the request as-is in `Err`.
|
||||
fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request<Body>>;
|
||||
fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request>;
|
||||
|
||||
/// Consumes the request, returning the body wrapped in [`Limited`] if a
|
||||
/// [default limit](crate::extract::DefaultBodyLimit) is in place, or not wrapped if the
|
||||
@@ -268,7 +265,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
|
||||
fn into_limited_body(self) -> Result<Limited<Body>, Body>;
|
||||
}
|
||||
|
||||
impl RequestExt for Request<Body> {
|
||||
impl RequestExt for Request {
|
||||
fn extract<E, M>(self) -> BoxFuture<'static, Result<E, E::Rejection>>
|
||||
where
|
||||
E: FromRequest<(), M> + 'static,
|
||||
@@ -321,7 +318,7 @@ impl RequestExt for Request<Body> {
|
||||
})
|
||||
}
|
||||
|
||||
fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request<Body>> {
|
||||
fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request> {
|
||||
// update docs in `axum-core/src/extract/default_body_limit.rs` and
|
||||
// `axum/src/docs/extract.md` if this changes
|
||||
const DEFAULT_LIMIT: usize = 2_097_152; // 2 mb
|
||||
@@ -426,7 +423,7 @@ mod tests {
|
||||
{
|
||||
type Rejection = <String as FromRequest<()>>::Rejection;
|
||||
|
||||
async fn from_request(mut req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(mut req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let RequiresState(from_state) = req.extract_parts_with_state(state).await.unwrap();
|
||||
let method = req.extract_parts().await.unwrap();
|
||||
let body = req.extract().await?;
|
||||
|
||||
@@ -30,22 +30,21 @@ use tower_layer::Layer;
|
||||
/// Router,
|
||||
/// routing::post,
|
||||
/// body::Body,
|
||||
/// extract::{DefaultBodyLimit, RawBody},
|
||||
/// http::Request,
|
||||
/// extract::{Request, DefaultBodyLimit},
|
||||
/// };
|
||||
///
|
||||
/// let app = Router::new()
|
||||
/// .route(
|
||||
/// "/",
|
||||
/// // even with `DefaultBodyLimit` the request body is still just `Body`
|
||||
/// post(|request: Request<Body>| async {}),
|
||||
/// post(|request: Request| async {}),
|
||||
/// )
|
||||
/// .layer(DefaultBodyLimit::max(1024));
|
||||
/// # let _: Router = app;
|
||||
/// ```
|
||||
///
|
||||
/// ```
|
||||
/// use axum::{Router, routing::post, body::Body, extract::RawBody, http::Request};
|
||||
/// use axum::{Router, routing::post, body::Body, extract::Request};
|
||||
/// use tower_http::limit::RequestBodyLimitLayer;
|
||||
/// use http_body::Limited;
|
||||
///
|
||||
@@ -54,7 +53,7 @@ use tower_layer::Layer;
|
||||
/// "/",
|
||||
/// // `RequestBodyLimitLayer` changes the request body type to `Limited<Body>`
|
||||
/// // extracting a different body type wont work
|
||||
/// post(|request: Request<Body>| async {}),
|
||||
/// post(|request: Request| async {}),
|
||||
/// )
|
||||
/// .layer(RequestBodyLimitLayer::new(1024));
|
||||
/// # let _: Router = app;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
use crate::{body::Body, response::IntoResponse};
|
||||
use async_trait::async_trait;
|
||||
use http::{request::Parts, Request};
|
||||
use http::request::Parts;
|
||||
use std::convert::Infallible;
|
||||
|
||||
pub mod rejection;
|
||||
@@ -19,6 +19,10 @@ mod tuple;
|
||||
pub(crate) use self::default_body_limit::DefaultBodyLimitKind;
|
||||
pub use self::{default_body_limit::DefaultBodyLimit, from_ref::FromRef};
|
||||
|
||||
/// Type alias for [`http::Request`] whose body type defaults to [`Body`], the most common body
|
||||
/// type used with axum.
|
||||
pub type Request<T = Body> = http::Request<T>;
|
||||
|
||||
mod private {
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ViaParts {}
|
||||
@@ -78,7 +82,7 @@ pub trait FromRequest<S, M = private::ViaRequest>: Sized {
|
||||
type Rejection: IntoResponse;
|
||||
|
||||
/// Perform the extraction.
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection>;
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -89,7 +93,7 @@ where
|
||||
{
|
||||
type Rejection = <Self as FromRequestParts<S>>::Rejection;
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let (mut parts, _) = req.into_parts();
|
||||
Self::from_request_parts(&mut parts, state).await
|
||||
}
|
||||
@@ -119,7 +123,7 @@ where
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Option<T>, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Option<T>, Self::Rejection> {
|
||||
Ok(T::from_request(req, state).await.ok())
|
||||
}
|
||||
}
|
||||
@@ -145,7 +149,7 @@ where
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
Ok(T::from_request(req, state).await)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use super::{rejection::*, FromRequest, FromRequestParts};
|
||||
use super::{rejection::*, FromRequest, FromRequestParts, Request};
|
||||
use crate::{body::Body, RequestExt};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use http::{request::Parts, HeaderMap, Method, Request, Uri, Version};
|
||||
use http::{request::Parts, HeaderMap, Method, Uri, Version};
|
||||
use std::convert::Infallible;
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequest<S> for Request<Body>
|
||||
impl<S> FromRequest<S> for Request
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<Body>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
|
||||
Ok(req)
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ where
|
||||
{
|
||||
type Rejection = BytesRejection;
|
||||
|
||||
async fn from_request(req: Request<Body>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
|
||||
let bytes = match req.into_limited_body() {
|
||||
Ok(limited_body) => crate::body::to_bytes(limited_body)
|
||||
.await
|
||||
@@ -98,7 +98,7 @@ where
|
||||
{
|
||||
type Rejection = StringRejection;
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let bytes = Bytes::from_request(req, state)
|
||||
.await
|
||||
.map_err(|err| match err {
|
||||
@@ -122,7 +122,19 @@ where
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request<Body>, _: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
|
||||
Ok(req.into_parts().0)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequest<S> for Body
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Infallible;
|
||||
|
||||
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
|
||||
Ok(req.into_body())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use super::{FromRequest, FromRequestParts};
|
||||
use crate::body::Body;
|
||||
use super::{FromRequest, FromRequestParts, Request};
|
||||
use crate::response::{IntoResponse, Response};
|
||||
use async_trait::async_trait;
|
||||
use http::request::{Parts, Request};
|
||||
use http::request::Parts;
|
||||
use std::convert::Infallible;
|
||||
|
||||
#[async_trait]
|
||||
@@ -57,7 +56,7 @@ macro_rules! impl_from_request {
|
||||
{
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let (mut parts, body) = req.into_parts();
|
||||
|
||||
$(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::{IntoResponseParts, Response, ResponseParts};
|
||||
use crate::{body, BoxError};
|
||||
use crate::{body::Body, BoxError};
|
||||
use bytes::{buf::Chain, Buf, Bytes, BytesMut};
|
||||
use http::{
|
||||
header::{self, HeaderMap, HeaderName, HeaderValue},
|
||||
@@ -74,9 +74,9 @@ use std::{
|
||||
/// body,
|
||||
/// routing::get,
|
||||
/// response::{IntoResponse, Response},
|
||||
/// body::Body,
|
||||
/// Router,
|
||||
/// };
|
||||
/// use http_body::Body;
|
||||
/// use http::HeaderMap;
|
||||
/// use bytes::Bytes;
|
||||
/// use std::{
|
||||
@@ -89,7 +89,7 @@ use std::{
|
||||
///
|
||||
/// // First implement `Body` for `MyBody`. This could for example use
|
||||
/// // some custom streaming protocol.
|
||||
/// impl Body for MyBody {
|
||||
/// impl http_body::Body for MyBody {
|
||||
/// type Data = Bytes;
|
||||
/// type Error = Infallible;
|
||||
///
|
||||
@@ -113,7 +113,7 @@ use std::{
|
||||
/// // Now we can implement `IntoResponse` directly for `MyBody`
|
||||
/// impl IntoResponse for MyBody {
|
||||
/// fn into_response(self) -> Response {
|
||||
/// Response::new(body::boxed(self))
|
||||
/// Response::new(Body::new(self))
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
@@ -165,25 +165,31 @@ where
|
||||
B::Error: Into<BoxError>,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
self.map(body::boxed)
|
||||
self.map(Body::new)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for http::response::Parts {
|
||||
fn into_response(self) -> Response {
|
||||
Response::from_parts(self, body::boxed(Empty::new()))
|
||||
Response::from_parts(self, Body::empty())
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Full<Bytes> {
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(body::boxed(self))
|
||||
Response::new(Body::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Empty<Bytes> {
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(body::boxed(self))
|
||||
Response::new(Body::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Body {
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,7 +198,7 @@ where
|
||||
E: Into<BoxError> + 'static,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(body::boxed(self))
|
||||
Response::new(Body::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,7 +207,7 @@ where
|
||||
E: Into<BoxError> + 'static,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(body::boxed(self))
|
||||
Response::new(Body::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +218,7 @@ where
|
||||
B::Error: Into<BoxError>,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(body::boxed(self))
|
||||
Response::new(Body::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +229,7 @@ where
|
||||
E: Into<BoxError>,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
Response::new(body::boxed(self))
|
||||
Response::new(Body::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,7 +280,7 @@ where
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
let (first, second) = self.into_inner();
|
||||
let mut res = Response::new(body::boxed(BytesChainBody {
|
||||
let mut res = Response::new(Body::new(BytesChainBody {
|
||||
first: Some(first),
|
||||
second: Some(second),
|
||||
}));
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//!
|
||||
//! [`axum::response`]: https://docs.rs/axum/latest/axum/response/index.html
|
||||
|
||||
use crate::body::BoxBody;
|
||||
use crate::body::Body;
|
||||
|
||||
mod append_headers;
|
||||
mod into_response;
|
||||
@@ -16,9 +16,9 @@ pub use self::{
|
||||
into_response_parts::{IntoResponseParts, ResponseParts, TryIntoHeaderError},
|
||||
};
|
||||
|
||||
/// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body
|
||||
/// Type alias for [`http::Response`] whose body type defaults to [`Body`], the most common body
|
||||
/// type used with axum.
|
||||
pub type Response<T = BoxBody> = http::Response<T>;
|
||||
pub type Response<T = Body> = http::Response<T>;
|
||||
|
||||
/// An [`IntoResponse`]-based result type that uses [`ErrorResponse`] as the error type.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user