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
//! A set of built-in `ModifyHandler`s.

pub use self::{default_options::DefaultOptions, map_output::MapOutput};

/// Creates a `ModifyHandler` that overwrites the handling when receiving `OPTIONS`.
pub fn default_options() -> DefaultOptions {
    DefaultOptions(())
}

mod default_options {
    use {
        crate::{
            future::{Poll, TryFuture},
            handler::{AllowedMethods, Handler, ModifyHandler},
            input::Input,
        },
        either::Either,
        http::{header::HeaderValue, Method, Response},
    };

    #[derive(Debug, Clone)]
    pub struct DefaultOptions(pub(super) ());

    impl<H> ModifyHandler<H> for DefaultOptions
    where
        H: Handler,
    {
        type Output = Either<Response<()>, H::Output>;
        type Handler = DefaultOptionsHandler<H>; // private

        fn modify(&self, inner: H) -> Self::Handler {
            let allowed_methods = inner.allowed_methods().cloned().map(|mut methods| {
                methods.extend(Some(http::Method::OPTIONS));
                methods
            });
            DefaultOptionsHandler {
                inner,
                allowed_methods,
            }
        }
    }

    #[allow(missing_debug_implementations)]
    pub struct DefaultOptionsHandler<H> {
        inner: H,
        allowed_methods: Option<AllowedMethods>,
    }

    impl<H> Handler for DefaultOptionsHandler<H>
    where
        H: Handler,
    {
        type Output = Either<Response<()>, H::Output>;
        type Error = H::Error;
        type Handle = HandleDefaultOptions<H::Handle>;

        fn handle(&self) -> Self::Handle {
            HandleDefaultOptions {
                inner: self.inner.handle(),
                allowed_methods_value: self.allowed_methods().map(|m| m.to_header_value()),
            }
        }

        fn allowed_methods(&self) -> Option<&AllowedMethods> {
            self.allowed_methods.as_ref()
        }
    }

    #[allow(missing_debug_implementations)]
    pub struct HandleDefaultOptions<H> {
        inner: H,
        allowed_methods_value: Option<HeaderValue>,
    }

    impl<H> TryFuture for HandleDefaultOptions<H>
    where
        H: TryFuture,
    {
        type Ok = Either<Response<()>, H::Ok>;
        type Error = H::Error;

        #[inline]
        fn poll_ready(&mut self, input: &mut Input<'_>) -> Poll<Self::Ok, Self::Error> {
            if input.request.method() == Method::OPTIONS {
                let allowed_methods_value =
                    self.allowed_methods_value.take().unwrap_or_else(|| {
                        HeaderValue::from_static(
                            "GET, POST, PUT, DELETE, HEAD, OPTIONS, CONNECT, PATCH, TRACE",
                        )
                    });
                Ok(Either::Left(
                    Response::builder()
                        .status(http::StatusCode::NO_CONTENT)
                        .header(http::header::ALLOW, allowed_methods_value)
                        .body(())
                        .expect("should be a valid response"),
                )
                .into())
            } else {
                self.inner.poll_ready(input).map(|x| x.map(Either::Right))
            }
        }
    }
}

/// Creates a `ModifyHandler` that converts the output value using the specified function.
pub fn map_output<F>(f: F) -> MapOutput<F> {
    self::map_output::MapOutput { f }
}

mod map_output {
    use crate::{
        future::{Poll, TryFuture},
        handler::{AllowedMethods, Handler, ModifyHandler},
        input::Input,
    };

    #[derive(Debug, Clone)]
    pub struct MapOutput<F> {
        pub(super) f: F,
    }

    impl<H, F, T> ModifyHandler<H> for MapOutput<F>
    where
        H: Handler,
        F: Fn(H::Output) -> T + Clone,
    {
        type Output = T;
        type Handler = MapOutputHandler<H, F>;

        fn modify(&self, handler: H) -> Self::Handler {
            MapOutputHandler {
                handler,
                f: self.f.clone(),
            }
        }
    }

    #[allow(missing_debug_implementations)]
    pub struct MapOutputHandler<H, F> {
        handler: H,
        f: F,
    }

    impl<H, F, T> Handler for MapOutputHandler<H, F>
    where
        H: Handler,
        F: Fn(H::Output) -> T + Clone,
    {
        type Output = T;
        type Error = H::Error;
        type Handle = HandleMapOutput<H::Handle, F>;

        fn handle(&self) -> Self::Handle {
            HandleMapOutput {
                handle: self.handler.handle(),
                f: self.f.clone(),
            }
        }

        fn allowed_methods(&self) -> Option<&AllowedMethods> {
            self.handler.allowed_methods()
        }
    }

    #[allow(missing_debug_implementations)]
    pub struct HandleMapOutput<H, F> {
        handle: H,
        f: F,
    }

    #[allow(clippy::redundant_closure)]
    impl<H, F, T> TryFuture for HandleMapOutput<H, F>
    where
        H: TryFuture,
        F: Fn(H::Ok) -> T,
    {
        type Ok = T;
        type Error = H::Error;

        #[inline]
        fn poll_ready(&mut self, input: &mut Input<'_>) -> Poll<Self::Ok, Self::Error> {
            self.handle
                .poll_ready(input)
                .map(|x| x.map(|out| (self.f)(out)))
        }
    }
}