poem 3.1.12

Poem is a full-featured and easy-to-use web framework with the Rust programming language.
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Commonly used middleware.

mod add_data;
mod catch_panic;
#[cfg(feature = "compression")]
mod compression;
#[cfg(feature = "cookie")]
mod cookie_jar_manager;
mod cors;
#[cfg(feature = "csrf")]
mod csrf;
mod force_https;
mod normalize_path;
#[cfg(feature = "opentelemetry")]
mod opentelemetry_metrics;
#[cfg(feature = "opentelemetry")]
mod opentelemetry_tracing;
mod propagate_header;
#[cfg(feature = "requestid")]
mod requestid;
mod sensitive_header;
mod set_header;
mod size_limit;
#[cfg(feature = "tokio-metrics")]
mod tokio_metrics_mw;
#[cfg(feature = "tower-compat")]
mod tower_compat;
mod tracing_mw;

use std::marker::PhantomData;

#[cfg(feature = "compression")]
pub use self::compression::{Compression, CompressionEndpoint};
#[cfg(feature = "cookie")]
pub use self::cookie_jar_manager::{CookieJarManager, CookieJarManagerEndpoint};
#[cfg(feature = "csrf")]
pub use self::csrf::{Csrf, CsrfEndpoint};
#[cfg(feature = "opentelemetry")]
pub use self::opentelemetry_metrics::{OpenTelemetryMetrics, OpenTelemetryMetricsEndpoint};
#[cfg(feature = "opentelemetry")]
pub use self::opentelemetry_tracing::{OpenTelemetryTracing, OpenTelemetryTracingEndpoint};
#[cfg(feature = "requestid")]
pub use self::requestid::{ReqId, RequestId, RequestIdEndpoint, ReuseId};
#[cfg(feature = "tokio-metrics")]
pub use self::tokio_metrics_mw::{TokioMetrics, TokioMetricsEndpoint};
#[cfg(feature = "tower-compat")]
pub use self::tower_compat::TowerLayerCompatExt;
pub use self::{
    add_data::{AddData, AddDataEndpoint},
    catch_panic::{CatchPanic, CatchPanicEndpoint, PanicHandler},
    cors::{Cors, CorsEndpoint},
    force_https::ForceHttps,
    normalize_path::{NormalizePath, NormalizePathEndpoint, TrailingSlash},
    propagate_header::{PropagateHeader, PropagateHeaderEndpoint},
    sensitive_header::{SensitiveHeader, SensitiveHeaderEndpoint},
    set_header::{SetHeader, SetHeaderEndpoint},
    size_limit::{SizeLimit, SizeLimitEndpoint},
    tracing_mw::{Tracing, TracingEndpoint},
};
use crate::endpoint::{EitherEndpoint, Endpoint};

/// Represents a middleware trait.
///
/// # Create your own middleware
///
/// ```
/// use poem::{
///     Endpoint, EndpointExt, Middleware, Request, Result, handler, test::TestClient, web::Data,
/// };
///
/// /// A middleware that extracts token from HTTP headers.
/// struct TokenMiddleware;
///
/// impl<E: Endpoint> Middleware<E> for TokenMiddleware {
///     type Output = TokenMiddlewareImpl<E>;
///
///     fn transform(&self, ep: E) -> Self::Output {
///         TokenMiddlewareImpl { ep }
///     }
/// }
///
/// /// The new endpoint type generated by the TokenMiddleware.
/// struct TokenMiddlewareImpl<E> {
///     ep: E,
/// }
///
/// const TOKEN_HEADER: &str = "X-Token";
///
/// /// Token data
/// #[derive(Clone)]
/// struct Token(String);
///
/// impl<E: Endpoint> Endpoint for TokenMiddlewareImpl<E> {
///     type Output = E::Output;
///
///     async fn call(&self, mut req: Request) -> Result<Self::Output> {
///         if let Some(value) = req
///             .headers()
///             .get(TOKEN_HEADER)
///             .and_then(|value| value.to_str().ok())
///         {
///             // Insert token data to extensions of request.
///             let token = value.to_string();
///             req.extensions_mut().insert(Token(token));
///         }
///
///         // call the next endpoint.
///         self.ep.call(req).await
///     }
/// }
///
/// #[handler]
/// async fn index(Data(token): Data<&Token>) -> String {
///     token.0.clone()
/// }
///
/// // Use the `TokenMiddleware` middleware to convert the `index` endpoint.
/// let ep = index.with(TokenMiddleware);
///
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// let mut resp = TestClient::new(ep)
///     .get("/")
///     .header(TOKEN_HEADER, "abc")
///     .send()
///     .await;
/// resp.assert_status_is_ok();
/// resp.assert_text("abc").await;
/// # });
/// ```
///
/// # Create middleware with functions
///
/// ```rust
/// use std::sync::Arc;
///
/// use poem::{
///     Endpoint, EndpointExt, IntoResponse, Request, Result, handler, test::TestClient, web::Data,
/// };
/// const TOKEN_HEADER: &str = "X-Token";
///
/// #[handler]
/// async fn index(Data(token): Data<&Token>) -> String {
///     token.0.clone()
/// }
///
/// /// Token data
/// #[derive(Clone)]
/// struct Token(String);
///
/// async fn token_middleware<E: Endpoint>(next: E, mut req: Request) -> Result<E::Output> {
///     if let Some(value) = req
///         .headers()
///         .get(TOKEN_HEADER)
///         .and_then(|value| value.to_str().ok())
///     {
///         // Insert token data to extensions of request.
///         let token = value.to_string();
///         req.extensions_mut().insert(Token(token));
///     }
///
///     // call the next endpoint.
///     next.call(req).await
/// }
///
/// let ep = index.around(token_middleware);
/// let cli = TestClient::new(ep);
///
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// let resp = cli.get("/").header(TOKEN_HEADER, "abc").send().await;
/// resp.assert_status_is_ok();
/// resp.assert_text("abc").await;
/// # });
/// ```
pub trait Middleware<E: Endpoint> {
    /// New endpoint type.
    ///
    /// If you don't know what type to use, then you can use
    /// [`BoxEndpoint`](crate::endpoint::BoxEndpoint), which will bring some
    /// performance loss, but it is insignificant.
    type Output: Endpoint;

    /// Transform the input [`Endpoint`] to another one.
    fn transform(&self, ep: E) -> Self::Output;

    /// Create a new middleware by combining two middlewares.
    ///
    /// # Example
    ///
    /// ```
    /// use poem::{
    ///     Endpoint, EndpointExt, Middleware, Request, Result, handler, middleware::SetHeader,
    /// };
    ///
    /// #[handler]
    /// fn index() -> &'static str {
    ///     "hello"
    /// }
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), std::io::Error> {
    ///     let ep = index.with(
    ///         SetHeader::new()
    ///             .appending("myheader", "a")
    ///             .combine(SetHeader::new().appending("myheader", "b")),
    ///     );
    ///
    ///     let resp = ep.call(Request::default()).await.unwrap();
    ///     let values = resp
    ///         .headers()
    ///         .get_all("myheader")
    ///         .iter()
    ///         .flat_map(|value| value.to_str().ok())
    ///         .collect::<Vec<_>>();
    ///     assert_eq!(values, vec!["a", "b"]);
    ///     Ok(())
    /// }
    /// ```
    fn combine<T>(self, other: T) -> CombineMiddleware<Self, T, E>
    where
        T: Middleware<Self::Output> + Sized,
        Self: Sized,
    {
        CombineMiddleware {
            a: self,
            b: other,
            _mark: PhantomData,
        }
    }

    /// if `enable` is `true` then combine the middleware.
    ///
    /// # Example
    ///
    /// ```
    /// use poem::{
    ///     Endpoint, EndpointExt, Middleware, Request, Result, handler, middleware::SetHeader,
    /// };
    ///
    /// #[handler]
    /// fn index() -> &'static str {
    ///     "hello"
    /// }
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), std::io::Error> {
    ///     let ep1 = index.with(
    ///         SetHeader::new()
    ///             .appending("myheader", "a")
    ///             .combine_if(false, SetHeader::new().appending("myheader", "b")),
    ///     );
    ///
    ///     let ep2 = index.with(
    ///         SetHeader::new()
    ///             .appending("myheader", "a")
    ///             .combine_if(true, SetHeader::new().appending("myheader", "b")),
    ///     );
    ///
    ///     let resp = ep1.call(Request::default()).await.unwrap();
    ///     let values = resp
    ///         .headers()
    ///         .get_all("myheader")
    ///         .iter()
    ///         .flat_map(|value| value.to_str().ok())
    ///         .collect::<Vec<_>>();
    ///     assert_eq!(values, vec!["a"]);
    ///
    ///     let resp = ep2.call(Request::default()).await.unwrap();
    ///     let values = resp
    ///         .headers()
    ///         .get_all("myheader")
    ///         .iter()
    ///         .flat_map(|value| value.to_str().ok())
    ///         .collect::<Vec<_>>();
    ///     assert_eq!(values, vec!["a", "b"]);
    ///     Ok(())
    /// }
    /// ```
    fn combine_if<T>(
        self,
        enable: bool,
        other: T,
    ) -> EitherMiddleware<Self, CombineMiddleware<Self, T, E>, E>
    where
        T: Middleware<Self::Output> + Sized,
        Self: Sized,
    {
        if !enable {
            EitherMiddleware::left(self)
        } else {
            EitherMiddleware::right(self.combine(other))
        }
    }
}

impl<E: Endpoint> Middleware<E> for () {
    type Output = E;

    #[inline]
    fn transform(&self, ep: E) -> Self::Output {
        ep
    }
}

impl<E: Endpoint, T: Middleware<E>> Middleware<E> for &T {
    type Output = T::Output;

    fn transform(&self, ep: E) -> Self::Output {
        T::transform(self, ep)
    }
}

/// A middleware that combines two middlewares.
pub struct CombineMiddleware<A, B, E> {
    a: A,
    b: B,
    _mark: PhantomData<E>,
}

impl<A, B, E> Middleware<E> for CombineMiddleware<A, B, E>
where
    A: Middleware<E>,
    B: Middleware<A::Output>,
    E: Endpoint,
{
    type Output = B::Output;

    #[inline]
    fn transform(&self, ep: E) -> Self::Output {
        self.b.transform(self.a.transform(ep))
    }
}

/// The enum `EitherMiddleware` with variants `Left` and `Right` is a general
/// purpose sum type with two cases.
pub enum EitherMiddleware<A, B, E> {
    /// A middleware of type `A`
    A(A, PhantomData<E>),
    /// B middleware of type `B`
    B(B, PhantomData<E>),
}

impl<A, B, E> EitherMiddleware<A, B, E> {
    /// Create a new `EitherMiddleware` with the left variant.
    #[inline]
    pub fn left(a: A) -> Self {
        EitherMiddleware::A(a, PhantomData)
    }

    /// Create a new `EitherMiddleware` with the right variant.
    #[inline]
    pub fn right(b: B) -> Self {
        EitherMiddleware::B(b, PhantomData)
    }
}

impl<A, B, E> Middleware<E> for EitherMiddleware<A, B, E>
where
    A: Middleware<E>,
    B: Middleware<E>,
    E: Endpoint,
{
    type Output = EitherEndpoint<A::Output, B::Output>;

    #[inline]
    fn transform(&self, ep: E) -> Self::Output {
        match self {
            EitherMiddleware::A(a, _) => EitherEndpoint::A(a.transform(ep)),
            EitherMiddleware::B(b, _) => EitherEndpoint::B(b.transform(ep)),
        }
    }
}

poem_derive::generate_implement_middlewares!();

/// A middleware implemented by a closure.
pub struct FnMiddleware<T>(T);

impl<T, E, E2> Middleware<E> for FnMiddleware<T>
where
    T: Fn(E) -> E2,
    E: Endpoint,
    E2: Endpoint,
{
    type Output = E2;

    fn transform(&self, ep: E) -> Self::Output {
        (self.0)(ep)
    }
}

/// Make middleware with a closure.
pub fn make<T>(f: T) -> FnMiddleware<T> {
    FnMiddleware(f)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        EndpointExt, IntoResponse, Request, Response, Result, handler,
        http::{HeaderValue, header::HeaderName},
        test::TestClient,
        web::Data,
    };

    #[tokio::test]
    async fn test_make() {
        #[handler(internal)]
        fn index() -> &'static str {
            "abc"
        }

        struct AddHeader<E> {
            ep: E,
            header: HeaderName,
            value: HeaderValue,
        }

        impl<E: Endpoint> Endpoint for AddHeader<E> {
            type Output = Response;

            async fn call(&self, req: Request) -> Result<Self::Output> {
                let mut resp = self.ep.call(req).await?.into_response();
                resp.headers_mut()
                    .insert(self.header.clone(), self.value.clone());
                Ok(resp)
            }
        }

        let ep = index.with(make(|ep| AddHeader {
            ep,
            header: HeaderName::from_static("hello"),
            value: HeaderValue::from_static("world"),
        }));
        let cli = TestClient::new(ep);

        let resp = cli.get("/").send().await;
        resp.assert_header("hello", "world");
        resp.assert_text("abc").await;
    }

    #[tokio::test]
    async fn test_with_multiple_middlewares() {
        #[handler(internal)]
        fn index(data: Data<&i32>) -> String {
            data.0.to_string()
        }

        let ep = index.with((
            AddData::new(10),
            SetHeader::new().appending("myheader-1", "a"),
            SetHeader::new().appending("myheader-2", "b"),
        ));
        let cli = TestClient::new(ep);

        let resp = cli.get("/").send().await;
        resp.assert_status_is_ok();
        resp.assert_header("myheader-1", "a");
        resp.assert_header("myheader-2", "b");
        resp.assert_text("10").await;
    }
}