mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-30 00:00:32 +02:00
@@ -138,7 +138,7 @@
|
||||
#![cfg_attr(test, allow(clippy::float_cmp))]
|
||||
|
||||
use axum::{
|
||||
body::{box_body, BoxBody, Bytes, Full, HttpBody},
|
||||
body::{boxed, BoxBody, Bytes, Full, HttpBody},
|
||||
extract::{FromRequest, RequestParts},
|
||||
http::{Request, Response, StatusCode},
|
||||
response::IntoResponse,
|
||||
@@ -281,8 +281,8 @@ where
|
||||
|
||||
let future = Box::pin(async move {
|
||||
match inner.oneshot(req).await {
|
||||
Ok(res) => Ok(res.map(box_body)),
|
||||
Err(err) => Ok(f(err).await.into_response().map(box_body)),
|
||||
Ok(res) => Ok(res.map(boxed)),
|
||||
Err(err) => Ok(f(err).await.into_response().map(boxed)),
|
||||
}
|
||||
});
|
||||
|
||||
@@ -329,7 +329,7 @@ macro_rules! impl_service {
|
||||
$(
|
||||
let $ty = match $ty::from_request(&mut req).await {
|
||||
Ok(value) => value,
|
||||
Err(rejection) => return Ok(rejection.into_response().map(box_body)),
|
||||
Err(rejection) => return Ok(rejection.into_response().map(boxed)),
|
||||
};
|
||||
)*
|
||||
|
||||
@@ -338,14 +338,14 @@ macro_rules! impl_service {
|
||||
Err(err) => {
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(box_body(Full::from(err.to_string())))
|
||||
.body(boxed(Full::from(err.to_string())))
|
||||
.unwrap());
|
||||
}
|
||||
};
|
||||
|
||||
match inner.oneshot(req).await {
|
||||
Ok(res) => Ok(res.map(box_body)),
|
||||
Err(err) => Ok(f($($ty),*, err).await.into_response().map(box_body)),
|
||||
Ok(res) => Ok(res.map(boxed)),
|
||||
Err(err) => Ok(f($($ty),*, err).await.into_response().map(boxed)),
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+3
-2
@@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
# Unreleased
|
||||
|
||||
- None.
|
||||
- **change:** `box_body` has been renamed to `boxed`. `box_body` still exists
|
||||
but is deprecated.
|
||||
|
||||
# 0.3.3 (13. November, 2021)
|
||||
|
||||
@@ -490,7 +491,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Improve error message of `MissingExtension` rejections ([#72](https://github.com/tokio-rs/axum/pull/72))
|
||||
- Improve documentation for routing ([#71](https://github.com/tokio-rs/axum/pull/71))
|
||||
- Clarify required response body type when routing to `tower::Service`s ([#69](https://github.com/tokio-rs/axum/pull/69))
|
||||
- Add `axum::body::box_body` to converting an `http_body::Body` to `axum::body::BoxBody` ([#69](https://github.com/tokio-rs/axum/pull/69))
|
||||
- Add `axum::body::boxed` to converting an `http_body::Body` to `axum::body::BoxBody` ([#69](https://github.com/tokio-rs/axum/pull/69))
|
||||
- Add `axum::sse` for Server-Sent Events ([#75](https://github.com/tokio-rs/axum/pull/75))
|
||||
- Mention required dependencies in docs ([#77](https://github.com/tokio-rs/axum/pull/77))
|
||||
- Fix WebSockets failing on Firefox ([#76](https://github.com/tokio-rs/axum/pull/76))
|
||||
|
||||
+11
-1
@@ -22,7 +22,17 @@ pub use bytes::Bytes;
|
||||
pub type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;
|
||||
|
||||
/// Convert a [`http_body::Body`] into a [`BoxBody`].
|
||||
#[deprecated(note = "use `axum::body::boxed`", since = "0.3.4")]
|
||||
pub fn box_body<B>(body: B) -> BoxBody
|
||||
where
|
||||
B: http_body::Body<Data = Bytes> + Send + 'static,
|
||||
B::Error: Into<BoxError>,
|
||||
{
|
||||
boxed(body)
|
||||
}
|
||||
|
||||
/// Convert a [`http_body::Body`] into a [`BoxBody`].
|
||||
pub fn boxed<B>(body: B) -> BoxBody
|
||||
where
|
||||
B: http_body::Body<Data = Bytes> + Send + 'static,
|
||||
B::Error: Into<BoxError>,
|
||||
@@ -31,5 +41,5 @@ where
|
||||
}
|
||||
|
||||
pub(crate) fn empty() -> BoxBody {
|
||||
box_body(http_body::Empty::new())
|
||||
boxed(http_body::Empty::new())
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ let app = Router::new()
|
||||
// it can be routed to directly.
|
||||
service_fn(|req: Request<Body>| async move {
|
||||
let body = Body::from(format!("Hi from `{} /foo`", req.method()));
|
||||
let body = axum::body::box_body(body);
|
||||
let body = axum::body::boxed(body);
|
||||
let res = Response::new(body);
|
||||
Ok::<_, Infallible>(res)
|
||||
})
|
||||
|
||||
@@ -151,7 +151,7 @@ pub mod future {
|
||||
//! Future types.
|
||||
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
response::IntoResponse,
|
||||
BoxError,
|
||||
};
|
||||
@@ -190,11 +190,11 @@ pub mod future {
|
||||
let this = self.project();
|
||||
|
||||
match ready!(this.inner.poll(cx)) {
|
||||
Ok(res) => Ok(res.map(box_body)).into(),
|
||||
Ok(res) => Ok(res.map(boxed)).into(),
|
||||
Err(err) => {
|
||||
let f = this.f.take().unwrap();
|
||||
let res = f(err);
|
||||
Ok(res.into_response().map(box_body)).into()
|
||||
Ok(res.into_response().map(boxed)).into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ where
|
||||
State::Call { future }
|
||||
}
|
||||
Err(err) => {
|
||||
let res = err.into_response().map(crate::body::box_body);
|
||||
let res = err.into_response().map(crate::body::boxed);
|
||||
return Poll::Ready(Ok(res));
|
||||
}
|
||||
}
|
||||
@@ -255,7 +255,7 @@ where
|
||||
StateProj::Call { future } => {
|
||||
return future
|
||||
.poll(cx)
|
||||
.map(|result| result.map(|response| response.map(crate::body::box_body)));
|
||||
.map(|result| result.map(|response| response.map(crate::body::boxed)));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::IntoResponse;
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
BoxError, Error,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
@@ -325,10 +325,10 @@ where
|
||||
|
||||
fn into_response(self) -> http::Response<Self::Body> {
|
||||
match self {
|
||||
Self::PayloadTooLarge(inner) => inner.into_response().map(box_body),
|
||||
Self::LengthRequired(inner) => inner.into_response().map(box_body),
|
||||
Self::HeadersAlreadyExtracted(inner) => inner.into_response().map(box_body),
|
||||
Self::Inner(inner) => inner.into_response().map(box_body),
|
||||
Self::PayloadTooLarge(inner) => inner.into_response().map(boxed),
|
||||
Self::LengthRequired(inner) => inner.into_response().map(boxed),
|
||||
Self::HeadersAlreadyExtracted(inner) => inner.into_response().map(boxed),
|
||||
Self::Inner(inner) => inner.into_response().map(boxed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::{FromRequest, RequestParts};
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
@@ -33,7 +33,7 @@ macro_rules! impl_from_request {
|
||||
type Rejection = Response<BoxBody>;
|
||||
|
||||
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
$( let $ty = $ty::from_request(req).await.map_err(|err| err.into_response().map(box_body))?; )*
|
||||
$( let $ty = $ty::from_request(req).await.map_err(|err| err.into_response().map(boxed))?; )*
|
||||
Ok(($($ty,)*))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
//! [axum-debug]: https://docs.rs/axum-debug
|
||||
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
extract::{
|
||||
connect_info::{Connected, IntoMakeServiceWithConnectInfo},
|
||||
FromRequest, RequestParts,
|
||||
@@ -272,7 +272,7 @@ where
|
||||
type Sealed = sealed::Hidden;
|
||||
|
||||
async fn call(self, _req: Request<B>) -> Response<BoxBody> {
|
||||
self().await.into_response().map(box_body)
|
||||
self().await.into_response().map(boxed)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,13 +296,13 @@ macro_rules! impl_handler {
|
||||
$(
|
||||
let $ty = match $ty::from_request(&mut req).await {
|
||||
Ok(value) => value,
|
||||
Err(rejection) => return rejection.into_response().map(box_body),
|
||||
Err(rejection) => return rejection.into_response().map(boxed),
|
||||
};
|
||||
)*
|
||||
|
||||
let res = self($($ty,)*).await;
|
||||
|
||||
res.into_response().map(box_body)
|
||||
res.into_response().map(boxed)
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -371,8 +371,8 @@ where
|
||||
.await
|
||||
.map_err(IntoResponse::into_response)
|
||||
{
|
||||
Ok(res) => res.map(box_body),
|
||||
Err(res) => res.map(box_body),
|
||||
Ok(res) => res.map(boxed),
|
||||
Err(res) => res.map(boxed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::IntoResponse;
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
BoxError,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
@@ -133,10 +133,10 @@ where
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
let headers = match self.0.try_into_header_map() {
|
||||
Ok(headers) => headers,
|
||||
Err(res) => return res.map(box_body),
|
||||
Err(res) => return res.map(boxed),
|
||||
};
|
||||
|
||||
(headers, self.1).into_response().map(box_body)
|
||||
(headers, self.1).into_response().map(boxed)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,10 +157,10 @@ where
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
let headers = match self.1.try_into_header_map() {
|
||||
Ok(headers) => headers,
|
||||
Err(res) => return res.map(box_body),
|
||||
Err(res) => return res.map(boxed),
|
||||
};
|
||||
|
||||
(self.0, headers, self.2).into_response().map(box_body)
|
||||
(self.0, headers, self.2).into_response().map(boxed)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#![doc = include_str!("../docs/response.md")]
|
||||
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
BoxError, Error,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
@@ -157,7 +157,7 @@ pub trait IntoResponse {
|
||||
/// contains exactly one chunk.
|
||||
/// - [`axum::body::BoxBody`]: If you need to unify multiple body types into
|
||||
/// one, or return a body type that cannot be named. Can be created with
|
||||
/// [`box_body`].
|
||||
/// [`boxed`].
|
||||
///
|
||||
/// [`axum::body::Body`]: crate::body::Body
|
||||
/// [`axum::body::Empty<Bytes>`]: crate::body::Empty
|
||||
@@ -209,8 +209,8 @@ where
|
||||
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
match self {
|
||||
Ok(value) => value.into_response().map(box_body),
|
||||
Err(err) => err.into_response().map(box_body),
|
||||
Ok(value) => value.into_response().map(boxed),
|
||||
Err(err) => err.into_response().map(boxed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Routing for handlers based on HTTP methods.
|
||||
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
handler::Handler,
|
||||
routing::{MethodFilter, MethodNotAllowed},
|
||||
util::{Either, EitherProj},
|
||||
@@ -446,7 +446,7 @@ where
|
||||
};
|
||||
|
||||
if this.req_method == &Method::HEAD {
|
||||
let response = response.map(|_| box_body(Empty::new()));
|
||||
let response = response.map(|_| boxed(Empty::new()));
|
||||
Poll::Ready(Ok(response))
|
||||
} else {
|
||||
Poll::Ready(Ok(response))
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use self::{future::RouterFuture, not_found::NotFound};
|
||||
use crate::{
|
||||
body::{box_body, Body, BoxBody},
|
||||
body::{boxed, Body, BoxBody},
|
||||
extract::{
|
||||
connect_info::{Connected, IntoMakeServiceWithConnectInfo},
|
||||
MatchedPath, OriginalUri,
|
||||
@@ -249,7 +249,7 @@ where
|
||||
{
|
||||
let layer = ServiceBuilder::new()
|
||||
.layer_fn(Route::new)
|
||||
.layer(MapResponseBodyLayer::new(box_body))
|
||||
.layer(MapResponseBodyLayer::new(boxed))
|
||||
.layer(layer);
|
||||
|
||||
let routes = self
|
||||
@@ -285,7 +285,7 @@ where
|
||||
{
|
||||
let layer = ServiceBuilder::new()
|
||||
.layer_fn(Route::new)
|
||||
.layer(MapResponseBodyLayer::new(box_body))
|
||||
.layer(MapResponseBodyLayer::new(boxed))
|
||||
.layer(layer);
|
||||
|
||||
let routes = self
|
||||
|
||||
@@ -97,7 +97,7 @@
|
||||
//! [`Service`'s]: tower::Service
|
||||
|
||||
use crate::{
|
||||
body::{box_body, BoxBody},
|
||||
body::{boxed, BoxBody},
|
||||
routing::{MethodFilter, MethodNotAllowed},
|
||||
util::{Either, EitherProj},
|
||||
BoxError,
|
||||
@@ -537,12 +537,12 @@ where
|
||||
let this = self.project();
|
||||
|
||||
let response = match this.inner.project() {
|
||||
EitherProj::A { inner } => ready!(inner.poll(cx))?.map(box_body),
|
||||
EitherProj::A { inner } => ready!(inner.poll(cx))?.map(boxed),
|
||||
EitherProj::B { inner } => ready!(inner.poll(cx))?,
|
||||
};
|
||||
|
||||
if this.req_method == &Method::HEAD {
|
||||
let response = response.map(|_| box_body(Empty::new()));
|
||||
let response = response.map(|_| boxed(Empty::new()));
|
||||
Poll::Ready(Ok(response))
|
||||
} else {
|
||||
Poll::Ready(Ok(response))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use crate::{body::box_body, error_handling::HandleErrorExt, extract::Extension};
|
||||
use crate::{body::boxed, error_handling::HandleErrorExt, extract::Extension};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[tokio::test]
|
||||
@@ -149,7 +149,7 @@ async fn nested_service_sees_stripped_uri() {
|
||||
Router::new().route(
|
||||
"/baz",
|
||||
service_fn(|req: Request<Body>| async move {
|
||||
let body = box_body(Body::from(req.uri().to_string()));
|
||||
let body = boxed(Body::from(req.uri().to_string()));
|
||||
Ok::<_, Infallible>(Response::new(body))
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
//! Example is based on <https://github.com/hyperium/hyper/blob/master/examples/http_proxy.rs>
|
||||
|
||||
use axum::{
|
||||
body::{box_body, Body},
|
||||
body::{boxed, Body},
|
||||
http::{Method, Request, Response, StatusCode},
|
||||
routing::get,
|
||||
Router,
|
||||
@@ -37,7 +37,7 @@ async fn main() {
|
||||
let router = router.clone();
|
||||
async move {
|
||||
if req.method() == Method::CONNECT {
|
||||
proxy(req).await.map(|res| res.map(box_body))
|
||||
proxy(req).await.map(|res| res.map(boxed))
|
||||
} else {
|
||||
router.oneshot(req).await.map_err(|err| match err {})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user