2019-09-09 06:26:38 +00:00
|
|
|
//! `Middleware` for conditionally enables another middleware.
|
2019-11-21 08:52:33 +00:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-09-09 06:26:38 +00:00
|
|
|
use actix_service::{Service, Transform};
|
2019-11-26 05:25:50 +00:00
|
|
|
use futures::future::{ok, Either, FutureExt, LocalBoxFuture};
|
2019-09-09 06:26:38 +00:00
|
|
|
|
|
|
|
/// `Middleware` for conditionally enables another middleware.
|
2020-04-21 03:09:35 +00:00
|
|
|
/// The controlled middleware must not change the `Service` interfaces.
|
2019-09-09 06:26:38 +00:00
|
|
|
/// This means you cannot control such middlewares like `Logger` or `Compress`.
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::middleware::{Condition, NormalizePath};
|
|
|
|
/// use actix_web::App;
|
|
|
|
///
|
2019-11-21 08:52:33 +00:00
|
|
|
/// # fn main() {
|
|
|
|
/// let enable_normalize = std::env::var("NORMALIZE_PATH") == Ok("true".into());
|
|
|
|
/// let app = App::new()
|
|
|
|
/// .wrap(Condition::new(enable_normalize, NormalizePath));
|
|
|
|
/// # }
|
2019-09-09 06:26:38 +00:00
|
|
|
/// ```
|
|
|
|
pub struct Condition<T> {
|
|
|
|
trans: T,
|
|
|
|
enable: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Condition<T> {
|
|
|
|
pub fn new(enable: bool, trans: T) -> Self {
|
|
|
|
Self { trans, enable }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, T> Transform<S> for Condition<T>
|
|
|
|
where
|
2019-11-21 08:52:33 +00:00
|
|
|
S: Service + 'static,
|
2019-09-09 06:26:38 +00:00
|
|
|
T: Transform<S, Request = S::Request, Response = S::Response, Error = S::Error>,
|
2019-11-21 08:52:33 +00:00
|
|
|
T::Future: 'static,
|
|
|
|
T::InitError: 'static,
|
|
|
|
T::Transform: 'static,
|
2019-09-09 06:26:38 +00:00
|
|
|
{
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type InitError = T::InitError;
|
|
|
|
type Transform = ConditionMiddleware<T::Transform, S>;
|
2019-11-21 08:52:33 +00:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>;
|
2019-09-09 06:26:38 +00:00
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
|
|
|
if self.enable {
|
2019-11-21 08:52:33 +00:00
|
|
|
let f = self.trans.new_transform(service).map(|res| {
|
|
|
|
res.map(
|
|
|
|
ConditionMiddleware::Enable as fn(T::Transform) -> Self::Transform,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
Either::Left(f)
|
2019-09-09 06:26:38 +00:00
|
|
|
} else {
|
2019-11-21 08:52:33 +00:00
|
|
|
Either::Right(ok(ConditionMiddleware::Disable(service)))
|
2019-09-09 06:26:38 +00:00
|
|
|
}
|
2019-11-21 08:52:33 +00:00
|
|
|
.boxed_local()
|
2019-09-09 06:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ConditionMiddleware<E, D> {
|
|
|
|
Enable(E),
|
|
|
|
Disable(D),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E, D> Service for ConditionMiddleware<E, D>
|
|
|
|
where
|
|
|
|
E: Service,
|
|
|
|
D: Service<Request = E::Request, Response = E::Response, Error = E::Error>,
|
|
|
|
{
|
|
|
|
type Request = E::Request;
|
|
|
|
type Response = E::Response;
|
|
|
|
type Error = E::Error;
|
|
|
|
type Future = Either<E::Future, D::Future>;
|
|
|
|
|
2019-12-07 18:46:51 +00:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-09-09 06:26:38 +00:00
|
|
|
use ConditionMiddleware::*;
|
|
|
|
match self {
|
2019-11-21 08:52:33 +00:00
|
|
|
Enable(service) => service.poll_ready(cx),
|
|
|
|
Disable(service) => service.poll_ready(cx),
|
2019-09-09 06:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: E::Request) -> Self::Future {
|
|
|
|
use ConditionMiddleware::*;
|
|
|
|
match self {
|
2019-11-21 08:52:33 +00:00
|
|
|
Enable(service) => Either::Left(service.call(req)),
|
|
|
|
Disable(service) => Either::Right(service.call(req)),
|
2019-09-09 06:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use actix_service::IntoService;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::dev::{ServiceRequest, ServiceResponse};
|
|
|
|
use crate::error::Result;
|
|
|
|
use crate::http::{header::CONTENT_TYPE, HeaderValue, StatusCode};
|
|
|
|
use crate::middleware::errhandlers::*;
|
2019-11-26 05:25:50 +00:00
|
|
|
use crate::test::{self, TestRequest};
|
2019-09-09 06:26:38 +00:00
|
|
|
use crate::HttpResponse;
|
|
|
|
|
|
|
|
fn render_500<B>(mut res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
|
|
|
|
res.response_mut()
|
|
|
|
.headers_mut()
|
|
|
|
.insert(CONTENT_TYPE, HeaderValue::from_static("0001"));
|
|
|
|
Ok(ErrorHandlerResponse::Response(res))
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_handler_enabled() {
|
|
|
|
let srv = |req: ServiceRequest| {
|
|
|
|
ok(req.into_response(HttpResponse::InternalServerError().finish()))
|
|
|
|
};
|
|
|
|
|
|
|
|
let mw =
|
|
|
|
ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, render_500);
|
|
|
|
|
|
|
|
let mut mw = Condition::new(true, mw)
|
|
|
|
.new_transform(srv.into_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let resp =
|
|
|
|
test::call_service(&mut mw, TestRequest::default().to_srv_request()).await;
|
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
2019-09-09 06:26:38 +00:00
|
|
|
}
|
2019-11-21 08:52:33 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_handler_disabled() {
|
|
|
|
let srv = |req: ServiceRequest| {
|
|
|
|
ok(req.into_response(HttpResponse::InternalServerError().finish()))
|
|
|
|
};
|
|
|
|
|
|
|
|
let mw =
|
|
|
|
ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, render_500);
|
|
|
|
|
|
|
|
let mut mw = Condition::new(false, mw)
|
|
|
|
.new_transform(srv.into_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let resp =
|
|
|
|
test::call_service(&mut mw, TestRequest::default().to_srv_request()).await;
|
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE), None);
|
2019-09-09 06:26:38 +00:00
|
|
|
}
|
|
|
|
}
|