diff --git a/Cargo.toml b/Cargo.toml index 8f92415..8165110 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] diff --git a/src/lib.rs b/src/lib.rs index 235453d..3ad3ed6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { @@ -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 { let orig_len = buf.len(); @@ -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))