|
| 1 | +use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, AGE}; |
| 2 | +use crate::Status; |
| 3 | + |
| 4 | +use std::fmt::Debug; |
| 5 | +use std::option; |
| 6 | +use std::time::Duration; |
| 7 | + |
| 8 | +/// HTTP `Age` header |
| 9 | +/// |
| 10 | +/// # Specifications |
| 11 | +/// |
| 12 | +/// - [RFC 7234 Hypertext Transfer Protocol (HTTP/1.1): Caching](https://tools.ietf.org/html/rfc7234#section-5.1) |
| 13 | +/// |
| 14 | +/// # Examples |
| 15 | +/// |
| 16 | +/// ``` |
| 17 | +/// # fn main() -> http_types::Result<()> { |
| 18 | +/// # |
| 19 | +/// use http_types::Response; |
| 20 | +/// use http_types::cache::Age; |
| 21 | +/// use std::time::Duration; |
| 22 | +/// |
| 23 | +/// let age = Age::new(Duration::from_secs(12)); |
| 24 | +/// |
| 25 | +/// let mut res = Response::new(200); |
| 26 | +/// age.apply(&mut res); |
| 27 | +/// |
| 28 | +/// let age = Age::from_headers(res)?.unwrap(); |
| 29 | +/// assert_eq!(age, Age::new(Duration::from_secs(12))); |
| 30 | +/// # |
| 31 | +/// # Ok(()) } |
| 32 | +/// ``` |
| 33 | +#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)] |
| 34 | +pub struct Age { |
| 35 | + dur: Duration, |
| 36 | +} |
| 37 | + |
| 38 | +impl Age { |
| 39 | + /// Create a new instance of `Age`. |
| 40 | + pub fn new(dur: Duration) -> Self { |
| 41 | + Self { dur } |
| 42 | + } |
| 43 | + |
| 44 | + /// Create an instance of `Age` from a `Headers` instance. |
| 45 | + /// |
| 46 | + /// # Implementation note |
| 47 | + /// |
| 48 | + /// A header value of `"null"` is treated the same as if no header was sent. |
| 49 | + pub fn from_headers(headers: impl AsRef<Headers>) -> crate::Result<Option<Self>> { |
| 50 | + let headers = match headers.as_ref().get(AGE) { |
| 51 | + Some(headers) => headers, |
| 52 | + None => return Ok(None), |
| 53 | + }; |
| 54 | + |
| 55 | + // If we successfully parsed the header then there's always at least one |
| 56 | + // entry. We want the last entry. |
| 57 | + let header = headers.iter().last().unwrap(); |
| 58 | + |
| 59 | + let num: u64 = header.as_str().parse().status(400)?; |
| 60 | + let dur = Duration::from_secs_f64(num as f64); |
| 61 | + |
| 62 | + Ok(Some(Self { dur })) |
| 63 | + } |
| 64 | + |
| 65 | + /// Insert a `HeaderName` + `HeaderValue` pair into a `Headers` instance. |
| 66 | + pub fn apply(&self, mut headers: impl AsMut<Headers>) { |
| 67 | + headers.as_mut().insert(AGE, self.value()); |
| 68 | + } |
| 69 | + |
| 70 | + /// Get the `HeaderName`. |
| 71 | + pub fn name(&self) -> HeaderName { |
| 72 | + AGE |
| 73 | + } |
| 74 | + |
| 75 | + /// Get the `HeaderValue`. |
| 76 | + pub fn value(&self) -> HeaderValue { |
| 77 | + let output = self.dur.as_secs().to_string(); |
| 78 | + |
| 79 | + // SAFETY: the internal string is validated to be ASCII. |
| 80 | + unsafe { HeaderValue::from_bytes_unchecked(output.into()) } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl ToHeaderValues for Age { |
| 85 | + type Iter = option::IntoIter<HeaderValue>; |
| 86 | + fn to_header_values(&self) -> crate::Result<Self::Iter> { |
| 87 | + // A HeaderValue will always convert into itself. |
| 88 | + Ok(self.value().to_header_values().unwrap()) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod test { |
| 94 | + use super::*; |
| 95 | + use crate::headers::Headers; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn smoke() -> crate::Result<()> { |
| 99 | + let age = Age::new(Duration::from_secs(12)); |
| 100 | + |
| 101 | + let mut headers = Headers::new(); |
| 102 | + age.apply(&mut headers); |
| 103 | + |
| 104 | + let age = Age::from_headers(headers)?.unwrap(); |
| 105 | + assert_eq!(age, Age::new(Duration::from_secs(12))); |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn bad_request_on_parse_error() -> crate::Result<()> { |
| 111 | + let mut headers = Headers::new(); |
| 112 | + headers.insert(AGE, "<nori ate the tag. yum.>"); |
| 113 | + let err = Age::from_headers(headers).unwrap_err(); |
| 114 | + assert_eq!(err.status(), 400); |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | +} |
0 commit comments