mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 05:41:50 +01:00
App::enable_encoding() allows to enable compression and decompression
This commit is contained in:
parent
5795850bbb
commit
605ce05127
99
src/app.rs
99
src/app.rs
@ -3,18 +3,21 @@ use std::marker::PhantomData;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::body::{Body, MessageBody};
|
use actix_http::body::{Body, MessageBody};
|
||||||
|
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
|
||||||
|
use actix_http::encoding::{Decoder, Encoder};
|
||||||
use actix_server_config::ServerConfig;
|
use actix_server_config::ServerConfig;
|
||||||
use actix_service::boxed::{self, BoxedNewService};
|
use actix_service::boxed::{self, BoxedNewService};
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
ApplyTransform, IntoNewService, IntoTransform, NewService, Transform,
|
ApplyTransform, IntoNewService, IntoTransform, NewService, Transform,
|
||||||
};
|
};
|
||||||
use futures::IntoFuture;
|
use bytes::Bytes;
|
||||||
|
use futures::{IntoFuture, Stream};
|
||||||
|
|
||||||
use crate::app_service::{AppChain, AppEntry, AppInit, AppRouting, AppRoutingFactory};
|
use crate::app_service::{AppChain, AppEntry, AppInit, AppRouting, AppRoutingFactory};
|
||||||
use crate::config::{AppConfig, AppConfigInner};
|
use crate::config::{AppConfig, AppConfigInner};
|
||||||
use crate::data::{Data, DataFactory};
|
use crate::data::{Data, DataFactory};
|
||||||
use crate::dev::{PayloadStream, ResourceDef};
|
use crate::dev::{Payload, PayloadStream, ResourceDef};
|
||||||
use crate::error::Error;
|
use crate::error::{Error, PayloadError};
|
||||||
use crate::resource::Resource;
|
use crate::resource::Resource;
|
||||||
use crate::route::Route;
|
use crate::route::Route;
|
||||||
use crate::service::{
|
use crate::service::{
|
||||||
@ -27,17 +30,17 @@ type HttpNewService<P> =
|
|||||||
|
|
||||||
/// Application builder - structure that follows the builder pattern
|
/// Application builder - structure that follows the builder pattern
|
||||||
/// for building application instances.
|
/// for building application instances.
|
||||||
pub struct App<P, T>
|
pub struct App<In, Out, T>
|
||||||
where
|
where
|
||||||
T: NewService<Request = ServiceRequest, Response = ServiceRequest<P>>,
|
T: NewService<Request = ServiceRequest<In>, Response = ServiceRequest<Out>>,
|
||||||
{
|
{
|
||||||
chain: T,
|
chain: T,
|
||||||
data: Vec<Box<DataFactory>>,
|
data: Vec<Box<DataFactory>>,
|
||||||
config: AppConfigInner,
|
config: AppConfigInner,
|
||||||
_t: PhantomData<(P,)>,
|
_t: PhantomData<(In, Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App<PayloadStream, AppChain> {
|
impl App<PayloadStream, PayloadStream, AppChain> {
|
||||||
/// Create application builder. Application can be configured with a builder-like pattern.
|
/// Create application builder. Application can be configured with a builder-like pattern.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
App {
|
App {
|
||||||
@ -49,12 +52,13 @@ impl App<PayloadStream, AppChain> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P, T> App<P, T>
|
impl<In, Out, T> App<In, Out, T>
|
||||||
where
|
where
|
||||||
P: 'static,
|
In: 'static,
|
||||||
|
Out: 'static,
|
||||||
T: NewService<
|
T: NewService<
|
||||||
Request = ServiceRequest,
|
Request = ServiceRequest<In>,
|
||||||
Response = ServiceRequest<P>,
|
Response = ServiceRequest<Out>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
@ -97,11 +101,11 @@ where
|
|||||||
/// Set application data factory. This function is
|
/// Set application data factory. This function is
|
||||||
/// similar to `.data()` but it accepts data factory. Data object get
|
/// similar to `.data()` but it accepts data factory. Data object get
|
||||||
/// constructed asynchronously during application initialization.
|
/// constructed asynchronously during application initialization.
|
||||||
pub fn data_factory<F, Out>(mut self, data: F) -> Self
|
pub fn data_factory<F, R>(mut self, data: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn() -> Out + 'static,
|
F: Fn() -> R + 'static,
|
||||||
Out: IntoFuture + 'static,
|
R: IntoFuture + 'static,
|
||||||
Out::Error: std::fmt::Debug,
|
R::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
self.data.push(Box::new(data));
|
self.data.push(Box::new(data));
|
||||||
self
|
self
|
||||||
@ -113,10 +117,10 @@ where
|
|||||||
mw: F,
|
mw: F,
|
||||||
) -> AppRouter<
|
) -> AppRouter<
|
||||||
T,
|
T,
|
||||||
P,
|
Out,
|
||||||
B,
|
B,
|
||||||
impl NewService<
|
impl NewService<
|
||||||
Request = ServiceRequest<P>,
|
Request = ServiceRequest<Out>,
|
||||||
Response = ServiceResponse<B>,
|
Response = ServiceResponse<B>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
@ -124,13 +128,13 @@ where
|
|||||||
>
|
>
|
||||||
where
|
where
|
||||||
M: Transform<
|
M: Transform<
|
||||||
AppRouting<P>,
|
AppRouting<Out>,
|
||||||
Request = ServiceRequest<P>,
|
Request = ServiceRequest<Out>,
|
||||||
Response = ServiceResponse<B>,
|
Response = ServiceResponse<B>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
F: IntoTransform<M, AppRouting<P>>,
|
F: IntoTransform<M, AppRouting<Out>>,
|
||||||
{
|
{
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let fref = Rc::new(RefCell::new(None));
|
||||||
let endpoint = ApplyTransform::new(mw, AppEntry::new(fref.clone()));
|
let endpoint = ApplyTransform::new(mw, AppEntry::new(fref.clone()));
|
||||||
@ -176,17 +180,17 @@ where
|
|||||||
mw: F,
|
mw: F,
|
||||||
) -> AppRouter<
|
) -> AppRouter<
|
||||||
T,
|
T,
|
||||||
P,
|
Out,
|
||||||
B,
|
B,
|
||||||
impl NewService<
|
impl NewService<
|
||||||
Request = ServiceRequest<P>,
|
Request = ServiceRequest<Out>,
|
||||||
Response = ServiceResponse<B>,
|
Response = ServiceResponse<B>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
F: FnMut(ServiceRequest<P>, &mut AppRouting<P>) -> R + Clone,
|
F: FnMut(ServiceRequest<Out>, &mut AppRouting<Out>) -> R + Clone,
|
||||||
R: IntoFuture<Item = ServiceResponse<B>, Error = Error>,
|
R: IntoFuture<Item = ServiceResponse<B>, Error = Error>,
|
||||||
{
|
{
|
||||||
self.wrap(mw)
|
self.wrap(mw)
|
||||||
@ -194,22 +198,23 @@ where
|
|||||||
|
|
||||||
/// Register a request modifier. It can modify any request parameters
|
/// Register a request modifier. It can modify any request parameters
|
||||||
/// including request payload type.
|
/// including request payload type.
|
||||||
pub fn chain<C, F, P1>(
|
pub fn chain<C, F, P>(
|
||||||
self,
|
self,
|
||||||
chain: F,
|
chain: F,
|
||||||
) -> App<
|
) -> App<
|
||||||
P1,
|
In,
|
||||||
|
P,
|
||||||
impl NewService<
|
impl NewService<
|
||||||
Request = ServiceRequest,
|
Request = ServiceRequest<In>,
|
||||||
Response = ServiceRequest<P1>,
|
Response = ServiceRequest<P>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
C: NewService<
|
C: NewService<
|
||||||
Request = ServiceRequest<P>,
|
Request = ServiceRequest<Out>,
|
||||||
Response = ServiceRequest<P1>,
|
Response = ServiceRequest<P>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
@ -246,8 +251,8 @@ where
|
|||||||
pub fn route(
|
pub fn route(
|
||||||
self,
|
self,
|
||||||
path: &str,
|
path: &str,
|
||||||
mut route: Route<P>,
|
mut route: Route<Out>,
|
||||||
) -> AppRouter<T, P, Body, AppEntry<P>> {
|
) -> AppRouter<T, Out, Body, AppEntry<Out>> {
|
||||||
self.service(
|
self.service(
|
||||||
Resource::new(path)
|
Resource::new(path)
|
||||||
.add_guards(route.take_guards())
|
.add_guards(route.take_guards())
|
||||||
@ -264,9 +269,9 @@ where
|
|||||||
/// * *Resource* is an entry in resource table which corresponds to requested URL.
|
/// * *Resource* is an entry in resource table which corresponds to requested URL.
|
||||||
/// * *Scope* is a set of resources with common root path.
|
/// * *Scope* is a set of resources with common root path.
|
||||||
/// * "StaticFiles" is a service for static files support
|
/// * "StaticFiles" is a service for static files support
|
||||||
pub fn service<F>(self, service: F) -> AppRouter<T, P, Body, AppEntry<P>>
|
pub fn service<F>(self, service: F) -> AppRouter<T, Out, Body, AppEntry<Out>>
|
||||||
where
|
where
|
||||||
F: HttpServiceFactory<P> + 'static,
|
F: HttpServiceFactory<Out> + 'static,
|
||||||
{
|
{
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let fref = Rc::new(RefCell::new(None));
|
||||||
|
|
||||||
@ -294,6 +299,34 @@ where
|
|||||||
self.config.host = val.to_owned();
|
self.config.host = val.to_owned();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
|
||||||
|
/// Enable content compression and decompression.
|
||||||
|
pub fn enable_encoding(
|
||||||
|
self,
|
||||||
|
) -> AppRouter<
|
||||||
|
impl NewService<
|
||||||
|
Request = ServiceRequest<In>,
|
||||||
|
Response = ServiceRequest<Decoder<Payload<Out>>>,
|
||||||
|
Error = Error,
|
||||||
|
InitError = (),
|
||||||
|
>,
|
||||||
|
Decoder<Payload<Out>>,
|
||||||
|
Encoder<Body>,
|
||||||
|
impl NewService<
|
||||||
|
Request = ServiceRequest<Decoder<Payload<Out>>>,
|
||||||
|
Response = ServiceResponse<Encoder<Body>>,
|
||||||
|
Error = Error,
|
||||||
|
InitError = (),
|
||||||
|
>,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
Out: Stream<Item = Bytes, Error = PayloadError>,
|
||||||
|
{
|
||||||
|
use crate::middleware::encoding::{Compress, Decompress};
|
||||||
|
|
||||||
|
self.chain(Decompress::new()).wrap(Compress::default())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Application router builder - Structure that follows the builder pattern
|
/// Application router builder - Structure that follows the builder pattern
|
||||||
|
@ -335,6 +335,34 @@ fn test_body_brotli() {
|
|||||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encoding() {
|
||||||
|
let mut srv = TestServer::new(move || {
|
||||||
|
HttpService::new(
|
||||||
|
App::new().enable_encoding().service(
|
||||||
|
web::resource("/")
|
||||||
|
.route(web::to(move |body: Bytes| Response::Ok().body(body))),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
// client request
|
||||||
|
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||||
|
e.write_all(STR.as_ref()).unwrap();
|
||||||
|
let enc = e.finish().unwrap();
|
||||||
|
|
||||||
|
let request = srv
|
||||||
|
.post()
|
||||||
|
.header(CONTENT_ENCODING, "gzip")
|
||||||
|
.send_body(enc.clone());
|
||||||
|
let mut response = srv.block_on(request).unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
|
// read response
|
||||||
|
let bytes = srv.block_on(HttpMessageBody::new(&mut response)).unwrap();
|
||||||
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gzip_encoding() {
|
fn test_gzip_encoding() {
|
||||||
let mut srv = TestServer::new(move || {
|
let mut srv = TestServer::new(move || {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user