Handle the remote returning a protocol error
This commit is contained in:
@@ -92,6 +92,16 @@ impl<T, P, B> Connection<T, P, B>
|
|||||||
|
|
||||||
/// Advances the internal state of the connection.
|
/// Advances the internal state of the connection.
|
||||||
pub fn poll(&mut self) -> Poll<(), ConnectionError> {
|
pub fn poll(&mut self) -> Poll<(), ConnectionError> {
|
||||||
|
match self.poll2() {
|
||||||
|
Err(e) => {
|
||||||
|
self.streams.recv_err(&e);
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
ret => ret,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll2(&mut self) -> Poll<(), ConnectionError> {
|
||||||
use frame::Frame::*;
|
use frame::Frame::*;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -178,9 +188,6 @@ impl<T, P, B> Connection<T, P, B>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Flush the write buffer
|
|
||||||
unimplemented!();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -143,6 +143,14 @@ impl<P, B> Recv<P, B>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn recv_err(&mut self, err: &ConnectionError, stream: &mut Stream<B>) {
|
||||||
|
// Receive an error
|
||||||
|
stream.state.recv_err(err);
|
||||||
|
|
||||||
|
// If a receiver is waiting, notify it
|
||||||
|
stream.notify_recv();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn dec_num_streams(&mut self) {
|
pub fn dec_num_streams(&mut self) {
|
||||||
self.num_streams -= 1;
|
self.num_streams -= 1;
|
||||||
}
|
}
|
||||||
@@ -308,6 +316,8 @@ impl<B> Recv<client::Peer, B>
|
|||||||
}
|
}
|
||||||
Some(frame) => unimplemented!(),
|
Some(frame) => unimplemented!(),
|
||||||
None => {
|
None => {
|
||||||
|
stream.state.ensure_recv_open()?;
|
||||||
|
|
||||||
stream.recv_task = Some(task::current());
|
stream.recv_task = Some(task::current());
|
||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ enum Inner {
|
|||||||
HalfClosedLocal(Peer), // TODO: explicitly name this value
|
HalfClosedLocal(Peer), // TODO: explicitly name this value
|
||||||
HalfClosedRemote(Peer),
|
HalfClosedRemote(Peer),
|
||||||
// When reset, a reason is provided
|
// When reset, a reason is provided
|
||||||
Closed(Option<Reason>),
|
Closed(Option<Cause>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
@@ -76,6 +76,12 @@ enum Peer {
|
|||||||
Streaming(FlowControl),
|
Streaming(FlowControl),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
enum Cause {
|
||||||
|
Proto(Reason),
|
||||||
|
Io,
|
||||||
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
/// Opens the send-half of a stream if it is not already open.
|
/// Opens the send-half of a stream if it is not already open.
|
||||||
pub fn send_open(&mut self, sz: WindowSize, eos: bool) -> Result<(), ConnectionError> {
|
pub fn send_open(&mut self, sz: WindowSize, eos: bool) -> Result<(), ConnectionError> {
|
||||||
@@ -178,6 +184,19 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn recv_err(&mut self, err: &ConnectionError) {
|
||||||
|
match self.inner {
|
||||||
|
Closed(..) => {}
|
||||||
|
_ => {
|
||||||
|
self.inner = Closed(match *err {
|
||||||
|
ConnectionError::Proto(reason) => Some(Cause::Proto(reason)),
|
||||||
|
ConnectionError::Io(..) => Some(Cause::Io),
|
||||||
|
_ => panic!("cannot terminate stream with user error"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Indicates that the local side will not send more data to the local.
|
/// Indicates that the local side will not send more data to the local.
|
||||||
pub fn send_close(&mut self) -> Result<(), ConnectionError> {
|
pub fn send_close(&mut self) -> Result<(), ConnectionError> {
|
||||||
match self.inner {
|
match self.inner {
|
||||||
@@ -225,6 +244,21 @@ impl State {
|
|||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ensure_recv_open(&self) -> Result<(), ConnectionError> {
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
// TODO: Is this correct?
|
||||||
|
match self.inner {
|
||||||
|
Closed(Some(Cause::Proto(reason))) => {
|
||||||
|
Err(ConnectionError::Proto(reason))
|
||||||
|
}
|
||||||
|
Closed(Some(Cause::Io)) => {
|
||||||
|
Err(ConnectionError::Io(io::ErrorKind::BrokenPipe.into()))
|
||||||
|
}
|
||||||
|
_ => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for State {
|
impl Default for State {
|
||||||
|
|||||||
@@ -90,6 +90,14 @@ impl<B> Store<B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn for_each<F>(&mut self, mut f: F)
|
||||||
|
where F: FnMut(&mut Stream<B>)
|
||||||
|
{
|
||||||
|
for &id in self.ids.values() {
|
||||||
|
f(&mut self.slab[id])
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== impl Ptr =====
|
// ===== impl Ptr =====
|
||||||
@@ -142,7 +150,7 @@ impl<'a, B> OccupiedEntry<'a, B> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ===== impl VacantEntry =====
|
// ===== impl VacantEntry =====
|
||||||
//
|
|
||||||
impl<'a, B> VacantEntry<'a, B> {
|
impl<'a, B> VacantEntry<'a, B> {
|
||||||
pub fn insert(self, value: Stream<B>) -> Key {
|
pub fn insert(self, value: Stream<B>) -> Key {
|
||||||
// Insert the value in the slab
|
// Insert the value in the slab
|
||||||
|
|||||||
@@ -141,6 +141,14 @@ impl<P, B> Streams<P, B>
|
|||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn recv_err(&mut self, err: &ConnectionError) {
|
||||||
|
let mut me = self.inner.lock().unwrap();
|
||||||
|
let me = &mut *me;
|
||||||
|
|
||||||
|
let actions = &mut me.actions;
|
||||||
|
me.store.for_each(|stream| actions.recv.recv_err(err, stream));
|
||||||
|
}
|
||||||
|
|
||||||
pub fn recv_window_update(&mut self, frame: frame::WindowUpdate)
|
pub fn recv_window_update(&mut self, frame: frame::WindowUpdate)
|
||||||
-> Result<(), ConnectionError> {
|
-> Result<(), ConnectionError> {
|
||||||
let id = frame.stream_id();
|
let id = frame.stream_id();
|
||||||
|
|||||||
@@ -14,78 +14,13 @@ fn handshake() {
|
|||||||
.write(SETTINGS_ACK)
|
.write(SETTINGS_ACK)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let h2 = client::handshake(mock)
|
let h2 = Client::handshake(mock)
|
||||||
.wait().unwrap();
|
.wait().unwrap();
|
||||||
|
|
||||||
trace!("hands have been shook");
|
trace!("hands have been shook");
|
||||||
|
|
||||||
// At this point, the connection should be closed
|
// At this point, the connection should be closed
|
||||||
assert!(Stream::wait(h2).next().is_none());
|
h2.wait().unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn send_request_with_zero_stream_id() {
|
|
||||||
let mock = mock_io::Builder::new()
|
|
||||||
.handshake()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let h2 = client::handshake(mock)
|
|
||||||
.wait().unwrap();
|
|
||||||
|
|
||||||
// Send the request
|
|
||||||
let mut request = request::Head::default();
|
|
||||||
request.uri = "https://http2.akamai.com/".parse().unwrap();
|
|
||||||
|
|
||||||
let err = h2.send_request(0.into(), request, true).wait().unwrap_err();
|
|
||||||
assert_user_err!(err, InvalidStreamId);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn send_request_with_server_stream_id() {
|
|
||||||
let mock = mock_io::Builder::new()
|
|
||||||
.handshake()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let h2 = client::handshake(mock)
|
|
||||||
.wait().unwrap();
|
|
||||||
|
|
||||||
// Send the request
|
|
||||||
let mut request = request::Head::default();
|
|
||||||
request.uri = "https://http2.akamai.com/".parse().unwrap();
|
|
||||||
|
|
||||||
let err = h2.send_request(2.into(), request, true).wait().unwrap_err();
|
|
||||||
assert_user_err!(err, InvalidStreamId);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[ignore]
|
|
||||||
fn request_without_scheme() {
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[ignore]
|
|
||||||
fn request_with_h1_version() {
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn send_invalid_client_stream_id() {
|
|
||||||
let _ = ::env_logger::init();
|
|
||||||
|
|
||||||
for &id in &[0, 2] {
|
|
||||||
let mock = mock_io::Builder::new()
|
|
||||||
.handshake()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let h2 = client::handshake(mock)
|
|
||||||
.wait().unwrap();
|
|
||||||
|
|
||||||
// Send the request
|
|
||||||
let mut request = request::Head::default();
|
|
||||||
request.uri = "https://http2.akamai.com/".parse().unwrap();
|
|
||||||
let err = h2.send_request(id.into(), request, true).wait().unwrap_err();
|
|
||||||
|
|
||||||
assert_user_err!(err, InvalidStreamId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -104,19 +39,35 @@ fn recv_invalid_server_stream_id() {
|
|||||||
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 2, 137])
|
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 2, 137])
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let h2 = client::handshake(mock)
|
let mut h2 = Client::handshake(mock)
|
||||||
.wait().unwrap();
|
.wait().unwrap();
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
let mut request = request::Head::default();
|
let request = Request::builder()
|
||||||
request.uri = "https://http2.akamai.com/".parse().unwrap();
|
.uri("https://http2.akamai.com/")
|
||||||
let h2 = h2.send_request(1.into(), request, true).wait().unwrap();
|
.body(()).unwrap();
|
||||||
|
|
||||||
// Get the response
|
info!("sending request");
|
||||||
let (err, _) = h2.into_future().wait().unwrap_err();
|
let mut stream = h2.request(request, true).unwrap();
|
||||||
assert_proto_err!(err, ProtocolError);
|
|
||||||
|
// The connection errors
|
||||||
|
assert_proto_err!(h2.wait().unwrap_err(), ProtocolError);
|
||||||
|
|
||||||
|
// The stream errors
|
||||||
|
assert_proto_err!(stream.wait().unwrap_err(), ProtocolError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn request_without_scheme() {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn request_with_h1_version() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn sending_request_on_closed_soket() {
|
fn sending_request_on_closed_soket() {
|
||||||
|
|||||||
@@ -97,37 +97,6 @@ fn send_recv_data() {
|
|||||||
|
|
||||||
// The H2 connection is closed
|
// The H2 connection is closed
|
||||||
h2.wait().unwrap();
|
h2.wait().unwrap();
|
||||||
|
|
||||||
/*
|
|
||||||
let b = "hello";
|
|
||||||
|
|
||||||
// Send the data
|
|
||||||
let h2 = h2.send_data(1.into(), b.into(), true).wait().expect("send data");
|
|
||||||
|
|
||||||
// Get the response headers
|
|
||||||
let (resp, h2) = h2.into_future().wait().expect("into future");
|
|
||||||
|
|
||||||
match resp.expect("response headers") {
|
|
||||||
Frame::Headers { headers, .. } => {
|
|
||||||
assert_eq!(headers.status, status::OK);
|
|
||||||
}
|
|
||||||
_ => panic!("unexpected frame"),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the response body
|
|
||||||
let (data, h2) = h2.into_future().wait().expect("into future");
|
|
||||||
|
|
||||||
match data.expect("response data") {
|
|
||||||
Frame::Data { id, data, end_of_stream, .. } => {
|
|
||||||
assert_eq!(id, 1.into());
|
|
||||||
assert_eq!(data, &b"world"[..]);
|
|
||||||
assert!(end_of_stream);
|
|
||||||
}
|
|
||||||
_ => panic!("unexpected frame"),
|
|
||||||
}
|
|
||||||
|
|
||||||
assert!(Stream::wait(h2).next().is_none());;
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -185,35 +154,6 @@ fn send_headers_recv_data_single_frame() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#[test]
|
|
||||||
fn send_headers_twice_with_same_stream_id() {
|
|
||||||
let _ = env_logger::init();
|
|
||||||
|
|
||||||
let mock = mock_io::Builder::new()
|
|
||||||
.handshake()
|
|
||||||
// Write GET /
|
|
||||||
.write(&[
|
|
||||||
0, 0, 0x10, 1, 5, 0, 0, 0, 1, 0x82, 0x87, 0x41, 0x8B, 0x9D, 0x29,
|
|
||||||
0xAC, 0x4B, 0x8F, 0xA8, 0xE9, 0x19, 0x97, 0x21, 0xE9, 0x84,
|
|
||||||
])
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let h2 = client::handshake(mock)
|
|
||||||
.wait().unwrap();
|
|
||||||
|
|
||||||
// Send the request
|
|
||||||
let mut request = request::Head::default();
|
|
||||||
request.uri = "https://http2.akamai.com/".parse().unwrap();
|
|
||||||
let h2 = h2.send_request(1.into(), request, true).wait().unwrap();
|
|
||||||
|
|
||||||
// Send another request with the same stream ID
|
|
||||||
let mut request = request::Head::default();
|
|
||||||
request.uri = "https://http2.akamai.com/".parse().unwrap();
|
|
||||||
let err = h2.send_request(1.into(), request, true).wait().unwrap_err();
|
|
||||||
|
|
||||||
assert_user_err!(err, UnexpectedFrameType);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn send_data_after_headers_eos() {
|
fn send_data_after_headers_eos() {
|
||||||
let _ = env_logger::init();
|
let _ = env_logger::init();
|
||||||
@@ -246,21 +186,6 @@ fn send_data_after_headers_eos() {
|
|||||||
assert_user_err!(err, UnexpectedFrameType);
|
assert_user_err!(err, UnexpectedFrameType);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn send_data_without_headers() {
|
|
||||||
let mock = mock_io::Builder::new()
|
|
||||||
.handshake()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let h2 = client::handshake(mock)
|
|
||||||
.wait().unwrap();
|
|
||||||
|
|
||||||
let b = Bytes::from_static(b"hello world");
|
|
||||||
let err = h2.send_data(1.into(), b, true).wait().unwrap_err();
|
|
||||||
|
|
||||||
assert_user_err!(err, UnexpectedFrameType);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn exceed_max_streams() {
|
fn exceed_max_streams() {
|
||||||
|
|||||||
Reference in New Issue
Block a user