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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
pub struct Headers {
    pub vec: HeadersType,
}

pub type HeaderType = (String, String);
pub type HeadersType = Vec<HeaderType>;

pub static DISALLOWED_TRAILERS: [&'static str; 34] = [
    //authorization
    "authorization",
    "www-authenticate",
    "proxy-authenticate",
    "proxy-authorization",
    "set-cookie",
    "cookie",
    "cookie2",
    "access-control-allow-origin",
    "access-control-allow-headers",

    //transfer
    "transfer-encoding",
    "content-length",
    "trailer",
    "location",
    "vary",
    "retry-after",
    "content-encoding",
    "accept-encoding",
    "content-type",
    "content-range",
    "keep-alive",
    "upgrade",

    //controls
    "cache-control",
    "expect",
    "max-forwards",
    "pragma",
    "range",
    "te",
    "expect",
    "dnt",
    "feature-policy",
    "via",

    //routing
    "host",
    "connection",
    "origin",

];


impl Headers {
    pub fn empty() -> Headers {
        Headers { vec: vec!() }
    }

    pub fn from(pairs: Vec<(&str, &str)>) -> Headers {
        let mut headers = Headers { vec: vec!() };
        for pair in pairs {
            headers = headers.add(pair)
        }
        headers
    }

    pub fn from_headers(headers: &Headers) -> Headers {
        let mut new = Headers { vec: vec!() };
        for pair in &headers.vec {
            new = new.add((pair.0.as_str(), pair.1.as_str()))
        }
        new
    }

    pub fn add(&self, header: (&str, &str)) -> Headers {
        let mut new: HeadersType = vec!();
        let mut exists = false;
        for existing in &self.vec {
            if existing.0.to_lowercase() == header.0.to_lowercase() {
                let string = existing.clone().1.to_string() + ", " + header.1;
                new.push((existing.clone().0, string.clone()));
                exists = true
            } else {
                new.push(existing.clone())
            }
        }
        if !exists {
            new.push((header.0.to_string(), header.1.to_string()))
        }
        Headers { vec: new }
    }

    pub fn ensure(&self, header: (&str, &str)) -> Headers {
        if !self.has(header.0) {
            self.add(header)
        } else {
            Headers::add_all(self, Headers::empty())
        }
    }

    pub fn add_all(&self, headers: Headers) -> Headers {
        let mut new = Headers::empty();
        headers.vec.iter().for_each(|x| {
            new = new.add((x.0.as_str(), x.1.as_str()))
        });
        self.vec.iter().for_each(|x| {
            new = new.add((x.0.as_str(), x.1.as_str()))
        });
        new
    }

    pub fn replace(&self, replacing: (&str, &str)) -> Headers {
        let mut new: HeadersType = vec!();
        let mut exists = false;
        for existing in &self.vec {
            if existing.0.to_lowercase() == replacing.0.to_lowercase() {
                new.push((existing.clone().0, replacing.1.to_string()));
                exists = true
            } else {
                new.push((existing.0.to_string(), existing.1.to_string()))
            }
        }
        if !exists {
            new.push((replacing.0.to_string(), replacing.1.to_string()));
        }
        Headers { vec: new }
    }

    pub fn remove(&self, name: &str) -> Headers {
        let mut new: HeadersType = vec!();
        for existing in &self.vec {
            if existing.0.to_lowercase() == name.to_lowercase() {} else {
                new.push((existing.0.to_string(), existing.1.to_string()))
            }
        }
        Headers { vec: new }
    }

    pub fn get(&self, name: &str) -> Option<String> {
        for header in &self.vec {
            if header.0.to_lowercase() == name.to_lowercase() {
                return Some(header.1.to_string());
            }
        }
        None
    }

    pub fn filter(&self, names: Vec<&str>) -> Headers {
        let mut vec = vec!();
        for name in names {
            if let Some(value) = self.get(name) {
                vec.push((name.to_string(), value) as HeaderType);
            }
        }
        Headers { vec }
    }

    pub fn is_empty(&self) -> bool {
        self.vec.is_empty()
    }

    pub fn has(&self, header_name: &str) -> bool {
        return self.get(header_name).is_some();
    }

    pub fn content_length_header(&self) -> Option<Result<usize, String>> {
        let value = self.get("Content-Length");
        let value = Self::parse_or_else_value(value);
        match value {
            Some(Err(ref header_value)) => {
                let split = header_value.split(", ").map(|it| it.to_string()).collect::<Vec<String>>();
                let first = split.first().map(|it| it.to_string());
                if split.iter().all(|v| v == first.as_ref().unwrap()) {
                    Self::parse_or_else_value(first)
                } else {
                    value.clone()
                }
            }
            _ => value
        }
    }

    fn parse_or_else_value(value: Option<String>) -> Option<Result<usize, String>> {
        value.as_ref().map(|x| x.parse::<usize>())
            .map(|r| r.map_err(|_| value.unwrap()))
    }

    pub fn parse_from(header_string: &str) -> Headers {
        if header_string.is_empty() {
            return Headers::empty();
        }
        header_string.split("\r\n").fold(Headers::empty(), |acc, pair| {
            let pair = pair.split(": ").collect::<Vec<&str>>();

            acc.add((pair[0], pair[1]))
        })
    }

    pub fn to_wire_string(&self) -> String {
        self.vec.iter().map(|h| {
            let clone = h.clone();
            clone.0.to_owned() + ": " + clone.1.as_str()
        }).collect::<Vec<String>>()
            .join("\r\n")
    }

    pub fn js_headers_from_string(str: &str) -> Headers {
        str.split("; ").fold(Headers::empty(), |acc: Headers, next: &str| {
            let mut split = next.split(": ");
            acc.add((split.next().unwrap(), split.next().unwrap()))
        })
    }

    pub fn js_headers_to_string(headers: &HeadersType) -> String {
        headers.iter().map(|h| {
            let clone = h.clone();
            clone.0 + ": " + clone.1.as_str()
        }).collect::<Vec<String>>()
            .join("; ")
    }
}

pub fn content_type_header(value: &str) -> (&'static str, &str) {
    ("Content-Type", value)
}

pub fn cache_control_header(value: &str) -> (&'static str, &str) { ("Cache-Control", value) }

pub fn content_length_header(value: &str) -> (&'static str, &str) { ("Content-Length", value) }