resets too

This commit is contained in:
Oliver Gould
2017-07-17 08:18:38 +00:00
parent 76cb8e15fd
commit fb4f0bc5af
16 changed files with 296 additions and 91 deletions

View File

@@ -4,7 +4,6 @@ use bytes::{BufMut, Bytes, Buf};
#[derive(Debug)]
pub struct Data<T = Bytes> {
stream_id: StreamId,
//data_len: FrameSize,
data: T,
flags: DataFlag,
pad_len: Option<u8>,
@@ -29,7 +28,6 @@ impl Data<Bytes> {
};
Ok(Data {
stream_id: head.stream_id(),
//data_len: payload.len() as FrameSize,
data: payload,
flags: flags,
pad_len: pad_len,
@@ -71,7 +69,6 @@ impl<T: Buf> Data<T> {
}
Data {
stream_id,
//data_len: data.remaining() as FrameSize,
data,
flags,
pad_len: None,

View File

@@ -48,6 +48,12 @@ pub struct PushPromise {
flags: HeadersFlag,
}
impl PushPromise {
pub fn stream_id(&self) -> StreamId {
self.stream_id
}
}
#[derive(Debug)]
pub struct Continuation {
/// Stream ID of continuation frame

View File

@@ -61,7 +61,25 @@ pub enum Frame<T = Bytes> {
PushPromise(PushPromise),
Settings(Settings),
Ping(Ping),
WindowUpdate(WindowUpdate)
WindowUpdate(WindowUpdate),
Reset(Reset)
}
impl<T> Frame<T> {
pub fn stream_id(&self) -> StreamId {
use self::Frame::*;
match self {
&Headers(ref v) => v.stream_id(),
&Data(ref v) => v.stream_id(),
&PushPromise(ref v) => v.stream_id(),
&WindowUpdate(ref v) => v.stream_id(),
&Reset(ref v) => v.stream_id(),
&Ping(_) |
&Settings(_) => StreamId::zero(),
}
}
}
/// Errors that can occur during parsing an HTTP/2 frame.

View File

@@ -1,5 +1,7 @@
use frame::{Head, Error};
use super::{StreamId};
use error::Reason;
use frame::{self, Head, Error, Kind, StreamId};
use bytes::{BufMut, BigEndian};
#[derive(Debug)]
pub struct Reset {
@@ -8,11 +10,15 @@ pub struct Reset {
}
impl Reset {
pub fn new(stream_id: StreamId, error: Reason) -> Reset {
Reset {
stream_id,
error_code: error.into(),
}
}
pub fn load(head: Head, payload: &[u8]) -> Result<Reset, Error> {
if payload.len() != 4 {
// Invalid payload len
// TODO: Handle error
unimplemented!();
return Err(Error::InvalidPayloadLength);
}
let error_code = unpack_octets_4!(payload, 0, u32);
@@ -22,4 +28,25 @@ impl Reset {
error_code: error_code,
})
}
pub fn encode<B: BufMut>(&self, dst: &mut B) {
trace!("encoding RESET; id={:?} code={}", self.stream_id, self.error_code);
let head = Head::new(Kind::Reset, 0, self.stream_id);
head.encode(4, dst);
dst.put_u32::<BigEndian>(self.error_code);
}
pub fn stream_id(&self) -> StreamId {
self.stream_id
}
pub fn reason(&self) -> Reason {
self.error_code.into()
}
}
impl<B> From<Reset> for frame::Frame<B> {
fn from(src: Reset) -> Self {
frame::Frame::Reset(src)
}
}

View File

@@ -1,8 +1,8 @@
use StreamId;
use byteorder::NetworkEndian;
use bytes::{BufMut};
use frame::{self, Head, Kind, Error};
use bytes::{BufMut, BigEndian};
const SIZE_INCREMENT_MASK: u32 = 1 << 31;
#[derive(Copy, Clone, Debug)]
@@ -48,7 +48,7 @@ impl WindowUpdate {
trace!("encoding WINDOW_UPDATE; id={:?}", self.stream_id);
let head = Head::new(Kind::Ping, 0, self.stream_id);
head.encode(4, dst);
dst.put_u32::<NetworkEndian>(self.size_increment);
dst.put_u32::<BigEndian>(self.size_increment);
}
}