From b040494f8a257c2a479ef35c070d1821b78d6385 Mon Sep 17 00:00:00 2001 From: Yu Li Date: Mon, 11 Mar 2024 14:54:07 +0800 Subject: [PATCH] feat: adapt configurable `max_headers` Adapts configurable `max_headers` by hyperium/hyper#3523. --- src/client/legacy/client.rs | 22 ++++++++++++++++++++++ src/server/conn/auto.rs | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/client/legacy/client.rs b/src/client/legacy/client.rs index 8022055..e50dca2 100644 --- a/src/client/legacy/client.rs +++ b/src/client/legacy/client.rs @@ -1234,6 +1234,28 @@ impl Builder { self } + /// Set the maximum number of headers. + /// + /// When a response is received, the parser will reserve a buffer to store headers for optimal + /// performance. + /// + /// If client receives more headers than the buffer size, the error "message header too large" + /// is returned. + /// + /// The headers is allocated on the stack by default, which has higher performance. After + /// setting this value, headers will be allocated in heap memory, that is, heap memory + /// allocation will occur for each response, and there will be a performance drop of about 5%. + /// + /// Note that this setting does not affect HTTP/2. + /// + /// Default is 100. + #[cfg(feature = "http1")] + #[cfg_attr(docsrs, doc(cfg(feature = "http1")))] + pub fn http1_max_headers(&mut self, val: usize) -> &mut Self { + self.h1_builder.max_headers(val); + self + } + /// Set whether HTTP/0.9 responses should be tolerated. /// /// Default is false. diff --git a/src/server/conn/auto.rs b/src/server/conn/auto.rs index 89004c4..7e74d16 100644 --- a/src/server/conn/auto.rs +++ b/src/server/conn/auto.rs @@ -560,6 +560,26 @@ impl Http1Builder<'_, E> { self } + /// Set the maximum number of headers. + /// + /// When a request is received, the parser will reserve a buffer to store headers for optimal + /// performance. + /// + /// If server receives more headers than the buffer size, it responds to the client with + /// "431 Request Header Fields Too Large". + /// + /// The headers is allocated on the stack by default, which has higher performance. After + /// setting this value, headers will be allocated in heap memory, that is, heap memory + /// allocation will occur for each request, and there will be a performance drop of about 5%. + /// + /// Note that this setting does not affect HTTP/2. + /// + /// Default is 100. + pub fn max_headers(&mut self, val: usize) -> &mut Self { + self.inner.http1.max_headers(val); + self + } + /// Set a timeout for reading client request headers. If a client does not /// transmit the entire header within this time, the connection is closed. ///