mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-27 23:39:03 +02:00
ClientRequest::send_body
takes impl MessageBody
(#2546)
This commit is contained in:
192
awc/src/responses/json_body.rs
Normal file
192
awc/src/responses/json_body.rs
Normal file
@ -0,0 +1,192 @@
|
||||
use std::{
|
||||
future::Future,
|
||||
marker::PhantomData,
|
||||
mem,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_http::{error::PayloadError, header, HttpMessage};
|
||||
use bytes::Bytes;
|
||||
use futures_core::{ready, Stream};
|
||||
use pin_project_lite::pin_project;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use super::{read_body::ReadBody, ResponseTimeout, DEFAULT_BODY_LIMIT};
|
||||
use crate::{error::JsonPayloadError, ClientResponse};
|
||||
|
||||
pin_project! {
|
||||
/// A `Future` that reads a body stream, parses JSON, resolving to a deserialized `T`.
|
||||
///
|
||||
/// # Errors
|
||||
/// `Future` implementation returns error if:
|
||||
/// - content type is not `application/json`;
|
||||
/// - content length is greater than [limit](JsonBody::limit) (default: 2 MiB).
|
||||
pub struct JsonBody<S, T> {
|
||||
#[pin]
|
||||
body: Option<ReadBody<S>>,
|
||||
length: Option<usize>,
|
||||
timeout: ResponseTimeout,
|
||||
err: Option<JsonPayloadError>,
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T> JsonBody<S, T>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>>,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
/// Creates a JSON body stream reader from a response by taking its payload.
|
||||
pub fn new(res: &mut ClientResponse<S>) -> Self {
|
||||
// check content-type
|
||||
let json = if let Ok(Some(mime)) = res.mime_type() {
|
||||
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if !json {
|
||||
return JsonBody {
|
||||
length: None,
|
||||
body: None,
|
||||
timeout: ResponseTimeout::default(),
|
||||
err: Some(JsonPayloadError::ContentType),
|
||||
_phantom: PhantomData,
|
||||
};
|
||||
}
|
||||
|
||||
let length = res
|
||||
.headers()
|
||||
.get(&header::CONTENT_LENGTH)
|
||||
.and_then(|len_hdr| len_hdr.to_str().ok())
|
||||
.and_then(|len_str| len_str.parse::<usize>().ok());
|
||||
|
||||
JsonBody {
|
||||
body: Some(ReadBody::new(res.take_payload(), DEFAULT_BODY_LIMIT)),
|
||||
length,
|
||||
timeout: mem::take(&mut res.timeout),
|
||||
err: None,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Change max size of payload. Default limit is 2 MiB.
|
||||
pub fn limit(mut self, limit: usize) -> Self {
|
||||
if let Some(ref mut fut) = self.body {
|
||||
fut.limit = limit;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T> Future for JsonBody<S, T>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>>,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
type Output = Result<T, JsonPayloadError>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
|
||||
if let Some(err) = this.err.take() {
|
||||
return Poll::Ready(Err(err));
|
||||
}
|
||||
|
||||
if let Some(len) = this.length.take() {
|
||||
let body = Option::as_ref(&this.body).unwrap();
|
||||
if len > body.limit {
|
||||
return Poll::Ready(Err(JsonPayloadError::Payload(PayloadError::Overflow)));
|
||||
}
|
||||
}
|
||||
|
||||
this.timeout
|
||||
.poll_timeout(cx)
|
||||
.map_err(JsonPayloadError::Payload)?;
|
||||
|
||||
let body = ready!(this.body.as_pin_mut().unwrap().poll(cx))?;
|
||||
Poll::Ready(serde_json::from_slice::<T>(&body).map_err(JsonPayloadError::from))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use actix_http::BoxedPayloadStream;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use static_assertions::assert_impl_all;
|
||||
|
||||
use super::*;
|
||||
use crate::{http::header, test::TestResponse};
|
||||
|
||||
assert_impl_all!(JsonBody<BoxedPayloadStream, String>: Unpin);
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
struct MyObject {
|
||||
name: String,
|
||||
}
|
||||
|
||||
fn json_eq(err: JsonPayloadError, other: JsonPayloadError) -> bool {
|
||||
match err {
|
||||
JsonPayloadError::Payload(PayloadError::Overflow) => {
|
||||
matches!(other, JsonPayloadError::Payload(PayloadError::Overflow))
|
||||
}
|
||||
JsonPayloadError::ContentType => matches!(other, JsonPayloadError::ContentType),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn read_json_body() {
|
||||
let mut req = TestResponse::default().finish();
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/text"),
|
||||
))
|
||||
.finish();
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
))
|
||||
.insert_header((
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("10000"),
|
||||
))
|
||||
.finish();
|
||||
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).limit(100).await;
|
||||
assert!(json_eq(
|
||||
json.err().unwrap(),
|
||||
JsonPayloadError::Payload(PayloadError::Overflow)
|
||||
));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
))
|
||||
.insert_header((
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
))
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.finish();
|
||||
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert_eq!(
|
||||
json.ok().unwrap(),
|
||||
MyObject {
|
||||
name: "test".to_owned()
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
49
awc/src/responses/mod.rs
Normal file
49
awc/src/responses/mod.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use std::{future::Future, io, pin::Pin, task::Context};
|
||||
|
||||
use actix_http::error::PayloadError;
|
||||
use actix_rt::time::Sleep;
|
||||
|
||||
mod json_body;
|
||||
mod read_body;
|
||||
mod response;
|
||||
mod response_body;
|
||||
|
||||
pub use self::json_body::JsonBody;
|
||||
pub use self::response::ClientResponse;
|
||||
#[allow(deprecated)]
|
||||
pub use self::response_body::{MessageBody, ResponseBody};
|
||||
|
||||
/// Default body size limit: 2 MiB
|
||||
const DEFAULT_BODY_LIMIT: usize = 2 * 1024 * 1024;
|
||||
|
||||
/// Helper enum with reusable sleep passed from `SendClientResponse`.
|
||||
///
|
||||
/// See [`ClientResponse::_timeout`] for reason.
|
||||
pub(crate) enum ResponseTimeout {
|
||||
Disabled(Option<Pin<Box<Sleep>>>),
|
||||
Enabled(Pin<Box<Sleep>>),
|
||||
}
|
||||
|
||||
impl Default for ResponseTimeout {
|
||||
fn default() -> Self {
|
||||
Self::Disabled(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseTimeout {
|
||||
fn poll_timeout(&mut self, cx: &mut Context<'_>) -> Result<(), PayloadError> {
|
||||
match *self {
|
||||
Self::Enabled(ref mut timeout) => {
|
||||
if timeout.as_mut().poll(cx).is_ready() {
|
||||
Err(PayloadError::Io(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"Response Payload IO timed out",
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Self::Disabled(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
61
awc/src/responses/read_body.rs
Normal file
61
awc/src/responses/read_body.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_http::{error::PayloadError, Payload};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures_core::{ready, Stream};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
pub(crate) struct ReadBody<S> {
|
||||
#[pin]
|
||||
pub(crate) stream: Payload<S>,
|
||||
pub(crate) buf: BytesMut,
|
||||
pub(crate) limit: usize,
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ReadBody<S> {
|
||||
pub(crate) fn new(stream: Payload<S>, limit: usize) -> Self {
|
||||
Self {
|
||||
stream,
|
||||
buf: BytesMut::new(),
|
||||
limit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Future for ReadBody<S>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>>,
|
||||
{
|
||||
type Output = Result<Bytes, PayloadError>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
|
||||
while let Some(chunk) = ready!(this.stream.as_mut().poll_next(cx)?) {
|
||||
if (this.buf.len() + chunk.len()) > *this.limit {
|
||||
return Poll::Ready(Err(PayloadError::Overflow));
|
||||
}
|
||||
|
||||
this.buf.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
Poll::Ready(Ok(this.buf.split().freeze()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use static_assertions::assert_impl_all;
|
||||
|
||||
use super::*;
|
||||
use crate::any_body::AnyBody;
|
||||
|
||||
assert_impl_all!(ReadBody<()>: Unpin);
|
||||
assert_impl_all!(ReadBody<AnyBody>: Unpin);
|
||||
}
|
257
awc/src/responses/response.rs
Normal file
257
awc/src/responses/response.rs
Normal file
@ -0,0 +1,257 @@
|
||||
use std::{
|
||||
cell::{Ref, RefMut},
|
||||
fmt, mem,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use actix_http::{
|
||||
error::PayloadError, header, header::HeaderMap, BoxedPayloadStream, Extensions,
|
||||
HttpMessage, Payload, ResponseHead, StatusCode, Version,
|
||||
};
|
||||
use actix_rt::time::{sleep, Sleep};
|
||||
use bytes::Bytes;
|
||||
use futures_core::Stream;
|
||||
use pin_project_lite::pin_project;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
use crate::cookie::{Cookie, ParseError as CookieParseError};
|
||||
|
||||
use super::{JsonBody, ResponseBody, ResponseTimeout};
|
||||
|
||||
pin_project! {
|
||||
/// Client Response
|
||||
pub struct ClientResponse<S = BoxedPayloadStream> {
|
||||
pub(crate) head: ResponseHead,
|
||||
#[pin]
|
||||
pub(crate) payload: Payload<S>,
|
||||
pub(crate) timeout: ResponseTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ClientResponse<S> {
|
||||
/// Create new Request instance
|
||||
pub(crate) fn new(head: ResponseHead, payload: Payload<S>) -> Self {
|
||||
ClientResponse {
|
||||
head,
|
||||
payload,
|
||||
timeout: ResponseTimeout::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn head(&self) -> &ResponseHead {
|
||||
&self.head
|
||||
}
|
||||
|
||||
/// Read the Request Version.
|
||||
#[inline]
|
||||
pub fn version(&self) -> Version {
|
||||
self.head().version
|
||||
}
|
||||
|
||||
/// Get the status from the server.
|
||||
#[inline]
|
||||
pub fn status(&self) -> StatusCode {
|
||||
self.head().status
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns request's headers.
|
||||
pub fn headers(&self) -> &HeaderMap {
|
||||
&self.head().headers
|
||||
}
|
||||
|
||||
/// Set a body and return previous body value
|
||||
pub fn map_body<F, U>(mut self, f: F) -> ClientResponse<U>
|
||||
where
|
||||
F: FnOnce(&mut ResponseHead, Payload<S>) -> Payload<U>,
|
||||
{
|
||||
let payload = f(&mut self.head, self.payload);
|
||||
|
||||
ClientResponse {
|
||||
payload,
|
||||
head: self.head,
|
||||
timeout: self.timeout,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a timeout duration for [`ClientResponse`](self::ClientResponse).
|
||||
///
|
||||
/// This duration covers the duration of processing the response body stream
|
||||
/// and would end it as timeout error when deadline met.
|
||||
///
|
||||
/// Disabled by default.
|
||||
pub fn timeout(self, dur: Duration) -> Self {
|
||||
let timeout = match self.timeout {
|
||||
ResponseTimeout::Disabled(Some(mut timeout))
|
||||
| ResponseTimeout::Enabled(mut timeout) => match Instant::now().checked_add(dur) {
|
||||
Some(deadline) => {
|
||||
timeout.as_mut().reset(deadline.into());
|
||||
ResponseTimeout::Enabled(timeout)
|
||||
}
|
||||
None => ResponseTimeout::Enabled(Box::pin(sleep(dur))),
|
||||
},
|
||||
_ => ResponseTimeout::Enabled(Box::pin(sleep(dur))),
|
||||
};
|
||||
|
||||
Self {
|
||||
payload: self.payload,
|
||||
head: self.head,
|
||||
timeout,
|
||||
}
|
||||
}
|
||||
|
||||
/// This method does not enable timeout. It's used to pass the boxed `Sleep` from
|
||||
/// `SendClientRequest` and reuse it's heap allocation together with it's slot in
|
||||
/// timer wheel.
|
||||
pub(crate) fn _timeout(mut self, timeout: Option<Pin<Box<Sleep>>>) -> Self {
|
||||
self.timeout = ResponseTimeout::Disabled(timeout);
|
||||
self
|
||||
}
|
||||
|
||||
/// Load request cookies.
|
||||
#[cfg(feature = "cookies")]
|
||||
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
||||
struct Cookies(Vec<Cookie<'static>>);
|
||||
|
||||
if self.extensions().get::<Cookies>().is_none() {
|
||||
let mut cookies = Vec::new();
|
||||
for hdr in self.headers().get_all(&header::SET_COOKIE) {
|
||||
let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
||||
cookies.push(Cookie::parse_encoded(s)?.into_owned());
|
||||
}
|
||||
self.extensions_mut().insert(Cookies(cookies));
|
||||
}
|
||||
Ok(Ref::map(self.extensions(), |ext| {
|
||||
&ext.get::<Cookies>().unwrap().0
|
||||
}))
|
||||
}
|
||||
|
||||
/// Return request cookie.
|
||||
#[cfg(feature = "cookies")]
|
||||
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||
if let Ok(cookies) = self.cookies() {
|
||||
for cookie in cookies.iter() {
|
||||
if cookie.name() == name {
|
||||
return Some(cookie.to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ClientResponse<S>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>>,
|
||||
{
|
||||
/// Returns a [`Future`] that consumes the body stream and resolves to [`Bytes`].
|
||||
///
|
||||
/// # Errors
|
||||
/// `Future` implementation returns error if:
|
||||
/// - content type is not `application/json`
|
||||
/// - content length is greater than [limit](JsonBody::limit) (default: 2 MiB)
|
||||
///
|
||||
/// # Examples
|
||||
/// ```no_run
|
||||
/// # use awc::Client;
|
||||
/// # use bytes::Bytes;
|
||||
/// # #[actix_rt::main]
|
||||
/// # async fn async_ctx() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// let client = Client::default();
|
||||
/// let mut res = client.get("https://httpbin.org/robots.txt").send().await?;
|
||||
/// let body: Bytes = res.body().await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`Future`]: std::future::Future
|
||||
pub fn body(&mut self) -> ResponseBody<S> {
|
||||
ResponseBody::new(self)
|
||||
}
|
||||
|
||||
/// Returns a [`Future`] consumes the body stream, parses JSON, and resolves to a deserialized
|
||||
/// `T` value.
|
||||
///
|
||||
/// # Errors
|
||||
/// Future returns error if:
|
||||
/// - content type is not `application/json`;
|
||||
/// - content length is greater than [limit](JsonBody::limit) (default: 2 MiB).
|
||||
///
|
||||
/// # Examples
|
||||
/// ```no_run
|
||||
/// # use awc::Client;
|
||||
/// # #[actix_rt::main]
|
||||
/// # async fn async_ctx() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// let client = Client::default();
|
||||
/// let mut res = client.get("https://httpbin.org/json").send().await?;
|
||||
/// let val = res.json::<serde_json::Value>().await?;
|
||||
/// assert!(val.is_object());
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`Future`]: std::future::Future
|
||||
pub fn json<T: DeserializeOwned>(&mut self) -> JsonBody<S, T> {
|
||||
JsonBody::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> fmt::Debug for ClientResponse<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
writeln!(f, "\nClientResponse {:?} {}", self.version(), self.status(),)?;
|
||||
writeln!(f, " headers:")?;
|
||||
for (key, val) in self.headers().iter() {
|
||||
writeln!(f, " {:?}: {:?}", key, val)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> HttpMessage for ClientResponse<S> {
|
||||
type Stream = S;
|
||||
|
||||
fn headers(&self) -> &HeaderMap {
|
||||
&self.head.headers
|
||||
}
|
||||
|
||||
fn take_payload(&mut self) -> Payload<S> {
|
||||
mem::replace(&mut self.payload, Payload::None)
|
||||
}
|
||||
|
||||
fn extensions(&self) -> Ref<'_, Extensions> {
|
||||
self.head.extensions()
|
||||
}
|
||||
|
||||
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
||||
self.head.extensions_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Stream for ClientResponse<S>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin,
|
||||
{
|
||||
type Item = Result<Bytes, PayloadError>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let this = self.project();
|
||||
this.timeout.poll_timeout(cx)?;
|
||||
this.payload.poll_next(cx)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use static_assertions::assert_impl_all;
|
||||
|
||||
use super::*;
|
||||
use crate::any_body::AnyBody;
|
||||
|
||||
assert_impl_all!(ClientResponse: Unpin);
|
||||
assert_impl_all!(ClientResponse<()>: Unpin);
|
||||
assert_impl_all!(ClientResponse<AnyBody>: Unpin);
|
||||
}
|
144
awc/src/responses/response_body.rs
Normal file
144
awc/src/responses/response_body.rs
Normal file
@ -0,0 +1,144 @@
|
||||
use std::{
|
||||
future::Future,
|
||||
mem,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_http::{error::PayloadError, header, HttpMessage};
|
||||
use bytes::Bytes;
|
||||
use futures_core::Stream;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use super::{read_body::ReadBody, ResponseTimeout, DEFAULT_BODY_LIMIT};
|
||||
use crate::ClientResponse;
|
||||
|
||||
pin_project! {
|
||||
/// A `Future` that reads a body stream, resolving as [`Bytes`].
|
||||
///
|
||||
/// # Errors
|
||||
/// `Future` implementation returns error if:
|
||||
/// - content type is not `application/json`;
|
||||
/// - content length is greater than [limit](JsonBody::limit) (default: 2 MiB).
|
||||
pub struct ResponseBody<S> {
|
||||
#[pin]
|
||||
body: Option<ReadBody<S>>,
|
||||
length: Option<usize>,
|
||||
timeout: ResponseTimeout,
|
||||
err: Option<PayloadError>,
|
||||
}
|
||||
}
|
||||
|
||||
#[deprecated(since = "3.0.0", note = "Renamed to `ResponseBody`.")]
|
||||
pub type MessageBody<B> = ResponseBody<B>;
|
||||
|
||||
impl<S> ResponseBody<S>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>>,
|
||||
{
|
||||
/// Creates a body stream reader from a response by taking its payload.
|
||||
pub fn new(res: &mut ClientResponse<S>) -> ResponseBody<S> {
|
||||
let length = match res.headers().get(&header::CONTENT_LENGTH) {
|
||||
Some(value) => {
|
||||
let len = value.to_str().ok().and_then(|s| s.parse::<usize>().ok());
|
||||
|
||||
match len {
|
||||
None => return Self::err(PayloadError::UnknownLength),
|
||||
len => len,
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
ResponseBody {
|
||||
body: Some(ReadBody::new(res.take_payload(), DEFAULT_BODY_LIMIT)),
|
||||
length,
|
||||
timeout: mem::take(&mut res.timeout),
|
||||
err: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Change max size limit of payload.
|
||||
///
|
||||
/// The default limit is 2 MiB.
|
||||
pub fn limit(mut self, limit: usize) -> Self {
|
||||
if let Some(ref mut body) = self.body {
|
||||
body.limit = limit;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
fn err(err: PayloadError) -> Self {
|
||||
ResponseBody {
|
||||
body: None,
|
||||
length: None,
|
||||
timeout: ResponseTimeout::default(),
|
||||
err: Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Future for ResponseBody<S>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>>,
|
||||
{
|
||||
type Output = Result<Bytes, PayloadError>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
|
||||
if let Some(err) = this.err.take() {
|
||||
return Poll::Ready(Err(err));
|
||||
}
|
||||
|
||||
if let Some(len) = this.length.take() {
|
||||
let body = Option::as_ref(&this.body).unwrap();
|
||||
if len > body.limit {
|
||||
return Poll::Ready(Err(PayloadError::Overflow));
|
||||
}
|
||||
}
|
||||
|
||||
this.timeout.poll_timeout(cx)?;
|
||||
|
||||
this.body.as_pin_mut().unwrap().poll(cx)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use static_assertions::assert_impl_all;
|
||||
|
||||
use super::*;
|
||||
use crate::{http::header, test::TestResponse};
|
||||
|
||||
assert_impl_all!(ResponseBody<()>: Unpin);
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn read_body() {
|
||||
let mut req = TestResponse::with_header((header::CONTENT_LENGTH, "xxxx")).finish();
|
||||
match req.body().await.err().unwrap() {
|
||||
PayloadError::UnknownLength => {}
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
|
||||
let mut req = TestResponse::with_header((header::CONTENT_LENGTH, "10000000")).finish();
|
||||
match req.body().await.err().unwrap() {
|
||||
PayloadError::Overflow => {}
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.set_payload(Bytes::from_static(b"test"))
|
||||
.finish();
|
||||
assert_eq!(req.body().await.ok().unwrap(), Bytes::from_static(b"test"));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.set_payload(Bytes::from_static(b"11111111111111"))
|
||||
.finish();
|
||||
match req.body().limit(5).await.err().unwrap() {
|
||||
PayloadError::Overflow => {}
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user