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

body ergo v4 using any body

This commit is contained in:
Rob Ede
2021-12-08 22:45:54 +00:00
parent fb5b4734a4
commit f12f62ba73
25 changed files with 427 additions and 208 deletions

View File

@ -10,7 +10,7 @@ use std::{
};
use actix_http::{
body::{EitherBody, MessageBody},
body::MessageBody,
encoding::Encoder,
header::{ContentEncoding, ACCEPT_ENCODING},
StatusCode,
@ -22,6 +22,7 @@ use once_cell::sync::Lazy;
use pin_project_lite::pin_project;
use crate::{
any_body::AnyBody,
dev::BodyEncoding,
service::{ServiceRequest, ServiceResponse},
Error, HttpResponse,
@ -61,7 +62,7 @@ where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
type Response = ServiceResponse<EitherBody<Encoder<B>>>;
type Response = ServiceResponse<Encoder<AnyBody<B>>>;
type Error = Error;
type Transform = CompressMiddleware<S>;
type InitError = ();
@ -111,7 +112,7 @@ where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
{
type Response = ServiceResponse<EitherBody<Encoder<B>>>;
type Response = ServiceResponse<Encoder<AnyBody<B>>>;
type Error = Error;
type Future = Either<CompressResponse<S, B>, Ready<Result<Self::Response, Self::Error>>>;
@ -146,12 +147,15 @@ where
let res = HttpResponse::with_body(
StatusCode::NOT_ACCEPTABLE,
SUPPORTED_ALGORITHM_NAMES.clone(),
);
)
.map_body(|_, body| match body {
AnyBody::Full { body } => AnyBody::Stream {
body: Encoder::not_acceptable(body),
},
_ => unreachable!("probably"),
});
Either::right(ok(req
.into_response(res)
.map_into_boxed_body()
.map_into_right_body()))
Either::right(ok(req.into_response(res)))
}
}
}
@ -174,7 +178,7 @@ where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
type Output = Result<ServiceResponse<EitherBody<Encoder<B>>>, Error>;
type Output = Result<ServiceResponse<Encoder<AnyBody<B>>>, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -187,8 +191,8 @@ where
*this.encoding
};
Poll::Ready(Ok(resp.map_body(move |head, body| {
EitherBody::left(Encoder::response(enc, head, body))
Poll::Ready(Ok(resp.map_body(move |head, body| AnyBody::Stream {
body: Encoder::response(enc, head, body),
})))
}

View File

@ -22,6 +22,7 @@ use regex::{Regex, RegexSet};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use crate::{
any_body::AnyBody,
body::{BodySize, MessageBody},
http::header::HeaderName,
service::{ServiceRequest, ServiceResponse},
@ -175,7 +176,7 @@ impl Default for Logger {
}
}
impl<S, B> Transform<S, ServiceRequest> for Logger
impl<S, B: 'static> Transform<S, ServiceRequest> for Logger
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
@ -210,7 +211,7 @@ pub struct LoggerMiddleware<S> {
service: S,
}
impl<S, B> Service<ServiceRequest> for LoggerMiddleware<S>
impl<S, B: 'static> Service<ServiceRequest> for LoggerMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
@ -262,7 +263,7 @@ pin_project! {
}
}
impl<S, B> Future for LoggerResponse<S, B>
impl<S, B: 'static> Future for LoggerResponse<S, B>
where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
@ -290,11 +291,13 @@ where
let time = *this.time;
let format = this.format.take();
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
body,
time,
format,
size: 0,
Poll::Ready(Ok(res.map_body(move |_, body| AnyBody::Stream {
body: StreamLog {
body: body.into_body(),
time,
format,
size: 0,
},
})))
}
}
@ -302,7 +305,7 @@ where
pin_project! {
pub struct StreamLog<B> {
#[pin]
body: B,
body: AnyBody<B>,
format: Option<Format>,
size: usize,
time: OffsetDateTime,