More hpack work

This commit is contained in:
Carl Lerche
2017-03-15 17:16:00 -07:00
parent 1deac1f16e
commit eb00f71caf
4 changed files with 146 additions and 52 deletions

View File

@@ -1,4 +1,7 @@
use super::DecoderError;
use tower::http::{HeaderName, Method, StatusCode, Str};
use bytes::Bytes;
/// HPack table entry
#[derive(Debug, Clone)]
@@ -14,6 +17,16 @@ pub enum Entry {
Status(StatusCode),
}
/// The name component of an Entry
pub enum Key<'a> {
Header(&'a HeaderName),
Authority,
Method,
Scheme,
Path,
Status,
}
impl Entry {
pub fn len(&self) -> usize {
match *self {
@@ -38,4 +51,46 @@ impl Entry {
}
}
}
pub fn key(&self) -> Key {
match *self {
Entry::Header { ref name, .. } => Key::Header(name),
Entry::Authority(..) => Key::Authority,
Entry::Method(..) => Key::Method,
Entry::Scheme(..) => Key::Scheme,
Entry::Path(..) => Key::Path,
Entry::Status(..) => Key::Status,
}
}
}
impl<'a> Key<'a> {
pub fn into_entry(self, value: Bytes) -> Result<Entry, DecoderError> {
match self {
Key::Header(name) => {
Ok(Entry::Header {
name: name.clone(),
value: try!(Str::from_utf8(value)),
})
}
Key::Authority => {
Ok(Entry::Authority(try!(Str::from_utf8(value))))
}
Key::Method => {
Ok(Entry::Scheme(try!(Str::from_utf8(value))))
}
Key::Scheme => {
Ok(Entry::Scheme(try!(Str::from_utf8(value))))
}
Key::Path => {
Ok(Entry::Path(try!(Str::from_utf8(value))))
}
Key::Status => {
match StatusCode::parse(&value) {
Some(status) => Ok(Entry::Status(status)),
None => Err(DecoderError::InvalidStatusCode),
}
}
}
}
}