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