1use std::time::Duration;
6
7const DEFAULT_HTTP2_KEEPALIVE_TIMEOUT_SECS: u64 = 20;
8
9#[derive(Debug, Clone)]
10pub struct Config {
11 init_stream_window_size: Option<u32>,
12 init_connection_window_size: Option<u32>,
13 max_concurrent_streams: Option<u32>,
14 pub(crate) tcp_keepalive: Option<Duration>,
15 pub(crate) tcp_nodelay: bool,
16 http2_keepalive_interval: Option<Duration>,
17 http2_keepalive_timeout: Option<Duration>,
18 http2_adaptive_window: Option<bool>,
19 http2_max_pending_accept_reset_streams: Option<usize>,
20 http2_max_header_list_size: Option<u32>,
21 max_frame_size: Option<u32>,
22 pub(crate) accept_http1: bool,
23 enable_connect_protocol: bool,
24 pub(crate) max_connection_age: Option<Duration>,
25 pub(crate) allow_insecure: bool,
26}
27
28impl Default for Config {
29 fn default() -> Self {
30 Self {
31 init_stream_window_size: None,
32 init_connection_window_size: None,
33 max_concurrent_streams: None,
34 tcp_keepalive: None,
35 tcp_nodelay: true,
36 http2_keepalive_interval: None,
37 http2_keepalive_timeout: None,
38 http2_adaptive_window: None,
39 http2_max_pending_accept_reset_streams: None,
40 http2_max_header_list_size: None,
41 max_frame_size: None,
42 accept_http1: true,
43 enable_connect_protocol: true,
44 max_connection_age: None,
45 allow_insecure: false,
46 }
47 }
48}
49
50impl Config {
51 pub fn initial_stream_window_size(self, sz: impl Into<Option<u32>>) -> Self {
58 Self {
59 init_stream_window_size: sz.into(),
60 ..self
61 }
62 }
63
64 pub fn initial_connection_window_size(self, sz: impl Into<Option<u32>>) -> Self {
68 Self {
69 init_connection_window_size: sz.into(),
70 ..self
71 }
72 }
73
74 pub fn max_concurrent_streams(self, max: impl Into<Option<u32>>) -> Self {
81 Self {
82 max_concurrent_streams: max.into(),
83 ..self
84 }
85 }
86
87 pub fn max_connection_age(self, max_connection_age: Duration) -> Self {
91 Self {
92 max_connection_age: Some(max_connection_age),
93 ..self
94 }
95 }
96
97 pub fn http2_keepalive_interval(self, http2_keepalive_interval: Option<Duration>) -> Self {
106 Self {
107 http2_keepalive_interval,
108 ..self
109 }
110 }
111
112 pub fn http2_keepalive_timeout(self, http2_keepalive_timeout: Option<Duration>) -> Self {
119 Self {
120 http2_keepalive_timeout,
121 ..self
122 }
123 }
124
125 pub fn http2_adaptive_window(self, enabled: Option<bool>) -> Self {
130 Self {
131 http2_adaptive_window: enabled,
132 ..self
133 }
134 }
135
136 pub fn http2_max_pending_accept_reset_streams(self, max: Option<usize>) -> Self {
144 Self {
145 http2_max_pending_accept_reset_streams: max,
146 ..self
147 }
148 }
149
150 pub fn tcp_keepalive(self, tcp_keepalive: Option<Duration>) -> Self {
158 Self {
159 tcp_keepalive,
160 ..self
161 }
162 }
163
164 pub fn tcp_nodelay(self, enabled: bool) -> Self {
167 Self {
168 tcp_nodelay: enabled,
169 ..self
170 }
171 }
172
173 pub fn http2_max_header_list_size(self, max: impl Into<Option<u32>>) -> Self {
178 Self {
179 http2_max_header_list_size: max.into(),
180 ..self
181 }
182 }
183
184 pub fn max_frame_size(self, frame_size: impl Into<Option<u32>>) -> Self {
190 Self {
191 max_frame_size: frame_size.into(),
192 ..self
193 }
194 }
195
196 pub fn accept_http1(self, accept_http1: bool) -> Self {
200 Config {
201 accept_http1,
202 ..self
203 }
204 }
205
206 pub fn allow_insecure(self, allow_insecure: bool) -> Self {
216 Config {
217 allow_insecure,
218 ..self
219 }
220 }
221
222 pub(crate) fn connection_builder(
223 &self,
224 ) -> hyper_util::server::conn::auto::Builder<hyper_util::rt::TokioExecutor> {
225 let mut builder =
226 hyper_util::server::conn::auto::Builder::new(hyper_util::rt::TokioExecutor::new());
227
228 if !self.accept_http1 {
229 builder = builder.http2_only();
230 }
231
232 if self.enable_connect_protocol {
233 builder.http2().enable_connect_protocol();
234 }
235
236 let http2_keepalive_timeout = self
237 .http2_keepalive_timeout
238 .unwrap_or_else(|| Duration::new(DEFAULT_HTTP2_KEEPALIVE_TIMEOUT_SECS, 0));
239
240 builder
241 .http2()
242 .timer(hyper_util::rt::TokioTimer::new())
243 .initial_connection_window_size(self.init_connection_window_size)
244 .initial_stream_window_size(self.init_stream_window_size)
245 .max_concurrent_streams(self.max_concurrent_streams)
246 .keep_alive_interval(self.http2_keepalive_interval)
247 .keep_alive_timeout(http2_keepalive_timeout)
248 .adaptive_window(self.http2_adaptive_window.unwrap_or_default())
249 .max_pending_accept_reset_streams(self.http2_max_pending_accept_reset_streams)
250 .max_frame_size(self.max_frame_size);
251
252 if let Some(max_header_list_size) = self.http2_max_header_list_size {
253 builder.http2().max_header_list_size(max_header_list_size);
254 }
255
256 builder
257 }
258}