This commit is contained in:
Carl Lerche
2017-03-10 13:02:04 -08:00
parent 1fe3a57338
commit e2871d92fa
12 changed files with 559 additions and 38 deletions

View File

@@ -1,5 +1,7 @@
use super::Error;
use bytes::{BufMut, BigEndian};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Head {
kind: Kind,
@@ -25,9 +27,19 @@ pub enum Kind {
pub type StreamId = u32;
const STREAM_ID_MASK: StreamId = 0x80000000;
// ===== impl Head =====
impl Head {
pub fn new(kind: Kind, flag: u8, stream_id: StreamId) -> Head {
Head {
kind: kind,
flag: flag,
stream_id: stream_id,
}
}
/// Parse an HTTP/2.0 frame header
pub fn parse(header: &[u8]) -> Head {
Head {
@@ -48,6 +60,21 @@ impl Head {
pub fn flag(&self) -> u8 {
self.flag
}
pub fn encode_len(&self) -> usize {
super::FRAME_HEADER_LEN
}
pub fn encode<T: BufMut>(&self, payload_len: usize, dst: &mut T) -> Result<(), Error> {
debug_assert_eq!(self.encode_len(), dst.remaining_mut());
debug_assert!(self.stream_id & STREAM_ID_MASK == 0);
dst.put_uint::<BigEndian>(payload_len as u64, 3);
dst.put_u8(self.kind as u8);
dst.put_u8(self.flag);
dst.put_u32::<BigEndian>(self.stream_id);
Ok(())
}
}
/// Parse the next 4 octets in the given buffer, assuming they represent an
@@ -58,7 +85,7 @@ impl Head {
fn parse_stream_id(buf: &[u8]) -> StreamId {
let unpacked = unpack_octets_4!(buf, 0, u32);
// Now clear the most significant bit, as that is reserved and MUST be ignored when received.
unpacked & !0x80000000
unpacked & !STREAM_ID_MASK
}
// ===== impl Kind =====