@@ -115,6 +115,7 @@ pub struct Builder {
115
115
h1_writev : Option < bool > ,
116
116
h1_title_case_headers : bool ,
117
117
h1_preserve_header_case : bool ,
118
+ h1_max_headers : Option < usize > ,
118
119
#[ cfg( feature = "ffi" ) ]
119
120
h1_preserve_header_order : bool ,
120
121
h1_read_buf_exact_size : Option < usize > ,
@@ -302,6 +303,7 @@ impl Builder {
302
303
h1_parser_config : Default :: default ( ) ,
303
304
h1_title_case_headers : false ,
304
305
h1_preserve_header_case : false ,
306
+ h1_max_headers : None ,
305
307
#[ cfg( feature = "ffi" ) ]
306
308
h1_preserve_header_order : false ,
307
309
h1_max_buf_size : None ,
@@ -434,6 +436,24 @@ impl Builder {
434
436
self
435
437
}
436
438
439
+ /// Set the maximum number of headers.
440
+ ///
441
+ /// When a response is received, the parser will reserve a buffer to store headers for optimal
442
+ /// performance.
443
+ ///
444
+ /// If client receives more headers than the buffer size, the error "message header too large"
445
+ /// is returned.
446
+ ///
447
+ /// Note that headers is allocated on the stack by default, which has higher performance. After
448
+ /// setting this value, headers will be allocated in heap memory, that is, heap memory
449
+ /// allocation will occur for each response, and there will be a performance drop of about 5%.
450
+ ///
451
+ /// Default is 100.
452
+ pub fn max_headers ( & mut self , val : usize ) -> & mut Self {
453
+ self . h1_max_headers = Some ( val) ;
454
+ self
455
+ }
456
+
437
457
/// Set whether to support preserving original header order.
438
458
///
439
459
/// Currently, this will record the order in which headers are received, and store this
@@ -514,6 +534,9 @@ impl Builder {
514
534
if opts. h1_preserve_header_case {
515
535
conn. set_preserve_header_case ( ) ;
516
536
}
537
+ if let Some ( max_headers) = opts. h1_max_headers {
538
+ conn. set_http1_max_headers ( max_headers) ;
539
+ }
517
540
#[ cfg( feature = "ffi" ) ]
518
541
if opts. h1_preserve_header_order {
519
542
conn. set_preserve_header_order ( ) ;
0 commit comments