1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::collections::hash_map;
use std::collections::HashMap;

/// The HTTP header map.
/// All the names are not case sensitive.
///
/// # Example
///
/// ```
/// let mut headers = mini_async_http::Headers::new();
///
/// assert!(headers.get_header("missing").is_none());
///
/// headers.set_header("Content-type","text/plain");
/// assert_eq!(headers.get_header("content-type").unwrap(),"text/plain");
///
/// headers.set_header("Content-type","application/json");
/// assert_eq!(headers.get_header("content-type").unwrap(),"application/json");
/// ```
#[derive(Debug, Clone)]
pub struct Headers {
    map: HashMap<String, String>,
}

impl Headers {
    /// Init an empty header struct
    pub fn new() -> Headers {
        Headers {
            map: HashMap::new(),
        }
    }

    /// Set the given header name to the given value. If the key already exists overwrite the value.
    pub fn set_header(&mut self, name: &str, value: &str) {
        let name = name.to_ascii_lowercase();
        let value = value.to_ascii_lowercase();

        self.map.insert(name, value);
    }

    /// Retrieve the value at the given key
    pub fn get_header(&self, name: &str) -> Option<&String> {
        let name = name.to_ascii_lowercase();

        self.map.get(&name)
    }

    /// Return an iterator over all the headers. All keys are lowercase
    pub fn iter(&self) -> HeaderIterator {
        HeaderIterator {
            inner: self.map.iter(),
        }
    }
}

impl PartialEq for Headers {
    fn eq(&self, other: &Headers) -> bool {
        if self.map == other.map {
            return true;
        }

        if self.map.len() != other.map.len() {
            return false;
        }

        self.map
            .iter()
            .map(|(key, value)| match other.get_header(key) {
                Some(val) => {
                    if val != value {
                        return false;
                    }
                    true
                }
                None => false,
            })
            .filter(|val| !*val)
            .count()
            == 0
    }
}

impl Default for Headers {
    fn default() -> Self {
        Headers::new()
    }
}

impl IntoIterator for Headers {
    type Item = (String, String);
    type IntoIter = hash_map::IntoIter<String, String>;

    fn into_iter(self) -> Self::IntoIter {
        self.map.into_iter()
    }
}

pub struct HeaderIterator<'a> {
    inner: hash_map::Iter<'a, String, String>,
}

impl<'a> Iterator for HeaderIterator<'a> {
    type Item = (&'a String, &'a String);

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl<'a> ExactSizeIterator for HeaderIterator<'a> {
    fn len(&self) -> usize {
        self.inner.len()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn eq() {
        let a = Headers::new();
        let b = Headers::new();

        assert_eq!(a, b)
    }

    #[test]
    fn key_eq() {
        let mut a = Headers::new();
        let mut b = Headers::new();

        a.set_header(&String::from("key"), &String::from("val"));
        a.set_header(&String::from("Content_length"), &String::from("89"));
        b.set_header(&String::from("KEY"), &String::from("val"));
        b.set_header(&String::from("Content_length"), &String::from("89"));

        assert_eq!(a, b)
    }

    #[test]
    fn not_eq() {
        let mut a = Headers::new();
        let b = Headers::new();

        a.set_header(&String::from("key"), &String::from("val"));

        assert_ne!(a, b)
    }

    #[test]
    fn not_eq_longer() {
        let mut a = Headers::new();
        let mut b = Headers::new();

        a.set_header(&String::from("key"), &String::from("val"));
        a.set_header(&String::from("Content_length"), &String::from("89"));
        b.set_header(&String::from("KEY"), &String::from("val"));
        b.set_header(&String::from("Content_length"), &String::from("89"));
        b.set_header(&String::from("diff"), &String::from("diff"));

        assert_ne!(a, b)
    }

    #[test]
    fn not_eq_val() {
        let mut a = Headers::new();
        let mut b = Headers::new();

        a.set_header(&String::from("key"), &String::from("valdiff"));
        a.set_header(&String::from("Content_length"), &String::from("89"));
        b.set_header(&String::from("KEY"), &String::from("val"));
        b.set_header(&String::from("Content_length"), &String::from("89"));

        assert_ne!(a, b)
    }
}