Skip to content
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ license = "MIT/Apache-2.0"
description = "A tiny, safe, speedy, zero-copy HTTP/1.x parser."
repository = "https://github.com/seanmonstar/httparse"

[dependencies]
unicase = "1"

[features]
default = ["std"]
std = []
Expand Down
23 changes: 21 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
//! intrinsic, and simd, are stabilized in rustc.

#[cfg(feature = "std")] extern crate std as core;
extern crate unicase;

use core::{fmt, result, str, slice};

use iter::Bytes;

use unicase::UniCase;

mod iter;

macro_rules! next {
Expand Down Expand Up @@ -249,6 +251,24 @@ impl<'h, 'b> Request<'h, 'b> {
}
}

/// Extract header value using key. If not found or can't be converted to &str then None else the &str value.
pub fn header(&self, key: &str) -> Option<&str> {
let value: &str;

for h in self.headers.iter() {
if UniCase(h.name) == UniCase(key) {
value = str::from_utf8(h.value).unwrap_or("");
if value.is_empty() {
return None;
} else {
return Some(value);
}
}
}

None
}

/// Try to parse a buffer of bytes into the Request.
pub fn parse(&mut self, buf: &'b [u8]) -> Result<usize> {
let orig_len = buf.len();
Expand Down Expand Up @@ -345,7 +365,6 @@ impl<'h, 'b> Response<'h, 'b> {
_ => return Err(Error::Status),
}


let len = orig_len - bytes.len();
let headers_len = complete!(parse_headers_iter(&mut self.headers, &mut bytes));
Ok(Status::Complete(len + headers_len))
Expand Down