Skip to content

Commit

Permalink
fix(server): panic on max_buf_size too small
Browse files Browse the repository at this point in the history
  • Loading branch information
estk committed Apr 19, 2018
1 parent 4ea5472 commit aac250f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/proto/h1/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/proto/h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/server/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit aac250f

Please sign in to comment.