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
use super::{Config, CorsService};
use middleware::Middleware;

use http;
use util::http::HttpService;

use std::sync::Arc;

/// Middleware providing an implementation of the CORS specification.
#[derive(Debug)]
pub struct CorsMiddleware {
    config: Arc<Config>,
}

impl CorsMiddleware {
    pub(super) fn new(config: Config) -> CorsMiddleware {
        let config = Arc::new(config);
        CorsMiddleware { config }
    }
}

impl<S> Middleware<S> for CorsMiddleware
where
    S: HttpService,
{
    type Request = http::Request<S::RequestBody>;
    type Response = http::Response<Option<S::ResponseBody>>;
    type Error = S::Error;
    type Service = CorsService<S>;

    fn wrap(&self, service: S) -> Self::Service {
        CorsService::new(service, self.config.clone())
    }
}