|
| 1 | +//! Apply the HTTP method if the ETag matches. |
| 2 | +
|
| 3 | +use crate::conditional::VaryDirective; |
| 4 | +use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, VARY}; |
| 5 | + |
| 6 | +use std::convert::TryInto; |
| 7 | +use std::fmt::{self, Debug, Write}; |
| 8 | +use std::iter::Iterator; |
| 9 | +use std::option; |
| 10 | +use std::slice; |
| 11 | +use std::str::FromStr; |
| 12 | + |
| 13 | +/// Apply the HTTP method if the ETag matches. |
| 14 | +/// |
| 15 | +/// # Specifications |
| 16 | +/// |
| 17 | +/// - [RFC 7231, section 7.1.4: Vary](https://tools.ietf.org/html/rfc7231#section-7.1.4) |
| 18 | +/// |
| 19 | +/// # Examples |
| 20 | +/// |
| 21 | +/// ``` |
| 22 | +/// # fn main() -> http_types::Result<()> { |
| 23 | +/// # |
| 24 | +/// use http_types::Response; |
| 25 | +/// use http_types::conditional::Vary; |
| 26 | +/// |
| 27 | +/// let mut entries = Vary::new(); |
| 28 | +/// entries.push("User-Agent")?; |
| 29 | +/// entries.push("Accept-Encoding")?; |
| 30 | +/// |
| 31 | +/// let mut res = Response::new(200); |
| 32 | +/// entries.apply(&mut res); |
| 33 | +/// |
| 34 | +/// let entries = Vary::from_headers(res)?.unwrap(); |
| 35 | +/// let mut entries = entries.iter(); |
| 36 | +/// assert_eq!(entries.next().unwrap(), "User-Agent"); |
| 37 | +/// assert_eq!(entries.next().unwrap(), "Accept-Encoding"); |
| 38 | +/// # |
| 39 | +/// # Ok(()) } |
| 40 | +/// ``` |
| 41 | +pub struct Vary { |
| 42 | + entries: Vec<VaryDirective>, |
| 43 | +} |
| 44 | + |
| 45 | +impl Vary { |
| 46 | + /// Create a new instance of `Vary`. |
| 47 | + pub fn new() -> Self { |
| 48 | + Self { entries: vec![] } |
| 49 | + } |
| 50 | + |
| 51 | + /// Create a new instance from headers. |
| 52 | + pub fn from_headers(headers: impl AsRef<Headers>) -> crate::Result<Option<Self>> { |
| 53 | + let mut entries = vec![]; |
| 54 | + let headers = match headers.as_ref().get(VARY) { |
| 55 | + Some(headers) => headers, |
| 56 | + None => return Ok(None), |
| 57 | + }; |
| 58 | + |
| 59 | + for value in headers { |
| 60 | + for part in value.as_str().trim().split(',') { |
| 61 | + let entry = VaryDirective::from_str(part)?; |
| 62 | + entries.push(entry); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Ok(Some(Self { entries })) |
| 67 | + } |
| 68 | + |
| 69 | + /// Sets the `If-Match` header. |
| 70 | + pub fn apply(&self, mut headers: impl AsMut<Headers>) { |
| 71 | + headers.as_mut().insert(VARY, self.value()); |
| 72 | + } |
| 73 | + |
| 74 | + /// Get the `HeaderName`. |
| 75 | + pub fn name(&self) -> HeaderName { |
| 76 | + VARY |
| 77 | + } |
| 78 | + |
| 79 | + /// Get the `HeaderValue`. |
| 80 | + pub fn value(&self) -> HeaderValue { |
| 81 | + let mut output = String::new(); |
| 82 | + for (n, directive) in self.entries.iter().enumerate() { |
| 83 | + let directive: HeaderValue = directive.clone().into(); |
| 84 | + match n { |
| 85 | + 0 => write!(output, "{}", directive).unwrap(), |
| 86 | + _ => write!(output, ", {}", directive).unwrap(), |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + // SAFETY: the internal string is validated to be ASCII. |
| 91 | + unsafe { HeaderValue::from_bytes_unchecked(output.into()) } |
| 92 | + } |
| 93 | + |
| 94 | + /// Push a directive into the list of entries. |
| 95 | + pub fn push( |
| 96 | + &mut self, |
| 97 | + directive: impl TryInto<VaryDirective, Error = crate::Error>, |
| 98 | + ) -> crate::Result<()> { |
| 99 | + self.entries.push(directive.try_into()?); |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + /// An iterator visiting all server entries. |
| 104 | + pub fn iter(&self) -> Iter<'_> { |
| 105 | + Iter { |
| 106 | + inner: self.entries.iter(), |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /// An iterator visiting all server entries. |
| 111 | + pub fn iter_mut(&mut self) -> IterMut<'_> { |
| 112 | + IterMut { |
| 113 | + inner: self.entries.iter_mut(), |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl IntoIterator for Vary { |
| 119 | + type Item = VaryDirective; |
| 120 | + type IntoIter = IntoIter; |
| 121 | + |
| 122 | + #[inline] |
| 123 | + fn into_iter(self) -> Self::IntoIter { |
| 124 | + IntoIter { |
| 125 | + inner: self.entries.into_iter(), |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<'a> IntoIterator for &'a Vary { |
| 131 | + type Item = &'a VaryDirective; |
| 132 | + type IntoIter = Iter<'a>; |
| 133 | + |
| 134 | + #[inline] |
| 135 | + fn into_iter(self) -> Self::IntoIter { |
| 136 | + self.iter() |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<'a> IntoIterator for &'a mut Vary { |
| 141 | + type Item = &'a mut VaryDirective; |
| 142 | + type IntoIter = IterMut<'a>; |
| 143 | + |
| 144 | + #[inline] |
| 145 | + fn into_iter(self) -> Self::IntoIter { |
| 146 | + self.iter_mut() |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/// A borrowing iterator over entries in `Vary`. |
| 151 | +#[derive(Debug)] |
| 152 | +pub struct IntoIter { |
| 153 | + inner: std::vec::IntoIter<VaryDirective>, |
| 154 | +} |
| 155 | + |
| 156 | +impl Iterator for IntoIter { |
| 157 | + type Item = VaryDirective; |
| 158 | + |
| 159 | + fn next(&mut self) -> Option<Self::Item> { |
| 160 | + self.inner.next() |
| 161 | + } |
| 162 | + |
| 163 | + #[inline] |
| 164 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 165 | + self.inner.size_hint() |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// A lending iterator over entries in `Vary`. |
| 170 | +#[derive(Debug)] |
| 171 | +pub struct Iter<'a> { |
| 172 | + inner: slice::Iter<'a, VaryDirective>, |
| 173 | +} |
| 174 | + |
| 175 | +impl<'a> Iterator for Iter<'a> { |
| 176 | + type Item = &'a VaryDirective; |
| 177 | + |
| 178 | + fn next(&mut self) -> Option<Self::Item> { |
| 179 | + self.inner.next() |
| 180 | + } |
| 181 | + |
| 182 | + #[inline] |
| 183 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 184 | + self.inner.size_hint() |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +/// A mutable iterator over entries in `Vary`. |
| 189 | +#[derive(Debug)] |
| 190 | +pub struct IterMut<'a> { |
| 191 | + inner: slice::IterMut<'a, VaryDirective>, |
| 192 | +} |
| 193 | + |
| 194 | +impl<'a> Iterator for IterMut<'a> { |
| 195 | + type Item = &'a mut VaryDirective; |
| 196 | + |
| 197 | + fn next(&mut self) -> Option<Self::Item> { |
| 198 | + self.inner.next() |
| 199 | + } |
| 200 | + |
| 201 | + #[inline] |
| 202 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 203 | + self.inner.size_hint() |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +impl ToHeaderValues for Vary { |
| 208 | + type Iter = option::IntoIter<HeaderValue>; |
| 209 | + fn to_header_values(&self) -> crate::Result<Self::Iter> { |
| 210 | + // A HeaderValue will always convert into itself. |
| 211 | + Ok(self.value().to_header_values().unwrap()) |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl Debug for Vary { |
| 216 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 217 | + let mut list = f.debug_list(); |
| 218 | + for directive in &self.entries { |
| 219 | + list.entry(directive); |
| 220 | + } |
| 221 | + list.finish() |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +#[cfg(test)] |
| 226 | +mod test { |
| 227 | + use crate::conditional::Vary; |
| 228 | + use crate::Response; |
| 229 | + |
| 230 | + #[test] |
| 231 | + fn smoke() -> crate::Result<()> { |
| 232 | + let mut entries = Vary::new(); |
| 233 | + entries.push("User-Agent")?; |
| 234 | + entries.push("Accept-Encoding")?; |
| 235 | + |
| 236 | + let mut res = Response::new(200); |
| 237 | + entries.apply(&mut res); |
| 238 | + |
| 239 | + let entries = Vary::from_headers(res)?.unwrap(); |
| 240 | + let mut entries = entries.iter(); |
| 241 | + assert_eq!(entries.next().unwrap(), "User-Agent"); |
| 242 | + assert_eq!(entries.next().unwrap(), "Accept-Encoding"); |
| 243 | + Ok(()) |
| 244 | + } |
| 245 | +} |
0 commit comments