@@ -115,6 +115,7 @@ pub struct Builder {
115115 h1_writev : Option < bool > ,
116116 h1_title_case_headers : bool ,
117117 h1_preserve_header_case : bool ,
118+ h1_max_headers : Option < usize > ,
118119 #[ cfg( feature = "ffi" ) ]
119120 h1_preserve_header_order : bool ,
120121 h1_read_buf_exact_size : Option < usize > ,
@@ -302,6 +303,7 @@ impl Builder {
302303 h1_parser_config : Default :: default ( ) ,
303304 h1_title_case_headers : false ,
304305 h1_preserve_header_case : false ,
306+ h1_max_headers : None ,
305307 #[ cfg( feature = "ffi" ) ]
306308 h1_preserve_header_order : false ,
307309 h1_max_buf_size : None ,
@@ -434,6 +436,24 @@ impl Builder {
434436 self
435437 }
436438
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+
437457 /// Set whether to support preserving original header order.
438458 ///
439459 /// Currently, this will record the order in which headers are received, and store this
@@ -514,6 +534,9 @@ impl Builder {
514534 if opts. h1_preserve_header_case {
515535 conn. set_preserve_header_case ( ) ;
516536 }
537+ if let Some ( max_headers) = opts. h1_max_headers {
538+ conn. set_http1_max_headers ( max_headers) ;
539+ }
517540 #[ cfg( feature = "ffi" ) ]
518541 if opts. h1_preserve_header_order {
519542 conn. set_preserve_header_order ( ) ;
0 commit comments