xitca_http/
config.rs

1//! Configuration for http service middlewares.
2
3use core::time::Duration;
4
5/// The default maximum read buffer size. If the head gets this big and
6/// a message is still not complete, a `TooLarge` error is triggered.
7///
8/// When handing request body ff the buffer gets this big a force yield
9/// from Io stream read would happen.
10pub const DEFAULT_READ_BUF_LIMIT: usize = 1024 * 1024;
11
12/// The default maximum write buffer size. If the buffer gets this big and
13/// a message is still not complete, a force draining of Io stream write
14/// would happen.
15pub const DEFAULT_WRITE_BUF_LIMIT: usize = 8192 + 4096 * 100;
16
17/// The default maximum request header fields possible for one request.
18///
19/// 64 chosen for no particular reason.
20pub const DEFAULT_HEADER_LIMIT: usize = 64;
21
22#[derive(Copy, Clone)]
23pub struct HttpServiceConfig<
24    const HEADER_LIMIT: usize = DEFAULT_HEADER_LIMIT,
25    const READ_BUF_LIMIT: usize = DEFAULT_READ_BUF_LIMIT,
26    const WRITE_BUF_LIMIT: usize = DEFAULT_WRITE_BUF_LIMIT,
27> {
28    pub(crate) vectored_write: bool,
29    pub(crate) keep_alive_timeout: Duration,
30    pub(crate) request_head_timeout: Duration,
31    pub(crate) tls_accept_timeout: Duration,
32    pub(crate) peek_protocol: bool,
33}
34
35impl Default for HttpServiceConfig {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl HttpServiceConfig {
42    pub const fn new() -> Self {
43        Self {
44            vectored_write: true,
45            keep_alive_timeout: Duration::from_secs(5),
46            request_head_timeout: Duration::from_secs(5),
47            tls_accept_timeout: Duration::from_secs(3),
48            peek_protocol: false,
49        }
50    }
51}
52
53impl<const HEADER_LIMIT: usize, const READ_BUF_LIMIT: usize, const WRITE_BUF_LIMIT: usize>
54    HttpServiceConfig<HEADER_LIMIT, READ_BUF_LIMIT, WRITE_BUF_LIMIT>
55{
56    /// Disable vectored write even when IO is able to perform it.
57    ///
58    /// This is beneficial when dealing with small size of response body.
59    pub fn disable_vectored_write(mut self) -> Self {
60        self.vectored_write = false;
61        self
62    }
63
64    /// Define duration of how long an idle connection is kept alive.
65    ///
66    /// connection have not done any IO after duration would be closed. IO operation
67    /// can possibly result in reset of the duration.
68    pub fn keep_alive_timeout(mut self, dur: Duration) -> Self {
69        self.keep_alive_timeout = dur;
70        self
71    }
72
73    /// Define duration of how long a connection must finish it's request head transferring.
74    /// starting from first byte(s) of current request(s) received from peer.
75    ///
76    /// connection can not make a single request after duration would be closed.
77    pub fn request_head_timeout(mut self, dur: Duration) -> Self {
78        self.request_head_timeout = dur;
79        self
80    }
81
82    /// Define duration of how long a connection must finish it's tls handshake.
83    /// (If tls is enabled)
84    ///
85    /// Connection can not finish handshake after duration would be closed.
86    pub fn tls_accept_timeout(mut self, dur: Duration) -> Self {
87        self.tls_accept_timeout = dur;
88        self
89    }
90
91    /// Define max read buffer size for a connection.
92    ///
93    /// See [DEFAULT_READ_BUF_LIMIT] for default value
94    /// and behavior.
95    pub fn max_read_buf_size<const READ_BUF_LIMIT_2: usize>(
96        self,
97    ) -> HttpServiceConfig<HEADER_LIMIT, READ_BUF_LIMIT_2, WRITE_BUF_LIMIT> {
98        self.mutate_const_generic::<HEADER_LIMIT, READ_BUF_LIMIT_2, WRITE_BUF_LIMIT>()
99    }
100
101    /// Define max write buffer size for a connection.
102    ///
103    /// See [DEFAULT_WRITE_BUF_LIMIT] for default value
104    /// and behavior.
105    pub fn max_write_buf_size<const WRITE_BUF_LIMIT_2: usize>(
106        self,
107    ) -> HttpServiceConfig<HEADER_LIMIT, READ_BUF_LIMIT, WRITE_BUF_LIMIT_2> {
108        self.mutate_const_generic::<HEADER_LIMIT, READ_BUF_LIMIT, WRITE_BUF_LIMIT_2>()
109    }
110
111    /// Define max request header count for a connection.    
112    ///
113    /// See [DEFAULT_HEADER_LIMIT] for default value
114    /// and behavior.
115    pub fn max_request_headers<const HEADER_LIMIT_2: usize>(
116        self,
117    ) -> HttpServiceConfig<HEADER_LIMIT_2, READ_BUF_LIMIT, WRITE_BUF_LIMIT> {
118        self.mutate_const_generic::<HEADER_LIMIT_2, READ_BUF_LIMIT, WRITE_BUF_LIMIT>()
119    }
120
121    /// Enable peek into connection to figure out it's protocol regardless the outcome
122    /// of alpn negotiation.
123    ///
124    /// This API is used to bypass alpn setting from tls and enable Http/2 protocol over
125    /// plain Tcp connection.
126    pub fn peek_protocol(mut self) -> Self {
127        self.peek_protocol = true;
128        self
129    }
130
131    #[doc(hidden)]
132    /// A shortcut for mutating const generic params.
133    pub fn mutate_const_generic<
134        const HEADER_LIMIT2: usize,
135        const READ_BUF_LIMIT2: usize,
136        const WRITE_BUF_LIMIT2: usize,
137    >(
138        self,
139    ) -> HttpServiceConfig<HEADER_LIMIT2, READ_BUF_LIMIT2, WRITE_BUF_LIMIT2> {
140        HttpServiceConfig {
141            vectored_write: self.vectored_write,
142            keep_alive_timeout: self.keep_alive_timeout,
143            request_head_timeout: self.request_head_timeout,
144            tls_accept_timeout: self.tls_accept_timeout,
145            peek_protocol: self.peek_protocol,
146        }
147    }
148}