diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index a66e54f3..27ae2cef 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -13,6 +13,9 @@ use proto::{Http1Transaction, MessageHead}; /// The initial buffer size allocated before trying to read from IO. pub(crate) const INIT_BUFFER_SIZE: usize = 8192; +/// The minimum value that can be set to max buffer size. +pub const MINIMUM_MAX_BUFFER_SIZE: usize = INIT_BUFFER_SIZE; + /// The default maximum read buffer size. If the buffer gets this big and /// a message is still not complete, a `TooLarge` error is triggered. // Note: if this changes, update server::conn::Http::max_buf_size docs. @@ -72,6 +75,10 @@ where } pub fn set_max_buf_size(&mut self, max: usize) { + assert!( + max >= MINIMUM_MAX_BUFFER_SIZE, + "The max_buf_size cannot be smaller than the initial buffer size." + ); self.max_buf_size = max; self.write_buf.max_buf_size = max; } diff --git a/src/proto/h1/mod.rs b/src/proto/h1/mod.rs index 0a9bff15..e2cc9087 100644 --- a/src/proto/h1/mod.rs +++ b/src/proto/h1/mod.rs @@ -3,6 +3,7 @@ pub(crate) use self::dispatch::Dispatcher; pub use self::decode::Decoder; pub use self::encode::{EncodedBuf, Encoder}; pub use self::io::Cursor; //TODO: move out of h1::io +pub use self::io::MINIMUM_MAX_BUFFER_SIZE; mod conn; mod date; diff --git a/src/server/conn.rs b/src/server/conn.rs index 40532adc..27eb57b7 100644 --- a/src/server/conn.rs +++ b/src/server/conn.rs @@ -148,7 +148,15 @@ impl Http { /// Set the maximum buffer size for the connection. /// /// Default is ~400kb. + /// + /// # Panics + /// + /// The minimum value allowed is 8192. This method panics if the passed `max` is less than the minimum. pub fn max_buf_size(&mut self, max: usize) -> &mut Self { + assert!( + max >= proto::h1::MINIMUM_MAX_BUFFER_SIZE, + "the max_buf_size cannot be smaller than the minimum that h1 specifies." + ); self.max_buf_size = Some(max); self } diff --git a/tests/server.rs b/tests/server.rs index b8020f08..7454b425 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1055,6 +1055,18 @@ fn illegal_request_length_returns_400_response() { fut.wait().unwrap_err(); } +#[test] +#[should_panic] +fn max_buf_size_panic_too_small() { + const MAX: usize = 8191; + Http::new().max_buf_size(MAX); +} +#[test] +fn max_buf_size_no_panic() { + const MAX: usize = 8193; + Http::new().max_buf_size(MAX); +} + #[test] fn max_buf_size() { let _ = pretty_env_logger::try_init();