Skip to content

Commit 681c1a5

Browse files
committed
Merge branch 'master' into headers_encoding
2 parents 0961ee1 + ffca4a9 commit 681c1a5

File tree

6 files changed

+94
-4
lines changed

6 files changed

+94
-4
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "headers"
3-
version = "0.3.5" # don't forget to update html_root_url
3+
version = "0.3.7" # don't forget to update html_root_url
44
description = "typed HTTP headers"
55
license = "MIT"
66
readme = "README.md"
@@ -24,7 +24,7 @@ bitflags = "1.0"
2424
itertools = "0.9"
2525
bytes = "1"
2626
mime = "0.3.14"
27-
sha-1 = "0.9"
27+
sha-1 = "0.10"
2828
httpdate = "1"
2929

3030
[features]

src/common/age.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::time::Duration;
2+
3+
use util::Seconds;
4+
5+
/// `Age` header, defined in [RFC7234](https://tools.ietf.org/html/rfc7234#section-5.1)
6+
///
7+
/// The "Age" header field conveys the sender's estimate of the amount of
8+
/// time since the response was generated or successfully validated at
9+
/// the origin server. Age values are calculated as specified in
10+
/// [Section 4.2.3](https://tools.ietf.org/html/rfc7234#section-4.2.3).
11+
///
12+
/// ## ABNF
13+
///
14+
/// ```text
15+
/// Age = delta-seconds
16+
/// ```
17+
///
18+
/// The Age field-value is a non-negative integer, representing time in
19+
/// seconds (see [Section 1.2.1](https://tools.ietf.org/html/rfc7234#section-1.2.1)).
20+
///
21+
/// The presence of an Age header field implies that the response was not
22+
/// generated or validated by the origin server for this request.
23+
/// However, lack of an Age header field does not imply the origin was
24+
/// contacted, since the response might have been received from an
25+
/// HTTP/1.0 cache that does not implement Age.
26+
///
27+
/// ## Example values
28+
///
29+
/// * `3600`
30+
///
31+
/// # Example
32+
///
33+
/// ```
34+
/// # extern crate headers;
35+
/// use headers::Age;
36+
///
37+
/// let len = Age::from_secs(60);
38+
/// ```
39+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
40+
pub struct Age(Seconds);
41+
42+
derive_header! {
43+
Age(_),
44+
name: AGE
45+
}
46+
47+
impl Age {
48+
/// Creates a new `Age` header from the specified number of whole seconds.
49+
pub fn from_secs(secs: u64) -> Self {
50+
Self(Seconds::from_secs(secs))
51+
}
52+
53+
/// Returns the number of seconds for this `Age` header.
54+
pub fn as_secs(&self) -> u64 {
55+
self.0.as_u64()
56+
}
57+
}
58+
59+
impl From<Duration> for Age {
60+
fn from(dur: Duration) -> Self {
61+
Age(Seconds::from(dur))
62+
}
63+
}
64+
65+
impl From<Age> for Duration {
66+
fn from(age: Age) -> Self {
67+
age.0.into()
68+
}
69+
}

src/common/authorization.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ impl Authorization<Basic> {
4545

4646
Authorization(Basic { decoded, colon_pos })
4747
}
48+
49+
/// View the decoded username.
50+
pub fn username(&self) -> &str {
51+
self.0.username()
52+
}
53+
54+
/// View the decoded password.
55+
pub fn password(&self) -> &str {
56+
self.0.password()
57+
}
4858
}
4959

5060
impl Authorization<Bearer> {
@@ -54,6 +64,11 @@ impl Authorization<Bearer> {
5464
.map(|val| Authorization(Bearer(val)))
5565
.ok_or_else(|| InvalidBearerToken { _inner: () })
5666
}
67+
68+
/// View the token part as a `&str`.
69+
pub fn token(&self) -> &str {
70+
self.0.token()
71+
}
5772
}
5873

5974
impl<C: Credentials> ::Header for Authorization<C> {

src/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub use self::access_control_expose_headers::AccessControlExposeHeaders;
1919
pub use self::access_control_max_age::AccessControlMaxAge;
2020
pub use self::access_control_request_headers::AccessControlRequestHeaders;
2121
pub use self::access_control_request_method::AccessControlRequestMethod;
22+
pub use self::age::Age;
2223
pub use self::allow::Allow;
2324
pub use self::authorization::Authorization;
2425
pub use self::cache_control::CacheControl;
@@ -139,6 +140,7 @@ mod access_control_expose_headers;
139140
mod access_control_max_age;
140141
mod access_control_request_headers;
141142
mod access_control_request_method;
143+
mod age;
142144
mod allow;
143145
pub mod authorization;
144146
mod cache_control;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![deny(missing_debug_implementations)]
33
#![cfg_attr(test, deny(warnings))]
44
#![cfg_attr(all(test, feature = "nightly"), feature(test))]
5-
#![doc(html_root_url = "https://docs.rs/headers/0.3.5")]
5+
#![doc(html_root_url = "https://docs.rs/headers/0.3.7")]
66

77
//! # Typed HTTP Headers
88
//!

src/util/seconds.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ impl Seconds {
1111
pub(crate) fn from_val(val: &HeaderValue) -> Option<Self> {
1212
let secs = val.to_str().ok()?.parse().ok()?;
1313

14-
Some(Seconds(Duration::from_secs(secs)))
14+
Some(Self::from_secs(secs))
15+
}
16+
17+
pub(crate) fn from_secs(secs: u64) -> Self {
18+
Self::from(Duration::from_secs(secs))
1519
}
1620

1721
pub(crate) fn as_u64(&self) -> u64 {

0 commit comments

Comments
 (0)