@@ -113,6 +113,7 @@ pub struct Http<E = Exec> {
113113 h1_keep_alive : bool ,
114114 h1_title_case_headers : bool ,
115115 h1_preserve_header_case : bool ,
116+ h1_max_headers : Option < usize > ;
116117 #[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
117118 h1_header_read_timeout: Option <Duration >,
118119 h1_writev: Option <bool>,
@@ -260,6 +261,7 @@ impl Http {
260261 h1_title_case_headers : false ,
261262 h1_preserve_header_case : false ,
262263 #[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
264+ h1_max_headers : None ;
263265 h1_header_read_timeout: None ,
264266 h1_writev: None ,
265267 #[ cfg( feature = "http2" ) ]
@@ -349,6 +351,26 @@ impl<E> Http<E> {
349351 self
350352 }
351353
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+
352374 /// Set a timeout for reading client request headers. If a client does not
353375 /// transmit the entire header within this time, the connection is closed.
354376 ///
@@ -623,6 +645,7 @@ impl<E> Http<E> {
623645 h1_keep_alive : self . h1_keep_alive ,
624646 h1_title_case_headers : self . h1_title_case_headers ,
625647 h1_preserve_header_case : self . h1_preserve_header_case ,
648+ h1_max_headers : self . h1_max_headers ,
626649 #[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
627650 h1_header_read_timeout : self . h1_header_read_timeout ,
628651 h1_writev : self . h1_writev ,
@@ -687,6 +710,9 @@ impl<E> Http<E> {
687710 if self . h1_preserve_header_case {
688711 conn. set_preserve_header_case( ) ;
689712 }
713+ if let Some ( max_headers) = self . h1_max_headers {
714+ conn. set_http1_max_headers( max_headers) ;
715+ }
690716 #[ cfg( all( feature = "http1" , feature = "runtime" ) ) ]
691717 if let Some ( header_read_timeout) = self . h1_header_read_timeout {
692718 conn. set_http1_header_read_timeout( header_read_timeout) ;
0 commit comments