Skip to content

Commit 6b065f9

Browse files
vincentmrgseanmonstar
authored andcommitted
add support for age header
1 parent 4cdea56 commit 6b065f9

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

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/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;
@@ -138,6 +139,7 @@ mod access_control_expose_headers;
138139
mod access_control_max_age;
139140
mod access_control_request_headers;
140141
mod access_control_request_method;
142+
mod age;
141143
mod allow;
142144
pub mod authorization;
143145
mod cache_control;

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)