Skip to content

Commit 99b77a5

Browse files
authored
feat: adapt configurable max_headers (#107)
1 parent a24c162 commit 99b77a5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/client/legacy/client.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,28 @@ impl Builder {
12341234
self
12351235
}
12361236

1237+
/// Set the maximum number of headers.
1238+
///
1239+
/// When a response is received, the parser will reserve a buffer to store headers for optimal
1240+
/// performance.
1241+
///
1242+
/// If client receives more headers than the buffer size, the error "message header too large"
1243+
/// is returned.
1244+
///
1245+
/// The headers is allocated on the stack by default, which has higher performance. After
1246+
/// setting this value, headers will be allocated in heap memory, that is, heap memory
1247+
/// allocation will occur for each response, and there will be a performance drop of about 5%.
1248+
///
1249+
/// Note that this setting does not affect HTTP/2.
1250+
///
1251+
/// Default is 100.
1252+
#[cfg(feature = "http1")]
1253+
#[cfg_attr(docsrs, doc(cfg(feature = "http1")))]
1254+
pub fn http1_max_headers(&mut self, val: usize) -> &mut Self {
1255+
self.h1_builder.max_headers(val);
1256+
self
1257+
}
1258+
12371259
/// Set whether HTTP/0.9 responses should be tolerated.
12381260
///
12391261
/// Default is false.

src/server/conn/auto.rs

+20
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,26 @@ impl<E> Http1Builder<'_, E> {
560560
self
561561
}
562562

563+
/// Set the maximum number of headers.
564+
///
565+
/// When a request is received, the parser will reserve a buffer to store headers for optimal
566+
/// performance.
567+
///
568+
/// If server receives more headers than the buffer size, it responds to the client with
569+
/// "431 Request Header Fields Too Large".
570+
///
571+
/// The headers is allocated on the stack by default, which has higher performance. After
572+
/// setting this value, headers will be allocated in heap memory, that is, heap memory
573+
/// allocation will occur for each request, and there will be a performance drop of about 5%.
574+
///
575+
/// Note that this setting does not affect HTTP/2.
576+
///
577+
/// Default is 100.
578+
pub fn max_headers(&mut self, val: usize) -> &mut Self {
579+
self.inner.http1.max_headers(val);
580+
self
581+
}
582+
563583
/// Set a timeout for reading client request headers. If a client does not
564584
/// transmit the entire header within this time, the connection is closed.
565585
///

0 commit comments

Comments
 (0)