|
| 1 | +use crate::util::escape_and_elide; |
| 2 | +use crate::AsciiString; |
| 3 | +use core::fmt::{Debug, Display, Formatter}; |
| 4 | +use std::ops::{Deref, DerefMut}; |
| 5 | + |
| 6 | +#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 7 | +pub struct Header { |
| 8 | + pub name: AsciiString, |
| 9 | + pub value: AsciiString, |
| 10 | +} |
| 11 | +impl Header { |
| 12 | + #[must_use] |
| 13 | + pub fn new(name: AsciiString, value: AsciiString) -> Self { |
| 14 | + Self { name, value } |
| 15 | + } |
| 16 | +} |
| 17 | +impl Debug for Header { |
| 18 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 19 | + write!( |
| 20 | + f, |
| 21 | + "Header({}:{})", |
| 22 | + escape_and_elide(self.name.as_bytes(), 30), |
| 23 | + escape_and_elide(self.value.as_bytes(), 1000) |
| 24 | + ) |
| 25 | + } |
| 26 | +} |
| 27 | +impl Display for Header { |
| 28 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 29 | + write!(f, "{}:{}", self.name.as_str(), self.value.as_str()) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Clone, Eq, PartialEq)] |
| 34 | +pub struct HeaderList(pub Vec<Header>); |
| 35 | +impl HeaderList { |
| 36 | + #[must_use] |
| 37 | + pub fn new() -> Self { |
| 38 | + Self(Vec::new()) |
| 39 | + } |
| 40 | + |
| 41 | + /// Adds a header. |
| 42 | + /// |
| 43 | + /// You can call this multiple times to add multiple headers with the same name. |
| 44 | + /// |
| 45 | + /// The [HTTP spec](https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4) |
| 46 | + /// limits header names to US-ASCII and header values to US-ASCII or ISO-8859-1. |
| 47 | + /// |
| 48 | + /// # Panics |
| 49 | + /// Panics when `name` is not US-ASCII. |
| 50 | + pub fn add(&mut self, name: impl AsRef<str>, value: AsciiString) { |
| 51 | + self.0 |
| 52 | + .push(Header::new(name.as_ref().try_into().unwrap(), value)); |
| 53 | + } |
| 54 | + |
| 55 | + /// Searches for a header that matches `name`. |
| 56 | + /// Uses a case-insensitive comparison. |
| 57 | + /// |
| 58 | + /// Returns the value of the header. |
| 59 | + /// |
| 60 | + /// Returns `None` when multiple headers matched or none matched. |
| 61 | + pub fn get_only(&self, name: impl AsRef<str>) -> Option<&AsciiString> { |
| 62 | + let mut value = None; |
| 63 | + for header in &self.0 { |
| 64 | + if header.name.eq_ignore_ascii_case(name.as_ref()) { |
| 65 | + if value.is_some() { |
| 66 | + return None; |
| 67 | + } |
| 68 | + value = Some(&header.value); |
| 69 | + } |
| 70 | + } |
| 71 | + value |
| 72 | + } |
| 73 | + |
| 74 | + /// Looks for headers with names that match `name`. |
| 75 | + /// Uses a case-insensitive comparison. |
| 76 | + /// Returns the values of the matching headers. |
| 77 | + pub fn get_all(&self, name: impl AsRef<str>) -> Vec<&AsciiString> { |
| 78 | + let mut headers = Vec::new(); |
| 79 | + for header in &self.0 { |
| 80 | + if header.name.eq_ignore_ascii_case(name.as_ref()) { |
| 81 | + headers.push(&header.value); |
| 82 | + } |
| 83 | + } |
| 84 | + headers |
| 85 | + } |
| 86 | + |
| 87 | + /// Removes all headers with the specified `name`. |
| 88 | + /// Uses a case-insensitive comparison. |
| 89 | + /// |
| 90 | + /// When only one header matched, returns the value of that header. |
| 91 | + /// |
| 92 | + /// Returns `None` when multiple headers matched or none matched. |
| 93 | + pub fn remove_only(&mut self, name: impl AsRef<str>) -> Option<AsciiString> { |
| 94 | + let mut iter = self.remove_all(name).into_iter(); |
| 95 | + match (iter.next(), iter.next()) { |
| 96 | + (Some(value), None) => Some(value), |
| 97 | + _ => None, |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// Removes all headers with the specified `name`. |
| 102 | + /// Uses a case-insensitive comparison. |
| 103 | + /// |
| 104 | + /// Returns the values of the headers. |
| 105 | + pub fn remove_all(&mut self, name: impl AsRef<str>) -> Vec<AsciiString> { |
| 106 | + let mut values = Vec::new(); |
| 107 | + let mut n = 0; |
| 108 | + while n < self.0.len() { |
| 109 | + if self.0[n].name.eq_ignore_ascii_case(name.as_ref()) { |
| 110 | + let header = self.0.swap_remove(n); |
| 111 | + values.push(header.value); |
| 112 | + } else { |
| 113 | + n += 1; |
| 114 | + } |
| 115 | + } |
| 116 | + values |
| 117 | + } |
| 118 | +} |
| 119 | +impl Debug for HeaderList { |
| 120 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 121 | + let mut strings: Vec<String> = self |
| 122 | + .iter() |
| 123 | + .map(|h| format!("{}: {:?}", h.name, h.value.as_str())) |
| 124 | + .collect(); |
| 125 | + strings.sort(); |
| 126 | + write!(f, "{{{}}}", strings.join(", "),) |
| 127 | + } |
| 128 | +} |
| 129 | +impl Default for HeaderList { |
| 130 | + fn default() -> Self { |
| 131 | + Self::new() |
| 132 | + } |
| 133 | +} |
| 134 | +impl Deref for HeaderList { |
| 135 | + type Target = Vec<Header>; |
| 136 | + |
| 137 | + fn deref(&self) -> &Self::Target { |
| 138 | + &self.0 |
| 139 | + } |
| 140 | +} |
| 141 | +impl DerefMut for HeaderList { |
| 142 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 143 | + &mut self.0 |
| 144 | + } |
| 145 | +} |
| 146 | +impl<'x> IntoIterator for &'x HeaderList { |
| 147 | + type Item = &'x Header; |
| 148 | + type IntoIter = core::slice::Iter<'x, Header>; |
| 149 | + |
| 150 | + fn into_iter(self) -> Self::IntoIter { |
| 151 | + self.0.iter() |
| 152 | + } |
| 153 | +} |
| 154 | +impl<'x> IntoIterator for &'x mut HeaderList { |
| 155 | + type Item = &'x mut Header; |
| 156 | + type IntoIter = core::slice::IterMut<'x, Header>; |
| 157 | + |
| 158 | + fn into_iter(self) -> Self::IntoIter { |
| 159 | + self.0.iter_mut() |
| 160 | + } |
| 161 | +} |
0 commit comments