@@ -113,6 +113,7 @@ pub struct Http<E = Exec> {
113
113
h1_keep_alive : bool ,
114
114
h1_title_case_headers : bool ,
115
115
h1_preserve_header_case : bool ,
116
+ h1_max_headers : Option < usize > ,
116
117
#[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
117
118
h1_header_read_timeout : Option < Duration > ,
118
119
h1_writev : Option < bool > ,
@@ -260,6 +261,7 @@ impl Http {
260
261
h1_title_case_headers : false ,
261
262
h1_preserve_header_case : false ,
262
263
#[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
264
+ h1_max_headers : None ,
263
265
h1_header_read_timeout : None ,
264
266
h1_writev : None ,
265
267
#[ cfg( feature = "http2" ) ]
@@ -349,6 +351,26 @@ impl<E> Http<E> {
349
351
self
350
352
}
351
353
354
+ /// Set the maximum number of headers.
355
+ ///
356
+ /// When a request is received, the parser will reserve a buffer to store headers for optimal
357
+ /// performance.
358
+ ///
359
+ /// If server receives more headers than the buffer size, it responds to the client with
360
+ /// "431 Request Header Fields Too Large".
361
+ ///
362
+ /// Note that headers is allocated on the stack by default, which has higher performance. After
363
+ /// setting this value, headers will be allocated in heap memory, that is, heap memory
364
+ /// allocation will occur for each request, and there will be a performance drop of about 5%.
365
+ ///
366
+ /// Default is 100.
367
+ #[ cfg( feature = "http1" ) ]
368
+ #[ cfg_attr( docsrs, doc( cfg( feature = "http1" ) ) ) ]
369
+ pub fn http1_max_headers ( & mut self , val : usize ) -> & mut Self {
370
+ self . h1_max_headers = Some ( val) ;
371
+ self
372
+ }
373
+
352
374
/// Set a timeout for reading client request headers. If a client does not
353
375
/// transmit the entire header within this time, the connection is closed.
354
376
///
@@ -623,6 +645,7 @@ impl<E> Http<E> {
623
645
h1_keep_alive : self . h1_keep_alive ,
624
646
h1_title_case_headers : self . h1_title_case_headers ,
625
647
h1_preserve_header_case : self . h1_preserve_header_case ,
648
+ h1_max_headers : self . h1_max_headers ,
626
649
#[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
627
650
h1_header_read_timeout : self . h1_header_read_timeout ,
628
651
h1_writev : self . h1_writev ,
@@ -687,6 +710,9 @@ impl<E> Http<E> {
687
710
if self . h1_preserve_header_case {
688
711
conn. set_preserve_header_case( ) ;
689
712
}
713
+ if let Some ( max_headers) = self . h1_max_headers {
714
+ conn. set_http1_max_headers( max_headers) ;
715
+ }
690
716
#[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
691
717
if let Some ( header_read_timeout) = self . h1_header_read_timeout {
692
718
conn. set_http1_header_read_timeout( header_read_timeout) ;
0 commit comments