perf(http): changes http parsing to use httparse crate

httparse is a http1 stateless push parser. This not only speeds up
parsing right now with sync io, but will also be useful for when we get
async io, since it's push based instead of pull.

BREAKING CHANGE: Several public functions and types in the `http` module
  have been removed. They have been replaced with 2 methods that handle
  all of the http1 parsing.
This commit is contained in:
Sean McArthur
2015-03-12 14:01:52 -07:00
parent 003b6551cf
commit b87bb20f0c
15 changed files with 358 additions and 731 deletions

View File

@@ -1,5 +1,9 @@
//! HTTP RequestUris
use std::str::FromStr;
use url::Url;
use url::ParseError as UrlError;
use error::HttpError;
/// The Request-URI of a Request's StartLine.
///
@@ -45,3 +49,40 @@ pub enum RequestUri {
Star,
}
impl FromStr for RequestUri {
type Err = HttpError;
fn from_str(s: &str) -> Result<RequestUri, HttpError> {
match s.as_bytes() {
[] => Err(HttpError::HttpUriError(UrlError::InvalidCharacter)),
[b'*'] => Ok(RequestUri::Star),
[b'/', ..] => Ok(RequestUri::AbsolutePath(s.to_string())),
bytes if bytes.contains(&b'/') => {
Ok(RequestUri::AbsoluteUri(try!(Url::parse(s))))
}
_ => {
let mut temp = "http://".to_string();
temp.push_str(s);
try!(Url::parse(&temp[..]));
todo!("compare vs u.authority()");
Ok(RequestUri::Authority(s.to_string()))
}
}
}
}
#[test]
fn test_uri_fromstr() {
use error::HttpResult;
fn read(s: &str, result: HttpResult<RequestUri>) {
assert_eq!(s.parse(), result);
}
read("*", Ok(RequestUri::Star));
read("http://hyper.rs/", Ok(RequestUri::AbsoluteUri(Url::parse("http://hyper.rs/").unwrap())));
read("hyper.rs", Ok(RequestUri::Authority("hyper.rs".to_string())));
read("/", Ok(RequestUri::AbsolutePath("/".to_string())));
}