Merge pull request #419 from pyfisch/refactorheaders1

refactor(headers): Introduce header!() macro, improve documentation
This commit is contained in:
Sean McArthur
2015-04-02 10:34:34 -07:00
19 changed files with 367 additions and 225 deletions

View File

@@ -2,32 +2,32 @@ use mime::Mime;
use header::QualityItem; use header::QualityItem;
/// The `Accept` header. header! {
/// #[doc="`Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2)"]
/// The `Accept` header is used to tell a server which content-types the client #[doc=""]
/// is capable of using. It can be a comma-separated list of `Mime`s, and the #[doc="The `Accept` header field can be used by user agents to specify"]
/// priority can be indicated with a `q` parameter. #[doc="response media types that are acceptable. Accept header fields can"]
/// #[doc="be used to indicate that the request is specifically limited to a"]
/// Example: #[doc="small set of desired types, as in the case of a request for an"]
/// #[doc="in-line image"]
/// ``` #[doc=""]
/// # use hyper::header::Headers; #[doc="# ABNF"]
/// # use hyper::header::Accept; #[doc="```plain"]
/// # use hyper::header::qitem; #[doc="Accept = #( media-range [ accept-params ] )"]
/// use hyper::mime::Mime; #[doc=""]
/// use hyper::mime::TopLevel::Text; #[doc="media-range = ( \"*/*\""]
/// use hyper::mime::SubLevel::{Html, Xml}; #[doc=" / ( type \"/\" \"*\" )"]
/// # let mut headers = Headers::new(); #[doc=" / ( type \"/\" subtype )"]
/// headers.set(Accept(vec![ #[doc=" ) *( OWS \";\" OWS parameter )"]
/// qitem(Mime(Text, Html, vec![])), #[doc="accept-params = weight *( accept-ext )"]
/// qitem(Mime(Text, Xml, vec![])) ])); #[doc="accept-ext = OWS \";\" OWS token [ \"=\" ( token / quoted-string ) ]"]
/// ``` #[doc="```"]
#[derive(Clone, PartialEq, Debug)] #[doc=""]
pub struct Accept(pub Vec<QualityItem<Mime>>); #[doc="# Notes"]
#[doc="* Using always Mime types to represent `media-range` differs from the ABNF."]
impl_list_header!(Accept, #[doc="* **FIXME**: `accept-ext` is not supported."]
"Accept", (Accept, "Accept") => (QualityItem<Mime>)+
Vec<QualityItem<Mime>>); }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@@ -1,15 +1,22 @@
use header::{Charset, QualityItem}; use header::{Charset, QualityItem};
/// The `Accept-Charset` header header! {
/// #[doc="`Accept-Charset` header, defined in"]
/// The `Accept-Charset` header can be used by clients to indicate what #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.3)"]
/// response charsets they accept. #[doc=""]
#[derive(Clone, PartialEq, Debug)] #[doc="The `Accept-Charset` header field can be sent by a user agent to"]
pub struct AcceptCharset(pub Vec<QualityItem<Charset>>); #[doc="indicate what charsets are acceptable in textual response content."]
#[doc="This field allows user agents capable of understanding more"]
impl_list_header!(AcceptCharset, #[doc="comprehensive or special-purpose charsets to signal that capability"]
"Accept-Charset", #[doc="to an origin server that is capable of representing information in"]
Vec<QualityItem<Charset>>); #[doc="those charsets."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Accept-Charset = 1#( ( charset / \"*\" ) [ weight ] )"]
#[doc="```"]
(AcceptCharset, "Accept-Charset") => (QualityItem<Charset>)+
}
#[test] #[test]

View File

@@ -1,15 +1,22 @@
use header::{Encoding, QualityItem}; use header::{Encoding, QualityItem};
/// The `Accept-Encoding` header header! {
/// #[doc="`Accept-Encoding` header, defined in"]
/// The `Accept-Encoding` header can be used by clients to indicate what #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.4)"]
/// response encodings they accept. #[doc=""]
#[derive(Clone, PartialEq, Debug)] #[doc="The `Accept-Encoding` header field can be used by user agents to"]
pub struct AcceptEncoding(pub Vec<QualityItem<Encoding>>); #[doc="indicate what response content-codings are"]
#[doc="acceptable in the response. An `identity` token is used as a synonym"]
impl_list_header!(AcceptEncoding, #[doc="for \"no encoding\" in order to communicate when no encoding is"]
"Accept-Encoding", #[doc="preferred."]
Vec<QualityItem<Encoding>>); #[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Accept-Encoding = #( codings [ weight ] )"]
#[doc="codings = content-coding / \"identity\" / \"*\""]
#[doc="```"]
(AcceptEncoding, "Accept-Encoding") => (QualityItem<Encoding>)*
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@@ -35,16 +35,21 @@ impl fmt::Display for Language {
} }
} }
/// The `Accept-Language` header header! {
/// #[doc="`Accept-Language` header, defined in"]
/// The `Accept-Language` header can be used by clients to indicate what #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)"]
/// response languages they accept. #[doc=""]
#[derive(Clone, PartialEq, Debug)] #[doc="The `Accept-Language` header field can be used by user agents to"]
pub struct AcceptLanguage(pub Vec<QualityItem<Language>>); #[doc="indicate the set of natural languages that are preferred in the"]
#[doc="response."]
impl_list_header!(AcceptLanguage, #[doc=""]
"Accept-Language", #[doc="# ABNF"]
Vec<QualityItem<Language>>); #[doc="```plain"]
#[doc="Accept-Language = 1#( language-range [ weight ] )"]
#[doc="language-range = <language-range, see [RFC4647], Section 2.1>"]
#[doc="```"]
(AcceptLanguage, "Accept-Language") => (QualityItem<Language>)+
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@@ -1,14 +1,19 @@
use method::Method; use method::Method;
/// The `Allow` header. header! {
/// See also https://tools.ietf.org/html/rfc7231#section-7.4.1 #[doc="`Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)"]
#[doc=""]
#[derive(Clone, PartialEq, Debug)] #[doc="The `Allow` header field lists the set of methods advertised as"]
pub struct Allow(pub Vec<Method>); #[doc="supported by the target resource. The purpose of this field is"]
#[doc="strictly to inform the recipient of valid request methods associated"]
impl_list_header!(Allow, #[doc="with the resource."]
"Allow", #[doc=""]
Vec<Method>); #[doc="# ABNF"]
#[doc="```plain"]
#[doc="Allow = #method"]
#[doc="```"]
(Allow, "Allow") => (Method)*
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@@ -1,19 +1,23 @@
use header::Encoding; use header::Encoding;
/// The `Content-Encoding` header. header! {
/// #[doc="`Content-Encoding` header, defined in"]
/// This header describes the encoding of the message body. It can be #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.2.2)"]
/// comma-separated, including multiple encodings. #[doc=""]
/// #[doc="The `Content-Encoding` header field indicates what content codings"]
/// ```notrust #[doc="have been applied to the representation, beyond those inherent in the"]
/// Content-Encoding: gzip #[doc="media type, and thus what decoding mechanisms have to be applied in"]
/// ``` #[doc="order to obtain data in the media type referenced by the Content-Type"]
#[derive(Clone, PartialEq, Debug)] #[doc="header field. Content-Encoding is primarily used to allow a"]
pub struct ContentEncoding(pub Vec<Encoding>); #[doc="representation's data to be compressed without losing the identity of"]
#[doc="its underlying media type."]
impl_list_header!(ContentEncoding, #[doc=""]
"Content-Encoding", #[doc="# ABNF"]
Vec<Encoding>); #[doc="```plain"]
#[doc="Content-Encoding = 1#content-coding"]
#[doc="```"]
(ContentEncoding, "ContentEncoding") => (Encoding)+
}
bench_header!(single, ContentEncoding, { vec![b"gzip".to_vec()] }); bench_header!(single, ContentEncoding, { vec![b"gzip".to_vec()] });
bench_header!(multiple, ContentEncoding, { vec![b"gzip, deflate".to_vec()] }); bench_header!(multiple, ContentEncoding, { vec![b"gzip, deflate".to_vec()] });

View File

@@ -1,11 +1,21 @@
/// The `Content-Length` header. header! {
/// #[doc="`Content-Length` header, defined in"]
/// Simply a wrapper around a `u64`. #[doc="[RFC7230](http://tools.ietf.org/html/rfc7230#section-3.3.2)"]
#[derive(Copy, Clone, PartialEq, Debug)] #[doc=""]
pub struct ContentLength(pub u64); #[doc="When a message does not have a `Transfer-Encoding` header field, a"]
#[doc="Content-Length header field can provide the anticipated size, as a"]
impl_header!(ContentLength, #[doc="decimal number of octets, for a potential payload body. For messages"]
"Content-Length", #[doc="that do include a payload body, the Content-Length field-value"]
u64); #[doc="provides the framing information necessary for determining where the"]
#[doc="body (and message) ends. For messages that do not include a payload"]
#[doc="body, the Content-Length indicates the size of the selected"]
#[doc="representation."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Content-Length = 1*DIGIT"]
#[doc="```"]
(ContentLength, "Content-Length") => [u64]
}
bench_header!(bench, ContentLength, { vec![b"42349984".to_vec()] }); bench_header!(bench, ContentLength, { vec![b"42349984".to_vec()] });

View File

@@ -1,14 +1,22 @@
use mime::Mime; use mime::Mime;
/// The `Content-Type` header. header! {
/// #[doc="`Content-Type` header, defined in"]
/// Used to describe the MIME type of message body. Can be used with both #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)"]
/// requests and responses. #[doc=""]
#[derive(Clone, PartialEq, Debug)] #[doc="The `Content-Type` header field indicates the media type of the"]
pub struct ContentType(pub Mime); #[doc="associated representation: either the representation enclosed in the"]
#[doc="message payload or the selected representation, as determined by the"]
impl_header!(ContentType, #[doc="message semantics. The indicated media type defines both the data"]
"Content-Type", #[doc="format and how that data is intended to be processed by a recipient,"]
Mime); #[doc="within the scope of the received message semantics, after any content"]
#[doc="codings indicated by Content-Encoding are decoded."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Content-Type = media-type"]
#[doc="```"]
(ContentType, "Content-Type") => [Mime]
}
bench_header!(bench, ContentType, { vec![b"application/json; charset=utf-8".to_vec()] }); bench_header!(bench, ContentType, { vec![b"application/json; charset=utf-8".to_vec()] });

View File

@@ -1,10 +1,17 @@
use header::HttpDate; use header::HttpDate;
/// The `Date` header field. header! {
#[derive(Copy, PartialEq, Clone, Debug)] #[doc="`Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2)"]
pub struct Date(pub HttpDate); #[doc=""]
#[doc="The `Date` header field represents the date and time at which the"]
impl_header!(Date, "Date", HttpDate); #[doc="message was originated."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Date = HTTP-date"]
#[doc="```"]
(Date, "Date") => [HttpDate]
}
bench_header!(imf_fixdate, Date, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] }); bench_header!(imf_fixdate, Date, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
bench_header!(rfc_850, Date, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] }); bench_header!(rfc_850, Date, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

View File

@@ -1,9 +1,21 @@
use header::HttpDate; use header::HttpDate;
/// The `Expires` header field. header! {
#[derive(Copy, PartialEq, Clone, Debug)] #[doc="`Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)"]
pub struct Expires(pub HttpDate); #[doc=""]
impl_header!(Expires, "Expires", HttpDate); #[doc="The `Expires` header field gives the date/time after which the"]
#[doc="response is considered stale."]
#[doc=""]
#[doc="The presence of an Expires field does not imply that the original"]
#[doc="resource will change or cease to exist at, before, or after that"]
#[doc="time."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Expires = HTTP-date"]
#[doc="```"]
(Expires, "Expires") => [HttpDate]
}
bench_header!(imf_fixdate, Expires, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] }); bench_header!(imf_fixdate, Expires, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
bench_header!(rfc_850, Expires, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] }); bench_header!(rfc_850, Expires, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

View File

@@ -1,9 +1,21 @@
use header::HttpDate; use header::HttpDate;
/// The `If-Modified-Since` header field. header! {
#[derive(Copy, PartialEq, Clone, Debug)] #[doc="`If-Modified-Since` header, defined in"]
pub struct IfModifiedSince(pub HttpDate); #[doc="[RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)"]
impl_header!(IfModifiedSince, "If-Modified-Since", HttpDate); #[doc=""]
#[doc="The `If-Modified-Since` header field makes a GET or HEAD request"]
#[doc="method conditional on the selected representation's modification date"]
#[doc="being more recent than the date provided in the field-value."]
#[doc="Transfer of the selected representation's data is avoided if that"]
#[doc="data has not changed."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="If-Unmodified-Since = HTTP-date"]
#[doc="```"]
(IfModifiedSince, "If-Modified-Since") => [HttpDate]
}
bench_header!(imf_fixdate, IfModifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] }); bench_header!(imf_fixdate, IfModifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
bench_header!(rfc_850, IfModifiedSince, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] }); bench_header!(rfc_850, IfModifiedSince, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

View File

@@ -1,10 +1,21 @@
use header::HttpDate; use header::HttpDate;
/// The `If-Unmodified-Since` header field. header! {
#[derive(Copy, PartialEq, Clone, Debug)] #[doc="`If-Unmodified-Since` header, defined in"]
pub struct IfUnmodifiedSince(pub HttpDate); #[doc="[RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)"]
#[doc=""]
impl_header!(IfUnmodifiedSince, "If-Unmodified-Since", HttpDate); #[doc="The `If-Unmodified-Since` header field makes the request method"]
#[doc="conditional on the selected representation's last modification date"]
#[doc="being earlier than or equal to the date provided in the field-value."]
#[doc="This field accomplishes the same purpose as If-Match for cases where"]
#[doc="the user agent does not have an entity-tag for the representation."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="If-Unmodified-Since = HTTP-date"]
#[doc="```"]
(IfUnmodifiedSince, "If-Unmodified-Since") => [HttpDate]
}
bench_header!(imf_fixdate, IfUnmodifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] }); bench_header!(imf_fixdate, IfUnmodifiedSince, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
bench_header!(rfc_850, IfUnmodifiedSince, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] }); bench_header!(rfc_850, IfUnmodifiedSince, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

View File

@@ -1,10 +1,19 @@
use header::HttpDate; use header::HttpDate;
/// The `LastModified` header field. header! {
#[derive(Copy, PartialEq, Clone, Debug)] #[doc="`Last-Modified` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)"]
pub struct LastModified(pub HttpDate); #[doc=""]
#[doc="The `Last-Modified` header field in a response provides a timestamp"]
impl_header!(LastModified, "Last-Modified", HttpDate); #[doc="indicating the date and time at which the origin server believes the"]
#[doc="selected representation was last modified, as determined at the"]
#[doc="conclusion of handling the request."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Expires = HTTP-date"]
#[doc="```"]
(LastModified, "Last-Modified") => [HttpDate]
}
bench_header!(imf_fixdate, LastModified, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] }); bench_header!(imf_fixdate, LastModified, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] });
bench_header!(rfc_850, LastModified, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] }); bench_header!(rfc_850, LastModified, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] });

View File

@@ -1,19 +1,19 @@
/// The `Location` header. header! {
/// #[doc="`Location` header, defined in"]
/// The Location response-header field is used to redirect the recipient to #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.2)"]
/// a location other than the Request-URI for completion of the request or identification #[doc=""]
/// of a new resource. For 201 (Created) responses, the Location is that of the new #[doc="The `Location` header field is used in some responses to refer to a"]
/// resource which was created by the request. For 3xx responses, the location SHOULD #[doc="specific resource in relation to the response. The type of"]
/// indicate the server's preferred URI for automatic redirection to the resource. #[doc="relationship is defined by the combination of request method and"]
/// The field value consists of a single absolute URI. #[doc="status code semantics."]
/// #[doc=""]
/// Currently is just a String, but it should probably become a better type, #[doc="# ABNF"]
/// like url::Url or something. #[doc="```plain"]
#[derive(Clone, PartialEq, Debug)] #[doc="Location = URI-reference"]
pub struct Location(pub String); #[doc="```"]
// TODO: Use URL
(Location, "Location") => [String]
impl_header!(Location, }
"Location",
String);
bench_header!(bench, Location, { vec![b"http://foo.com/hello:3000".to_vec()] }); bench_header!(bench, Location, { vec![b"http://foo.com/hello:3000".to_vec()] });

View File

@@ -89,64 +89,91 @@ macro_rules! deref(
); );
#[macro_export] #[macro_export]
macro_rules! impl_list_header( macro_rules! header {
($from:ident, $name:expr, $item:ty) => { // $a:meta: Attributes associated with the header item (usually docs)
deref!($from => $item); // $id:ident: Identifier of the header
// $n:expr: Lowercase name of the header
// $nn:expr: Nice name of the header
impl $crate::header::Header for $from { // List header, zero or more items
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)*) => {
$(#[$a])*
#[derive(Clone, Debug, PartialEq)]
pub struct $id(pub Vec<$item>);
deref!($id => Vec<$item>);
impl $crate::header::Header for $id {
fn header_name() -> &'static str { fn header_name() -> &'static str {
$name $n
} }
fn parse_header(raw: &[Vec<u8>]) -> Option<Self> {
fn parse_header(raw: &[Vec<u8>]) -> Option<$from> { $crate::header::parsing::from_comma_delimited(raw).map($id)
$crate::header::parsing::from_comma_delimited(raw).map($from)
} }
} }
impl $crate::header::HeaderFormat for $id {
impl $crate::header::HeaderFormat for $from { fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt_header(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { $crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
$crate::header::parsing::fmt_comma_delimited(fmt, &self[..])
} }
} }
impl ::std::fmt::Display for $id {
impl ::std::fmt::Display for $from {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use $crate::header::HeaderFormat; use $crate::header::HeaderFormat;
self.fmt_header(f) self.fmt_header(f)
} }
} }
}
);
#[macro_export] };
macro_rules! impl_header( // List header, one or more items
($from:ident, $name:expr, $item:ty) => { ($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+) => {
deref!($from => $item); $(#[$a])*
#[derive(Clone, Debug, PartialEq)]
impl $crate::header::Header for $from { pub struct $id(pub Vec<$item>);
deref!($id => Vec<$item>);
impl $crate::header::Header for $id {
fn header_name() -> &'static str { fn header_name() -> &'static str {
$name $n
} }
fn parse_header(raw: &[Vec<u8>]) -> Option<Self> {
fn parse_header(raw: &[Vec<u8>]) -> Option<$from> { $crate::header::parsing::from_comma_delimited(raw).map($id)
$crate::header::parsing::from_one_raw_str(raw).map($from)
} }
} }
impl $crate::header::HeaderFormat for $id {
impl $crate::header::HeaderFormat for $from { fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
}
}
impl ::std::fmt::Display for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use $crate::header::HeaderFormat;
self.fmt_header(f)
}
}
};
// Single value header
($(#[$a:meta])*($id:ident, $n:expr) => [$value:ty]) => {
$(#[$a])*
#[derive(Clone, Debug, PartialEq)]
pub struct $id(pub $value);
deref!($id => $value);
impl $crate::header::Header for $id {
fn header_name() -> &'static str {
$n
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Self> {
$crate::header::parsing::from_one_raw_str(raw).map($id)
}
}
impl $crate::header::HeaderFormat for $id {
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::std::fmt::Display::fmt(&**self, f) ::std::fmt::Display::fmt(&**self, f)
} }
} }
impl ::std::fmt::Display for $id {
impl ::std::fmt::Display for $from {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use $crate::header::HeaderFormat; ::std::fmt::Display::fmt(&**self, f)
self.fmt_header(f)
} }
} }
};
} }
);
mod access_control; mod access_control;
mod accept; mod accept;

View File

@@ -1,16 +1,19 @@
/// The `Referer` header. header! {
/// #[doc="`Referer` header, defined in"]
/// The Referer header is used by user agents to inform server about #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-5.5.2)"]
/// the page URL user has came from. #[doc=""]
/// #[doc="The `Referer` [sic] header field allows the user agent to specify a"]
/// See alse [RFC 1945, section 10.13](http://tools.ietf.org/html/rfc1945#section-10.13). #[doc="URI reference for the resource from which the target URI was obtained"]
/// #[doc="(i.e., the \"referrer\", though the field name is misspelled). A user"]
/// Currently just a string, but maybe better replace it with url::Url or something like it. #[doc="agent MUST NOT include the fragment and userinfo components of the"]
#[derive(Clone, PartialEq, Debug)] #[doc="URI reference, if any, when generating the Referer field value."]
pub struct Referer(pub String); #[doc=""]
#[doc="# ABNF"]
impl_header!(Referer, #[doc="```plain"]
"Referer", #[doc="Referer = absolute-URI / partial-URI"]
String); #[doc="```"]
// TODO: Use URL
(Referer, "Referer") => [String]
}
bench_header!(bench, Referer, { vec![b"http://foo.com/hello:3000".to_vec()] }); bench_header!(bench, Referer, { vec![b"http://foo.com/hello:3000".to_vec()] });

View File

@@ -1,11 +1,20 @@
/// The `Server` header field. header! {
/// #[doc="`Server` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.2)"]
/// They can contain any value, so it just wraps a `String`. #[doc=""]
#[derive(Clone, PartialEq, Debug)] #[doc="The `Server` header field contains information about the software"]
pub struct Server(pub String); #[doc="used by the origin server to handle the request, which is often used"]
#[doc="by clients to help identify the scope of reported interoperability"]
impl_header!(Server, #[doc="problems, to work around or tailor requests to avoid particular"]
"Server", #[doc="server limitations, and for analytics regarding server or operating"]
String); #[doc="system use. An origin server MAY generate a Server field in its"]
#[doc="responses."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="Server = product *( RWS ( product / comment ) )"]
#[doc="```"]
// TODO: Maybe parse as defined in the spec?
(Server, "Server") => [String]
}
bench_header!(bench, Server, { vec![b"Some String".to_vec()] }); bench_header!(bench, Server, { vec![b"Some String".to_vec()] });

View File

@@ -1,24 +1,20 @@
use header::Encoding; use header::Encoding;
/// The `Transfer-Encoding` header. header! {
/// #[doc="`Transfer-Encoding` header, defined in"]
/// This header describes the encoding of the message body. It can be #[doc="[RFC7230](http://tools.ietf.org/html/rfc7230#section-3.3.1)"]
/// comma-separated, including multiple encodings. #[doc=""]
/// #[doc="The `Transfer-Encoding` header field lists the transfer coding names"]
/// ```notrust #[doc="corresponding to the sequence of transfer codings that have been (or"]
/// Transfer-Encoding: gzip, chunked #[doc="will be) applied to the payload body in order to form the message"]
/// ``` #[doc="body."]
/// #[doc=""]
/// According to the spec, if a `Content-Length` header is not included, #[doc="# ABNF"]
/// this header should include `chunked` as the last encoding. #[doc="```plain"]
/// #[doc="Transfer-Encoding = 1#transfer-coding"]
/// The implementation uses a vector of `Encoding` values. #[doc="```"]
#[derive(Clone, PartialEq, Debug)] (TransferEncoding, "Transfer-Encoding") => (Encoding)+
pub struct TransferEncoding(pub Vec<Encoding>); }
impl_list_header!(TransferEncoding,
"Transfer-Encoding",
Vec<Encoding>);
bench_header!(normal, TransferEncoding, { vec![b"chunked, gzip".to_vec()] }); bench_header!(normal, TransferEncoding, { vec![b"chunked, gzip".to_vec()] });
bench_header!(ext, TransferEncoding, { vec![b"ext".to_vec()] }); bench_header!(ext, TransferEncoding, { vec![b"ext".to_vec()] });

View File

@@ -1,14 +1,24 @@
/// The `User-Agent` header field. header! {
/// #[doc="`User-Agent` header, defined in"]
/// They can contain any value, so it just wraps a `String`. #[doc="[RFC7231](http://tools.ietf.org/html/rfc7231#section-5.5.3)"]
#[derive(Clone, PartialEq, Debug)] #[doc=""]
pub struct UserAgent(pub String); #[doc="The `User-Agent` header field contains information about the user"]
#[doc="agent originating the request, which is often used by servers to help"]
impl_header!(UserAgent, #[doc="identify the scope of reported interoperability problems, to work"]
"User-Agent", #[doc="around or tailor responses to avoid particular user agent"]
String); #[doc="limitations, and for analytics regarding browser or operating system"]
#[doc="use. A user agent SHOULD send a User-Agent field in each request"]
bench_header!(bench, UserAgent, { vec![b"cargo bench".to_vec()] }); #[doc="unless specifically configured not to do so."]
#[doc=""]
#[doc="# ABNF"]
#[doc="```plain"]
#[doc="User-Agent = product *( RWS ( product / comment ) )"]
#[doc="product = token [\"/\" product-version]"]
#[doc="product-version = token"]
#[doc="```"]
// TODO: Maybe write parsing according to the spec? (Split the String)
(UserAgent, "User-Agent") => [String]
}
#[test] fn test_format() { #[test] fn test_format() {
use std::borrow::ToOwned; use std::borrow::ToOwned;