wip: problems with Frame::len()

This commit is contained in:
Oliver Gould
2017-07-17 01:07:21 +00:00
11 changed files with 486 additions and 192 deletions

View File

@@ -8,7 +8,7 @@ extern crate env_logger;
use h2::client; use h2::client;
use http::request; use http::*;
use futures::*; use futures::*;
@@ -34,11 +34,19 @@ pub fn main() {
println!("sending request"); println!("sending request");
let mut request = request::Head::default(); let mut request = request::Head::default();
request.method = method::POST;
request.uri = "https://http2.akamai.com/".parse().unwrap(); request.uri = "https://http2.akamai.com/".parse().unwrap();
// request.version = version::H2; // request.version = version::H2;
conn.send_request(1.into(), request, true) conn.send_request(1.into(), request, true)
}) })
/*
.then(|res| {
let conn = res.unwrap();
conn.send_data(1.into(), "hello".into(), true)
})
*/
.then(|res| { .then(|res| {
let conn = res.unwrap(); let conn = res.unwrap();
// Get the next message // Get the next message

View File

@@ -38,6 +38,12 @@ pub fn main() {
// Receive a request // Receive a request
conn.into_future() conn.into_future()
.then(|res| {
let (frame, conn) = res.unwrap();
println!("Zomg frame; {:?}", frame);
conn.into_future()
})
.then(|res| { .then(|res| {
let (frame, conn) = res.unwrap(); let (frame, conn) = res.unwrap();
println!("Zomg frame; {:?}", frame); println!("Zomg frame; {:?}", frame);
@@ -47,12 +53,6 @@ pub fn main() {
conn.send_response(1.into(), response, false) conn.send_response(1.into(), response, false)
}) })
.then(|res| {
let conn = res.unwrap();
println!("... sending data frame");
conn.send_data(1.into(), "hello".into(), false)
})
.then(|res| { .then(|res| {
let conn = res.unwrap(); let conn = res.unwrap();
println!("... sending next frame"); println!("... sending next frame");

View File

@@ -56,8 +56,8 @@ impl Peer for Client {
id.is_client_initiated() id.is_client_initiated()
} }
fn is_valid_remote_stream_id(id: StreamId) -> bool { fn is_valid_remote_stream_id(_id: StreamId) -> bool {
id.is_server_initiated() false
} }
fn convert_send_message( fn convert_send_message(

View File

@@ -62,6 +62,9 @@ pub enum User {
/// transmit a Data frame to the remote. /// transmit a Data frame to the remote.
FlowControlViolation, FlowControlViolation,
/// The connection state is corrupt and the connection should be dropped.
Corrupt,
// TODO: reserve additional variants // TODO: reserve additional variants
} }
@@ -100,6 +103,7 @@ macro_rules! user_desc {
InactiveStreamId => concat!($prefix, "inactive stream ID"), InactiveStreamId => concat!($prefix, "inactive stream ID"),
UnexpectedFrameType => concat!($prefix, "unexpected frame type"), UnexpectedFrameType => concat!($prefix, "unexpected frame type"),
FlowControlViolation => concat!($prefix, "flow control violation"), FlowControlViolation => concat!($prefix, "flow control violation"),
Corrupt => concat!($prefix, "connection state corrupt"),
} }
}); });
} }

View File

@@ -5,7 +5,7 @@ use bytes::{BufMut, Bytes, Buf};
#[derive(Debug)] #[derive(Debug)]
pub struct Data<T = Bytes> { pub struct Data<T = Bytes> {
stream_id: StreamId, stream_id: StreamId,
data_len: FrameSize, //data_len: FrameSize,
data: T, data: T,
flags: DataFlag, flags: DataFlag,
pad_len: Option<u8>, pad_len: Option<u8>,
@@ -30,7 +30,7 @@ impl Data<Bytes> {
}; };
Ok(Data { Ok(Data {
stream_id: head.stream_id(), stream_id: head.stream_id(),
data_len: payload.len() as FrameSize, //data_len: payload.len() as FrameSize,
data: payload, data: payload,
flags: flags, flags: flags,
pad_len: pad_len, pad_len: pad_len,
@@ -55,16 +55,16 @@ impl<T> Data<T> {
Head::new(Kind::Data, self.flags.into(), self.stream_id) Head::new(Kind::Data, self.flags.into(), self.stream_id)
} }
pub fn len(&self) -> FrameSize {
self.data_len
}
pub fn into_payload(self) -> T { pub fn into_payload(self) -> T {
self.data self.data
} }
} }
impl<T: Buf> Data<T> { impl<T: Buf> Data<T> {
pub fn len(&self) -> usize {
self.data.remaining()
}
pub fn from_buf(stream_id: StreamId, data: T, eos: bool) -> Self { pub fn from_buf(stream_id: StreamId, data: T, eos: bool) -> Self {
let mut flags = DataFlag::default(); let mut flags = DataFlag::default();
if eos { if eos {
@@ -72,7 +72,7 @@ impl<T: Buf> Data<T> {
} }
Data { Data {
stream_id, stream_id,
data_len: data.remaining() as FrameSize, //data_len: data.remaining() as FrameSize,
data, data,
flags, flags,
pad_len: None, pad_len: None,

View File

@@ -54,7 +54,7 @@ pub enum Frame<T, B = Bytes> {
id: StreamId, id: StreamId,
data: B, data: B,
/// TODO figure out how to make this a requirement on `B` /// TODO figure out how to make this a requirement on `B`
data_len: FrameSize, //data_len: FrameSize,
end_of_stream: bool, end_of_stream: bool,
}, },
Trailers { Trailers {

View File

@@ -1,23 +1,23 @@
use {ConnectionError, Frame, FrameSize}; use {ConnectionError, Frame, FrameSize};
use client::Client; use client::Client;
use error;
use frame::{self, SettingSet, StreamId}; use frame::{self, SettingSet, StreamId};
use proto::{self, ControlFlow, ControlPing, ControlSettings, Peer, PingPayload, ReadySink, WindowSize}; use proto::{self, ControlFlow, ControlPing, ControlSettings, Peer, PingPayload, ReadySink, WindowSize};
use server::Server; use server::Server;
use tokio_io::{AsyncRead, AsyncWrite};
use http::{request, response};
use bytes::{Bytes, IntoBuf}; use bytes::{Bytes, IntoBuf};
use http::{request, response};
use futures::*; use futures::*;
use tokio_io::{AsyncRead, AsyncWrite};
use std::marker::PhantomData; use std::marker::PhantomData;
/// An H2 connection /// An H2 connection
#[derive(Debug)] #[derive(Debug)]
pub struct Connection<T, P, B: IntoBuf = Bytes> { pub struct Connection<T, P, B: IntoBuf = Bytes> {
inner: proto::Transport<T, P, B::Buf>, inner: proto::Transport<T, P, B::Buf>,
peer: PhantomData<P>, // Set to `true` as long as the connection is in a valid state.
active: bool,
_phantom: PhantomData<(P, B)>,
} }
pub fn new<T, P, B>(transport: proto::Transport<T, P, B::Buf>) pub fn new<T, P, B>(transport: proto::Transport<T, P, B::Buf>)
@@ -28,13 +28,14 @@ pub fn new<T, P, B>(transport: proto::Transport<T, P, B::Buf>)
{ {
Connection { Connection {
inner: transport, inner: transport,
peer: PhantomData, active: true,
_phantom: PhantomData,
} }
} }
impl<T, P, B> ControlSettings for Connection<T, P, B> impl<T, P, B> ControlSettings for Connection<T, P, B>
where T: ControlSettings, where T: AsyncRead + AsyncWrite,
B: IntoBuf, B: IntoBuf,
{ {
fn update_local_settings(&mut self, local: frame::SettingSet) -> Result<(), ConnectionError> { fn update_local_settings(&mut self, local: frame::SettingSet) -> Result<(), ConnectionError> {
@@ -51,7 +52,7 @@ impl<T, P, B> ControlSettings for Connection<T, P, B>
} }
impl<T, P, B> ControlFlow for Connection<T, P, B> impl<T, P, B> ControlFlow for Connection<T, P, B>
where T: ControlFlow, where T: AsyncRead + AsyncWrite,
B: IntoBuf, B: IntoBuf,
{ {
fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> { fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> {
@@ -65,7 +66,6 @@ impl<T, P, B> ControlFlow for Connection<T, P, B>
impl<T, P, B> ControlPing for Connection<T, P, B> impl<T, P, B> ControlPing for Connection<T, P, B>
where T: AsyncRead + AsyncWrite, where T: AsyncRead + AsyncWrite,
T: ControlPing,
P: Peer, P: Peer,
B: IntoBuf, B: IntoBuf,
{ {
@@ -146,6 +146,10 @@ impl<T, P, B> Stream for Connection<T, P, B>
use frame::Frame::*; use frame::Frame::*;
trace!("poll"); trace!("poll");
if !self.active {
return Err(error::User::Corrupt.into());
}
loop { loop {
let frame = match try!(self.inner.poll()) { let frame = match try!(self.inner.poll()) {
Async::Ready(f) => f, Async::Ready(f) => f,
@@ -153,7 +157,7 @@ impl<T, P, B> Stream for Connection<T, P, B>
// Receiving new frames may depend on ensuring that the write buffer // Receiving new frames may depend on ensuring that the write buffer
// is clear (e.g. if window updates need to be sent), so `poll_complete` // is clear (e.g. if window updates need to be sent), so `poll_complete`
// is called here. // is called here.
try_ready!(self.inner.poll_complete()); try_ready!(self.poll_complete());
// If the write buffer is cleared, attempt to poll the underlying // If the write buffer is cleared, attempt to poll the underlying
// stream once more because it, may have been made ready. // stream once more because it, may have been made ready.
@@ -172,7 +176,7 @@ impl<T, P, B> Stream for Connection<T, P, B>
Some(Data(v)) => Frame::Data { Some(Data(v)) => Frame::Data {
id: v.stream_id(), id: v.stream_id(),
end_of_stream: v.is_end_stream(), end_of_stream: v.is_end_stream(),
data_len: v.len(), //data_len: v.len(),
data: v.into_payload(), data: v.into_payload(),
}, },
@@ -199,9 +203,13 @@ impl<T, P, B> Sink for Connection<T, P, B>
{ {
trace!("start_send"); trace!("start_send");
if !self.active {
return Err(error::User::Corrupt.into());
}
// Ensure the transport is ready to send a frame before we transform the external // Ensure the transport is ready to send a frame before we transform the external
// `Frame` into an internal `frame::Framme`. // `Frame` into an internal `frame::Frame`.
if self.inner.poll_ready()? == Async::NotReady { if !try!(self.poll_ready()).is_ready() {
return Ok(AsyncSink::NotReady(item)); return Ok(AsyncSink::NotReady(item));
} }

View File

@@ -47,7 +47,7 @@ impl<T, U> FlowControl<T>
initial_local_window_size, initial_local_window_size,
initial_remote_window_size, initial_remote_window_size,
connection_local_flow_controller: FlowControlState::with_initial_size(initial_local_window_size), connection_local_flow_controller: FlowControlState::with_initial_size(initial_local_window_size),
connection_remote_flow_controller: FlowControlState::with_next_update(initial_remote_window_size), connection_remote_flow_controller: FlowControlState::with_initial_size(initial_remote_window_size),
blocked_remote_window_update: None, blocked_remote_window_update: None,
sending_local_window_update: None, sending_local_window_update: None,
pending_local_window_updates: VecDeque::new(), pending_local_window_updates: VecDeque::new(),

View File

@@ -1,6 +1,6 @@
use ConnectionError; use ConnectionError;
use error::Reason::ProtocolError; use error::Reason;
use error::User::InvalidStreamId; use error::User;
use frame::{self, Frame}; use frame::{self, Frame};
use proto::*; use proto::*;
@@ -125,7 +125,7 @@ impl<T, P> Stream for StreamTracker<T, P>
// connections should not be factored. // connections should not be factored.
if !P::is_valid_remote_stream_id(id) { if !P::is_valid_remote_stream_id(id) {
unimplemented!(); return Err(Reason::ProtocolError.into());
} }
} }
@@ -134,7 +134,7 @@ impl<T, P> Stream for StreamTracker<T, P>
Some(Data(v)) => { Some(Data(v)) => {
match self.streams.get_mut(&v.stream_id()) { match self.streams.get_mut(&v.stream_id()) {
None => return Err(ProtocolError.into()), None => return Err(Reason::ProtocolError.into()),
Some(state) => state.recv_data(v.is_end_stream())?, Some(state) => state.recv_data(v.is_end_stream())?,
} }
Ok(Async::Ready(Some(Data(v)))) Ok(Async::Ready(Some(Data(v))))
@@ -179,14 +179,14 @@ impl<T, P, U> Sink for StreamTracker<T, P>
// connections should not be factored. // connections should not be factored.
if !P::is_valid_local_stream_id(id) { if !P::is_valid_local_stream_id(id) {
// TODO: clear state // TODO: clear state
return Err(InvalidStreamId.into()); return Err(User::InvalidStreamId.into());
} }
} }
} }
&Data(ref v) => { &Data(ref v) => {
match self.streams.get_mut(&v.stream_id()) { match self.streams.get_mut(&v.stream_id()) {
None => return Err(ProtocolError.into()), None => return Err(User::InactiveStreamId.into()),
Some(state) => state.send_data(v.is_end_stream())?, Some(state) => state.send_data(v.is_end_stream())?,
} }
} }

View File

@@ -110,8 +110,8 @@ impl Peer for Server {
type Send = http::response::Head; type Send = http::response::Head;
type Poll = http::request::Head; type Poll = http::request::Head;
fn is_valid_local_stream_id(id: StreamId) -> bool { fn is_valid_local_stream_id(_id: StreamId) -> bool {
id.is_server_initiated() false
} }
fn is_valid_remote_stream_id(id: StreamId) -> bool { fn is_valid_remote_stream_id(id: StreamId) -> bool {

View File

@@ -5,198 +5,472 @@ extern crate mock_io;
extern crate env_logger; extern crate env_logger;
extern crate bytes; extern crate bytes;
use h2::client; // scoped so `cargo test client_request` dtrt.
use http::request; mod client_request {
use bytes::Bytes; use h2::{client, Frame};
use http::*;
use futures::*; use futures::*;
use bytes::Bytes;
use mock_io;
// TODO: move into another file // TODO: move into another file
macro_rules! assert_user_err { macro_rules! assert_user_err {
($actual:expr, $err:ident) => {{ ($actual:expr, $err:ident) => {{
use h2::error::{ConnectionError, User}; use h2::error::{ConnectionError, User};
match $actual { match $actual {
ConnectionError::User(e) => assert_eq!(e, User::$err), ConnectionError::User(e) => assert_eq!(e, User::$err),
_ => panic!("unexpected connection error type"), _ => panic!("unexpected connection error type"),
}
}};
}
macro_rules! assert_proto_err {
($actual:expr, $err:ident) => {{
use h2::error::{ConnectionError, Reason};
match $actual {
ConnectionError::Proto(e) => assert_eq!(e, Reason::$err),
_ => panic!("unexpected connection error type"),
}
}};
}
#[test]
fn handshake() {
let _ = ::env_logger::init();
let mock = mock_io::Builder::new()
.handshake()
.write(SETTINGS_ACK)
.build();
let h2 = client::handshake(mock)
.wait().unwrap();
// At this point, the connection should be closed
assert!(Stream::wait(h2).next().is_none());
}
#[test]
fn get_with_204_response() {
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,
])
.write(SETTINGS_ACK)
// Read response
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 1, 0x89])
.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();
// Get the response
let (resp, h2) = h2.into_future().wait().unwrap();
match resp.unwrap() {
Frame::Headers { headers, .. } => {
assert_eq!(headers.status, status::NO_CONTENT);
}
_ => panic!("unexpected frame"),
} }
}};
}
#[test] // No more frames
fn handshake() { assert!(Stream::wait(h2).next().is_none());
let _ = ::env_logger::init(); }
let mock = mock_io::Builder::new() #[test]
.handshake() fn get_with_200_response() {
.write(SETTINGS_ACK) let _ = ::env_logger::init();
.build();
let mut h2 = client::handshake(mock) let mock = mock_io::Builder::new()
.wait().unwrap(); .handshake()
// Write GET /
.write(&[
0, 0, 16, 1, 5, 0, 0, 0, 1, 130, 135, 65, 139, 157, 41, 172, 75,
143, 168, 233, 25, 151, 33, 233, 132
])
.write(SETTINGS_ACK)
// Read response
.read(&[
0, 0, 1, 1, 4, 0, 0, 0, 1, 136, 0, 0, 5, 0, 0, 0, 0, 0, 1, 104, 101,
108, 108, 111, 0, 0, 5, 0, 1, 0, 0, 0, 1, 119, 111, 114, 108, 100,
])
.build();
// At this point, the connection should be closed let h2 = client::handshake(mock)
assert!(Stream::wait(h2).next().is_none()); .wait().unwrap();
}
#[test] // Send the request
fn get_with_204_response() { let mut request = request::Head::default();
let _ = ::env_logger::init(); request.uri = "https://http2.akamai.com/".parse().unwrap();
let h2 = h2.send_request(1.into(), request, true).wait().unwrap();
let mock = mock_io::Builder::new() // Get the response headers
.handshake() let (resp, h2) = h2.into_future().wait().unwrap();
// 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,
])
.write(SETTINGS_ACK)
// Read response
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 1, 0x89])
.build();
let mut h2 = client::handshake(mock) match resp.unwrap() {
.wait().unwrap(); Frame::Headers { headers, .. } => {
assert_eq!(headers.status, status::OK);
}
_ => panic!("unexpected frame"),
}
// Send the request // Get the response body
let mut request = request::Head::default(); let (data, h2) = h2.into_future().wait().unwrap();
request.uri = "https://http2.akamai.com/".parse().unwrap();
let h2 = h2.send_request(1.into(), request, true).wait().unwrap();
// Get the response match data.unwrap() {
let (resp, h2) = h2.into_future().wait().unwrap(); Frame::Data { id, data, end_of_stream, .. } => {
assert_eq!(id, 1.into());
assert_eq!(data, &b"hello"[..]);
assert!(!end_of_stream);
}
_ => panic!("unexpected frame"),
}
assert!(Stream::wait(h2).next().is_none()); // Get the response body
} let (data, h2) = h2.into_future().wait().unwrap();
#[test] match data.unwrap() {
#[ignore] Frame::Data { id, data, end_of_stream, .. } => {
fn get_with_200_response() { assert_eq!(id, 1.into());
let _ = ::env_logger::init(); assert_eq!(data, &b"world"[..]);
assert!(end_of_stream);
}
_ => panic!("unexpected frame"),
}
let mock = mock_io::Builder::new() assert!(Stream::wait(h2).next().is_none());
.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,
])
.write(SETTINGS_ACK)
// Read response
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 1, 0x89])
.build();
let mut h2 = client::handshake(mock) #[test]
.wait().unwrap(); #[ignore]
fn post_with_large_body() {
let _ = ::env_logger::init();
// Send the request let mock = mock_io::Builder::new()
let mut request = request::Head::default(); .handshake()
request.uri = "https://http2.akamai.com/".parse().unwrap(); .write(&[
let h2 = h2.send_request(1.into(), request, true).wait().unwrap(); // POST /
0, 0, 16, 1, 4, 0, 0, 0, 1, 131, 135, 65, 139, 157, 41,
172, 75, 143, 168, 233, 25, 151, 33, 233, 132,
])
.write(&[
// DATA
0, 0, 5, 0, 1, 0, 0, 0, 1, 104, 101, 108, 108, 111,
])
.write(SETTINGS_ACK)
// Read response
.read(&[
// HEADERS
0, 0, 1, 1, 4, 0, 0, 0, 1, 136,
// DATA
0, 0, 5, 0, 1, 0, 0, 0, 1, 119, 111, 114, 108, 100
])
.build();
// Get the response let h2 = client::handshake(mock)
let (resp, h2) = h2.into_future().wait().unwrap(); .wait().unwrap();
assert!(Stream::wait(h2).next().is_none()); // Send the request
} let mut request = request::Head::default();
request.method = method::POST;
request.uri = "https://http2.akamai.com/".parse().unwrap();
let h2 = h2.send_request(1.into(), request, false).wait().unwrap();
#[test] // Send the data
fn request_with_zero_stream_id() { let b = [0; 300];
let mock = mock_io::Builder::new() let h2 = h2.send_data(1.into(), (&b[..]).into(), true).wait().unwrap();
.handshake()
.build();
let h2 = client::handshake(mock) // Get the response headers
.wait().unwrap(); let (resp, h2) = h2.into_future().wait().unwrap();
// Send the request match resp.unwrap() {
let mut request = request::Head::default(); Frame::Headers { headers, .. } => {
request.uri = "https://http2.akamai.com/".parse().unwrap(); assert_eq!(headers.status, status::OK);
}
_ => panic!("unexpected frame"),
}
let err = h2.send_request(0.into(), request, true).wait().unwrap_err(); // Get the response body
assert_user_err!(err, InvalidStreamId); let (data, h2) = h2.into_future().wait().unwrap();
}
#[test] match data.unwrap() {
fn request_with_server_stream_id() { Frame::Data { id, data, end_of_stream, .. } => {
let mock = mock_io::Builder::new() assert_eq!(id, 1.into());
.handshake() assert_eq!(data, &b"world"[..]);
.build(); assert!(end_of_stream);
}
_ => panic!("unexpected frame"),
}
let h2 = client::handshake(mock) assert!(Stream::wait(h2).next().is_none());
.wait().unwrap(); }
// Send the request #[test]
let mut request = request::Head::default(); fn request_with_zero_stream_id() {
request.uri = "https://http2.akamai.com/".parse().unwrap(); let mock = mock_io::Builder::new()
.handshake()
.build();
let err = h2.send_request(2.into(), request, true).wait().unwrap_err(); let h2 = client::handshake(mock)
assert_user_err!(err, InvalidStreamId); .wait().unwrap();
}
#[test] // Send the request
#[ignore] let mut request = request::Head::default();
fn send_data_without_headers() { request.uri = "https://http2.akamai.com/".parse().unwrap();
let mock = mock_io::Builder::new()
.handshake()
.build();
let h2 = client::handshake(mock) let err = h2.send_request(0.into(), request, true).wait().unwrap_err();
.wait().unwrap(); assert_user_err!(err, InvalidStreamId);
}
// Send the request #[test]
let mut request = request::Head::default(); fn post_with_200_response() {
request.uri = "https://http2.akamai.com/".parse().unwrap(); let _ = ::env_logger::init();
/* let mock = mock_io::Builder::new()
let err = h2.send_request(2.into(), request, true).wait().unwrap_err(); .handshake()
assert_user_err!(err, InvalidStreamId); .write(&[
*/ // POST /
} 0, 0, 16, 1, 4, 0, 0, 0, 1, 131, 135, 65, 139, 157, 41,
172, 75, 143, 168, 233, 25, 151, 33, 233, 132,
])
.write(&[
// DATA
0, 0, 5, 0, 1, 0, 0, 0, 1, 104, 101, 108, 108, 111,
])
.write(SETTINGS_ACK)
// Read response
.read(&[
// HEADERS
0, 0, 1, 1, 4, 0, 0, 0, 1, 136,
// DATA
0, 0, 5, 0, 1, 0, 0, 0, 1, 119, 111, 114, 108, 100
])
.build();
#[test] let h2 = client::handshake(mock).wait().expect("handshake");
#[ignore]
fn send_data_after_headers_eos() {
}
#[test] // Send the request
#[ignore] let mut request = request::Head::default();
fn request_without_scheme() { request.method = method::POST;
} request.uri = "https://http2.akamai.com/".parse().unwrap();
let h2 = h2.send_request(1.into(), request, false).wait().expect("send request");
#[test] let b = "hello";
#[ignore]
fn request_with_h1_version() {
}
#[test] // Send the data
#[ignore] let h2 = h2.send_data(1.into(), b.into(), true).wait().expect("send data");
fn invalid_client_stream_id() {
}
#[test] // Get the response headers
#[ignore] let (resp, h2) = h2.into_future().wait().expect("into future");
fn invalid_server_stream_id() {
}
#[test] match resp.expect("response headers") {
#[ignore] Frame::Headers { headers, .. } => {
fn exceed_max_streams() { assert_eq!(headers.status, status::OK);
} }
_ => panic!("unexpected frame"),
}
const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0]; // Get the response body
const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0]; let (data, h2) = h2.into_future().wait().expect("into future");
trait MockH2 { match data.expect("response data") {
fn handshake(&mut self) -> &mut Self; Frame::Data { id, data, end_of_stream, .. } => {
} assert_eq!(id, 1.into());
assert_eq!(data, &b"world"[..]);
assert!(end_of_stream);
}
_ => panic!("unexpected frame"),
}
impl MockH2 for mock_io::Builder { assert!(Stream::wait(h2).next().is_none());
fn handshake(&mut self) -> &mut Self { }
self.write(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
// Settings frame #[test]
.write(SETTINGS) fn request_with_server_stream_id() {
.read(SETTINGS) let mock = mock_io::Builder::new()
.read(SETTINGS_ACK) .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]
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]
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, InactiveStreamId);
}
#[test]
fn send_data_after_headers_eos() {
let _ = ::env_logger::init();
let mock = mock_io::Builder::new()
.handshake()
// Write GET /
.write(&[
// GET /, no EOS
0, 0, 16, 1, 5, 0, 0, 0, 1, 131, 135, 65, 139, 157, 41, 172,
75, 143, 168, 233, 25, 151, 33, 233, 132
])
.build();
let h2 = client::handshake(mock)
.wait().expect("handshake");
// Send the request
let mut request = request::Head::default();
request.method = method::POST;
request.uri = "https://http2.akamai.com/".parse().unwrap();
let id = 1.into();
let h2 = h2.send_request(id, request, true).wait().expect("send request");
let body = "hello";
// Send the data
let err = h2.send_data(id, body.into(), true).wait().unwrap_err();
assert_user_err!(err, UnexpectedFrameType);
}
#[test]
#[ignore]
fn request_without_scheme() {
}
#[test]
#[ignore]
fn request_with_h1_version() {
}
#[test]
fn 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]
fn invalid_server_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,
])
.write(SETTINGS_ACK)
// Read response
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 2, 137])
.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();
// Get the response
let (err, _) = h2.into_future().wait().unwrap_err();
assert_proto_err!(err, ProtocolError);
}
#[test]
#[ignore]
fn exceed_max_streams() {
}
const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0];
const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0];
trait MockH2 {
fn handshake(&mut self) -> &mut Self;
}
impl MockH2 for mock_io::Builder {
fn handshake(&mut self) -> &mut Self {
self.write(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
// Settings frame
.write(SETTINGS)
.read(SETTINGS)
.read(SETTINGS_ACK)
}
} }
} }