1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-01 08:45:10 +02:00

remove responsebody indirection from response (#2201)

This commit is contained in:
Rob Ede
2021-05-09 20:12:48 +01:00
committed by GitHub
parent a9dc1586a0
commit 900c9e270e
31 changed files with 381 additions and 263 deletions

View File

@ -1,12 +1,13 @@
//! For middleware documentation, see [`Compat`].
use std::{
error::Error as StdError,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use actix_http::body::{Body, MessageBody, ResponseBody};
use actix_http::body::{Body, MessageBody};
use actix_service::{Service, Transform};
use futures_core::{future::LocalBoxFuture, ready};
@ -116,10 +117,10 @@ pub trait MapServiceResponseBody {
impl<B> MapServiceResponseBody for ServiceResponse<B>
where
B: MessageBody + Unpin + 'static,
B::Error: Into<Error>,
B::Error: Into<Box<dyn StdError + 'static>>,
{
fn map_body(self) -> ServiceResponse {
self.map_body(|_, body| ResponseBody::Other(Body::from_message(body)))
self.map_body(|_, body| Body::from_message(body))
}
}

View File

@ -10,7 +10,7 @@ use std::{
};
use actix_http::{
body::MessageBody,
body::{MessageBody, ResponseBody},
encoding::Encoder,
http::header::{ContentEncoding, ACCEPT_ENCODING},
Error,
@ -59,7 +59,7 @@ where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
type Response = ServiceResponse<Encoder<B>>;
type Response = ServiceResponse<ResponseBody<Encoder<B>>>;
type Error = Error;
type Transform = CompressMiddleware<S>;
type InitError = ();
@ -83,7 +83,7 @@ where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
type Response = ServiceResponse<Encoder<B>>;
type Response = ServiceResponse<ResponseBody<Encoder<B>>>;
type Error = Error;
type Future = CompressResponse<S, B>;
@ -127,7 +127,7 @@ where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
type Output = Result<ServiceResponse<Encoder<B>>, Error>;
type Output = Result<ServiceResponse<ResponseBody<Encoder<B>>>, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -140,9 +140,9 @@ where
*this.encoding
};
Poll::Ready(Ok(
resp.map_body(move |head, body| Encoder::response(enc, head, body))
))
Poll::Ready(Ok(resp.map_body(move |head, body| {
Encoder::response(enc, head, ResponseBody::Body(body))
})))
}
Err(e) => Poll::Ready(Err(e)),
}

View File

@ -21,7 +21,7 @@ use regex::{Regex, RegexSet};
use time::OffsetDateTime;
use crate::{
dev::{BodySize, MessageBody, ResponseBody},
dev::{BodySize, MessageBody},
http::{HeaderName, StatusCode},
service::{ServiceRequest, ServiceResponse},
Error, HttpResponse, Result,
@ -289,13 +289,11 @@ where
let time = *this.time;
let format = this.format.take();
Poll::Ready(Ok(res.map_body(move |_, body| {
ResponseBody::Body(StreamLog {
body,
time,
format,
size: 0,
})
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
body,
time,
format,
size: 0,
})))
}
}
@ -305,7 +303,7 @@ use pin_project::{pin_project, pinned_drop};
#[pin_project(PinnedDrop)]
pub struct StreamLog<B> {
#[pin]
body: ResponseBody<B>,
body: B,
format: Option<Format>,
size: usize,
time: OffsetDateTime,
@ -342,12 +340,15 @@ where
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>> {
let this = self.project();
match this.body.poll_next(cx) {
Poll::Ready(Some(Ok(chunk))) => {
// TODO: MSRV 1.51: poll_map_err
match ready!(this.body.poll_next(cx)) {
Some(Ok(chunk)) => {
*this.size += chunk.len();
Poll::Ready(Some(Ok(chunk)))
}
val => val,
Some(Err(err)) => Poll::Ready(Some(Err(err.into()))),
None => Poll::Ready(None),
}
}
}