1use core::time::Duration;
4
5pub const DEFAULT_READ_BUF_LIMIT: usize = 1024 * 1024;
11
12pub const DEFAULT_WRITE_BUF_LIMIT: usize = 8192 + 4096 * 100;
16
17pub 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 pub(crate) h2_max_concurrent_streams: u32,
34 pub(crate) h2_initial_window_size: u32,
35 pub(crate) h2_max_frame_size: u32,
36 pub(crate) h2_max_header_list_size: u32,
37}
38
39impl Default for HttpServiceConfig {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45impl HttpServiceConfig {
46 pub const fn new() -> Self {
47 Self {
48 vectored_write: true,
49 keep_alive_timeout: Duration::from_secs(5),
50 request_head_timeout: Duration::from_secs(5),
51 tls_accept_timeout: Duration::from_secs(3),
52 peek_protocol: false,
53 h2_max_concurrent_streams: 256,
54 h2_initial_window_size: 65_535,
55 h2_max_frame_size: 16_384,
56 h2_max_header_list_size: 16 * 1024 * 1024,
57 }
58 }
59}
60
61impl<const HEADER_LIMIT: usize, const READ_BUF_LIMIT: usize, const WRITE_BUF_LIMIT: usize>
62 HttpServiceConfig<HEADER_LIMIT, READ_BUF_LIMIT, WRITE_BUF_LIMIT>
63{
64 pub fn disable_vectored_write(mut self) -> Self {
68 self.vectored_write = false;
69 self
70 }
71
72 pub fn keep_alive_timeout(mut self, dur: Duration) -> Self {
77 self.keep_alive_timeout = dur;
78 self
79 }
80
81 pub fn request_head_timeout(mut self, dur: Duration) -> Self {
86 self.request_head_timeout = dur;
87 self
88 }
89
90 pub fn tls_accept_timeout(mut self, dur: Duration) -> Self {
95 self.tls_accept_timeout = dur;
96 self
97 }
98
99 pub fn max_read_buf_size<const READ_BUF_LIMIT_2: usize>(
104 self,
105 ) -> HttpServiceConfig<HEADER_LIMIT, READ_BUF_LIMIT_2, WRITE_BUF_LIMIT> {
106 self.mutate_const_generic::<HEADER_LIMIT, READ_BUF_LIMIT_2, WRITE_BUF_LIMIT>()
107 }
108
109 pub fn max_write_buf_size<const WRITE_BUF_LIMIT_2: usize>(
114 self,
115 ) -> HttpServiceConfig<HEADER_LIMIT, READ_BUF_LIMIT, WRITE_BUF_LIMIT_2> {
116 self.mutate_const_generic::<HEADER_LIMIT, READ_BUF_LIMIT, WRITE_BUF_LIMIT_2>()
117 }
118
119 pub fn max_request_headers<const HEADER_LIMIT_2: usize>(
124 self,
125 ) -> HttpServiceConfig<HEADER_LIMIT_2, READ_BUF_LIMIT, WRITE_BUF_LIMIT> {
126 self.mutate_const_generic::<HEADER_LIMIT_2, READ_BUF_LIMIT, WRITE_BUF_LIMIT>()
127 }
128
129 pub fn h2_max_concurrent_streams(mut self, val: u32) -> Self {
131 self.h2_max_concurrent_streams = val;
132 self
133 }
134
135 pub fn h2_initial_window_size(mut self, val: u32) -> Self {
139 self.h2_initial_window_size = val;
140 self
141 }
142
143 pub fn h2_max_frame_size(mut self, val: u32) -> Self {
147 self.h2_max_frame_size = val;
148 self
149 }
150
151 pub fn h2_max_header_list_size(mut self, val: u32) -> Self {
153 self.h2_max_header_list_size = val;
154 self
155 }
156
157 pub fn peek_protocol(mut self) -> Self {
163 self.peek_protocol = true;
164 self
165 }
166
167 #[doc(hidden)]
168 pub fn mutate_const_generic<
170 const HEADER_LIMIT2: usize,
171 const READ_BUF_LIMIT2: usize,
172 const WRITE_BUF_LIMIT2: usize,
173 >(
174 self,
175 ) -> HttpServiceConfig<HEADER_LIMIT2, READ_BUF_LIMIT2, WRITE_BUF_LIMIT2> {
176 HttpServiceConfig {
177 vectored_write: self.vectored_write,
178 keep_alive_timeout: self.keep_alive_timeout,
179 request_head_timeout: self.request_head_timeout,
180 tls_accept_timeout: self.tls_accept_timeout,
181 peek_protocol: self.peek_protocol,
182 h2_max_concurrent_streams: self.h2_max_concurrent_streams,
183 h2_initial_window_size: self.h2_initial_window_size,
184 h2_max_frame_size: self.h2_max_frame_size,
185 h2_max_header_list_size: self.h2_max_header_list_size,
186 }
187 }
188}