mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
document new body map types
This commit is contained in:
parent
008753f07a
commit
50894e392e
@ -152,7 +152,6 @@ pub(crate) trait MessageType: Sized {
|
|||||||
let k = key.as_str().as_bytes();
|
let k = key.as_str().as_bytes();
|
||||||
let k_len = k.len();
|
let k_len = k.len();
|
||||||
|
|
||||||
// TODO: drain?
|
|
||||||
for val in value.iter() {
|
for val in value.iter() {
|
||||||
let v = val.as_ref();
|
let v = val.as_ref();
|
||||||
let v_len = v.len();
|
let v_len = v.len();
|
||||||
|
@ -6,6 +6,7 @@ use std::{
|
|||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
use crate::error::PayloadError;
|
use crate::error::PayloadError;
|
||||||
|
|
||||||
@ -15,7 +16,7 @@ pub type BoxedPayloadStream = Pin<Box<dyn Stream<Item = Result<Bytes, PayloadErr
|
|||||||
#[deprecated(since = "4.0.0", note = "Renamed to `BoxedPayloadStream`.")]
|
#[deprecated(since = "4.0.0", note = "Renamed to `BoxedPayloadStream`.")]
|
||||||
pub type PayloadStream = BoxedPayloadStream;
|
pub type PayloadStream = BoxedPayloadStream;
|
||||||
|
|
||||||
pin_project_lite::pin_project! {
|
pin_project! {
|
||||||
/// A streaming payload.
|
/// A streaming payload.
|
||||||
#[project = PayloadProj]
|
#[project = PayloadProj]
|
||||||
pub enum Payload<S = BoxedPayloadStream> {
|
pub enum Payload<S = BoxedPayloadStream> {
|
||||||
|
@ -185,7 +185,7 @@ impl<B> Response<B> {
|
|||||||
self.replace_body(())
|
self.replace_body(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map the current body type to another using a closure. Returns a new response.
|
/// Map the current body type to another using a closure, returning a new response.
|
||||||
///
|
///
|
||||||
/// Closure receives the response head and the current body type.
|
/// Closure receives the response head and the current body type.
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -202,6 +202,7 @@ impl<B> Response<B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map the current body to a type-erased `BoxBody`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn map_into_boxed_body(self) -> Response<BoxBody>
|
pub fn map_into_boxed_body(self) -> Response<BoxBody>
|
||||||
where
|
where
|
||||||
@ -210,7 +211,7 @@ impl<B> Response<B> {
|
|||||||
self.map_body(|_, body| body.boxed())
|
self.map_body(|_, body| body.boxed())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns body, consuming this response.
|
/// Returns the response body, dropping all other parts.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_body(self) -> B {
|
pub fn into_body(self) -> B {
|
||||||
self.body
|
self.body
|
||||||
|
@ -94,10 +94,9 @@ impl Client {
|
|||||||
let mut req = ClientRequest::new(method, url, self.0.clone());
|
let mut req = ClientRequest::new(method, url, self.0.clone());
|
||||||
|
|
||||||
for header in self.0.default_headers.iter() {
|
for header in self.0.default_headers.iter() {
|
||||||
// header map is empty
|
req = req.append_header(header);
|
||||||
// TODO: probably append instead
|
|
||||||
req = req.insert_header_if_none(header);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req
|
req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,7 +163,7 @@ where
|
|||||||
| StatusCode::PERMANENT_REDIRECT
|
| StatusCode::PERMANENT_REDIRECT
|
||||||
if *max_redirect_times > 0 =>
|
if *max_redirect_times > 0 =>
|
||||||
{
|
{
|
||||||
let is_redirect = res.head().status == StatusCode::TEMPORARY_REDIRECT
|
let reuse_body = res.head().status == StatusCode::TEMPORARY_REDIRECT
|
||||||
|| res.head().status == StatusCode::PERMANENT_REDIRECT;
|
|| res.head().status == StatusCode::PERMANENT_REDIRECT;
|
||||||
|
|
||||||
let prev_uri = uri.take().unwrap();
|
let prev_uri = uri.take().unwrap();
|
||||||
@ -176,7 +176,7 @@ where
|
|||||||
let connector = connector.take();
|
let connector = connector.take();
|
||||||
|
|
||||||
// reset method
|
// reset method
|
||||||
let method = if is_redirect {
|
let method = if reuse_body {
|
||||||
method.take().unwrap()
|
method.take().unwrap()
|
||||||
} else {
|
} else {
|
||||||
let method = method.take().unwrap();
|
let method = method.take().unwrap();
|
||||||
@ -187,18 +187,19 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut body = body.take();
|
let mut body = body.take();
|
||||||
let body_new = if is_redirect {
|
let body_new = if reuse_body {
|
||||||
// try to reuse body
|
// try to reuse saved body
|
||||||
match body {
|
match body {
|
||||||
Some(ref bytes) => AnyBody::Bytes {
|
Some(ref bytes) => AnyBody::Bytes {
|
||||||
body: bytes.clone(),
|
body: bytes.clone(),
|
||||||
},
|
},
|
||||||
// TODO: should this be AnyBody::Empty or AnyBody::None.
|
|
||||||
|
// body was a non-reusable type so send an empty body instead
|
||||||
_ => AnyBody::empty(),
|
_ => AnyBody::empty(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body = None;
|
body = None;
|
||||||
// remove body
|
// remove body since we're downgrading to a GET
|
||||||
AnyBody::None
|
AnyBody::None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -169,7 +169,6 @@ impl AppInitServiceState {
|
|||||||
Rc::new(AppInitServiceState {
|
Rc::new(AppInitServiceState {
|
||||||
rmap,
|
rmap,
|
||||||
config,
|
config,
|
||||||
// TODO: AppConfig can be used to pass user defined HttpRequestPool capacity.
|
|
||||||
pool: HttpRequestPool::default(),
|
pool: HttpRequestPool::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,14 @@ use actix_http::{body::BoxBody, Response};
|
|||||||
|
|
||||||
use crate::{HttpResponse, ResponseError};
|
use crate::{HttpResponse, ResponseError};
|
||||||
|
|
||||||
/// General purpose actix web error.
|
/// General purpose Actix Web error.
|
||||||
///
|
///
|
||||||
/// An actix web error is used to carry errors from `std::error`
|
/// An Actix Web error is used to carry errors from `std::error` through actix in a convenient way.
|
||||||
/// through actix in a convenient way. It can be created through
|
/// It can be created through converting errors with `into()`.
|
||||||
/// converting errors with `into()`.
|
|
||||||
///
|
///
|
||||||
/// Whenever it is created from an external object a response error is created
|
/// Whenever it is created from an external object a response error is created for it that can be
|
||||||
/// for it that can be used to create an HTTP response from it this means that
|
/// used to create an HTTP response from it this means that if you have access to an actix `Error`
|
||||||
/// if you have access to an actix `Error` you can always get a
|
/// you can always get a `ResponseError` reference from it.
|
||||||
/// `ResponseError` reference from it.
|
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
cause: Box<dyn ResponseError>,
|
cause: Box<dyn ResponseError>,
|
||||||
}
|
}
|
||||||
|
@ -256,7 +256,7 @@ impl<B> HttpResponse<B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map the current body type to another using a closure. Returns a new response.
|
/// Map the current body type to another using a closure, returning a new response.
|
||||||
///
|
///
|
||||||
/// Closure receives the response head and the current body type.
|
/// Closure receives the response head and the current body type.
|
||||||
pub fn map_body<F, B2>(self, f: F) -> HttpResponse<B2>
|
pub fn map_body<F, B2>(self, f: F) -> HttpResponse<B2>
|
||||||
@ -269,18 +269,23 @@ impl<B> HttpResponse<B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: docs for the body map methods below
|
/// Map the current body type `B` to `EitherBody::Left(B)`.
|
||||||
|
///
|
||||||
|
/// Useful for middleware which can generate their own responses.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn map_into_left_body<R>(self) -> HttpResponse<EitherBody<B, R>> {
|
pub fn map_into_left_body<R>(self) -> HttpResponse<EitherBody<B, R>> {
|
||||||
self.map_body(|_, body| EitherBody::left(body))
|
self.map_body(|_, body| EitherBody::left(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map the current body type `B` to `EitherBody::Right(B)`.
|
||||||
|
///
|
||||||
|
/// Useful for middleware which can generate their own responses.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn map_into_right_body<L>(self) -> HttpResponse<EitherBody<L, B>> {
|
pub fn map_into_right_body<L>(self) -> HttpResponse<EitherBody<L, B>> {
|
||||||
self.map_body(|_, body| EitherBody::right(body))
|
self.map_body(|_, body| EitherBody::right(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map the current body to a type-erased `BoxBody`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn map_into_boxed_body(self) -> HttpResponse<BoxBody>
|
pub fn map_into_boxed_body(self) -> HttpResponse<BoxBody>
|
||||||
where
|
where
|
||||||
@ -289,7 +294,7 @@ impl<B> HttpResponse<B> {
|
|||||||
self.map_body(|_, body| body.boxed())
|
self.map_body(|_, body| body.boxed())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract response body
|
/// Returns the response body, dropping all other parts.
|
||||||
pub fn into_body(self) -> B {
|
pub fn into_body(self) -> B {
|
||||||
self.res.into_body()
|
self.res.into_body()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user