use std::future::{ready, Ready};
use actix_service::{Service, Transform};
use actix_web::{
body::MessageBody,
dev::{forward_ready, ServiceRequest, ServiceResponse},
http::header,
Error, HttpResponse,
};
#[derive(Clone, Debug)]
pub struct Compression {
pub gzip: bool,
}
impl Default for Compression {
fn default() -> Self {
Self { gzip: true }
}
}
impl Compression {
pub fn new() -> Self {
Self::default()
}
pub fn gzip(mut self, enable: bool) -> Self {
self.gzip = enable;
self
}
}
impl<S, B> Transform<S, ServiceRequest> for Compression
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: MessageBody + 'static,
{
type Response = ServiceResponse;
type Error = Error;
type Transform = CompressionMiddleware<S>;
type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(CompressionMiddleware {
service,
gzip: self.gzip,
}))
}
}
pub struct CompressionMiddleware<S> {
service: S,
gzip: bool,
}
impl<S, B> Service<ServiceRequest> for CompressionMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: MessageBody + 'static,
{
type Response = ServiceResponse;
type Error = Error;
type Future =
std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>>>>;
forward_ready!(service);
fn call(&self, req: ServiceRequest) -> Self::Future {
let accept_encoding = req
.headers()
.get(header::ACCEPT_ENCODING)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.to_string();
let supports_gzip = self.gzip && accept_encoding.contains("gzip");
let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
if supports_gzip {
let content_type = res
.headers()
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.to_string();
let should_compress = content_type.starts_with("text/")
|| content_type.contains("json")
|| content_type.contains("xml")
|| content_type.contains("javascript");
if should_compress {
let (req, response) = res.into_parts();
let status = response.status();
let body = response.into_body();
let bytes = actix_web::body::to_bytes(body)
.await
.map_err(|e| actix_web::error::ErrorInternalServerError(e.into()))?;
use std::io::Write;
let mut encoder =
flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
encoder.write_all(&bytes)?;
let compressed = encoder.finish()?;
let new_response = HttpResponse::build(status)
.insert_header((header::CONTENT_ENCODING, "gzip"))
.insert_header((header::CONTENT_TYPE, content_type))
.body(compressed);
return Ok(ServiceResponse::new(req, new_response));
}
}
Ok(res.map_into_boxed_body())
})
}
}