initial commit

This commit is contained in:
Martin Algesten
2017-12-12 04:40:30 +01:00
parent f1b9318994
commit 4df9876c24
15 changed files with 2475 additions and 5 deletions

58
src/header.rs Normal file
View File

@@ -0,0 +1,58 @@
use ascii::AsciiString;
use error::Error;
use std::str::FromStr;
#[derive(Debug, Clone)]
/// Wrapper type for a header line.
pub struct Header {
line: AsciiString,
index: usize,
}
impl Header {
/// The header name.
///
/// ```
/// let header = "X-Forwarded-For: 127.0.0.1".parse::<ureq::Header>().unwrap();
/// assert_eq!("X-Forwarded-For", header.name());
/// ```
pub fn name(&self) -> &str {
&self.line.as_str()[0..self.index]
}
/// The header value.
///
/// ```
/// let header = "X-Forwarded-For: 127.0.0.1".parse::<ureq::Header>().unwrap();
/// assert_eq!("127.0.0.1", header.value());
/// ```
pub fn value(&self) -> &str {
&self.line.as_str()[self.index + 1..].trim()
}
/// Compares the given str to the header name ignoring case.
///
/// ```
/// let header = "X-Forwarded-For: 127.0.0.1".parse::<ureq::Header>().unwrap();
/// assert!(header.is_name("x-forwarded-for"));
/// ```
pub fn is_name(&self, other: &str) -> bool {
self.name().eq_ignore_ascii_case(other)
}
}
impl FromStr for Header {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
//
let line = AsciiString::from_str(s).map_err(|_| Error::BadHeader)?;
let index = s.find(":").ok_or_else(|| Error::BadHeader)?;
// no value?
if index >= s.len() {
return Err(Error::BadHeader);
}
Ok(Header { line, index })
}
}