|
| 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 | +} |
0 commit comments