Skip to main content

vibeio_http/h2/
options.rs

1const DEFAULT_CONN_WINDOW: u32 = 1024 * 1024;
2const DEFAULT_STREAM_WINDOW: u32 = 1024 * 1024;
3const DEFAULT_MAX_FRAME_SIZE: u32 = 1024 * 16;
4const DEFAULT_MAX_SEND_BUF_SIZE: usize = 1024 * 400;
5const DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE: u32 = 1024 * 16;
6const DEFAULT_MAX_CONCURRENT_STREAMS: u32 = 200;
7const DEFAULT_MAX_LOCAL_ERROR_RESET_STREAMS: Option<usize> = Some(1024);
8
9/// Configuration options for the HTTP/2 connection handler.
10///
11/// Use the builder-style methods to customise behaviour, then pass the finished
12/// value to [`Http2::new`](super::Http2::new).
13///
14/// # Examples
15///
16/// ```rust,ignore
17/// let options = Http2Options::default()
18///     .handshake_timeout(Some(std::time::Duration::from_secs(10)))
19///     .accept_timeout(Some(std::time::Duration::from_secs(60)));
20/// ```
21#[derive(Debug, Clone)]
22pub struct Http2Options {
23    pub(super) h2: h2::server::Builder,
24    pub(super) accept_timeout: Option<std::time::Duration>,
25    pub(super) handshake_timeout: Option<std::time::Duration>,
26    pub(super) send_continue_response: bool,
27    pub(crate) send_date_header: bool,
28}
29
30impl Http2Options {
31    /// Creates a new `Http2Options` from an `h2` server builder with the
32    /// following defaults:
33    ///
34    /// | Option | Default |
35    /// |---|---|
36    /// | `accept_timeout` | 30 seconds |
37    /// | `handshake_timeout` | 30 seconds |
38    /// | `send_continue_response` | `true` |
39    /// | `send_date_header` | `true` |
40    ///
41    /// The `h2` builder is used as-is and is not modified by this method.
42    pub fn new(h2: h2::server::Builder) -> Self {
43        Self {
44            h2,
45            accept_timeout: Some(std::time::Duration::from_secs(30)),
46            handshake_timeout: Some(std::time::Duration::from_secs(30)),
47            send_continue_response: true,
48            send_date_header: true,
49        }
50    }
51
52    /// Returns a mutable reference to the underlying `h2::server::Builder`.
53    ///
54    /// Use this to tune HTTP/2 protocol settings such as flow-control windows,
55    /// frame size limits, and header list size.
56    pub fn h2_builder(&mut self) -> &mut h2::server::Builder {
57        &mut self.h2
58    }
59
60    /// Sets the timeout for waiting on the next accepted HTTP/2 stream.
61    ///
62    /// If no new stream arrives before this duration, the connection is
63    /// gracefully shut down and the handler returns a timeout error.
64    /// Pass `None` to disable this timeout. Defaults to **30 seconds**.
65    pub fn accept_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
66        self.accept_timeout = timeout;
67        self
68    }
69
70    /// Sets the timeout for the initial HTTP/2 handshake.
71    ///
72    /// If the handshake does not complete within this duration, the handler
73    /// returns an I/O timeout error. Pass `None` to disable this timeout.
74    /// Defaults to **30 seconds**.
75    pub fn handshake_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
76        self.handshake_timeout = timeout;
77        self
78    }
79
80    /// Controls whether a `100 Continue` interim response is sent when a
81    /// request contains an `Expect: 100-continue` header.
82    ///
83    /// Defaults to **`true`**.
84    pub fn send_continue_response(mut self, send: bool) -> Self {
85        self.send_continue_response = send;
86        self
87    }
88
89    /// Controls whether a `Date` header is automatically added to every
90    /// response.
91    ///
92    /// The value is cached and refreshed at most once per second.
93    /// Defaults to **`true`**.
94    pub fn send_date_header(mut self, send: bool) -> Self {
95        self.send_date_header = send;
96        self
97    }
98}
99
100impl Default for Http2Options {
101    fn default() -> Self {
102        let mut builder = h2::server::Builder::new();
103        builder
104            .initial_window_size(DEFAULT_STREAM_WINDOW)
105            .initial_connection_window_size(DEFAULT_CONN_WINDOW)
106            .max_frame_size(DEFAULT_MAX_FRAME_SIZE)
107            .max_send_buffer_size(DEFAULT_MAX_SEND_BUF_SIZE)
108            .max_header_list_size(DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE)
109            .max_concurrent_streams(DEFAULT_MAX_CONCURRENT_STREAMS)
110            .max_local_error_reset_streams(DEFAULT_MAX_LOCAL_ERROR_RESET_STREAMS);
111        Self::new(builder)
112    }
113}