Merge pull request #1489 from estk/minimum_max_buf_size

Enforce minimum max buf size.
This commit is contained in:
Sean McArthur
2018-04-20 14:18:00 -07:00
committed by GitHub
4 changed files with 28 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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
}

View File

@@ -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();