refactor(lib): update unicase to 2.0

BREAKING CHANGE: Some headers used `UniCase`, but now use
  `unicase::Ascii`. Upgrade code to `Ascii::new(s)`.
This commit is contained in:
Sean McArthur
2017-02-01 15:14:18 -08:00
parent fed04dfb58
commit c81edd41d7
12 changed files with 69 additions and 72 deletions

View File

@@ -32,7 +32,7 @@ tokio-core = "0.1.6"
tokio-proto = "0.1"
tokio-service = "0.1"
tokio-io = "0.1"
unicase = "1.0"
unicase = "2.0"
url = "1.0"
[dev-dependencies]

View File

@@ -1,6 +1,6 @@
use std::fmt::{self, Display};
use std::str;
use unicase::UniCase;
use unicase;
use header::{Header, Raw};
/// `Access-Control-Allow-Credentials` header, part of
@@ -38,7 +38,7 @@ use header::{Header, Raw};
#[derive(Clone, PartialEq, Debug)]
pub struct AccessControlAllowCredentials;
const ACCESS_CONTROL_ALLOW_CREDENTIALS_TRUE: UniCase<&'static str> = UniCase("true");
const ACCESS_CONTROL_ALLOW_CREDENTIALS_TRUE: &'static str = "true";
impl Header for AccessControlAllowCredentials {
fn header_name() -> &'static str {
@@ -56,7 +56,7 @@ impl Header for AccessControlAllowCredentials {
// None. No big deal.
str::from_utf8_unchecked(line)
};
if UniCase(text) == ACCESS_CONTROL_ALLOW_CREDENTIALS_TRUE {
if unicase::eq_ascii(text, ACCESS_CONTROL_ALLOW_CREDENTIALS_TRUE) {
return Ok(AccessControlAllowCredentials);
}
}

View File

@@ -1,4 +1,4 @@
use unicase::UniCase;
use unicase::Ascii;
header! {
/// `Access-Control-Allow-Headers` header, part of
@@ -24,11 +24,11 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, AccessControlAllowHeaders};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// AccessControlAllowHeaders(vec![UniCase("date".to_owned())])
/// AccessControlAllowHeaders(vec![Ascii::new("date".to_owned())])
/// );
/// # }
/// ```
@@ -39,18 +39,18 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, AccessControlAllowHeaders};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// AccessControlAllowHeaders(vec![
/// UniCase("accept-language".to_owned()),
/// UniCase("date".to_owned()),
/// Ascii::new("accept-language".to_owned()),
/// Ascii::new("date".to_owned()),
/// ])
/// );
/// # }
/// ```
(AccessControlAllowHeaders, "Access-Control-Allow-Headers") => (UniCase<String>)*
(AccessControlAllowHeaders, "Access-Control-Allow-Headers") => (Ascii<String>)*
test_access_control_allow_headers {
test_header!(test1, vec![b"accept-language, date"]);

View File

@@ -1,4 +1,4 @@
use unicase::UniCase;
use unicase::Ascii;
header! {
/// `Access-Control-Expose-Headers` header, part of
@@ -23,13 +23,13 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, AccessControlExposeHeaders};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// AccessControlExposeHeaders(vec![
/// UniCase("etag".to_owned()),
/// UniCase("content-length".to_owned())
/// Ascii::new("etag".to_owned()),
/// Ascii::new("content-length".to_owned())
/// ])
/// );
/// # }
@@ -41,18 +41,18 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, AccessControlExposeHeaders};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// AccessControlExposeHeaders(vec![
/// UniCase("etag".to_owned()),
/// UniCase("content-length".to_owned())
/// Ascii::new("etag".to_owned()),
/// Ascii::new("content-length".to_owned())
/// ])
/// );
/// # }
/// ```
(AccessControlExposeHeaders, "Access-Control-Expose-Headers") => (UniCase<String>)*
(AccessControlExposeHeaders, "Access-Control-Expose-Headers") => (Ascii<String>)*
test_access_control_expose_headers {
test_header!(test1, vec![b"etag, content-length"]);

View File

@@ -1,4 +1,4 @@
use unicase::UniCase;
use unicase::Ascii;
header! {
/// `Access-Control-Request-Headers` header, part of
@@ -24,11 +24,11 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, AccessControlRequestHeaders};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// AccessControlRequestHeaders(vec![UniCase("date".to_owned())])
/// AccessControlRequestHeaders(vec![Ascii::new("date".to_owned())])
/// );
/// # }
/// ```
@@ -39,18 +39,18 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, AccessControlRequestHeaders};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// AccessControlRequestHeaders(vec![
/// UniCase("accept-language".to_owned()),
/// UniCase("date".to_owned()),
/// Ascii::new("accept-language".to_owned()),
/// Ascii::new("date".to_owned()),
/// ])
/// );
/// # }
/// ```
(AccessControlRequestHeaders, "Access-Control-Request-Headers") => (UniCase<String>)*
(AccessControlRequestHeaders, "Access-Control-Request-Headers") => (Ascii<String>)*
test_access_control_request_headers {
test_header!(test1, vec![b"accept-language, date"]);

View File

@@ -1,11 +1,11 @@
use std::fmt::{self, Display};
use std::str::FromStr;
use unicase::UniCase;
use unicase::Ascii;
pub use self::ConnectionOption::{KeepAlive, Close, ConnectionHeader};
const KEEP_ALIVE: UniCase<&'static str> = UniCase("keep-alive");
const CLOSE: UniCase<&'static str> = UniCase("close");
static KEEP_ALIVE: &'static str = "keep-alive";
static CLOSE: &'static str = "close";
/// Values that can be in the `Connection` header.
#[derive(Clone, PartialEq, Debug)]
@@ -22,18 +22,18 @@ pub enum ConnectionOption {
// TODO: it would be nice if these "Strings" could be stronger types, since
// they are supposed to relate to other Header fields (which we have strong
// types for).
ConnectionHeader(UniCase<String>),
ConnectionHeader(Ascii<String>),
}
impl FromStr for ConnectionOption {
type Err = ();
fn from_str(s: &str) -> Result<ConnectionOption, ()> {
if UniCase(s) == KEEP_ALIVE {
if Ascii::new(s) == KEEP_ALIVE {
Ok(KeepAlive)
} else if UniCase(s) == CLOSE {
} else if Ascii::new(s) == CLOSE {
Ok(Close)
} else {
Ok(ConnectionHeader(UniCase(s.to_owned())))
Ok(ConnectionHeader(Ascii::new(s.to_owned())))
}
}
}
@@ -43,7 +43,7 @@ impl Display for ConnectionOption {
f.write_str(match *self {
KeepAlive => "keep-alive",
Close => "close",
ConnectionHeader(UniCase(ref s)) => s.as_ref()
ConnectionHeader(ref s) => s.as_ref()
})
}
}
@@ -83,12 +83,12 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, Connection, ConnectionOption};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// Connection(vec![
/// ConnectionOption::ConnectionHeader(UniCase("upgrade".to_owned())),
/// ConnectionOption::ConnectionHeader(Ascii::new("upgrade".to_owned())),
/// ])
/// );
/// # }
@@ -124,7 +124,7 @@ bench_header!(header, Connection, { vec![b"authorization".to_vec()] });
mod tests {
use super::{Connection,ConnectionHeader};
use header::Header;
use unicase::UniCase;
use unicase::Ascii;
fn parse_option(header: Vec<u8>) -> Connection {
let val = header.into();
@@ -137,7 +137,7 @@ mod tests {
assert_eq!(Connection::close(),parse_option(b"close".to_vec()));
assert_eq!(Connection::keep_alive(),parse_option(b"keep-alive".to_vec()));
assert_eq!(Connection::keep_alive(),parse_option(b"Keep-Alive".to_vec()));
assert_eq!(Connection(vec![ConnectionHeader(UniCase("upgrade".to_owned()))]),
assert_eq!(Connection(vec![ConnectionHeader(Ascii::new("upgrade".to_owned()))]),
parse_option(b"upgrade".to_vec()));
}
}

View File

@@ -8,7 +8,7 @@
use language_tags::LanguageTag;
use std::fmt;
use unicase::UniCase;
use unicase;
use header::{Header, Raw, parsing};
use header::parsing::{parse_extended_value, http_percent_encode};
@@ -102,9 +102,9 @@ impl Header for ContentDisposition {
};
let mut cd = ContentDisposition {
disposition: if UniCase(&*disposition) == UniCase("inline") {
disposition: if unicase::eq_ascii(&*disposition, "inline") {
DispositionType::Inline
} else if UniCase(&*disposition) == UniCase("attachment") {
} else if unicase::eq_ascii(&*disposition, "attachment") {
DispositionType::Attachment
} else {
DispositionType::Ext(disposition.to_owned())
@@ -128,11 +128,11 @@ impl Header for ContentDisposition {
};
cd.parameters.push(
if UniCase(&*key) == UniCase("filename") {
if unicase::eq_ascii(&*key, "filename") {
DispositionParam::Filename(
Charset::Ext("UTF-8".to_owned()), None,
val.trim_matches('"').as_bytes().to_owned())
} else if UniCase(&*key) == UniCase("filename*") {
} else if unicase::eq_ascii(&*key, "filename*") {
let extended_value = try!(parse_extended_value(val));
DispositionParam::Filename(extended_value.charset, extended_value.language_tag, extended_value.value)
} else {
@@ -164,7 +164,7 @@ impl fmt::Display for ContentDisposition {
let mut use_simple_format: bool = false;
if opt_lang.is_none() {
if let Charset::Ext(ref ext) = *charset {
if UniCase(&**ext) == UniCase("utf-8") {
if unicase::eq_ascii(&**ext, "utf-8") {
use_simple_format = true;
}
}

View File

@@ -1,7 +1,7 @@
use std::fmt;
use std::str;
use unicase::UniCase;
use unicase;
use header::{Header, Raw};
@@ -26,8 +26,6 @@ pub enum Expect {
Continue
}
const EXPECT_CONTINUE: UniCase<&'static str> = UniCase("100-continue");
impl Header for Expect {
fn header_name() -> &'static str {
static NAME: &'static str = "Expect";
@@ -44,7 +42,7 @@ impl Header for Expect {
// None. No big deal.
str::from_utf8_unchecked(line)
};
if UniCase(text) == EXPECT_CONTINUE {
if unicase::eq_ascii(text, "100-continue") {
Ok(Expect::Continue)
} else {
Err(::Error::Header)

View File

@@ -1,7 +1,7 @@
use std::fmt;
use std::str::{self, FromStr};
use unicase::UniCase;
use unicase;
use header::{Header, Raw, parsing};
@@ -85,13 +85,13 @@ impl FromStr for StrictTransportSecurity {
fn from_str(s: &str) -> ::Result<StrictTransportSecurity> {
s.split(';')
.map(str::trim)
.map(|sub| if UniCase(sub) == UniCase("includeSubdomains") {
.map(|sub| if unicase::eq_ascii(sub, "includeSubdomains") {
Ok(Directive::IncludeSubdomains)
} else {
let mut sub = sub.splitn(2, '=');
match (sub.next(), sub.next()) {
(Some(left), Some(right))
if UniCase(left.trim()) == UniCase("max-age") => {
if unicase::eq_ascii(left.trim(), "max-age") => {
right
.trim()
.trim_matches('"')

View File

@@ -1,6 +1,6 @@
use std::fmt::{self, Display};
use std::str::FromStr;
use unicase::UniCase;
use unicase;
header! {
/// `Upgrade` header, defined in [RFC7230](http://tools.ietf.org/html/rfc7230#section-6.7)
@@ -99,7 +99,7 @@ impl FromStr for ProtocolName {
"TLS" => ProtocolName::Tls,
"h2c" => ProtocolName::H2c,
_ => {
if UniCase(s) == UniCase("websocket") {
if unicase::eq_ascii(s, "websocket") {
ProtocolName::WebSocket
} else {
ProtocolName::Unregistered(s.to_owned())

View File

@@ -1,4 +1,4 @@
use unicase::UniCase;
use unicase::Ascii;
header! {
/// `Vary` header, defined in [RFC7231](https://tools.ietf.org/html/rfc7231#section-7.1.4)
@@ -34,18 +34,18 @@ header! {
/// // extern crate unicase;
///
/// use hyper::header::{Headers, Vary};
/// use unicase::UniCase;
/// use unicase::Ascii;
///
/// let mut headers = Headers::new();
/// headers.set(
/// Vary::Items(vec![
/// UniCase("accept-encoding".to_owned()),
/// UniCase("accept-language".to_owned()),
/// Ascii::new("accept-encoding".to_owned()),
/// Ascii::new("accept-language".to_owned()),
/// ])
/// );
/// # }
/// ```
(Vary, "Vary") => {Any / (UniCase<String>)+}
(Vary, "Vary") => {Any / (Ascii<String>)+}
test_vary {
test_header!(test1, vec![b"accept-encoding, accept-language"]);

View File

@@ -80,7 +80,7 @@ use std::borrow::{Cow, ToOwned};
use std::iter::{FromIterator, IntoIterator};
use std::{mem, fmt};
use unicase::UniCase;
use unicase::Ascii;
use self::internals::{Item, VecMap, Entry};
use self::sealed::{GetType, HeaderClone};
@@ -329,7 +329,7 @@ macro_rules! literals {
match s.len() {
$($len => {
$(
if UniCase(<$header>::header_name()) == s {
if Ascii::new(<$header>::header_name()) == Ascii::new(s) {
return Cow::Borrowed(<$header>::header_name());
}
)+
@@ -390,19 +390,19 @@ impl Headers {
/// The field is determined by the type of the value being set.
pub fn set<H: Header>(&mut self, value: H) {
trace!("Headers.set( {:?}, {:?} )", header_name::<H>(), HeaderValueString(&value));
self.data.insert(HeaderName(UniCase(Cow::Borrowed(header_name::<H>()))),
self.data.insert(HeaderName(Ascii::new(Cow::Borrowed(header_name::<H>()))),
Item::new_typed(Box::new(value)));
}
/// Get a reference to the header field's value, if it exists.
pub fn get<H: Header>(&self) -> Option<&H> {
self.data.get(&HeaderName(UniCase(Cow::Borrowed(header_name::<H>()))))
self.data.get(&HeaderName(Ascii::new(Cow::Borrowed(header_name::<H>()))))
.and_then(Item::typed::<H>)
}
/// Get a mutable reference to the header field's value, if it exists.
pub fn get_mut<H: Header>(&mut self) -> Option<&mut H> {
self.data.get_mut(&HeaderName(UniCase(Cow::Borrowed(header_name::<H>()))))
self.data.get_mut(&HeaderName(Ascii::new(Cow::Borrowed(header_name::<H>()))))
.and_then(Item::typed_mut::<H>)
}
@@ -418,7 +418,7 @@ impl Headers {
/// assert!(headers.has::<ContentType>());
/// ```
pub fn has<H: Header>(&self) -> bool {
self.data.contains_key(&HeaderName(UniCase(Cow::Borrowed(header_name::<H>()))))
self.data.contains_key(&HeaderName(Ascii::new(Cow::Borrowed(header_name::<H>()))))
}
/// Removes a header from the map, if one existed.
@@ -428,7 +428,7 @@ impl Headers {
/// know whether a header exists, rather rely on `has`.
pub fn remove<H: Header>(&mut self) -> Option<H> {
trace!("Headers.remove( {:?} )", header_name::<H>());
self.data.remove(&HeaderName(UniCase(Cow::Borrowed(header_name::<H>()))))
self.data.remove(&HeaderName(Ascii::new(Cow::Borrowed(header_name::<H>()))))
.and_then(Item::into_typed::<H>)
}
@@ -484,7 +484,7 @@ impl Headers {
let name = name.into();
let value = value.into();
trace!("Headers.set_raw( {:?}, {:?} )", name, value);
self.data.insert(HeaderName(UniCase(name)), Item::new_raw(value));
self.data.insert(HeaderName(Ascii::new(name)), Item::new_raw(value));
}
/// Append a value to raw value of this header.
@@ -506,7 +506,7 @@ impl Headers {
let name = name.into();
let value = value.into();
trace!("Headers.append_raw( {:?}, {:?} )", name, value);
let name = HeaderName(UniCase(name));
let name = HeaderName(Ascii::new(name));
if let Some(item) = self.data.get_mut(&name) {
item.raw_mut().push(value);
return;
@@ -520,7 +520,6 @@ impl Headers {
self.data.remove(name);
}
}
impl PartialEq for Headers {
@@ -577,7 +576,7 @@ impl<'a> HeaderView<'a> {
/// Check if a HeaderView is a certain Header.
#[inline]
pub fn is<H: Header>(&self) -> bool {
HeaderName(UniCase(Cow::Borrowed(header_name::<H>()))) == *self.0
HeaderName(Ascii::new(Cow::Borrowed(header_name::<H>()))) == *self.0
}
/// Get the Header name as a slice.
@@ -633,7 +632,7 @@ impl<'a> Extend<HeaderView<'a>> for Headers {
impl<'a> Extend<(&'a str, Bytes)> for Headers {
fn extend<I: IntoIterator<Item=(&'a str, Bytes)>>(&mut self, iter: I) {
for (name, value) in iter {
let name = HeaderName(UniCase(maybe_literal(name)));
let name = HeaderName(Ascii::new(maybe_literal(name)));
//let trim = header.value.iter().rev().take_while(|&&x| x == b' ').count();
match self.data.entry(name) {
@@ -657,7 +656,7 @@ impl<'a> FromIterator<HeaderView<'a>> for Headers {
}
#[derive(Clone, Debug)]
struct HeaderName(UniCase<Cow<'static, str>>);
struct HeaderName(Ascii<Cow<'static, str>>);
impl fmt::Display for HeaderName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -667,7 +666,7 @@ impl fmt::Display for HeaderName {
impl AsRef<str> for HeaderName {
fn as_ref(&self) -> &str {
((self.0).0).as_ref()
self.0.as_ref()
}
}