mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 18:09:22 +02:00
add rustfmt config
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
use mime::{self, Mime};
|
||||
use header::{QualityItem, qitem};
|
||||
use header::{qitem, QualityItem};
|
||||
use http::header as http;
|
||||
use mime::{self, Mime};
|
||||
|
||||
header! {
|
||||
/// `Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2)
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{ACCEPT_CHARSET, Charset, QualityItem};
|
||||
use header::{Charset, QualityItem, ACCEPT_CHARSET};
|
||||
|
||||
header! {
|
||||
/// `Accept-Charset` header, defined in
|
||||
|
@ -1,5 +1,5 @@
|
||||
use header::{QualityItem, ACCEPT_LANGUAGE};
|
||||
use language_tags::LanguageTag;
|
||||
use header::{ACCEPT_LANGUAGE, QualityItem};
|
||||
|
||||
header! {
|
||||
/// `Accept-Language` header, defined in
|
||||
|
@ -1,8 +1,8 @@
|
||||
use header::{Header, IntoHeaderValue, Writer};
|
||||
use header::{fmt_comma_delimited, from_comma_delimited};
|
||||
use http::header;
|
||||
use std::fmt::{self, Write};
|
||||
use std::str::FromStr;
|
||||
use http::header;
|
||||
use header::{Header, IntoHeaderValue, Writer};
|
||||
use header::{from_comma_delimited, fmt_comma_delimited};
|
||||
|
||||
/// `Cache-Control` header, defined in [RFC7234](https://tools.ietf.org/html/rfc7234#section-5.2)
|
||||
///
|
||||
@ -30,9 +30,7 @@ use header::{from_comma_delimited, fmt_comma_delimited};
|
||||
/// use actix_web::http::header::{CacheControl, CacheDirective};
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.set(
|
||||
/// CacheControl(vec![CacheDirective::MaxAge(86400u32)])
|
||||
/// );
|
||||
/// builder.set(CacheControl(vec![CacheDirective::MaxAge(86400u32)]));
|
||||
/// ```
|
||||
///
|
||||
/// ```rust
|
||||
@ -40,15 +38,12 @@ use header::{from_comma_delimited, fmt_comma_delimited};
|
||||
/// use actix_web::http::header::{CacheControl, CacheDirective};
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.set(
|
||||
/// CacheControl(vec![
|
||||
/// CacheDirective::NoCache,
|
||||
/// CacheDirective::Private,
|
||||
/// CacheDirective::MaxAge(360u32),
|
||||
/// CacheDirective::Extension("foo".to_owned(),
|
||||
/// Some("bar".to_owned())),
|
||||
/// ])
|
||||
/// );
|
||||
/// builder.set(CacheControl(vec![
|
||||
/// CacheDirective::NoCache,
|
||||
/// CacheDirective::Private,
|
||||
/// CacheDirective::MaxAge(360u32),
|
||||
/// CacheDirective::Extension("foo".to_owned(), Some("bar".to_owned())),
|
||||
/// ]));
|
||||
/// ```
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
pub struct CacheControl(pub Vec<CacheDirective>);
|
||||
@ -63,7 +58,8 @@ impl Header for CacheControl {
|
||||
|
||||
#[inline]
|
||||
fn parse<T>(msg: &T) -> Result<Self, ::error::ParseError>
|
||||
where T: ::HttpMessage
|
||||
where
|
||||
T: ::HttpMessage,
|
||||
{
|
||||
let directives = from_comma_delimited(msg.headers().get_all(Self::name()))?;
|
||||
if !directives.is_empty() {
|
||||
@ -123,32 +119,36 @@ pub enum CacheDirective {
|
||||
SMaxAge(u32),
|
||||
|
||||
/// Extension directives. Optionally include an argument.
|
||||
Extension(String, Option<String>)
|
||||
Extension(String, Option<String>),
|
||||
}
|
||||
|
||||
impl fmt::Display for CacheDirective {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::CacheDirective::*;
|
||||
fmt::Display::fmt(match *self {
|
||||
NoCache => "no-cache",
|
||||
NoStore => "no-store",
|
||||
NoTransform => "no-transform",
|
||||
OnlyIfCached => "only-if-cached",
|
||||
fmt::Display::fmt(
|
||||
match *self {
|
||||
NoCache => "no-cache",
|
||||
NoStore => "no-store",
|
||||
NoTransform => "no-transform",
|
||||
OnlyIfCached => "only-if-cached",
|
||||
|
||||
MaxAge(secs) => return write!(f, "max-age={}", secs),
|
||||
MaxStale(secs) => return write!(f, "max-stale={}", secs),
|
||||
MinFresh(secs) => return write!(f, "min-fresh={}", secs),
|
||||
MaxAge(secs) => return write!(f, "max-age={}", secs),
|
||||
MaxStale(secs) => return write!(f, "max-stale={}", secs),
|
||||
MinFresh(secs) => return write!(f, "min-fresh={}", secs),
|
||||
|
||||
MustRevalidate => "must-revalidate",
|
||||
Public => "public",
|
||||
Private => "private",
|
||||
ProxyRevalidate => "proxy-revalidate",
|
||||
SMaxAge(secs) => return write!(f, "s-maxage={}", secs),
|
||||
MustRevalidate => "must-revalidate",
|
||||
Public => "public",
|
||||
Private => "private",
|
||||
ProxyRevalidate => "proxy-revalidate",
|
||||
SMaxAge(secs) => return write!(f, "s-maxage={}", secs),
|
||||
|
||||
Extension(ref name, None) => &name[..],
|
||||
Extension(ref name, Some(ref arg)) => return write!(f, "{}={}", name, arg),
|
||||
|
||||
}, f)
|
||||
Extension(ref name, None) => &name[..],
|
||||
Extension(ref name, Some(ref arg)) => {
|
||||
return write!(f, "{}={}", name, arg)
|
||||
}
|
||||
},
|
||||
f,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,16 +167,20 @@ impl FromStr for CacheDirective {
|
||||
"proxy-revalidate" => Ok(ProxyRevalidate),
|
||||
"" => Err(None),
|
||||
_ => match s.find('=') {
|
||||
Some(idx) if idx+1 < s.len() => match (&s[..idx], (&s[idx+1..]).trim_matches('"')) {
|
||||
("max-age" , secs) => secs.parse().map(MaxAge).map_err(Some),
|
||||
("max-stale", secs) => secs.parse().map(MaxStale).map_err(Some),
|
||||
("min-fresh", secs) => secs.parse().map(MinFresh).map_err(Some),
|
||||
("s-maxage", secs) => secs.parse().map(SMaxAge).map_err(Some),
|
||||
(left, right) => Ok(Extension(left.to_owned(), Some(right.to_owned())))
|
||||
},
|
||||
Some(idx) if idx + 1 < s.len() => {
|
||||
match (&s[..idx], (&s[idx + 1..]).trim_matches('"')) {
|
||||
("max-age", secs) => secs.parse().map(MaxAge).map_err(Some),
|
||||
("max-stale", secs) => secs.parse().map(MaxStale).map_err(Some),
|
||||
("min-fresh", secs) => secs.parse().map(MinFresh).map_err(Some),
|
||||
("s-maxage", secs) => secs.parse().map(SMaxAge).map_err(Some),
|
||||
(left, right) => {
|
||||
Ok(Extension(left.to_owned(), Some(right.to_owned())))
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(_) => Err(None),
|
||||
None => Ok(Extension(s.to_owned(), None))
|
||||
}
|
||||
None => Ok(Extension(s.to_owned(), None)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -189,38 +193,56 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_parse_multiple_headers() {
|
||||
let req = TestRequest::with_header(
|
||||
header::CACHE_CONTROL, "no-cache, private").finish();
|
||||
let req = TestRequest::with_header(header::CACHE_CONTROL, "no-cache, private")
|
||||
.finish();
|
||||
let cache = Header::parse(&req);
|
||||
assert_eq!(cache.ok(), Some(CacheControl(vec![CacheDirective::NoCache,
|
||||
CacheDirective::Private])))
|
||||
assert_eq!(
|
||||
cache.ok(),
|
||||
Some(CacheControl(vec![
|
||||
CacheDirective::NoCache,
|
||||
CacheDirective::Private,
|
||||
]))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_argument() {
|
||||
let req = TestRequest::with_header(
|
||||
header::CACHE_CONTROL, "max-age=100, private").finish();
|
||||
let req =
|
||||
TestRequest::with_header(header::CACHE_CONTROL, "max-age=100, private")
|
||||
.finish();
|
||||
let cache = Header::parse(&req);
|
||||
assert_eq!(cache.ok(), Some(CacheControl(vec![CacheDirective::MaxAge(100),
|
||||
CacheDirective::Private])))
|
||||
assert_eq!(
|
||||
cache.ok(),
|
||||
Some(CacheControl(vec![
|
||||
CacheDirective::MaxAge(100),
|
||||
CacheDirective::Private,
|
||||
]))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_quote_form() {
|
||||
let req = TestRequest::with_header(
|
||||
header::CACHE_CONTROL, "max-age=\"200\"").finish();
|
||||
let req =
|
||||
TestRequest::with_header(header::CACHE_CONTROL, "max-age=\"200\"").finish();
|
||||
let cache = Header::parse(&req);
|
||||
assert_eq!(cache.ok(), Some(CacheControl(vec![CacheDirective::MaxAge(200)])))
|
||||
assert_eq!(
|
||||
cache.ok(),
|
||||
Some(CacheControl(vec![CacheDirective::MaxAge(200)]))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_extension() {
|
||||
let req = TestRequest::with_header(
|
||||
header::CACHE_CONTROL, "foo, bar=baz").finish();
|
||||
let req =
|
||||
TestRequest::with_header(header::CACHE_CONTROL, "foo, bar=baz").finish();
|
||||
let cache = Header::parse(&req);
|
||||
assert_eq!(cache.ok(), Some(CacheControl(vec![
|
||||
CacheDirective::Extension("foo".to_owned(), None),
|
||||
CacheDirective::Extension("bar".to_owned(), Some("baz".to_owned()))])))
|
||||
assert_eq!(
|
||||
cache.ok(),
|
||||
Some(CacheControl(vec![
|
||||
CacheDirective::Extension("foo".to_owned(), None),
|
||||
CacheDirective::Extension("bar".to_owned(), Some("baz".to_owned())),
|
||||
]))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1,21 +1,21 @@
|
||||
use header::{QualityItem, CONTENT_LANGUAGE};
|
||||
use language_tags::LanguageTag;
|
||||
use header::{CONTENT_LANGUAGE, QualityItem};
|
||||
|
||||
header! {
|
||||
/// `Content-Language` header, defined in
|
||||
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)
|
||||
///
|
||||
///
|
||||
/// The `Content-Language` header field describes the natural language(s)
|
||||
/// of the intended audience for the representation. Note that this
|
||||
/// might not be equivalent to all the languages used within the
|
||||
/// representation.
|
||||
///
|
||||
///
|
||||
/// # ABNF
|
||||
///
|
||||
/// ```text
|
||||
/// Content-Language = 1#language-tag
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
/// # Example values
|
||||
///
|
||||
/// * `da`
|
||||
@ -28,7 +28,7 @@ header! {
|
||||
/// # #[macro_use] extern crate language_tags;
|
||||
/// use actix_web::HttpResponse;
|
||||
/// # use actix_web::http::header::{ContentLanguage, qitem};
|
||||
/// #
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.set(
|
||||
@ -46,7 +46,7 @@ header! {
|
||||
/// # use actix_web::http::header::{ContentLanguage, qitem};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
///
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.set(
|
||||
/// ContentLanguage(vec
|
||||
///
|
||||
/// # ABNF
|
||||
@ -99,7 +98,7 @@ pub enum ContentRangeSpec {
|
||||
range: Option<(u64, u64)>,
|
||||
|
||||
/// Total length of the instance, can be omitted if unknown
|
||||
instance_length: Option<u64>
|
||||
instance_length: Option<u64>,
|
||||
},
|
||||
|
||||
/// Custom range, with unit not registered at IANA
|
||||
@ -108,15 +107,15 @@ pub enum ContentRangeSpec {
|
||||
unit: String,
|
||||
|
||||
/// other-range-resp
|
||||
resp: String
|
||||
}
|
||||
resp: String,
|
||||
},
|
||||
}
|
||||
|
||||
fn split_in_two(s: &str, separator: char) -> Option<(&str, &str)> {
|
||||
let mut iter = s.splitn(2, separator);
|
||||
match (iter.next(), iter.next()) {
|
||||
(Some(a), Some(b)) => Some((a, b)),
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,40 +125,40 @@ impl FromStr for ContentRangeSpec {
|
||||
fn from_str(s: &str) -> Result<Self, ParseError> {
|
||||
let res = match split_in_two(s, ' ') {
|
||||
Some(("bytes", resp)) => {
|
||||
let (range, instance_length) = split_in_two(
|
||||
resp, '/').ok_or(ParseError::Header)?;
|
||||
let (range, instance_length) =
|
||||
split_in_two(resp, '/').ok_or(ParseError::Header)?;
|
||||
|
||||
let instance_length = if instance_length == "*" {
|
||||
None
|
||||
} else {
|
||||
Some(instance_length.parse()
|
||||
.map_err(|_| ParseError::Header)?)
|
||||
Some(instance_length
|
||||
.parse()
|
||||
.map_err(|_| ParseError::Header)?)
|
||||
};
|
||||
|
||||
let range = if range == "*" {
|
||||
None
|
||||
} else {
|
||||
let (first_byte, last_byte) = split_in_two(
|
||||
range, '-').ok_or(ParseError::Header)?;
|
||||
let first_byte = first_byte.parse()
|
||||
.map_err(|_| ParseError::Header)?;
|
||||
let last_byte = last_byte.parse()
|
||||
.map_err(|_| ParseError::Header)?;
|
||||
let (first_byte, last_byte) =
|
||||
split_in_two(range, '-').ok_or(ParseError::Header)?;
|
||||
let first_byte = first_byte.parse().map_err(|_| ParseError::Header)?;
|
||||
let last_byte = last_byte.parse().map_err(|_| ParseError::Header)?;
|
||||
if last_byte < first_byte {
|
||||
return Err(ParseError::Header);
|
||||
}
|
||||
Some((first_byte, last_byte))
|
||||
};
|
||||
|
||||
ContentRangeSpec::Bytes {range, instance_length}
|
||||
}
|
||||
Some((unit, resp)) => {
|
||||
ContentRangeSpec::Unregistered {
|
||||
unit: unit.to_owned(),
|
||||
resp: resp.to_owned()
|
||||
ContentRangeSpec::Bytes {
|
||||
range,
|
||||
instance_length,
|
||||
}
|
||||
}
|
||||
_ => return Err(ParseError::Header)
|
||||
Some((unit, resp)) => ContentRangeSpec::Unregistered {
|
||||
unit: unit.to_owned(),
|
||||
resp: resp.to_owned(),
|
||||
},
|
||||
_ => return Err(ParseError::Header),
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
@ -168,12 +167,15 @@ impl FromStr for ContentRangeSpec {
|
||||
impl Display for ContentRangeSpec {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ContentRangeSpec::Bytes { range, instance_length } => {
|
||||
ContentRangeSpec::Bytes {
|
||||
range,
|
||||
instance_length,
|
||||
} => {
|
||||
try!(f.write_str("bytes "));
|
||||
match range {
|
||||
Some((first_byte, last_byte)) => {
|
||||
try!(write!(f, "{}-{}", first_byte, last_byte));
|
||||
},
|
||||
}
|
||||
None => {
|
||||
try!(f.write_str("*"));
|
||||
}
|
||||
@ -185,7 +187,10 @@ impl Display for ContentRangeSpec {
|
||||
f.write_str("*")
|
||||
}
|
||||
}
|
||||
ContentRangeSpec::Unregistered { ref unit, ref resp } => {
|
||||
ContentRangeSpec::Unregistered {
|
||||
ref unit,
|
||||
ref resp,
|
||||
} => {
|
||||
try!(f.write_str(unit));
|
||||
try!(f.write_str(" "));
|
||||
f.write_str(resp)
|
||||
|
@ -1,6 +1,5 @@
|
||||
use mime::{self, Mime};
|
||||
use header::CONTENT_TYPE;
|
||||
|
||||
use mime::{self, Mime};
|
||||
|
||||
header! {
|
||||
/// `Content-Type` header, defined in
|
||||
@ -68,13 +67,15 @@ header! {
|
||||
}
|
||||
|
||||
impl ContentType {
|
||||
/// A constructor to easily create a `Content-Type: application/json` header.
|
||||
/// A constructor to easily create a `Content-Type: application/json`
|
||||
/// header.
|
||||
#[inline]
|
||||
pub fn json() -> ContentType {
|
||||
ContentType(mime::APPLICATION_JSON)
|
||||
}
|
||||
|
||||
/// A constructor to easily create a `Content-Type: text/plain; charset=utf-8` header.
|
||||
/// A constructor to easily create a `Content-Type: text/plain;
|
||||
/// charset=utf-8` header.
|
||||
#[inline]
|
||||
pub fn plaintext() -> ContentType {
|
||||
ContentType(mime::TEXT_PLAIN_UTF_8)
|
||||
@ -92,7 +93,8 @@ impl ContentType {
|
||||
ContentType(mime::TEXT_XML)
|
||||
}
|
||||
|
||||
/// A constructor to easily create a `Content-Type: application/www-form-url-encoded` header.
|
||||
/// A constructor to easily create a `Content-Type:
|
||||
/// application/www-form-url-encoded` header.
|
||||
#[inline]
|
||||
pub fn form_url_encoded() -> ContentType {
|
||||
ContentType(mime::APPLICATION_WWW_FORM_URLENCODED)
|
||||
@ -109,7 +111,8 @@ impl ContentType {
|
||||
ContentType(mime::IMAGE_PNG)
|
||||
}
|
||||
|
||||
/// A constructor to easily create a `Content-Type: application/octet-stream` header.
|
||||
/// A constructor to easily create a `Content-Type:
|
||||
/// application/octet-stream` header.
|
||||
#[inline]
|
||||
pub fn octet_stream() -> ContentType {
|
||||
ContentType(mime::APPLICATION_OCTET_STREAM)
|
||||
|
@ -1,6 +1,5 @@
|
||||
use header::{HttpDate, DATE};
|
||||
use std::time::SystemTime;
|
||||
use header::{DATE, HttpDate};
|
||||
|
||||
|
||||
header! {
|
||||
/// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2)
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{ETAG, EntityTag};
|
||||
use header::{EntityTag, ETAG};
|
||||
|
||||
header! {
|
||||
/// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3)
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{EXPIRES, HttpDate};
|
||||
use header::{HttpDate, EXPIRES};
|
||||
|
||||
header! {
|
||||
/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{IF_MATCH, EntityTag};
|
||||
use header::{EntityTag, IF_MATCH};
|
||||
|
||||
header! {
|
||||
/// `If-Match` header, defined in
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{IF_MODIFIED_SINCE, HttpDate};
|
||||
use header::{HttpDate, IF_MODIFIED_SINCE};
|
||||
|
||||
header! {
|
||||
/// `If-Modified-Since` header, defined in
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{IF_NONE_MATCH, EntityTag};
|
||||
use header::{EntityTag, IF_NONE_MATCH};
|
||||
|
||||
header! {
|
||||
/// `If-None-Match` header, defined in
|
||||
@ -66,8 +66,8 @@ header! {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::IfNoneMatch;
|
||||
use header::{EntityTag, Header, IF_NONE_MATCH};
|
||||
use test::TestRequest;
|
||||
use header::{IF_NONE_MATCH, Header, EntityTag};
|
||||
|
||||
#[test]
|
||||
fn test_if_none_match() {
|
||||
@ -77,8 +77,9 @@ mod tests {
|
||||
if_none_match = Header::parse(&req);
|
||||
assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Any));
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
IF_NONE_MATCH, &b"\"foobar\", W/\"weak-etag\""[..]).finish();
|
||||
let req =
|
||||
TestRequest::with_header(IF_NONE_MATCH, &b"\"foobar\", W/\"weak-etag\""[..])
|
||||
.finish();
|
||||
|
||||
if_none_match = Header::parse(&req);
|
||||
let mut entities: Vec<EntityTag> = Vec::new();
|
||||
|
@ -1,10 +1,10 @@
|
||||
use std::fmt::{self, Display, Write};
|
||||
use error::ParseError;
|
||||
use httpmessage::HttpMessage;
|
||||
use http::header;
|
||||
use header::from_one_raw_str;
|
||||
use header::{IntoHeaderValue, Header, HeaderName, HeaderValue,
|
||||
EntityTag, HttpDate, Writer, InvalidHeaderValueBytes};
|
||||
use header::{EntityTag, Header, HeaderName, HeaderValue, HttpDate, IntoHeaderValue,
|
||||
InvalidHeaderValueBytes, Writer};
|
||||
use http::header;
|
||||
use httpmessage::HttpMessage;
|
||||
use std::fmt::{self, Display, Write};
|
||||
|
||||
/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2)
|
||||
///
|
||||
@ -36,16 +36,19 @@ use header::{IntoHeaderValue, Header, HeaderName, HeaderValue,
|
||||
///
|
||||
/// ```rust
|
||||
/// use actix_web::HttpResponse;
|
||||
/// use actix_web::http::header::{IfRange, EntityTag};
|
||||
/// use actix_web::http::header::{EntityTag, IfRange};
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.set(IfRange::EntityTag(EntityTag::new(false, "xyzzy".to_owned())));
|
||||
/// builder.set(IfRange::EntityTag(EntityTag::new(
|
||||
/// false,
|
||||
/// "xyzzy".to_owned(),
|
||||
/// )));
|
||||
/// ```
|
||||
///
|
||||
/// ```rust
|
||||
/// use actix_web::HttpResponse;
|
||||
/// use actix_web::http::header::IfRange;
|
||||
/// use std::time::{SystemTime, Duration};
|
||||
/// use std::time::{Duration, SystemTime};
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// let fetched = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
|
||||
@ -64,7 +67,9 @@ impl Header for IfRange {
|
||||
header::IF_RANGE
|
||||
}
|
||||
#[inline]
|
||||
fn parse<T>(msg: &T) -> Result<Self, ParseError> where T: HttpMessage
|
||||
fn parse<T>(msg: &T) -> Result<Self, ParseError>
|
||||
where
|
||||
T: HttpMessage,
|
||||
{
|
||||
let etag: Result<EntityTag, _> =
|
||||
from_one_raw_str(msg.headers().get(header::IF_RANGE));
|
||||
@ -99,12 +104,11 @@ impl IntoHeaderValue for IfRange {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_if_range {
|
||||
use std::str;
|
||||
use header::*;
|
||||
use super::IfRange as HeaderField;
|
||||
use header::*;
|
||||
use std::str;
|
||||
test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
test_header!(test2, vec![b"\"xyzzy\""]);
|
||||
test_header!(test3, vec![b"this-is-invalid"], None::<IfRange>);
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{IF_UNMODIFIED_SINCE, HttpDate};
|
||||
use header::{HttpDate, IF_UNMODIFIED_SINCE};
|
||||
|
||||
header! {
|
||||
/// `If-Unmodified-Since` header, defined in
|
||||
|
@ -1,4 +1,4 @@
|
||||
use header::{LAST_MODIFIED, HttpDate};
|
||||
use header::{HttpDate, LAST_MODIFIED};
|
||||
|
||||
header! {
|
||||
/// `Last-Modified` header, defined in
|
||||
|
@ -5,6 +5,7 @@
|
||||
//! Several header fields use MIME values for their contents. Keeping with the
|
||||
//! strongly-typed theme, the [mime](https://docs.rs/mime) crate
|
||||
//! is used, such as `ContentType(pub Mime)`.
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
pub use self::accept_charset::AcceptCharset;
|
||||
//pub use self::accept_encoding::AcceptEncoding;
|
||||
|
@ -1,8 +1,8 @@
|
||||
use std::fmt::{self, Display};
|
||||
use std::str::FromStr;
|
||||
|
||||
use header::parsing::from_one_raw_str;
|
||||
use header::{Header, Raw};
|
||||
use header::parsing::{from_one_raw_str};
|
||||
|
||||
/// `Range` header, defined in [RFC7233](https://tools.ietf.org/html/rfc7233#section-3.1)
|
||||
///
|
||||
@ -65,7 +65,7 @@ pub enum Range {
|
||||
Bytes(Vec<ByteRangeSpec>),
|
||||
/// Custom range, with unit not registered at IANA
|
||||
/// (`other-range-unit`: String , `other-range-set`: String)
|
||||
Unregistered(String, String)
|
||||
Unregistered(String, String),
|
||||
}
|
||||
|
||||
/// Each `Range::Bytes` header can contain one or more `ByteRangeSpecs`.
|
||||
@ -77,25 +77,25 @@ pub enum ByteRangeSpec {
|
||||
/// Get all bytes starting from x ("x-")
|
||||
AllFrom(u64),
|
||||
/// Get last x bytes ("-x")
|
||||
Last(u64)
|
||||
Last(u64),
|
||||
}
|
||||
|
||||
impl ByteRangeSpec {
|
||||
/// Given the full length of the entity, attempt to normalize the byte range
|
||||
/// into an satisfiable end-inclusive (from, to) range.
|
||||
///
|
||||
/// The resulting range is guaranteed to be a satisfiable range within the bounds
|
||||
/// of `0 <= from <= to < full_length`.
|
||||
/// The resulting range is guaranteed to be a satisfiable range within the
|
||||
/// bounds of `0 <= from <= to < full_length`.
|
||||
///
|
||||
/// If the byte range is deemed unsatisfiable, `None` is returned.
|
||||
/// An unsatisfiable range is generally cause for a server to either reject
|
||||
/// the client request with a `416 Range Not Satisfiable` status code, or to
|
||||
/// simply ignore the range header and serve the full entity using a `200 OK`
|
||||
/// status code.
|
||||
/// simply ignore the range header and serve the full entity using a `200
|
||||
/// OK` status code.
|
||||
///
|
||||
/// This function closely follows [RFC 7233][1] section 2.1.
|
||||
/// As such, it considers ranges to be satisfiable if they meet the following
|
||||
/// conditions:
|
||||
/// As such, it considers ranges to be satisfiable if they meet the
|
||||
/// following conditions:
|
||||
///
|
||||
/// > If a valid byte-range-set includes at least one byte-range-spec with
|
||||
/// a first-byte-pos that is less than the current length of the
|
||||
@ -125,14 +125,14 @@ impl ByteRangeSpec {
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
&ByteRangeSpec::AllFrom(from) => {
|
||||
if from < full_length {
|
||||
Some((from, full_length - 1))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
&ByteRangeSpec::Last(last) => {
|
||||
if last > 0 {
|
||||
// From the RFC: If the selected representation is shorter
|
||||
@ -160,11 +160,15 @@ impl Range {
|
||||
/// Get byte range header with multiple subranges
|
||||
/// ("bytes=from1-to1,from2-to2,fromX-toX")
|
||||
pub fn bytes_multi(ranges: Vec<(u64, u64)>) -> Range {
|
||||
Range::Bytes(ranges.iter().map(|r| ByteRangeSpec::FromTo(r.0, r.1)).collect())
|
||||
Range::Bytes(
|
||||
ranges
|
||||
.iter()
|
||||
.map(|r| ByteRangeSpec::FromTo(r.0, r.1))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl fmt::Display for ByteRangeSpec {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
@ -175,7 +179,6 @@ impl fmt::Display for ByteRangeSpec {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl fmt::Display for Range {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
@ -189,10 +192,10 @@ impl fmt::Display for Range {
|
||||
try!(Display::fmt(range, f));
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
Range::Unregistered(ref unit, ref range_str) => {
|
||||
write!(f, "{}={}", unit, range_str)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -211,11 +214,10 @@ impl FromStr for Range {
|
||||
}
|
||||
Ok(Range::Bytes(ranges))
|
||||
}
|
||||
(Some(unit), Some(range_str)) if unit != "" && range_str != "" => {
|
||||
Ok(Range::Unregistered(unit.to_owned(), range_str.to_owned()))
|
||||
|
||||
},
|
||||
_ => Err(::Error::Header)
|
||||
(Some(unit), Some(range_str)) if unit != "" && range_str != "" => Ok(
|
||||
Range::Unregistered(unit.to_owned(), range_str.to_owned()),
|
||||
),
|
||||
_ => Err(::Error::Header),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -227,19 +229,20 @@ impl FromStr for ByteRangeSpec {
|
||||
let mut parts = s.splitn(2, '-');
|
||||
|
||||
match (parts.next(), parts.next()) {
|
||||
(Some(""), Some(end)) => {
|
||||
end.parse().or(Err(::Error::Header)).map(ByteRangeSpec::Last)
|
||||
},
|
||||
(Some(start), Some("")) => {
|
||||
start.parse().or(Err(::Error::Header)).map(ByteRangeSpec::AllFrom)
|
||||
},
|
||||
(Some(start), Some(end)) => {
|
||||
match (start.parse(), end.parse()) {
|
||||
(Ok(start), Ok(end)) if start <= end => Ok(ByteRangeSpec::FromTo(start, end)),
|
||||
_ => Err(::Error::Header)
|
||||
(Some(""), Some(end)) => end.parse()
|
||||
.or(Err(::Error::Header))
|
||||
.map(ByteRangeSpec::Last),
|
||||
(Some(start), Some("")) => start
|
||||
.parse()
|
||||
.or(Err(::Error::Header))
|
||||
.map(ByteRangeSpec::AllFrom),
|
||||
(Some(start), Some(end)) => match (start.parse(), end.parse()) {
|
||||
(Ok(start), Ok(end)) if start <= end => {
|
||||
Ok(ByteRangeSpec::FromTo(start, end))
|
||||
}
|
||||
_ => Err(::Error::Header),
|
||||
},
|
||||
_ => Err(::Error::Header)
|
||||
_ => Err(::Error::Header),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -248,14 +251,13 @@ fn from_comma_delimited<T: FromStr>(s: &str) -> Vec<T> {
|
||||
s.split(',')
|
||||
.filter_map(|x| match x.trim() {
|
||||
"" => None,
|
||||
y => Some(y)
|
||||
y => Some(y),
|
||||
})
|
||||
.filter_map(|x| x.parse().ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl Header for Range {
|
||||
|
||||
fn header_name() -> &'static str {
|
||||
static NAME: &'static str = "Range";
|
||||
NAME
|
||||
@ -268,51 +270,52 @@ impl Header for Range {
|
||||
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
|
||||
f.fmt_line(self)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_bytes_range_valid() {
|
||||
let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap();
|
||||
let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap();
|
||||
let r3 = Range::bytes(1, 100);
|
||||
let r3 = Range::bytes(1, 100);
|
||||
assert_eq!(r, r2);
|
||||
assert_eq!(r2, r3);
|
||||
|
||||
let r: Range = Header::parse_header(&"bytes=1-100,200-".into()).unwrap();
|
||||
let r2: Range = Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap();
|
||||
let r3 = Range::Bytes(
|
||||
vec![ByteRangeSpec::FromTo(1, 100), ByteRangeSpec::AllFrom(200)]
|
||||
);
|
||||
let r2: Range =
|
||||
Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap();
|
||||
let r3 = Range::Bytes(vec![
|
||||
ByteRangeSpec::FromTo(1, 100),
|
||||
ByteRangeSpec::AllFrom(200),
|
||||
]);
|
||||
assert_eq!(r, r2);
|
||||
assert_eq!(r2, r3);
|
||||
|
||||
let r: Range = Header::parse_header(&"bytes=1-100,-100".into()).unwrap();
|
||||
let r2: Range = Header::parse_header(&"bytes=1-100, ,,-100".into()).unwrap();
|
||||
let r3 = Range::Bytes(
|
||||
vec![ByteRangeSpec::FromTo(1, 100), ByteRangeSpec::Last(100)]
|
||||
);
|
||||
let r3 = Range::Bytes(vec![
|
||||
ByteRangeSpec::FromTo(1, 100),
|
||||
ByteRangeSpec::Last(100),
|
||||
]);
|
||||
assert_eq!(r, r2);
|
||||
assert_eq!(r2, r3);
|
||||
|
||||
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
|
||||
assert_eq!(r, r2);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_unregistered_range_valid() {
|
||||
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
|
||||
assert_eq!(r, r2);
|
||||
|
||||
let r: Range = Header::parse_header(&"custom=abcd".into()).unwrap();
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "abcd".to_owned());
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "abcd".to_owned());
|
||||
assert_eq!(r, r2);
|
||||
|
||||
let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap();
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned());
|
||||
let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned());
|
||||
assert_eq!(r, r2);
|
||||
}
|
||||
|
||||
@ -346,10 +349,10 @@ fn test_fmt() {
|
||||
|
||||
let mut headers = Headers::new();
|
||||
|
||||
headers.set(
|
||||
Range::Bytes(
|
||||
vec![ByteRangeSpec::FromTo(0, 1000), ByteRangeSpec::AllFrom(2000)]
|
||||
));
|
||||
headers.set(Range::Bytes(vec![
|
||||
ByteRangeSpec::FromTo(0, 1000),
|
||||
ByteRangeSpec::AllFrom(2000),
|
||||
]));
|
||||
assert_eq!(&headers.to_string(), "Range: bytes=0-1000,2000-\r\n");
|
||||
|
||||
headers.clear();
|
||||
@ -358,30 +361,74 @@ fn test_fmt() {
|
||||
assert_eq!(&headers.to_string(), "Range: bytes=\r\n");
|
||||
|
||||
headers.clear();
|
||||
headers.set(Range::Unregistered("custom".to_owned(), "1-xxx".to_owned()));
|
||||
headers.set(Range::Unregistered(
|
||||
"custom".to_owned(),
|
||||
"1-xxx".to_owned(),
|
||||
));
|
||||
|
||||
assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_range_spec_to_satisfiable_range() {
|
||||
assert_eq!(Some((0, 0)), ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3));
|
||||
assert_eq!(Some((1, 2)), ByteRangeSpec::FromTo(1, 2).to_satisfiable_range(3));
|
||||
assert_eq!(Some((1, 2)), ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0));
|
||||
assert_eq!(
|
||||
Some((0, 0)),
|
||||
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
Some((1, 2)),
|
||||
ByteRangeSpec::FromTo(1, 2).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
Some((1, 2)),
|
||||
ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
None,
|
||||
ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
None,
|
||||
ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
None,
|
||||
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0)
|
||||
);
|
||||
|
||||
assert_eq!(Some((0, 2)), ByteRangeSpec::AllFrom(0).to_satisfiable_range(3));
|
||||
assert_eq!(Some((2, 2)), ByteRangeSpec::AllFrom(2).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::AllFrom(3).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::AllFrom(5).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::AllFrom(0).to_satisfiable_range(0));
|
||||
assert_eq!(
|
||||
Some((0, 2)),
|
||||
ByteRangeSpec::AllFrom(0).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
Some((2, 2)),
|
||||
ByteRangeSpec::AllFrom(2).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
None,
|
||||
ByteRangeSpec::AllFrom(3).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
None,
|
||||
ByteRangeSpec::AllFrom(5).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
None,
|
||||
ByteRangeSpec::AllFrom(0).to_satisfiable_range(0)
|
||||
);
|
||||
|
||||
assert_eq!(Some((1, 2)), ByteRangeSpec::Last(2).to_satisfiable_range(3));
|
||||
assert_eq!(Some((2, 2)), ByteRangeSpec::Last(1).to_satisfiable_range(3));
|
||||
assert_eq!(Some((0, 2)), ByteRangeSpec::Last(5).to_satisfiable_range(3));
|
||||
assert_eq!(
|
||||
Some((1, 2)),
|
||||
ByteRangeSpec::Last(2).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
Some((2, 2)),
|
||||
ByteRangeSpec::Last(1).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(
|
||||
Some((0, 2)),
|
||||
ByteRangeSpec::Last(5).to_satisfiable_range(3)
|
||||
);
|
||||
assert_eq!(None, ByteRangeSpec::Last(0).to_satisfiable_range(3));
|
||||
assert_eq!(None, ByteRangeSpec::Last(2).to_satisfiable_range(0));
|
||||
}
|
||||
|
||||
|
@ -5,9 +5,9 @@ use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use modhttp::{Error as HttpError};
|
||||
use modhttp::header::GetAll;
|
||||
use mime::Mime;
|
||||
use modhttp::Error as HttpError;
|
||||
use modhttp::header::GetAll;
|
||||
|
||||
pub use modhttp::header::*;
|
||||
|
||||
@ -21,11 +21,12 @@ pub use self::common::*;
|
||||
#[doc(hidden)]
|
||||
pub use self::shared::*;
|
||||
|
||||
|
||||
#[doc(hidden)]
|
||||
/// A trait for any object that will represent a header field and value.
|
||||
pub trait Header where Self: IntoHeaderValue {
|
||||
|
||||
pub trait Header
|
||||
where
|
||||
Self: IntoHeaderValue,
|
||||
{
|
||||
/// Returns the name of the header field
|
||||
fn name() -> HeaderName;
|
||||
|
||||
@ -112,7 +113,7 @@ pub enum ContentEncoding {
|
||||
/// Automatically select encoding based on encoding negotiation
|
||||
Auto,
|
||||
/// A format using the Brotli algorithm
|
||||
#[cfg(feature="brotli")]
|
||||
#[cfg(feature = "brotli")]
|
||||
Br,
|
||||
/// A format using the zlib structure with deflate algorithm
|
||||
Deflate,
|
||||
@ -123,19 +124,18 @@ pub enum ContentEncoding {
|
||||
}
|
||||
|
||||
impl ContentEncoding {
|
||||
|
||||
#[inline]
|
||||
pub fn is_compression(&self) -> bool {
|
||||
match *self {
|
||||
ContentEncoding::Identity | ContentEncoding::Auto => false,
|
||||
_ => true
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match *self {
|
||||
#[cfg(feature="brotli")]
|
||||
#[cfg(feature = "brotli")]
|
||||
ContentEncoding::Br => "br",
|
||||
ContentEncoding::Gzip => "gzip",
|
||||
ContentEncoding::Deflate => "deflate",
|
||||
@ -147,7 +147,7 @@ impl ContentEncoding {
|
||||
/// default quality value
|
||||
pub fn quality(&self) -> f64 {
|
||||
match *self {
|
||||
#[cfg(feature="brotli")]
|
||||
#[cfg(feature = "brotli")]
|
||||
ContentEncoding::Br => 1.1,
|
||||
ContentEncoding::Gzip => 1.0,
|
||||
ContentEncoding::Deflate => 0.9,
|
||||
@ -160,7 +160,7 @@ impl ContentEncoding {
|
||||
impl<'a> From<&'a str> for ContentEncoding {
|
||||
fn from(s: &'a str) -> ContentEncoding {
|
||||
match s.trim().to_lowercase().as_ref() {
|
||||
#[cfg(feature="brotli")]
|
||||
#[cfg(feature = "brotli")]
|
||||
"br" => ContentEncoding::Br,
|
||||
"gzip" => ContentEncoding::Gzip,
|
||||
"deflate" => ContentEncoding::Deflate,
|
||||
@ -176,7 +176,9 @@ pub(crate) struct Writer {
|
||||
|
||||
impl Writer {
|
||||
fn new() -> Writer {
|
||||
Writer{buf: BytesMut::new()}
|
||||
Writer {
|
||||
buf: BytesMut::new(),
|
||||
}
|
||||
}
|
||||
fn take(&mut self) -> Bytes {
|
||||
self.buf.take().freeze()
|
||||
@ -199,18 +201,20 @@ impl fmt::Write for Writer {
|
||||
#[inline]
|
||||
#[doc(hidden)]
|
||||
/// Reads a comma-delimited raw header into a Vec.
|
||||
pub fn from_comma_delimited<T: FromStr>(all: GetAll<HeaderValue>)
|
||||
-> Result<Vec<T>, ParseError>
|
||||
{
|
||||
pub fn from_comma_delimited<T: FromStr>(
|
||||
all: GetAll<HeaderValue>
|
||||
) -> Result<Vec<T>, ParseError> {
|
||||
let mut result = Vec::new();
|
||||
for h in all {
|
||||
let s = h.to_str().map_err(|_| ParseError::Header)?;
|
||||
result.extend(s.split(',')
|
||||
.filter_map(|x| match x.trim() {
|
||||
"" => None,
|
||||
y => Some(y)
|
||||
})
|
||||
.filter_map(|x| x.trim().parse().ok()))
|
||||
result.extend(
|
||||
s.split(',')
|
||||
.filter_map(|x| match x.trim() {
|
||||
"" => None,
|
||||
y => Some(y),
|
||||
})
|
||||
.filter_map(|x| x.trim().parse().ok()),
|
||||
)
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
@ -218,13 +222,11 @@ pub fn from_comma_delimited<T: FromStr>(all: GetAll<HeaderValue>)
|
||||
#[inline]
|
||||
#[doc(hidden)]
|
||||
/// Reads a single string when parsing a header.
|
||||
pub fn from_one_raw_str<T: FromStr>(val: Option<&HeaderValue>)
|
||||
-> Result<T, ParseError>
|
||||
{
|
||||
pub fn from_one_raw_str<T: FromStr>(val: Option<&HeaderValue>) -> Result<T, ParseError> {
|
||||
if let Some(line) = val {
|
||||
let line = line.to_str().map_err(|_| ParseError::Header)?;
|
||||
if !line.is_empty() {
|
||||
return T::from_str(line).or(Err(ParseError::Header))
|
||||
return T::from_str(line).or(Err(ParseError::Header));
|
||||
}
|
||||
}
|
||||
Err(ParseError::Header)
|
||||
@ -234,7 +236,8 @@ pub fn from_one_raw_str<T: FromStr>(val: Option<&HeaderValue>)
|
||||
#[doc(hidden)]
|
||||
/// Format an array into a comma-delimited string.
|
||||
pub fn fmt_comma_delimited<T>(f: &mut fmt::Formatter, parts: &[T]) -> fmt::Result
|
||||
where T: fmt::Display
|
||||
where
|
||||
T: fmt::Display,
|
||||
{
|
||||
let mut iter = parts.iter();
|
||||
if let Some(part) = iter.next() {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![allow(unused, deprecated)]
|
||||
use std::ascii::AsciiExt;
|
||||
use std::fmt::{self, Display};
|
||||
use std::str::FromStr;
|
||||
use std::ascii::AsciiExt;
|
||||
|
||||
use self::Charset::*;
|
||||
|
||||
@ -12,9 +12,9 @@ use self::Charset::*;
|
||||
/// See [http://www.iana.org/assignments/character-sets/character-sets.xhtml][url].
|
||||
///
|
||||
/// [url]: http://www.iana.org/assignments/character-sets/character-sets.xhtml
|
||||
#[derive(Clone,Debug,PartialEq)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum Charset{
|
||||
pub enum Charset {
|
||||
/// US ASCII
|
||||
Us_Ascii,
|
||||
/// ISO-8859-1
|
||||
@ -64,7 +64,7 @@ pub enum Charset{
|
||||
/// KOI8-R
|
||||
Koi8_R,
|
||||
/// An arbitrary charset specified as a string
|
||||
Ext(String)
|
||||
Ext(String),
|
||||
}
|
||||
|
||||
impl Charset {
|
||||
@ -94,7 +94,7 @@ impl Charset {
|
||||
Gb2312 => "GB2312",
|
||||
Big5 => "5",
|
||||
Koi8_R => "KOI8-R",
|
||||
Ext(ref s) => s
|
||||
Ext(ref s) => s,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -133,18 +133,18 @@ impl FromStr for Charset {
|
||||
"GB2312" => Gb2312,
|
||||
"5" => Big5,
|
||||
"KOI8-R" => Koi8_R,
|
||||
s => Ext(s.to_owned())
|
||||
s => Ext(s.to_owned()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
assert_eq!(Us_Ascii,"us-ascii".parse().unwrap());
|
||||
assert_eq!(Us_Ascii,"US-Ascii".parse().unwrap());
|
||||
assert_eq!(Us_Ascii,"US-ASCII".parse().unwrap());
|
||||
assert_eq!(Shift_Jis,"Shift-JIS".parse().unwrap());
|
||||
assert_eq!(Ext("ABCD".to_owned()),"abcd".parse().unwrap());
|
||||
assert_eq!(Us_Ascii, "us-ascii".parse().unwrap());
|
||||
assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap());
|
||||
assert_eq!(Us_Ascii, "US-ASCII".parse().unwrap());
|
||||
assert_eq!(Shift_Jis, "Shift-JIS".parse().unwrap());
|
||||
assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1,7 +1,8 @@
|
||||
use std::fmt;
|
||||
use std::str;
|
||||
|
||||
pub use self::Encoding::{Chunked, Brotli, Gzip, Deflate, Compress, Identity, EncodingExt, Trailers};
|
||||
pub use self::Encoding::{Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip,
|
||||
Identity, Trailers};
|
||||
|
||||
/// A value to represent an encoding used in `Transfer-Encoding`
|
||||
/// or `Accept-Encoding` header.
|
||||
@ -22,7 +23,7 @@ pub enum Encoding {
|
||||
/// The `trailers` encoding.
|
||||
Trailers,
|
||||
/// Some other encoding that is less common, can be any String.
|
||||
EncodingExt(String)
|
||||
EncodingExt(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for Encoding {
|
||||
@ -35,7 +36,7 @@ impl fmt::Display for Encoding {
|
||||
Compress => "compress",
|
||||
Identity => "identity",
|
||||
Trailers => "trailers",
|
||||
EncodingExt(ref s) => s.as_ref()
|
||||
EncodingExt(ref s) => s.as_ref(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -51,7 +52,7 @@ impl str::FromStr for Encoding {
|
||||
"compress" => Ok(Compress),
|
||||
"identity" => Ok(Identity),
|
||||
"trailers" => Ok(Trailers),
|
||||
_ => Ok(EncodingExt(s.to_owned()))
|
||||
_ => Ok(EncodingExt(s.to_owned())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,23 @@
|
||||
use std::str::FromStr;
|
||||
use header::{HeaderValue, IntoHeaderValue, InvalidHeaderValueBytes, Writer};
|
||||
use std::fmt::{self, Display, Write};
|
||||
use header::{HeaderValue, Writer, IntoHeaderValue, InvalidHeaderValueBytes};
|
||||
use std::str::FromStr;
|
||||
|
||||
/// check that each char in the slice is either:
|
||||
/// 1. `%x21`, or
|
||||
/// 2. in the range `%x23` to `%x7E`, or
|
||||
/// 3. above `%x80`
|
||||
fn check_slice_validity(slice: &str) -> bool {
|
||||
slice.bytes().all(|c|
|
||||
c == b'\x21' || (c >= b'\x23' && c <= b'\x7e') | (c >= b'\x80'))
|
||||
slice
|
||||
.bytes()
|
||||
.all(|c| c == b'\x21' || (c >= b'\x23' && c <= b'\x7e') | (c >= b'\x80'))
|
||||
}
|
||||
|
||||
/// An entity tag, defined in [RFC7232](https://tools.ietf.org/html/rfc7232#section-2.3)
|
||||
///
|
||||
/// An entity tag consists of a string enclosed by two literal double quotes.
|
||||
/// Preceding the first double quote is an optional weakness indicator,
|
||||
/// which always looks like `W/`. Examples for valid tags are `"xyzzy"` and `W/"xyzzy"`.
|
||||
/// which always looks like `W/`. Examples for valid tags are `"xyzzy"` and
|
||||
/// `W/"xyzzy"`.
|
||||
///
|
||||
/// # ABNF
|
||||
///
|
||||
@ -28,9 +30,9 @@ fn check_slice_validity(slice: &str) -> bool {
|
||||
/// ```
|
||||
///
|
||||
/// # Comparison
|
||||
/// To check if two entity tags are equivalent in an application always use the `strong_eq` or
|
||||
/// `weak_eq` methods based on the context of the Tag. Only use `==` to check if two tags are
|
||||
/// identical.
|
||||
/// To check if two entity tags are equivalent in an application always use the
|
||||
/// `strong_eq` or `weak_eq` methods based on the context of the Tag. Only use
|
||||
/// `==` to check if two tags are identical.
|
||||
///
|
||||
/// The example below shows the results for a set of entity-tag pairs and
|
||||
/// both the weak and strong comparison function results:
|
||||
@ -46,7 +48,7 @@ pub struct EntityTag {
|
||||
/// Weakness indicator for the tag
|
||||
pub weak: bool,
|
||||
/// The opaque string in between the DQUOTEs
|
||||
tag: String
|
||||
tag: String,
|
||||
}
|
||||
|
||||
impl EntityTag {
|
||||
@ -85,8 +87,8 @@ impl EntityTag {
|
||||
self.tag = tag
|
||||
}
|
||||
|
||||
/// For strong comparison two entity-tags are equivalent if both are not weak and their
|
||||
/// opaque-tags match character-by-character.
|
||||
/// For strong comparison two entity-tags are equivalent if both are not
|
||||
/// weak and their opaque-tags match character-by-character.
|
||||
pub fn strong_eq(&self, other: &EntityTag) -> bool {
|
||||
!self.weak && !other.weak && self.tag == other.tag
|
||||
}
|
||||
@ -131,13 +133,21 @@ impl FromStr for EntityTag {
|
||||
}
|
||||
// The etag is weak if its first char is not a DQUOTE.
|
||||
if slice.len() >= 2 && slice.starts_with('"')
|
||||
&& check_slice_validity(&slice[1..length-1]) {
|
||||
&& check_slice_validity(&slice[1..length - 1])
|
||||
{
|
||||
// No need to check if the last char is a DQUOTE,
|
||||
// we already did that above.
|
||||
return Ok(EntityTag { weak: false, tag: slice[1..length-1].to_owned() });
|
||||
return Ok(EntityTag {
|
||||
weak: false,
|
||||
tag: slice[1..length - 1].to_owned(),
|
||||
});
|
||||
} else if slice.len() >= 4 && slice.starts_with("W/\"")
|
||||
&& check_slice_validity(&slice[3..length-1]) {
|
||||
return Ok(EntityTag { weak: true, tag: slice[3..length-1].to_owned() });
|
||||
&& check_slice_validity(&slice[3..length - 1])
|
||||
{
|
||||
return Ok(EntityTag {
|
||||
weak: true,
|
||||
tag: slice[3..length - 1].to_owned(),
|
||||
});
|
||||
}
|
||||
Err(::error::ParseError::Header)
|
||||
}
|
||||
@ -149,7 +159,7 @@ impl IntoHeaderValue for EntityTag {
|
||||
fn try_into(self) -> Result<HeaderValue, Self::Error> {
|
||||
let mut wrt = Writer::new();
|
||||
write!(wrt, "{}", self).unwrap();
|
||||
unsafe{Ok(HeaderValue::from_shared_unchecked(wrt.take()))}
|
||||
unsafe { Ok(HeaderValue::from_shared_unchecked(wrt.take())) }
|
||||
}
|
||||
}
|
||||
|
||||
@ -160,22 +170,37 @@ mod tests {
|
||||
#[test]
|
||||
fn test_etag_parse_success() {
|
||||
// Expected success
|
||||
assert_eq!("\"foobar\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::strong("foobar".to_owned()));
|
||||
assert_eq!("\"\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::strong("".to_owned()));
|
||||
assert_eq!("W/\"weaktag\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("weaktag".to_owned()));
|
||||
assert_eq!("W/\"\x65\x62\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("\x65\x62".to_owned()));
|
||||
assert_eq!("W/\"\"".parse::<EntityTag>().unwrap(), EntityTag::weak("".to_owned()));
|
||||
assert_eq!(
|
||||
"\"foobar\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::strong("foobar".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"\"\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::strong("".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"W/\"weaktag\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("weaktag".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"W/\"\x65\x62\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("\x65\x62".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"W/\"\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("".to_owned())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_etag_parse_failures() {
|
||||
// Expected failures
|
||||
assert!("no-dquotes".parse::<EntityTag>().is_err());
|
||||
assert!("w/\"the-first-w-is-case-sensitive\"".parse::<EntityTag>().is_err());
|
||||
assert!(
|
||||
"w/\"the-first-w-is-case-sensitive\""
|
||||
.parse::<EntityTag>()
|
||||
.is_err()
|
||||
);
|
||||
assert!("".parse::<EntityTag>().is_err());
|
||||
assert!("\"unmatched-dquotes1".parse::<EntityTag>().is_err());
|
||||
assert!("unmatched-dquotes2\"".parse::<EntityTag>().is_err());
|
||||
@ -184,11 +209,26 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_etag_fmt() {
|
||||
assert_eq!(format!("{}", EntityTag::strong("foobar".to_owned())), "\"foobar\"");
|
||||
assert_eq!(format!("{}", EntityTag::strong("".to_owned())), "\"\"");
|
||||
assert_eq!(format!("{}", EntityTag::weak("weak-etag".to_owned())), "W/\"weak-etag\"");
|
||||
assert_eq!(format!("{}", EntityTag::weak("\u{0065}".to_owned())), "W/\"\x65\"");
|
||||
assert_eq!(format!("{}", EntityTag::weak("".to_owned())), "W/\"\"");
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::strong("foobar".to_owned())),
|
||||
"\"foobar\""
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::strong("".to_owned())),
|
||||
"\"\""
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::weak("weak-etag".to_owned())),
|
||||
"W/\"weak-etag\""
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::weak("\u{0065}".to_owned())),
|
||||
"W/\"\x65\""
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::weak("".to_owned())),
|
||||
"W/\"\""
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -3,14 +3,13 @@ use std::io::Write;
|
||||
use std::str::FromStr;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use time;
|
||||
use bytes::{BytesMut, BufMut};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use http::header::{HeaderValue, InvalidHeaderValueBytes};
|
||||
use time;
|
||||
|
||||
use error::ParseError;
|
||||
use header::IntoHeaderValue;
|
||||
|
||||
|
||||
/// A timestamp with HTTP formatting and parsing
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct HttpDate(time::Tm);
|
||||
@ -19,11 +18,10 @@ impl FromStr for HttpDate {
|
||||
type Err = ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<HttpDate, ParseError> {
|
||||
match time::strptime(s, "%a, %d %b %Y %T %Z").or_else(|_| {
|
||||
time::strptime(s, "%A, %d-%b-%y %T %Z")
|
||||
}).or_else(|_| {
|
||||
time::strptime(s, "%c")
|
||||
}) {
|
||||
match time::strptime(s, "%a, %d %b %Y %T %Z")
|
||||
.or_else(|_| time::strptime(s, "%A, %d-%b-%y %T %Z"))
|
||||
.or_else(|_| time::strptime(s, "%c"))
|
||||
{
|
||||
Ok(t) => Ok(HttpDate(t)),
|
||||
Err(_) => Err(ParseError::Header),
|
||||
}
|
||||
@ -47,11 +45,14 @@ impl From<SystemTime> for HttpDate {
|
||||
let tmspec = match sys.duration_since(UNIX_EPOCH) {
|
||||
Ok(dur) => {
|
||||
time::Timespec::new(dur.as_secs() as i64, dur.subsec_nanos() as i32)
|
||||
},
|
||||
}
|
||||
Err(err) => {
|
||||
let neg = err.duration();
|
||||
time::Timespec::new(-(neg.as_secs() as i64), -(neg.subsec_nanos() as i32))
|
||||
},
|
||||
time::Timespec::new(
|
||||
-(neg.as_secs() as i64),
|
||||
-(neg.subsec_nanos() as i32),
|
||||
)
|
||||
}
|
||||
};
|
||||
HttpDate(time::at_utc(tmspec))
|
||||
}
|
||||
@ -63,7 +64,11 @@ impl IntoHeaderValue for HttpDate {
|
||||
fn try_into(self) -> Result<HeaderValue, Self::Error> {
|
||||
let mut wrt = BytesMut::with_capacity(29).writer();
|
||||
write!(wrt, "{}", self.0.rfc822()).unwrap();
|
||||
unsafe{Ok(HeaderValue::from_shared_unchecked(wrt.get_mut().take().freeze()))}
|
||||
unsafe {
|
||||
Ok(HeaderValue::from_shared_unchecked(
|
||||
wrt.get_mut().take().freeze(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,18 +85,43 @@ impl From<HttpDate> for SystemTime {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use time::Tm;
|
||||
use super::HttpDate;
|
||||
use time::Tm;
|
||||
|
||||
const NOV_07: HttpDate = HttpDate(Tm {
|
||||
tm_nsec: 0, tm_sec: 37, tm_min: 48, tm_hour: 8, tm_mday: 7, tm_mon: 10, tm_year: 94,
|
||||
tm_wday: 0, tm_isdst: 0, tm_yday: 0, tm_utcoff: 0});
|
||||
tm_nsec: 0,
|
||||
tm_sec: 37,
|
||||
tm_min: 48,
|
||||
tm_hour: 8,
|
||||
tm_mday: 7,
|
||||
tm_mon: 10,
|
||||
tm_year: 94,
|
||||
tm_wday: 0,
|
||||
tm_isdst: 0,
|
||||
tm_yday: 0,
|
||||
tm_utcoff: 0,
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn test_date() {
|
||||
assert_eq!("Sun, 07 Nov 1994 08:48:37 GMT".parse::<HttpDate>().unwrap(), NOV_07);
|
||||
assert_eq!("Sunday, 07-Nov-94 08:48:37 GMT".parse::<HttpDate>().unwrap(), NOV_07);
|
||||
assert_eq!("Sun Nov 7 08:48:37 1994".parse::<HttpDate>().unwrap(), NOV_07);
|
||||
assert_eq!(
|
||||
"Sun, 07 Nov 1994 08:48:37 GMT"
|
||||
.parse::<HttpDate>()
|
||||
.unwrap(),
|
||||
NOV_07
|
||||
);
|
||||
assert_eq!(
|
||||
"Sunday, 07-Nov-94 08:48:37 GMT"
|
||||
.parse::<HttpDate>()
|
||||
.unwrap(),
|
||||
NOV_07
|
||||
);
|
||||
assert_eq!(
|
||||
"Sun Nov 7 08:48:37 1994"
|
||||
.parse::<HttpDate>()
|
||||
.unwrap(),
|
||||
NOV_07
|
||||
);
|
||||
assert!("this-is-no-date".parse::<HttpDate>().is_err());
|
||||
}
|
||||
}
|
||||
|
@ -4,11 +4,11 @@ pub use self::charset::Charset;
|
||||
pub use self::encoding::Encoding;
|
||||
pub use self::entity::EntityTag;
|
||||
pub use self::httpdate::HttpDate;
|
||||
pub use self::quality_item::{q, qitem, Quality, QualityItem};
|
||||
pub use language_tags::LanguageTag;
|
||||
pub use self::quality_item::{Quality, QualityItem, qitem, q};
|
||||
|
||||
mod charset;
|
||||
mod entity;
|
||||
mod encoding;
|
||||
mod entity;
|
||||
mod httpdate;
|
||||
mod quality_item;
|
||||
|
@ -13,11 +13,13 @@ use self::internal::IntoQuality;
|
||||
///
|
||||
/// # Implementation notes
|
||||
///
|
||||
/// The quality value is defined as a number between 0 and 1 with three decimal places. This means
|
||||
/// there are 1001 possible values. Since floating point numbers are not exact and the smallest
|
||||
/// floating point data type (`f32`) consumes four bytes, hyper uses an `u16` value to store the
|
||||
/// quality internally. For performance reasons you may set quality directly to a value between
|
||||
/// 0 and 1000 e.g. `Quality(532)` matches the quality `q=0.532`.
|
||||
/// The quality value is defined as a number between 0 and 1 with three decimal
|
||||
/// places. This means there are 1001 possible values. Since floating point
|
||||
/// numbers are not exact and the smallest floating point data type (`f32`)
|
||||
/// consumes four bytes, hyper uses an `u16` value to store the
|
||||
/// quality internally. For performance reasons you may set quality directly to
|
||||
/// a value between 0 and 1000 e.g. `Quality(532)` matches the quality
|
||||
/// `q=0.532`.
|
||||
///
|
||||
/// [RFC7231 Section 5.3.1](https://tools.ietf.org/html/rfc7231#section-5.3.1)
|
||||
/// gives more information on quality values in HTTP header fields.
|
||||
@ -61,7 +63,11 @@ impl<T: fmt::Display> fmt::Display for QualityItem<T> {
|
||||
match self.quality.0 {
|
||||
1000 => Ok(()),
|
||||
0 => f.write_str("; q=0"),
|
||||
x => write!(f, "; q=0.{}", format!("{:03}", x).trim_right_matches('0'))
|
||||
x => write!(
|
||||
f,
|
||||
"; q=0.{}",
|
||||
format!("{:03}", x).trim_right_matches('0')
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -96,7 +102,7 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
|
||||
} else {
|
||||
return Err(::error::ParseError::Header);
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(_) => return Err(::error::ParseError::Header),
|
||||
}
|
||||
}
|
||||
@ -114,7 +120,10 @@ fn from_f32(f: f32) -> Quality {
|
||||
// this function is only used internally. A check that `f` is within range
|
||||
// should be done before calling this method. Just in case, this
|
||||
// debug_assert should catch if we were forgetful
|
||||
debug_assert!(f >= 0f32 && f <= 1f32, "q value must be between 0.0 and 1.0");
|
||||
debug_assert!(
|
||||
f >= 0f32 && f <= 1f32,
|
||||
"q value must be between 0.0 and 1.0"
|
||||
);
|
||||
Quality((f * 1000f32) as u16)
|
||||
}
|
||||
|
||||
@ -125,7 +134,7 @@ pub fn qitem<T>(item: T) -> QualityItem<T> {
|
||||
}
|
||||
|
||||
/// Convenience function to create a `Quality` from a float or integer.
|
||||
///
|
||||
///
|
||||
/// Implemented for `u16` and `f32`. Panics if value is out of range.
|
||||
pub fn q<T: IntoQuality>(val: T) -> Quality {
|
||||
val.into_quality()
|
||||
@ -147,7 +156,10 @@ mod internal {
|
||||
|
||||
impl IntoQuality for f32 {
|
||||
fn into_quality(self) -> Quality {
|
||||
assert!(self >= 0f32 && self <= 1f32, "float must be between 0.0 and 1.0");
|
||||
assert!(
|
||||
self >= 0f32 && self <= 1f32,
|
||||
"float must be between 0.0 and 1.0"
|
||||
);
|
||||
super::from_f32(self)
|
||||
}
|
||||
}
|
||||
@ -159,7 +171,6 @@ mod internal {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub trait Sealed {}
|
||||
impl Sealed for u16 {}
|
||||
impl Sealed for f32 {}
|
||||
@ -167,8 +178,8 @@ mod internal {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::super::encoding::*;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_quality_item_fmt_q_1() {
|
||||
@ -183,7 +194,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_quality_item_fmt_q_05() {
|
||||
// Custom value
|
||||
let x = QualityItem{
|
||||
let x = QualityItem {
|
||||
item: EncodingExt("identity".to_owned()),
|
||||
quality: Quality(500),
|
||||
};
|
||||
@ -193,7 +204,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_quality_item_fmt_q_0() {
|
||||
// Custom value
|
||||
let x = QualityItem{
|
||||
let x = QualityItem {
|
||||
item: EncodingExt("identity".to_owned()),
|
||||
quality: Quality(0),
|
||||
};
|
||||
@ -203,22 +214,46 @@ mod tests {
|
||||
#[test]
|
||||
fn test_quality_item_from_str1() {
|
||||
let x: Result<QualityItem<Encoding>, _> = "chunked".parse();
|
||||
assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: Quality(1000), });
|
||||
assert_eq!(
|
||||
x.unwrap(),
|
||||
QualityItem {
|
||||
item: Chunked,
|
||||
quality: Quality(1000),
|
||||
}
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_quality_item_from_str2() {
|
||||
let x: Result<QualityItem<Encoding>, _> = "chunked; q=1".parse();
|
||||
assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: Quality(1000), });
|
||||
assert_eq!(
|
||||
x.unwrap(),
|
||||
QualityItem {
|
||||
item: Chunked,
|
||||
quality: Quality(1000),
|
||||
}
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_quality_item_from_str3() {
|
||||
let x: Result<QualityItem<Encoding>, _> = "gzip; q=0.5".parse();
|
||||
assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: Quality(500), });
|
||||
assert_eq!(
|
||||
x.unwrap(),
|
||||
QualityItem {
|
||||
item: Gzip,
|
||||
quality: Quality(500),
|
||||
}
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_quality_item_from_str4() {
|
||||
let x: Result<QualityItem<Encoding>, _> = "gzip; q=0.273".parse();
|
||||
assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: Quality(273), });
|
||||
assert_eq!(
|
||||
x.unwrap(),
|
||||
QualityItem {
|
||||
item: Gzip,
|
||||
quality: Quality(273),
|
||||
}
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_quality_item_from_str5() {
|
||||
@ -245,14 +280,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
#[should_panic] // FIXME - 32-bit msvc unwinding broken
|
||||
#[cfg_attr(all(target_arch="x86", target_env="msvc"), ignore)]
|
||||
#[cfg_attr(all(target_arch = "x86", target_env = "msvc"), ignore)]
|
||||
fn test_quality_invalid() {
|
||||
q(-1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic] // FIXME - 32-bit msvc unwinding broken
|
||||
#[cfg_attr(all(target_arch="x86", target_env="msvc"), ignore)]
|
||||
#[cfg_attr(all(target_arch = "x86", target_env = "msvc"), ignore)]
|
||||
fn test_quality_invalid2() {
|
||||
q(2.0);
|
||||
}
|
||||
@ -260,6 +295,10 @@ mod tests {
|
||||
#[test]
|
||||
fn test_fuzzing_bugs() {
|
||||
assert!("99999;".parse::<QualityItem<String>>().is_err());
|
||||
assert!("\x0d;;;=\u{d6aa}==".parse::<QualityItem<String>>().is_err())
|
||||
assert!(
|
||||
"\x0d;;;=\u{d6aa}=="
|
||||
.parse::<QualityItem<String>>()
|
||||
.is_err()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user