Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cache:
- target/debug/build

script:
- cargo build
- cargo build --verbose
- cargo test --verbose
- cargo build --verbose --no-default-features
- cargo test --verbose --no-default-features
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ description = "A tiny, safe, speedy, zero-copy HTTP/1.x parser."
repository = "https://github.com/seanmonstar/httparse"

[features]
default = ["no_std"]
no_std = []
default = ["std"]
std = []

[dev-dependencies]
pico-sys = "0.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A push parser for the HTTP 1.x protocol. Avoids allocations. Fast.

[Documentation](https://seanmonstar.github.io/httparse)
[Documentation](https://docs.rs/httparse)

## Usage

Expand Down
4 changes: 0 additions & 4 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#[cfg(feature = "no_std")]
use core::slice;

#[cfg(not(feature = "no_std"))]
use std::slice;

pub struct Bytes<'a> {
slice: &'a [u8],
pos: usize
Expand Down
47 changes: 29 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(all(feature = "no_std", not(test)), no_std)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]
//! # httparse
Expand All @@ -14,14 +14,10 @@
//! 1.6 times slower than pico. Improvements can be made as a `likely`
//! intrinsic, and simd, are stabilized in rustc.

#[cfg(test)] extern crate core;
#[cfg(feature = "std")] extern crate std as core;
Copy link
Contributor

@jethrogb jethrogb Nov 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just do extern crate core on std. There are some minute differences in the API and this could prevent possible confusion in the future.


#[cfg(feature = "no_std")]
use core::{fmt, result, str, slice};

#[cfg(not(feature = "no_std"))]
use std::{fmt, result, str, slice};

use iter::Bytes;

mod iter;
Expand Down Expand Up @@ -137,7 +133,7 @@ impl fmt::Display for Error {
}
}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn description(&self) -> &str {
self.description_str()
Expand Down Expand Up @@ -670,18 +666,21 @@ mod tests {
}

macro_rules! req {
($name:ident, $buf:expr, $closure:expr) => (
req! {$name, $buf, Ok(Status::Complete($buf.len())), $closure }
($name:ident, $buf:expr, |$arg:ident| $body:expr) => (
req! {$name, $buf, Ok(Status::Complete($buf.len())), |$arg| $body }
);
($name:ident, $buf:expr, $len:expr, $closure:expr) => (
($name:ident, $buf:expr, $len:expr, |$arg:ident| $body:expr) => (
#[test]
fn $name() {
let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
let mut req = Request::new(&mut headers[..]);
let closure: Box<Fn(Request)> = Box::new($closure);
let status = req.parse($buf.as_ref());
assert_eq!(status, $len);
closure(req);

fn closure($arg: Request) {
$body
}
}
)
}
Expand Down Expand Up @@ -744,7 +743,7 @@ mod tests {
req! {
test_request_newlines,
b"GET / HTTP/1.1\nHost: foo.bar\n\n",
|_| {}
|_r| {}
}

req! {
Expand Down Expand Up @@ -773,22 +772,25 @@ mod tests {
test_request_with_invalid_token_delimiter,
b"GET\n/ HTTP/1.1\r\nHost: foo.bar\r\n\r\n",
Err(::Error::Token),
|_| {}
|_r| {}
}

macro_rules! res {
($name:ident, $buf:expr, $closure:expr) => (
res! {$name, $buf, Ok(Status::Complete($buf.len())), $closure }
($name:ident, $buf:expr, |$arg:ident| $body:expr) => (
res! {$name, $buf, Ok(Status::Complete($buf.len())), |$arg| $body }
);
($name:ident, $buf:expr, $len:expr, $closure:expr) => (
($name:ident, $buf:expr, $len:expr, |$arg:ident| $body:expr) => (
#[test]
fn $name() {
let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
let mut res = Response::new(&mut headers[..]);
let closure: Box<Fn(Response)> = Box::new($closure);
let status = res.parse($buf.as_ref());
assert_eq!(status, $len);
closure(res);

fn closure($arg: Response) {
$body
}
}
)
}
Expand All @@ -806,7 +808,7 @@ mod tests {
res! {
test_response_newlines,
b"HTTP/1.0 403 Forbidden\nServer: foo.bar\n\n",
|_| {}
|_r| {}
}

res! {
Expand Down Expand Up @@ -877,4 +879,13 @@ mod tests {
assert_eq!(parse_chunk_size(b"567f8a\rfoo"), Err(::InvalidChunkSize));
assert_eq!(parse_chunk_size(b"567xf8a\r\n"), Err(::InvalidChunkSize));
}

#[cfg(feature = "std")]
#[test]
fn test_std_error() {
use super::Error;
use std::error::Error as StdError;
let err = Error::HeaderName;
assert_eq!(err.to_string(), err.description());
}
}