Skip to main content

vibeio_http/h1/
options.rs

1/// Configuration options for the HTTP/1.x connection handler.
2///
3/// Use the builder-style methods to customise behaviour, then pass the finished
4/// value to [`Http1::new`](super::Http1::new).
5///
6/// # Examples
7///
8/// ```rust,ignore
9/// let options = Http1Options::new()
10///     .max_header_size(8192)
11///     .send_date_header(false)
12///     .header_read_timeout(Some(std::time::Duration::from_secs(10)));
13/// ```
14#[derive(Debug, Clone)]
15pub struct Http1Options {
16    pub(crate) max_header_size: usize,
17    pub(crate) max_header_count: usize,
18    pub(crate) send_date_header: bool,
19    pub(crate) header_read_timeout: Option<std::time::Duration>,
20    pub(crate) send_continue_response: bool,
21    pub(crate) enable_early_hints: bool,
22    pub(crate) enable_vectored_write: bool,
23}
24
25impl Http1Options {
26    /// Creates a new `Http1Options` with the following defaults:
27    ///
28    /// | Option | Default |
29    /// |---|---|
30    /// | `max_header_size` | 16 384 bytes |
31    /// | `max_header_count` | 128 |
32    /// | `send_date_header` | `true` |
33    /// | `header_read_timeout` | 30 seconds |
34    /// | `send_continue_response` | `true` |
35    /// | `enable_early_hints` | `false` |
36    /// | `enable_vectored_write` | `true` |
37    pub fn new() -> Self {
38        Self {
39            max_header_size: 16384,
40            max_header_count: 128,
41            send_date_header: true,
42            header_read_timeout: Some(std::time::Duration::from_secs(30)),
43            send_continue_response: true,
44            enable_early_hints: false,
45            enable_vectored_write: true,
46        }
47    }
48
49    /// Sets the maximum number of bytes that may be read while parsing the
50    /// request head (status line + headers).
51    ///
52    /// Requests whose head exceeds this limit are rejected with an
53    /// `InvalidData` I/O error. Defaults to **16 384** bytes.
54    pub fn max_header_size(mut self, size: usize) -> Self {
55        self.max_header_size = size;
56        self
57    }
58
59    /// Sets the maximum number of headers that will be parsed per request.
60    ///
61    /// Headers beyond this count are silently ignored by the underlying
62    /// `httparse` parser. Defaults to **128**.
63    pub fn max_header_count(mut self, count: usize) -> Self {
64        self.max_header_count = count;
65        self
66    }
67
68    /// Controls whether a `Date` header is automatically added to every
69    /// response.
70    ///
71    /// The value is cached and refreshed at most once per second.
72    /// Defaults to **`true`**.
73    pub fn send_date_header(mut self, send: bool) -> Self {
74        self.send_date_header = send;
75        self
76    }
77
78    /// Controls whether vectored I/O (`write_vectored`) is used when writing
79    /// responses.
80    ///
81    /// When the underlying stream does not support vectored writes the
82    /// implementation falls back to a flattened `write` regardless of this
83    /// setting. Defaults to **`true`**.
84    pub fn enable_vectored_write(mut self, enable: bool) -> Self {
85        self.enable_vectored_write = enable;
86        self
87    }
88
89    /// Sets the timeout for reading the complete request head.
90    ///
91    /// If the client does not send a full set of request headers within this
92    /// duration the connection is closed with a `408 Request Timeout` response.
93    /// Pass `None` to disable the timeout entirely. Defaults to **30 seconds**.
94    pub fn header_read_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
95        self.header_read_timeout = timeout;
96        self
97    }
98
99    /// Controls whether a `100 Continue` interim response is sent when the
100    /// request contains an `Expect: 100-continue` header.
101    ///
102    /// Defaults to **`true`**.
103    pub fn send_continue_response(mut self, send: bool) -> Self {
104        self.send_continue_response = send;
105        self
106    }
107
108    /// Controls whether `103 Early Hints` responses can be sent before the
109    /// final response.
110    ///
111    /// When enabled, an `EarlyHints` handle is inserted into each request's
112    /// extensions so that the handler can push early hint headers to the client.
113    /// Defaults to **`false`**.
114    pub fn enable_early_hints(mut self, enable: bool) -> Self {
115        self.enable_early_hints = enable;
116        self
117    }
118}
119
120impl Default for Http1Options {
121    fn default() -> Self {
122        Self::new()
123    }
124}