|
|
@ -3,8 +3,10 @@ use std::str::from_utf8; |
|
|
|
use std::slice; |
|
|
|
use std::slice; |
|
|
|
|
|
|
|
|
|
|
|
use httparse; |
|
|
|
use httparse; |
|
|
|
|
|
|
|
use httparse::Status; |
|
|
|
|
|
|
|
|
|
|
|
use error::Result; |
|
|
|
use error::Result; |
|
|
|
|
|
|
|
use super::machine::TryParse; |
|
|
|
|
|
|
|
|
|
|
|
// Limit the number of header lines.
|
|
|
|
// Limit the number of header lines.
|
|
|
|
pub const MAX_HEADERS: usize = 124; |
|
|
|
pub const MAX_HEADERS: usize = 124; |
|
|
@ -71,16 +73,15 @@ pub trait FromHttparse<T>: Sized { |
|
|
|
fn from_httparse(raw: T) -> Result<Self>; |
|
|
|
fn from_httparse(raw: T) -> Result<Self>; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
impl TryParse for Headers { |
|
|
|
impl TryParse for Headers { |
|
|
|
fn httparse(buf: &[u8]) -> Result<Option<(usize, Self)>> { |
|
|
|
fn try_parse(buf: &[u8]) -> Result<Option<(usize, Self)>> { |
|
|
|
let mut hbuffer = [httparse::EMPTY_HEADER; MAX_HEADERS]; |
|
|
|
let mut hbuffer = [httparse::EMPTY_HEADER; MAX_HEADERS]; |
|
|
|
Ok(match httparse::parse_headers(buf, &mut hbuffer)? { |
|
|
|
Ok(match httparse::parse_headers(buf, &mut hbuffer)? { |
|
|
|
Status::Partial => None, |
|
|
|
Status::Partial => None, |
|
|
|
Status::Complete((size, hdr)) => Some((size, Headers::from_httparse(hdr)?)), |
|
|
|
Status::Complete((size, hdr)) => Some((size, Headers::from_httparse(hdr)?)), |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
}*/ |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers { |
|
|
|
impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers { |
|
|
|
fn from_httparse(raw: &'b [httparse::Header<'h>]) -> Result<Self> { |
|
|
|
fn from_httparse(raw: &'b [httparse::Header<'h>]) -> Result<Self> { |
|
|
@ -96,8 +97,7 @@ impl<'b: 'h, 'h> FromHttparse<&'b [httparse::Header<'h>]> for Headers { |
|
|
|
mod tests { |
|
|
|
mod tests { |
|
|
|
|
|
|
|
|
|
|
|
use super::Headers; |
|
|
|
use super::Headers; |
|
|
|
|
|
|
|
use super::super::machine::TryParse; |
|
|
|
use std::io::Cursor; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn headers() { |
|
|
|
fn headers() { |
|
|
@ -106,8 +106,7 @@ mod tests { |
|
|
|
Connection: Upgrade\r\n\ |
|
|
|
Connection: Upgrade\r\n\ |
|
|
|
Upgrade: websocket\r\n\ |
|
|
|
Upgrade: websocket\r\n\ |
|
|
|
\r\n"; |
|
|
|
\r\n"; |
|
|
|
let mut inp = Cursor::new(data); |
|
|
|
let (_, hdr) = Headers::try_parse(data).unwrap().unwrap(); |
|
|
|
let hdr = Headers::parse(&mut inp).unwrap().unwrap(); |
|
|
|
|
|
|
|
assert_eq!(hdr.find_first("Host"), Some(&b"foo.com"[..])); |
|
|
|
assert_eq!(hdr.find_first("Host"), Some(&b"foo.com"[..])); |
|
|
|
assert_eq!(hdr.find_first("Upgrade"), Some(&b"websocket"[..])); |
|
|
|
assert_eq!(hdr.find_first("Upgrade"), Some(&b"websocket"[..])); |
|
|
|
assert_eq!(hdr.find_first("Connection"), Some(&b"Upgrade"[..])); |
|
|
|
assert_eq!(hdr.find_first("Connection"), Some(&b"Upgrade"[..])); |
|
|
@ -126,8 +125,7 @@ mod tests { |
|
|
|
Sec-WebSocket-ExtenSIONS: permessage-unknown\r\n\ |
|
|
|
Sec-WebSocket-ExtenSIONS: permessage-unknown\r\n\ |
|
|
|
Upgrade: websocket\r\n\ |
|
|
|
Upgrade: websocket\r\n\ |
|
|
|
\r\n"; |
|
|
|
\r\n"; |
|
|
|
let mut inp = Cursor::new(data); |
|
|
|
let (_, hdr) = Headers::try_parse(data).unwrap().unwrap(); |
|
|
|
let hdr = Headers::parse(&mut inp).unwrap().unwrap(); |
|
|
|
|
|
|
|
let mut iter = hdr.find("Sec-WebSocket-Extensions"); |
|
|
|
let mut iter = hdr.find("Sec-WebSocket-Extensions"); |
|
|
|
assert_eq!(iter.next(), Some(&b"permessage-deflate"[..])); |
|
|
|
assert_eq!(iter.next(), Some(&b"permessage-deflate"[..])); |
|
|
|
assert_eq!(iter.next(), Some(&b"permessage-unknown"[..])); |
|
|
|
assert_eq!(iter.next(), Some(&b"permessage-unknown"[..])); |
|
|
@ -140,8 +138,7 @@ mod tests { |
|
|
|
b"Host: foo.com\r\n\ |
|
|
|
b"Host: foo.com\r\n\ |
|
|
|
Connection: Upgrade\r\n\ |
|
|
|
Connection: Upgrade\r\n\ |
|
|
|
Upgrade: websocket\r\n"; |
|
|
|
Upgrade: websocket\r\n"; |
|
|
|
let mut inp = Cursor::new(data); |
|
|
|
let hdr = Headers::try_parse(data).unwrap(); |
|
|
|
let hdr = Headers::parse(&mut inp).unwrap(); |
|
|
|
|
|
|
|
assert!(hdr.is_none()); |
|
|
|
assert!(hdr.is_none()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|