use std::future::{Ready, ready};
use std::rc::Rc;
use actix_web::{
Error,
body::BoxBody,
dev::{Service, ServiceRequest, ServiceResponse, Transform},
};
use crate::ModSecurityService;
use crate::modsecurity::ModSecurity;
use crate::service::ModSecurityInner;
pub struct Middleware(Rc<ModSecurity>);
impl Middleware {
#[inline]
pub fn new(modsecurity: ModSecurity) -> Self {
Self(Rc::new(modsecurity))
}
}
impl<S> Transform<S, ServiceRequest> for Middleware
where
S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
S::Future: 'static,
{
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type InitError = ();
type Transform = ModSecurityService<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(ModSecurityService(Rc::new(ModSecurityInner {
service: Rc::new(service),
modsecurity: Rc::clone(&self.0),
}))))
}
}