This, uh, grew into something far bigger than expected, but it turns out, all of it was needed to eventually support this correctly. - Adds configuration to client and server to set [SETTINGS_MAX_HEADER_LIST_SIZE](http://httpwg.org/specs/rfc7540.html#SETTINGS_MAX_HEADER_LIST_SIZE) - If not set, a "sane default" of 16 MB is used (taken from golang's http2) - Decoding header blocks now happens as they are received, instead of buffering up possibly forever until the last continuation frame is parsed. - As each field is decoded, it's undecoded size is added to the total. Whenever a header block goes over the maximum size, the `frame` will be marked as such. - Whenever a header block is deemed over max limit, decoding will still continue, but new fields will not be appended to `HeaderMap`. This is also can save wasted hashing. - To protect against enormous string literals, such that they span multiple continuation frames, a check is made that the combined encoded bytes is less than the max allowed size. While technically not exactly what the spec suggests (counting decoded size instead), this should hopefully only happen when someone is indeed malicious. If found, a `GOAWAY` of `COMPRESSION_ERROR` is sent, and the connection shut down. - After an oversize header block frame is finished decoding, the streams state machine will notice it is oversize, and handle that. - If the local peer is a server, a 431 response is sent, as suggested by the spec. - A `REFUSED_STREAM` reset is sent, since we cannot actually give the stream to the user. - In order to be able to send both the 431 headers frame, and a reset frame afterwards, the scheduled `Canceled` machinery was made more general to a `Scheduled(Reason)` state instead. Closes #18 Closes #191
418 lines
12 KiB
Rust
418 lines
12 KiB
Rust
use codec::{RecvError, UserError};
|
|
use codec::UserError::*;
|
|
use frame::Reason;
|
|
use proto;
|
|
|
|
use self::Inner::*;
|
|
use self::Peer::*;
|
|
|
|
/// Represents the state of an H2 stream
|
|
///
|
|
/// ```not_rust
|
|
/// +--------+
|
|
/// send PP | | recv PP
|
|
/// ,--------| idle |--------.
|
|
/// / | | \
|
|
/// v +--------+ v
|
|
/// +----------+ | +----------+
|
|
/// | | | send H / | |
|
|
/// ,------| reserved | | recv H | reserved |------.
|
|
/// | | (local) | | | (remote) | |
|
|
/// | +----------+ v +----------+ |
|
|
/// | | +--------+ | |
|
|
/// | | recv ES | | send ES | |
|
|
/// | send H | ,-------| open |-------. | recv H |
|
|
/// | | / | | \ | |
|
|
/// | v v +--------+ v v |
|
|
/// | +----------+ | +----------+ |
|
|
/// | | half | | | half | |
|
|
/// | | closed | | send R / | closed | |
|
|
/// | | (remote) | | recv R | (local) | |
|
|
/// | +----------+ | +----------+ |
|
|
/// | | | | |
|
|
/// | | send ES / | recv ES / | |
|
|
/// | | send R / v send R / | |
|
|
/// | | recv R +--------+ recv R | |
|
|
/// | send R / `----------->| |<-----------' send R / |
|
|
/// | recv R | closed | recv R |
|
|
/// `----------------------->| |<----------------------'
|
|
/// +--------+
|
|
///
|
|
/// send: endpoint sends this frame
|
|
/// recv: endpoint receives this frame
|
|
///
|
|
/// H: HEADERS frame (with implied CONTINUATIONs)
|
|
/// PP: PUSH_PROMISE frame (with implied CONTINUATIONs)
|
|
/// ES: END_STREAM flag
|
|
/// R: RST_STREAM frame
|
|
/// ```
|
|
#[derive(Debug, Clone)]
|
|
pub struct State {
|
|
inner: Inner,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
enum Inner {
|
|
Idle,
|
|
// TODO: these states shouldn't count against concurrency limits:
|
|
//ReservedLocal,
|
|
ReservedRemote,
|
|
Open { local: Peer, remote: Peer },
|
|
HalfClosedLocal(Peer), // TODO: explicitly name this value
|
|
HalfClosedRemote(Peer),
|
|
Closed(Cause),
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
enum Peer {
|
|
AwaitingHeaders,
|
|
Streaming,
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
enum Cause {
|
|
EndStream,
|
|
Proto(Reason),
|
|
LocallyReset(Reason),
|
|
Io,
|
|
|
|
/// This indicates to the connection that a reset frame must be sent out
|
|
/// once the send queue has been flushed.
|
|
///
|
|
/// Examples of when this could happen:
|
|
/// - User drops all references to a stream, so we want to CANCEL the it.
|
|
/// - Header block size was too large, so we want to REFUSE, possibly
|
|
/// after sending a 431 response frame.
|
|
Scheduled(Reason),
|
|
}
|
|
|
|
impl State {
|
|
/// Opens the send-half of a stream if it is not already open.
|
|
pub fn send_open(&mut self, eos: bool) -> Result<(), UserError> {
|
|
let local = Streaming;
|
|
|
|
self.inner = match self.inner {
|
|
Idle => if eos {
|
|
HalfClosedLocal(AwaitingHeaders)
|
|
} else {
|
|
Open {
|
|
local,
|
|
remote: AwaitingHeaders,
|
|
}
|
|
},
|
|
Open {
|
|
local: AwaitingHeaders,
|
|
remote,
|
|
} => if eos {
|
|
HalfClosedLocal(remote)
|
|
} else {
|
|
Open {
|
|
local,
|
|
remote,
|
|
}
|
|
},
|
|
HalfClosedRemote(AwaitingHeaders) => if eos {
|
|
Closed(Cause::EndStream)
|
|
} else {
|
|
HalfClosedRemote(local)
|
|
},
|
|
_ => {
|
|
// All other transitions result in a protocol error
|
|
return Err(UnexpectedFrameType);
|
|
},
|
|
};
|
|
|
|
return Ok(());
|
|
}
|
|
|
|
/// Opens the receive-half of the stream when a HEADERS frame is received.
|
|
///
|
|
/// Returns true if this transitions the state to Open.
|
|
pub fn recv_open(&mut self, eos: bool) -> Result<bool, RecvError> {
|
|
let remote = Streaming;
|
|
let mut initial = false;
|
|
|
|
self.inner = match self.inner {
|
|
Idle => {
|
|
initial = true;
|
|
|
|
if eos {
|
|
HalfClosedRemote(AwaitingHeaders)
|
|
} else {
|
|
Open {
|
|
local: AwaitingHeaders,
|
|
remote,
|
|
}
|
|
}
|
|
},
|
|
ReservedRemote => {
|
|
initial = true;
|
|
|
|
if eos {
|
|
Closed(Cause::EndStream)
|
|
} else {
|
|
Open {
|
|
local: AwaitingHeaders,
|
|
remote,
|
|
}
|
|
}
|
|
},
|
|
Open {
|
|
local,
|
|
remote: AwaitingHeaders,
|
|
} => if eos {
|
|
HalfClosedRemote(local)
|
|
} else {
|
|
Open {
|
|
local,
|
|
remote,
|
|
}
|
|
},
|
|
HalfClosedLocal(AwaitingHeaders) => if eos {
|
|
Closed(Cause::EndStream)
|
|
} else {
|
|
HalfClosedLocal(remote)
|
|
},
|
|
_ => {
|
|
// All other transitions result in a protocol error
|
|
return Err(RecvError::Connection(Reason::PROTOCOL_ERROR));
|
|
},
|
|
};
|
|
|
|
return Ok(initial);
|
|
}
|
|
|
|
/// Transition from Idle -> ReservedRemote
|
|
pub fn reserve_remote(&mut self) -> Result<(), RecvError> {
|
|
match self.inner {
|
|
Idle => {
|
|
self.inner = ReservedRemote;
|
|
Ok(())
|
|
},
|
|
_ => Err(RecvError::Connection(Reason::PROTOCOL_ERROR)),
|
|
}
|
|
}
|
|
|
|
/// Indicates that the remote side will not send more data to the local.
|
|
pub fn recv_close(&mut self) -> Result<(), RecvError> {
|
|
match self.inner {
|
|
Open {
|
|
local, ..
|
|
} => {
|
|
// The remote side will continue to receive data.
|
|
trace!("recv_close: Open => HalfClosedRemote({:?})", local);
|
|
self.inner = HalfClosedRemote(local);
|
|
Ok(())
|
|
},
|
|
HalfClosedLocal(..) => {
|
|
trace!("recv_close: HalfClosedLocal => Closed");
|
|
self.inner = Closed(Cause::EndStream);
|
|
Ok(())
|
|
},
|
|
_ => Err(RecvError::Connection(Reason::PROTOCOL_ERROR)),
|
|
}
|
|
}
|
|
|
|
/// The remote explicitly sent a RST_STREAM.
|
|
pub fn recv_reset(&mut self, reason: Reason) {
|
|
match self.inner {
|
|
Closed(..) => {},
|
|
_ => {
|
|
trace!("recv_reset; reason={:?}", reason);
|
|
self.inner = Closed(Cause::Proto(reason));
|
|
},
|
|
}
|
|
}
|
|
|
|
/// We noticed a protocol error.
|
|
pub fn recv_err(&mut self, err: &proto::Error) {
|
|
use proto::Error::*;
|
|
|
|
match self.inner {
|
|
Closed(..) => {},
|
|
_ => {
|
|
trace!("recv_err; err={:?}", err);
|
|
self.inner = Closed(match *err {
|
|
Proto(reason) => Cause::LocallyReset(reason),
|
|
Io(..) => Cause::Io,
|
|
});
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn recv_eof(&mut self) {
|
|
match self.inner {
|
|
Closed(..) => {},
|
|
s => {
|
|
trace!("recv_eof; state={:?}", s);
|
|
self.inner = Closed(Cause::Io);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Indicates that the local side will not send more data to the local.
|
|
pub fn send_close(&mut self) {
|
|
match self.inner {
|
|
Open {
|
|
remote, ..
|
|
} => {
|
|
// The remote side will continue to receive data.
|
|
trace!("send_close: Open => HalfClosedLocal({:?})", remote);
|
|
self.inner = HalfClosedLocal(remote);
|
|
},
|
|
HalfClosedRemote(..) => {
|
|
trace!("send_close: HalfClosedRemote => Closed");
|
|
self.inner = Closed(Cause::EndStream);
|
|
},
|
|
_ => panic!("transition send_close on unexpected state"),
|
|
}
|
|
}
|
|
|
|
/// Set the stream state to reset locally.
|
|
pub fn set_reset(&mut self, reason: Reason) {
|
|
self.inner = Closed(Cause::LocallyReset(reason));
|
|
}
|
|
|
|
/// Set the stream state to a scheduled reset.
|
|
pub fn set_scheduled_reset(&mut self, reason: Reason) {
|
|
debug_assert!(!self.is_closed());
|
|
self.inner = Closed(Cause::Scheduled(reason));
|
|
}
|
|
|
|
pub fn get_scheduled_reset(&self) -> Option<Reason> {
|
|
match self.inner {
|
|
Closed(Cause::Scheduled(reason)) => Some(reason),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn is_scheduled_reset(&self) -> bool {
|
|
match self.inner {
|
|
Closed(Cause::Scheduled(..)) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn is_local_reset(&self) -> bool {
|
|
match self.inner {
|
|
Closed(Cause::LocallyReset(_)) => true,
|
|
Closed(Cause::Scheduled(..)) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// Returns true if the stream is already reset.
|
|
pub fn is_reset(&self) -> bool {
|
|
match self.inner {
|
|
Closed(Cause::EndStream) => false,
|
|
Closed(_) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// Returns true if a stream is open or half-closed.
|
|
pub fn is_at_least_half_open(&self) -> bool {
|
|
match self.inner {
|
|
Open {
|
|
..
|
|
} => true,
|
|
HalfClosedLocal(..) => true,
|
|
HalfClosedRemote(..) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn is_send_streaming(&self) -> bool {
|
|
match self.inner {
|
|
Open {
|
|
local: Streaming,
|
|
..
|
|
} => true,
|
|
HalfClosedRemote(Streaming) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// Returns true when the stream is in a state to receive headers
|
|
pub fn is_recv_headers(&self) -> bool {
|
|
match self.inner {
|
|
Idle => true,
|
|
Open {
|
|
remote: AwaitingHeaders,
|
|
..
|
|
} => true,
|
|
HalfClosedLocal(AwaitingHeaders) => true,
|
|
ReservedRemote => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn is_recv_streaming(&self) -> bool {
|
|
match self.inner {
|
|
Open {
|
|
remote: Streaming,
|
|
..
|
|
} => true,
|
|
HalfClosedLocal(Streaming) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn is_closed(&self) -> bool {
|
|
match self.inner {
|
|
Closed(_) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn is_recv_closed(&self) -> bool {
|
|
match self.inner {
|
|
Closed(..) | HalfClosedRemote(..) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn is_send_closed(&self) -> bool {
|
|
match self.inner {
|
|
Closed(..) | HalfClosedLocal(..) | ReservedRemote => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn is_idle(&self) -> bool {
|
|
match self.inner {
|
|
Idle => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn ensure_recv_open(&self) -> Result<bool, proto::Error> {
|
|
use std::io;
|
|
|
|
// TODO: Is this correct?
|
|
match self.inner {
|
|
Closed(Cause::Proto(reason)) |
|
|
Closed(Cause::LocallyReset(reason)) |
|
|
Closed(Cause::Scheduled(reason)) => Err(proto::Error::Proto(reason)),
|
|
Closed(Cause::Io) => Err(proto::Error::Io(io::ErrorKind::BrokenPipe.into())),
|
|
Closed(Cause::EndStream) |
|
|
HalfClosedRemote(..) => Ok(false),
|
|
_ => Ok(true),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for State {
|
|
fn default() -> State {
|
|
State {
|
|
inner: Inner::Idle,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for Peer {
|
|
fn default() -> Self {
|
|
AwaitingHeaders
|
|
}
|
|
}
|