Skip to main content

ntex/http/h1/
default.rs

1use crate::http::{ResponseError, error::DispatchError};
2use crate::{Ctx, Service, ServiceFactory, io::Filter};
3
4use super::control::{Control, ControlAck};
5
6#[derive(Debug, Default)]
7/// Control service that acknowledges every HTTP/1 lifecycle event using its
8/// default action.
9pub struct DefaultControlService;
10
11impl<St, F, Err> Service<St, Control<F, Err>> for DefaultControlService
12where
13    F: Filter,
14    Err: ResponseError,
15{
16    type Res = ControlAck<F>;
17    type Error = DispatchError;
18
19    #[inline]
20    async fn call(
21        &self,
22        r: Control<F, Err>,
23        _: Ctx<'_, Self, St>,
24    ) -> Result<Self::Res, Self::Error> {
25        Ok(r.ack())
26    }
27}
28
29impl<St, F, Err> ServiceFactory<St, Control<F, Err>> for DefaultControlService
30where
31    F: Filter,
32    Err: ResponseError,
33{
34    type Res = ControlAck<F>;
35    type Error = DispatchError;
36
37    type Service = DefaultControlService;
38    type InitError = DispatchError;
39
40    async fn create(&self, _: &St) -> Result<Self::Service, Self::InitError> {
41        Ok(DefaultControlService)
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use std::io;
48
49    use super::*;
50    use crate::http::{Request, h1::control::ControlResult};
51    use crate::{Pipeline, io::Base};
52
53    #[crate::rt_test]
54    async fn default_acks() {
55        let svc: Pipeline<Control<Base, io::Error>, _, _> =
56            ServiceFactory::pipeline(&DefaultControlService, ())
57                .await
58                .unwrap();
59
60        let ack = svc
61            .call(Control::<Base, io::Error>::request(Request::new()))
62            .await
63            .unwrap();
64        assert!(matches!(ack.result, ControlResult::Publish(_)));
65
66        let ack = svc
67            .call(Control::<Base, io::Error>::keepalive(false))
68            .await
69            .unwrap();
70        assert!(matches!(ack.result, ControlResult::Stop));
71    }
72}