add support for configuring max frame size

- Adds `max_frame_size` to client and server builders
- Pushes max_frame_size into Codec
- Detects when the Codec triggers an error from a frame too big
- Sends a GOAWAY when FRAME_SIZE_ERROR is encountered reading a frame
This commit is contained in:
Sean McArthur
2017-09-13 14:10:27 -07:00
parent 8984a46a92
commit c32015d48e
13 changed files with 221 additions and 18 deletions

View File

@@ -110,22 +110,24 @@ fn read_headers_empty_payload() {}
#[test]
fn update_max_frame_len_at_rest() {
let _ = ::env_logger::init();
// TODO: add test for updating max frame length in flight as well?
let mut codec = raw_codec! {
read => [
0, 0, 5, 0, 0, 0, 0, 0, 1,
"hello",
"world",
0, 64, 1, 0, 0, 0, 0, 0, 1,
vec![0; 16_385],
];
};
assert_eq!(poll_data!(codec).payload(), &b"hello"[..]);
codec.set_max_recv_frame_size(2);
codec.set_max_recv_frame_size(16_384);
assert_eq!(codec.max_recv_frame_size(), 2);
assert_eq!(codec.max_recv_frame_size(), 16_384);
assert_eq!(
codec.poll().unwrap_err().description(),
"frame size too big"
"frame with invalid size"
);
}

View File

@@ -195,6 +195,110 @@ fn closed_streams_are_released() {
let _ = h2.join(srv).wait().unwrap();
}
#[test]
fn errors_if_recv_frame_exceeds_max_frame_size() {
let _ = ::env_logger::init();
let (io, mut srv) = mock::new();
let h2 = Client::handshake(io).unwrap().and_then(|mut h2| {
let request = Request::builder()
.method(Method::GET)
.uri("https://example.com/")
.body(())
.unwrap();
let req = h2.request(request, true)
.unwrap()
.unwrap()
.and_then(|resp| {
assert_eq!(resp.status(), StatusCode::OK);
let body = resp.into_parts().1;
body.concat2().then(|res| {
let err = res.unwrap_err();
assert_eq!(err.to_string(), "protocol error: frame with invalid size");
Ok::<(), ()>(())
})
});
// client should see a conn error
let conn = h2.then(|res| {
let err = res.unwrap_err();
assert_eq!(err.to_string(), "protocol error: frame with invalid size");
Ok::<(), ()>(())
});
conn.unwrap().join(req)
});
// a bad peer
srv.codec_mut().set_max_send_frame_size(16_384 * 4);
let srv = srv.assert_client_handshake()
.unwrap()
.ignore_settings()
.recv_frame(
frames::headers(1)
.request("GET", "https://example.com/")
.eos(),
)
.send_frame(frames::headers(1).response(200))
.send_frame(frames::data(1, vec![0; 16_385]).eos())
.recv_frame(frames::go_away(0).frame_size())
.close();
let _ = h2.join(srv).wait().unwrap();
}
#[test]
fn configure_max_frame_size() {
let _ = ::env_logger::init();
let (io, mut srv) = mock::new();
let h2 = Client::builder()
.max_frame_size(16_384 * 2)
.handshake::<_, Bytes>(io)
.expect("handshake")
.and_then(|mut h2| {
let request = Request::builder()
.method(Method::GET)
.uri("https://example.com/")
.body(())
.unwrap();
let req = h2.request(request, true)
.unwrap()
.expect("response")
.and_then(|resp| {
assert_eq!(resp.status(), StatusCode::OK);
let body = resp.into_parts().1;
body.concat2().expect("body")
})
.and_then(|buf| {
assert_eq!(buf.len(), 16_385);
Ok(())
});
h2.expect("client").join(req)
});
// a good peer
srv.codec_mut().set_max_send_frame_size(16_384 * 2);
let srv = srv.assert_client_handshake()
.unwrap()
.ignore_settings()
.recv_frame(
frames::headers(1)
.request("GET", "https://example.com/")
.eos(),
)
.send_frame(frames::headers(1).response(200))
.send_frame(frames::data(1, vec![0; 16_385]).eos())
.close();
let _ = h2.join(srv).wait().expect("wait");
}
/*
#[test]
fn send_data_after_headers_eos() {

View File

@@ -146,6 +146,10 @@ impl Mock<frame::GoAway> {
pub fn flow_control(self) -> Self {
Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::FlowControlError))
}
pub fn frame_size(self) -> Self {
Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::FrameSizeError))
}
}
impl From<Mock<frame::GoAway>> for SendFrame {

View File

@@ -12,6 +12,18 @@ pub trait FutureExt: Future {
Unwrap { inner: self }
}
/// Panic on error, with a message.
fn expect<T>(self, msg: T) -> Expect<Self>
where Self: Sized,
Self::Error: fmt::Debug,
T: fmt::Display,
{
Expect {
inner: self,
msg: msg.to_string(),
}
}
/// Drive `other` by polling `self`.
///
/// `self` must not resolve before `other` does.
@@ -51,6 +63,28 @@ impl<T> Future for Unwrap<T>
}
}
// ===== Expect ======
/// Panic on error
pub struct Expect<T> {
inner: T,
msg: String,
}
impl<T> Future for Expect<T>
where T: Future,
T::Item: fmt::Debug,
T::Error: fmt::Debug,
{
type Item = T::Item;
type Error = ();
fn poll(&mut self) -> Poll<T::Item, ()> {
Ok(self.inner.poll().expect(&self.msg))
}
}
// ===== Drive ======
/// Drive a future to completion while also polling the driver

View File

@@ -26,7 +26,7 @@ pub struct Handle {
}
#[derive(Debug)]
struct Pipe {
pub struct Pipe {
inner: Arc<Mutex<Inner>>,
}
@@ -67,6 +67,11 @@ pub fn new() -> (Mock, Handle) {
// ===== impl Handle =====
impl Handle {
/// Get a mutable reference to inner Codec.
pub fn codec_mut(&mut self) -> &mut ::Codec<Pipe> {
&mut self.codec
}
/// Send a frame
pub fn send(&mut self, item: SendFrame) -> Result<(), SendError> {
// Queue the frame
@@ -237,7 +242,7 @@ impl io::Write for Mock {
let mut me = self.pipe.inner.lock().unwrap();
if me.closed {
return Err(io::ErrorKind::BrokenPipe.into());
return Err(io::Error::new(io::ErrorKind::BrokenPipe, "mock closed"));
}
me.tx.extend(buf);

View File

@@ -44,3 +44,9 @@ impl<'a> Chunk for &'a str {
dst.extend(self.as_bytes())
}
}
impl Chunk for Vec<u8> {
fn push(&self, dst: &mut Vec<u8>) {
dst.extend(self.iter())
}
}