Skip to main content

g_server/http/
header.rs

1use std::collections::HashMap;
2
3/// HTTP headers.
4#[derive(Debug, Default)]
5pub struct Header {
6    values: HashMap<String, String>,
7}
8
9impl Header {
10    /// Creates an empty header collection.
11    pub fn new() -> Self {
12        Self::default()
13    }
14
15    /// Returns the value of a header.
16    pub fn get(&self, name: &str) -> Option<&str> {
17        self.values.get(name).map(String::as_str)
18    }
19
20    /// Inserts a header.
21    pub fn insert(&mut self, name: impl Into<String>, value: impl Into<String>) -> Option<String> {
22        self.values.insert(name.into(), value.into())
23    }
24
25    /// Removes a header.
26    pub fn remove(&mut self, name: &str) -> Option<String> {
27        self.values.remove(name)
28    }
29
30    /// Returns whether the headers contain the given name.
31    pub fn contains(&self, name: &str) -> bool {
32        self.values.contains_key(name)
33    }
34
35    /// Returns the number of headers.
36    pub fn len(&self) -> usize {
37        self.values.len()
38    }
39
40    /// Returns whether there are no headers.
41    pub fn is_empty(&self) -> bool {
42        self.values.is_empty()
43    }
44
45    /// Returns an iterator over the headers.
46    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
47        self.values
48            .iter()
49            .map(|(name, value)| (name.as_str(), value.as_str()))
50    }
51}