@@ -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 > ,
@@ -309,6 +310,7 @@ impl Builder {
309
310
h1_parser_config : Default :: default ( ) ,
310
311
h1_title_case_headers : false ,
311
312
h1_preserve_header_case : false ,
313
+ h1_max_headers : None ,
312
314
#[ cfg( feature = "ffi" ) ]
313
315
h1_preserve_header_order : false ,
314
316
h1_max_buf_size : None ,
@@ -439,6 +441,24 @@ impl Builder {
439
441
self
440
442
}
441
443
444
+ /// Set the maximum number of headers.
445
+ ///
446
+ /// When a response is received, the parser will reserve a buffer to store headers for optimal
447
+ /// performance.
448
+ ///
449
+ /// If client receives more headers than the buffer size, the error "message header too large"
450
+ /// is returned.
451
+ ///
452
+ /// Note that headers is allocated on the stack by default, which has higher performance. After
453
+ /// setting this value, headers will be allocated in heap memory, that is, heap memory
454
+ /// allocation will occur for each response, and there will be a performance drop of about 5%.
455
+ ///
456
+ /// Default is 100.
457
+ pub fn max_headers ( & mut self , val : usize ) -> & mut Self {
458
+ self . h1_max_headers = Some ( val) ;
459
+ self
460
+ }
461
+
442
462
/// Set whether to support preserving original header order.
443
463
///
444
464
/// Currently, this will record the order in which headers are received, and store this
@@ -519,6 +539,9 @@ impl Builder {
519
539
if opts. h1_preserve_header_case {
520
540
conn. set_preserve_header_case ( ) ;
521
541
}
542
+ if let Some ( max_headers) = opts. h1_max_headers {
543
+ conn. set_http1_max_headers ( max_headers) ;
544
+ }
522
545
#[ cfg( feature = "ffi" ) ]
523
546
if opts. h1_preserve_header_order {
524
547
conn. set_preserve_header_order ( ) ;
0 commit comments