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:
72
src/lib.rs
72
src/lib.rs
@@ -1,6 +1,6 @@
|
||||
#![feature(core, collections, io, net, os, path,
|
||||
std_misc, box_syntax, unsafe_destructor)]
|
||||
#![cfg_attr(test, deny(missing_docs))]
|
||||
#![deny(missing_docs)]
|
||||
#![cfg_attr(test, deny(warnings))]
|
||||
#![cfg_attr(test, feature(alloc, test))]
|
||||
|
||||
@@ -132,6 +132,7 @@ extern crate url;
|
||||
extern crate openssl;
|
||||
extern crate cookie;
|
||||
extern crate unicase;
|
||||
extern crate httparse;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
@@ -143,17 +144,11 @@ extern crate test;
|
||||
pub use mimewrapper::mime;
|
||||
pub use url::Url;
|
||||
pub use client::Client;
|
||||
pub use error::{HttpResult, HttpError};
|
||||
pub use method::Method::{Get, Head, Post, Delete};
|
||||
pub use status::StatusCode::{Ok, BadRequest, NotFound};
|
||||
pub use server::Server;
|
||||
|
||||
use std::error::{Error, FromError};
|
||||
use std::fmt;
|
||||
use std::io::Error as IoError;
|
||||
|
||||
use self::HttpError::{HttpMethodError, HttpUriError, HttpVersionError,
|
||||
HttpHeaderError, HttpStatusError, HttpIoError};
|
||||
|
||||
macro_rules! todo(
|
||||
($($arg:tt)*) => (if cfg!(not(ndebug)) {
|
||||
trace!("TODO: {:?}", format_args!($($arg)*))
|
||||
@@ -173,6 +168,7 @@ macro_rules! inspect(
|
||||
mod mock;
|
||||
|
||||
pub mod client;
|
||||
pub mod error;
|
||||
pub mod method;
|
||||
pub mod header;
|
||||
pub mod http;
|
||||
@@ -188,66 +184,6 @@ mod mimewrapper {
|
||||
extern crate mime;
|
||||
}
|
||||
|
||||
|
||||
/// Result type often returned from methods that can have `HttpError`s.
|
||||
pub type HttpResult<T> = Result<T, HttpError>;
|
||||
|
||||
/// A set of errors that can occur parsing HTTP streams.
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum HttpError {
|
||||
/// An invalid `Method`, such as `GE,T`.
|
||||
HttpMethodError,
|
||||
/// An invalid `RequestUri`, such as `exam ple.domain`.
|
||||
HttpUriError(url::ParseError),
|
||||
/// An invalid `HttpVersion`, such as `HTP/1.1`
|
||||
HttpVersionError,
|
||||
/// An invalid `Header`.
|
||||
HttpHeaderError,
|
||||
/// An invalid `Status`, such as `1337 ELITE`.
|
||||
HttpStatusError,
|
||||
/// An `IoError` that occured while trying to read or write to a network stream.
|
||||
HttpIoError(IoError),
|
||||
}
|
||||
|
||||
impl fmt::Display for HttpError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(self.description())
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for HttpError {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
HttpMethodError => "Invalid Method specified",
|
||||
HttpUriError(_) => "Invalid Request URI specified",
|
||||
HttpVersionError => "Invalid HTTP version specified",
|
||||
HttpHeaderError => "Invalid Header provided",
|
||||
HttpStatusError => "Invalid Status provided",
|
||||
HttpIoError(_) => "An IoError occurred while connecting to the specified network",
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
match *self {
|
||||
HttpIoError(ref error) => Some(error as &Error),
|
||||
HttpUriError(ref error) => Some(error as &Error),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromError<IoError> for HttpError {
|
||||
fn from_error(err: IoError) -> HttpError {
|
||||
HttpIoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromError<url::ParseError> for HttpError {
|
||||
fn from_error(err: url::ParseError) -> HttpError {
|
||||
HttpUriError(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unconditional_recursion)]
|
||||
fn _assert_send<T: Send>() {
|
||||
_assert_send::<client::Request<net::Fresh>>();
|
||||
|
||||
Reference in New Issue
Block a user