From 0350d901b6cd905a441e824efeeb088b6b9cd681 Mon Sep 17 00:00:00 2001 From: Ajit Rajurkar Date: Thu, 25 Sep 2025 16:07:55 -0700 Subject: [PATCH] feat: Allow MAX_URI_LEN configuration via environment variable Add ability to configure maximum URI length through HYPER_MAX_URI_LEN environment variable while maintaining the default value when the variable is not set. - Maintains backward compatibility by keeping same default value - Only uses environment variable when explicitly set - Falls back to default value for invalid configurations --- src/proto/h1/role.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index 1674e26bc6..90424ad20b 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -28,10 +28,23 @@ use crate::proto::h1::{ use crate::proto::RequestHead; use crate::proto::{BodyLength, MessageHead, RequestLine}; +#[cfg(feature = "server")] +use std::sync::OnceLock; pub(crate) const DEFAULT_MAX_HEADERS: usize = 100; const AVERAGE_HEADER_SIZE: usize = 30; // totally scientific #[cfg(feature = "server")] -const MAX_URI_LEN: usize = (u16::MAX - 1) as usize; +const DEFAULT_MAX_URI_LEN: usize = (u16::MAX - 1) as usize; + +#[cfg(feature = "server")] +fn max_uri_len() -> usize { + static MAX_URI_LEN: OnceLock = OnceLock::new(); + *MAX_URI_LEN.get_or_init(|| { + std::env::var("HYPER_MAX_URI_LEN") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(DEFAULT_MAX_URI_LEN) + }) +} macro_rules! header_name { ($bytes:expr) => {{ @@ -169,7 +182,7 @@ impl Http1Transaction for Server { trace!("Request.parse Complete({})", parsed_len); len = parsed_len; let uri = req.path.unwrap(); - if uri.len() > MAX_URI_LEN { + if uri.len() > max_uri_len() { return Err(Parse::UriTooLong); } method = Method::from_bytes(req.method.unwrap().as_bytes())?;