@@ -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 > ,
@@ -309,6 +310,7 @@ impl Builder {
309310 h1_parser_config : Default :: default ( ) ,
310311 h1_title_case_headers : false ,
311312 h1_preserve_header_case : false ,
313+ h1_max_headers : None ,
312314 #[ cfg( feature = "ffi" ) ]
313315 h1_preserve_header_order : false ,
314316 h1_max_buf_size : None ,
@@ -439,6 +441,24 @@ impl Builder {
439441 self
440442 }
441443
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+
442462 /// Set whether to support preserving original header order.
443463 ///
444464 /// Currently, this will record the order in which headers are received, and store this
@@ -519,6 +539,9 @@ impl Builder {
519539 if opts. h1_preserve_header_case {
520540 conn. set_preserve_header_case ( ) ;
521541 }
542+ if let Some ( max_headers) = opts. h1_max_headers {
543+ conn. set_http1_max_headers ( max_headers) ;
544+ }
522545 #[ cfg( feature = "ffi" ) ]
523546 if opts. h1_preserve_header_order {
524547 conn. set_preserve_header_order ( ) ;
0 commit comments