mirror of
https://github.com/fafhrd91/actix-web
synced 2025-08-23 05:55:13 +02:00
major cleanup of middleware module (#1875)
* major cleanup of middleware module * update changelog
This commit is contained in:
@@ -1,45 +1,47 @@
|
||||
//! `Middleware` for compressing response body.
|
||||
use std::cmp;
|
||||
use std::future::Future;
|
||||
use std::marker::PhantomData;
|
||||
use std::pin::Pin;
|
||||
use std::str::FromStr;
|
||||
use std::task::{Context, Poll};
|
||||
//! For middleware documentation, see [`Compress`].
|
||||
|
||||
use actix_http::body::MessageBody;
|
||||
use actix_http::encoding::Encoder;
|
||||
use actix_http::http::header::{ContentEncoding, ACCEPT_ENCODING};
|
||||
use actix_http::Error;
|
||||
use std::{
|
||||
cmp,
|
||||
future::Future,
|
||||
marker::PhantomData,
|
||||
pin::Pin,
|
||||
str::FromStr,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_http::{
|
||||
body::MessageBody,
|
||||
encoding::Encoder,
|
||||
http::header::{ContentEncoding, ACCEPT_ENCODING},
|
||||
Error,
|
||||
};
|
||||
use actix_service::{Service, Transform};
|
||||
use futures_util::future::{ok, Ready};
|
||||
use pin_project::pin_project;
|
||||
|
||||
use crate::dev::BodyEncoding;
|
||||
use crate::service::{ServiceRequest, ServiceResponse};
|
||||
use crate::{
|
||||
dev::BodyEncoding,
|
||||
service::{ServiceRequest, ServiceResponse},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// `Middleware` for compressing response body.
|
||||
/// Middleware for compressing response payloads.
|
||||
///
|
||||
/// Use `BodyEncoding` trait for overriding response compression.
|
||||
/// To disable compression set encoding to `ContentEncoding::Identity` value.
|
||||
/// Use `BodyEncoding` trait for overriding response compression. To disable compression set
|
||||
/// encoding to `ContentEncoding::Identity`.
|
||||
///
|
||||
/// # Usage
|
||||
/// ```rust
|
||||
/// use actix_web::{web, middleware, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .wrap(middleware::Compress::default())
|
||||
/// .service(
|
||||
/// web::resource("/test")
|
||||
/// .route(web::get().to(|| HttpResponse::Ok()))
|
||||
/// .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
|
||||
/// );
|
||||
/// }
|
||||
/// let app = App::new()
|
||||
/// .wrap(middleware::Compress::default())
|
||||
/// .default_service(web::to(|| HttpResponse::NotFound()));
|
||||
/// ```
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Compress(ContentEncoding);
|
||||
|
||||
impl Compress {
|
||||
/// Create new `Compress` middleware with default encoding.
|
||||
/// Create new `Compress` middleware with the specified encoding.
|
||||
pub fn new(encoding: ContentEncoding) -> Self {
|
||||
Compress(encoding)
|
||||
}
|
||||
@@ -84,9 +86,7 @@ where
|
||||
type Error = Error;
|
||||
type Future = CompressResponse<S, B>;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.poll_ready(cx)
|
||||
}
|
||||
actix_service::forward_ready!(service);
|
||||
|
||||
#[allow(clippy::borrow_interior_mutable_const)]
|
||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||
@@ -109,7 +109,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[pin_project]
|
||||
pub struct CompressResponse<S, B>
|
||||
where
|
||||
|
Reference in New Issue
Block a user