1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
*
* Use of this source code is governed by the Apache 2.0 and/or the MIT
* License.
*/
//! ```rust
//! use std::rc::Rc;
//!
//! use actix_optional_middleware::{Group, Dummy};
//! use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
//! use actix_web::middleware::DefaultHeaders;
//! use actix_web::{web, body::BoxBody, App, Error, HttpServer, Responder, get};
//!
//!#[get("/test", wrap = "get_group_middleware()")]
//! async fn h1() -> impl Responder {
//!     "Handler 1"
//! }
//!
//! // flip this value to see dummy in action
//! const ACTIVE: bool = true;
//!
//! fn get_group_middleware<S>() -> Group<Dummy, DefaultHeaders, S>
//! where
//!     S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
//! {
//!     if ACTIVE {
//!         Group::Real(Rc::new(DefaultHeaders::new()
//!                .header("Permissions-Policy", "interest-cohort=()"
//!         )))
//!     } else {
//!         Group::default()
//!     }
//! }
//! ```

#![allow(clippy::type_complexity)]
use std::marker::PhantomData;
use std::rc::Rc;

use actix_http::body::BoxBody;
use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::Error;

use futures::future::{ok, Either, LocalBoxFuture, Ready};

/// Dummy Middleware: it simply forwards the request without operating on it
pub struct Dummy;

impl<S> Transform<S, ServiceRequest> for Dummy
where
    S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error>,
    S::Future: 'static,
{
    type Response = ServiceResponse<BoxBody>;
    type Error = Error;
    type Transform = DummyMiddleware<S>;
    type InitError = ();
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ok(DummyMiddleware { service })
    }
}

/// Dummy Middleware: it simply forwards the request without operating on it
pub struct DummyMiddleware<S> {
    service: S,
}

impl<S, Req> Service<Req> for DummyMiddleware<S>
where
    S: Service<Req, Response = ServiceResponse<BoxBody>, Error = Error>,
    S::Future: 'static,
{
    type Response = ServiceResponse<BoxBody>;
    type Future = Either<S::Future, Ready<Result<Self::Response, Self::Error>>>;
    type Error = Error;

    actix_service::forward_ready!(service);

    fn call(&self, req: Req) -> Self::Future {
        Either::Left(self.service.call(req))
    }
}

/// Collection datatype that encapsulates dummy and real middlewares
///
/// The appropriate middleware is executed based on the variant chosen
pub enum Group<D, R, Ser>
where
    D: Transform<Ser, ServiceRequest>,
    R: Transform<Ser, ServiceRequest>,
{
    Dummy(Rc<D>),
    Real(Rc<R>),
    __(PhantomData<Ser>),
}

impl<R, Ser> Default for Group<Dummy, R, Ser>
where
    R: Transform<Ser, ServiceRequest>,
    Ser: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
{
    fn default() -> Self {
        Self::Dummy(Rc::new(Dummy))
    }
}

// D is dummy
// R is real
pub enum GroupMiddleware<D, R>
where
    D: Service<ServiceRequest>,
    R: Service<ServiceRequest>,
{
    Dummy(Rc<D>),
    Real(Rc<R>),
}

impl<D, R, S, DS, RS> Transform<S, ServiceRequest> for Group<D, R, S>
where
    S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
    D: Transform<S, ServiceRequest, Transform = DS, InitError = (), Error = Error> + 'static,
    R: Transform<S, ServiceRequest, Transform = RS, InitError = (), Error = Error> + 'static,
    DS: Service<ServiceRequest, Error = Error, Response = ServiceResponse> + 'static,
    RS: Service<ServiceRequest, Error = Error, Response = ServiceResponse> + 'static,
{
    type Response = ServiceResponse<BoxBody>;
    type Error = Error;
    type Transform = GroupMiddleware<DS, RS>;
    type InitError = ();
    type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        match self {
            Self::Real(val) => {
                let val = Rc::clone(val);
                Box::pin(async move {
                    match val.new_transform(service).await {
                        Ok(val) => Ok(GroupMiddleware::Real(Rc::new(val))),
                        Err(e) => Err(e),
                    }
                })
            }

            Self::Dummy(val) => {
                let val = Rc::clone(val);
                Box::pin(async move {
                    match val.new_transform(service).await {
                        Ok(val) => Ok(GroupMiddleware::Dummy(Rc::new(val))),
                        Err(e) => Err(e),
                    }
                })
            }

            _ => panic!(),
        }
    }
}

impl<D, R> Service<ServiceRequest> for GroupMiddleware<D, R>
where
    D: Service<ServiceRequest, Error = Error, Response = ServiceResponse> + 'static,
    R: Service<ServiceRequest, Error = Error, Response = ServiceResponse> + 'static,
{
    type Response = ServiceResponse<BoxBody>;
    type Error = Error;
    type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(
        &self,
        cx: &mut ::core::task::Context<'_>,
    ) -> ::core::task::Poll<Result<(), Self::Error>> {
        match self {
            Self::Real(val) => val.poll_ready(cx),
            Self::Dummy(val) => val.poll_ready(cx),
            #[allow(unreachable_patterns)]
            _ => panic!(),
        }
    }

    fn call(&self, req: ServiceRequest) -> Self::Future {
        match self {
            Self::Real(val) => {
                let val = Rc::clone(val);
                Box::pin(async move { val.call(req).await })
            }
            Self::Dummy(val) => {
                let val = Rc::clone(val);
                Box::pin(async move { val.call(req).await })
            }
            #[allow(unreachable_patterns)]
            _ => panic!(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use actix_web::middleware::DefaultHeaders;
    use actix_web::Responder;
    use actix_web::{http, test, App};

    const ENABLED: &str = "/enabled";
    const DISABLED: &str = "/disabled";
    const DEFAULT_HEADER: (&str, &str) = ("Permissions-Policy", "interest-cohort=()");

    #[my_codegen::get(path = "ENABLED", wrap = "get_enabled()")]
    async fn enabled() -> impl Responder {
        "Enabled"
    }

    #[my_codegen::get(path = "DISABLED", wrap = "get_disabled()")]
    async fn disabled() -> impl Responder {
        "Disabled"
    }

    fn get_enabled<S>() -> Group<Dummy, DefaultHeaders, S>
    where
        S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
    {
        get_group_middleware(true)
    }

    fn get_disabled<S>() -> Group<Dummy, DefaultHeaders, S>
    where
        S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
    {
        get_group_middleware(false)
    }

    fn get_group_middleware<S>(active: bool) -> Group<Dummy, DefaultHeaders, S>
    where
        S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
    {
        if active {
            Group::Real(Rc::new(
                DefaultHeaders::new().header(DEFAULT_HEADER.0, DEFAULT_HEADER.1),
            ))
        } else {
            Group::default()
        }
    }

    fn service(cfg: &mut actix_web::web::ServiceConfig) {
        cfg.service(enabled);
        cfg.service(disabled);
    }
    #[actix_rt::test]
    async fn test() {
        let app = test::init_service(App::new().configure(service)).await;

        // test enabled
        let resp =
            test::call_service(&app, test::TestRequest::get().uri(ENABLED).to_request()).await;
        assert_eq!(resp.status(), http::StatusCode::OK);
        assert!(resp
            .headers()
            .iter()
            .any(|(k, v)| k == DEFAULT_HEADER.0 && v == DEFAULT_HEADER.1));

        // test disabled
        let resp =
            test::call_service(&app, test::TestRequest::get().uri(DISABLED).to_request()).await;
        assert_eq!(resp.status(), http::StatusCode::OK);
        assert!(!resp
            .headers()
            .iter()
            .any(|(k, v)| k == DEFAULT_HEADER.0 && v == DEFAULT_HEADER.1))
    }
}