Skip to content

Commit 66d54d0

Browse files
committed
refactor(headers): Improve docs, fix nits, make formatting faster
src/header/parsing.rs now uses unsafe get_unchecked() again, I don't know why it was removed.
1 parent b916a7b commit 66d54d0

21 files changed

+187
-96
lines changed

src/header/common/access_control_allow_headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use unicase::UniCase;
22

33
header! {
44
#[doc="`Access-Control-Allow-Headers` header, part of"]
5-
#[doc="[CORS](www.w3.org/TR/cors/#access-control-allow-headers-response-header)"]
5+
#[doc="[CORS](http://www.w3.org/TR/cors/#access-control-allow-headers-response-header)"]
66
#[doc=""]
77
#[doc="The `Access-Control-Allow-Headers` header indicates, as part of the"]
88
#[doc="response to a preflight request, which header field names can be used"]

src/header/common/access_control_allow_methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use method::Method;
22

33
header! {
44
#[doc="`Access-Control-Allow-Methods` header, part of"]
5-
#[doc="[CORS](www.w3.org/TR/cors/#access-control-allow-methods-response-header)"]
5+
#[doc="[CORS](http://www.w3.org/TR/cors/#access-control-allow-methods-response-header)"]
66
#[doc=""]
77
#[doc="The `Access-Control-Allow-Methods` header indicates, as part of the"]
88
#[doc="response to a preflight request, which methods can be used during the"]

src/header/common/access_control_max_age.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
header! {
22
#[doc="`Access-Control-Max-Age` header, part of"]
3-
#[doc="[CORS](www.w3.org/TR/cors/#access-control-max-age-response-header)"]
3+
#[doc="[CORS](http://www.w3.org/TR/cors/#access-control-max-age-response-header)"]
44
#[doc=""]
55
#[doc="The `Access-Control-Max-Age` header indicates how long the results of a"]
66
#[doc="preflight request can be cached in a preflight result cache."]

src/header/common/access_control_request_headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use unicase::UniCase;
22

33
header! {
44
#[doc="`Access-Control-Request-Headers` header, part of"]
5-
#[doc="[CORS](www.w3.org/TR/cors/#access-control-request-headers-request-header)"]
5+
#[doc="[CORS](http://www.w3.org/TR/cors/#access-control-request-headers-request-header)"]
66
#[doc=""]
77
#[doc="The `Access-Control-Request-Headers` header indicates which headers will"]
88
#[doc="be used in the actual request as part of the preflight request."]

src/header/common/access_control_request_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use method::Method;
22

33
header! {
44
#[doc="`Access-Control-Request-Method` header, part of"]
5-
#[doc="[CORS](www.w3.org/TR/cors/#access-control-request-method-request-header)"]
5+
#[doc="[CORS](http://www.w3.org/TR/cors/#access-control-request-method-request-header)"]
66
#[doc=""]
77
#[doc="The `Access-Control-Request-Method` header indicates which method will be"]
88
#[doc="used in the actual request as part of the preflight request."]

src/header/common/authorization.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
use std::any::Any;
2-
use std::fmt;
2+
use std::fmt::{self, Display};
33
use std::str::{FromStr, from_utf8};
44
use std::ops::{Deref, DerefMut};
55
use serialize::base64::{ToBase64, FromBase64, Standard, Config, Newline};
66
use header::{Header, HeaderFormat};
77

8-
/// The `Authorization` header field.
8+
/// `Authorization` header, defined in [RFC7235](https://tools.ietf.org/html/rfc7235#section-4.2)
9+
///
10+
/// The `Authorization` header field allows a user agent to authenticate
11+
/// itself with an origin server -- usually, but not necessarily, after
12+
/// receiving a 401 (Unauthorized) response. Its value consists of
13+
/// credentials containing the authentication information of the user
14+
/// agent for the realm of the resource being requested.
15+
///
16+
/// # ABNF
17+
/// ```plain
18+
/// Authorization = credentials
19+
/// ```
20+
///
21+
/// # Example values
22+
/// * `Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==`
923
#[derive(Clone, PartialEq, Debug)]
1024
pub struct Authorization<S: Scheme>(pub S);
1125

@@ -69,7 +83,7 @@ impl Scheme for String {
6983
}
7084

7185
fn fmt_scheme(&self, f: &mut fmt::Formatter) -> fmt::Result {
72-
fmt::Display::fmt(self, f)
86+
Display::fmt(self, f)
7387
}
7488
}
7589

@@ -97,12 +111,12 @@ impl Scheme for Basic {
97111
if let Some(ref pass) = self.password {
98112
text.push_str(&pass[..]);
99113
}
100-
write!(f, "{}", text.as_bytes().to_base64(Config {
114+
f.write_str(&text.as_bytes().to_base64(Config {
101115
char_set: Standard,
102116
newline: Newline::CRLF,
103117
pad: true,
104118
line_length: None
105-
}))
119+
})[..])
106120
}
107121
}
108122

src/header/common/cache_control.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,23 @@ use std::str::FromStr;
33
use header::{Header, HeaderFormat};
44
use header::parsing::{from_one_comma_delimited, fmt_comma_delimited};
55

6-
/// The Cache-Control header.
6+
/// `Cache-Control` header, defined in [RFC7234](https://tools.ietf.org/html/rfc7234#section-5.2)
7+
///
8+
/// The `Cache-Control` header field is used to specify directives for
9+
/// caches along the request/response chain. Such cache directives are
10+
/// unidirectional in that the presence of a directive in a request does
11+
/// not imply that the same directive is to be given in the response.
12+
///
13+
/// # ABNF
14+
/// ```plain
15+
/// Cache-Control = 1#cache-directive
16+
/// cache-directive = token [ "=" ( token / quoted-string ) ]
17+
/// ```
18+
///
19+
/// # Example values
20+
/// * `no-cache`
21+
/// * `private, community="UCI"`
22+
/// * `max-age=30`
723
#[derive(PartialEq, Clone, Debug)]
824
pub struct CacheControl(pub Vec<CacheDirective>);
925

@@ -28,8 +44,8 @@ impl Header for CacheControl {
2844
}
2945

3046
impl HeaderFormat for CacheControl {
31-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
32-
fmt_comma_delimited(fmt, &self[..])
47+
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
48+
fmt_comma_delimited(f, &self[..])
3349
}
3450
}
3551

src/header/common/connection.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
use header::{Header, HeaderFormat};
2-
use std::fmt;
2+
use std::fmt::{self, Display};
33
use std::str::FromStr;
44
use header::parsing::{from_comma_delimited, fmt_comma_delimited};
55
use unicase::UniCase;
66

77
pub use self::ConnectionOption::{KeepAlive, Close, ConnectionHeader};
88

9-
/// The `Connection` header.
9+
/// `Connection` header, defined in [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.1)
10+
///
11+
/// The `Connection` header field allows the sender to indicate desired
12+
/// control options for the current connection. In order to avoid
13+
/// confusing downstream recipients, a proxy or gateway MUST remove or
14+
/// replace any received connection options before forwarding the
15+
/// message.
16+
///
17+
/// # ABNF
18+
/// ```plain
19+
/// Connection = 1#connection-option
20+
/// connection-option = token
21+
/// ```
22+
///
23+
/// # Example values
24+
/// * `close`
25+
/// * `upgrade`
26+
/// * `keep-alive`
1027
#[derive(Clone, PartialEq, Debug)]
1128
pub struct Connection(pub Vec<ConnectionOption>);
1229

@@ -33,20 +50,20 @@ pub enum ConnectionOption {
3350
impl FromStr for ConnectionOption {
3451
type Err = ();
3552
fn from_str(s: &str) -> Result<ConnectionOption, ()> {
36-
match s {
37-
"keep-alive" => Ok(KeepAlive),
38-
"close" => Ok(Close),
39-
s => Ok(ConnectionHeader(UniCase(s.to_string())))
40-
}
53+
Ok(match s {
54+
"keep-alive" => KeepAlive,
55+
"close" => Close,
56+
s => ConnectionHeader(UniCase(s.to_string())),
57+
})
4158
}
4259
}
4360

44-
impl fmt::Display for ConnectionOption {
45-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
46-
write!(fmt, "{}", match *self {
61+
impl Display for ConnectionOption {
62+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63+
f.write_str(match *self {
4764
KeepAlive => "keep-alive",
4865
Close => "close",
49-
ConnectionHeader(UniCase(ref s)) => s.as_ref()
66+
ConnectionHeader(UniCase(ref s)) => s,
5067
})
5168
}
5269
}
@@ -62,13 +79,12 @@ impl Header for Connection {
6279
}
6380

6481
impl HeaderFormat for Connection {
65-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
82+
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
6683
let Connection(ref parts) = *self;
67-
fmt_comma_delimited(fmt, &parts[..])
84+
fmt_comma_delimited(f, &parts[..])
6885
}
6986
}
7087

7188
bench_header!(close, Connection, { vec![b"close".to_vec()] });
7289
bench_header!(keep_alive, Connection, { vec![b"keep-alive".to_vec()] });
7390
bench_header!(header, Connection, { vec![b"authorization".to_vec()] });
74-

src/header/common/cookie.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use header::{Header, HeaderFormat};
2-
use std::fmt;
2+
use std::fmt::{self, Display};
33
use std::str::from_utf8;
44

55
use cookie::Cookie as CookiePair;
66
use cookie::CookieJar;
77

8-
/// The `Cookie` header. Defined in [RFC6265](tools.ietf.org/html/rfc6265#section-5.4):
8+
/// `Cookie` header, defined in [RFC6265](http://tools.ietf.org/html/rfc6265#section-5.4)
99
///
10-
/// > If the user agent does attach a Cookie header field to an HTTP
11-
/// > request, the user agent must send the cookie-string
12-
/// > as the value of the header field.
10+
/// If the user agent does attach a Cookie header field to an HTTP
11+
/// request, the user agent must send the cookie-string
12+
/// as the value of the header field.
1313
///
14-
/// > When the user agent generates an HTTP request, the user agent MUST NOT
15-
/// > attach more than one Cookie header field.
14+
/// When the user agent generates an HTTP request, the user agent MUST NOT
15+
/// attach more than one Cookie header field.
16+
///
17+
/// # Example values
18+
/// * `SID=31d4d96e407aad42`
19+
/// * `SID=31d4d96e407aad42; lang=en-US`
1620
#[derive(Clone, PartialEq, Debug)]
1721
pub struct Cookie(pub Vec<CookiePair>);
1822

@@ -48,13 +52,13 @@ impl Header for Cookie {
4852
}
4953

5054
impl HeaderFormat for Cookie {
51-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
55+
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
5256
let cookies = &self.0;
5357
for (i, cookie) in cookies.iter().enumerate() {
5458
if i != 0 {
55-
try!(fmt.write_str("; "));
59+
try!(f.write_str("; "));
5660
}
57-
try!(write!(fmt, "{}", cookie.pair()));
61+
try!(Display::fmt(&cookie.pair(), f));
5862
}
5963
Ok(())
6064
}

src/header/common/host.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ impl Header for Host {
6565
}
6666

6767
impl HeaderFormat for Host {
68-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
68+
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
6969
match self.port {
70-
None | Some(80) | Some(443) => write!(fmt, "{}", self.hostname),
71-
Some(port) => write!(fmt, "{}:{}", self.hostname, port)
70+
None | Some(80) | Some(443) => f.write_str(&self.hostname[..]),
71+
Some(port) => write!(f, "{}:{}", self.hostname, port)
7272
}
7373
}
7474
}
@@ -97,4 +97,3 @@ mod tests {
9797
}
9898

9999
bench_header!(bench, Host, { vec![b"foo.com:3000".to_vec()] });
100-

src/header/common/if_range.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use header::{self, EntityTag, HttpDate};
1+
use std::fmt::{self, Display};
2+
use header::{self, Header, HeaderFormat, EntityTag, HttpDate};
23

34
/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2)
45
///
@@ -31,7 +32,7 @@ pub enum IfRange {
3132
Date(HttpDate),
3233
}
3334

34-
impl header::Header for IfRange {
35+
impl Header for IfRange {
3536
fn header_name() -> &'static str {
3637
"If-Range"
3738
}
@@ -48,18 +49,17 @@ impl header::Header for IfRange {
4849
}
4950
}
5051

51-
impl header::HeaderFormat for IfRange {
52+
impl HeaderFormat for IfRange {
5253
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
5354
match self {
54-
&IfRange::EntityTag(ref x) => write!(f, "{}", x),
55-
&IfRange::Date(ref x) => write!(f, "{}", x),
55+
&IfRange::EntityTag(ref x) => Display::fmt(x, f),
56+
&IfRange::Date(ref x) => Display::fmt(x, f),
5657
}
5758
}
5859
}
5960

60-
impl ::std::fmt::Display for IfRange {
61-
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
62-
use header::HeaderFormat;
61+
impl Display for IfRange {
62+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6363
self.fmt_header(f)
6464
}
6565
}

src/header/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ macro_rules! header {
258258
impl $crate::header::HeaderFormat for $id {
259259
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
260260
match *self {
261-
$id::Any => write!(f, "*"),
261+
$id::Any => f.write_str("*"),
262262
$id::Items(ref fields) => $crate::header::parsing::fmt_comma_delimited(f, &fields[..])
263263
}
264264
}

src/header/common/pragma.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ impl Header for Pragma {
4242

4343
impl HeaderFormat for Pragma {
4444
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
45-
match *self {
46-
Pragma::NoCache => write!(f, "no-cache"),
47-
Pragma::Ext(ref string) => write!(f, "{}", string),
48-
}
45+
f.write_str(match *self {
46+
Pragma::NoCache => "no-cache",
47+
Pragma::Ext(ref string) => &string[..],
48+
})
4949
}
5050
}
5151

0 commit comments

Comments
 (0)