pub const ENTRIES: [(&str, &str); 61] = [
(":authority", ""),
(":method", "GET"),
(":method", "POST"),
(":path", "/"),
(":path", "/index.html"),
(":scheme", "http"),
(":scheme", "https"),
(":status", "200"),
(":status", "204"),
(":status", "206"),
(":status", "304"),
(":status", "400"),
(":status", "404"),
(":status", "500"),
("accept-charset", ""),
("accept-encoding", "gzip, deflate"),
("accept-language", ""),
("accept-ranges", ""),
("accept", ""),
("access-control-allow-origin", ""),
("age", ""),
("allow", ""),
("authorization", ""),
("cache-control", ""),
("content-disposition", ""),
("content-encoding", ""),
("content-language", ""),
("content-length", ""),
("content-location", ""),
("content-range", ""),
("content-type", ""),
("cookie", ""),
("date", ""),
("etag", ""),
("expect", ""),
("expires", ""),
("from", ""),
("host", ""),
("if-match", ""),
("if-modified-since", ""),
("if-none-match", ""),
("if-range", ""),
("if-unmodified-since", ""),
("last-modified", ""),
("link", ""),
("location", ""),
("max-forwards", ""),
("proxy-authenticate", ""),
("proxy-authorization", ""),
("range", ""),
("referer", ""),
("refresh", ""),
("retry-after", ""),
("server", ""),
("set-cookie", ""),
("strict-transport-security", ""),
("transfer-encoding", ""),
("user-agent", ""),
("vary", ""),
("via", ""),
("www-authenticate", ""),
];
pub const DYNAMIC_BASE: usize = ENTRIES.len() + 1;
pub fn get(index: usize) -> Option<(&'static str, &'static str)> {
if index == 0 {
return None;
}
ENTRIES.get(index - 1).copied()
}
pub fn find(name: &str, value: &str) -> Option<usize> {
ENTRIES
.iter()
.position(|(n, v)| *n == name && *v == value)
.map(|i| i + 1)
}
pub fn find_name(name: &str) -> Option<usize> {
ENTRIES.iter().position(|(n, _)| *n == name).map(|i| i + 1)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_table_is_the_length_and_shape_the_rfc_defines() {
assert_eq!(ENTRIES.len(), 61);
assert_eq!(DYNAMIC_BASE, 62);
assert_eq!(get(1), Some((":authority", "")));
assert_eq!(get(2), Some((":method", "GET")));
assert_eq!(get(61), Some(("www-authenticate", "")));
assert_eq!(get(62), None);
assert_eq!(get(0), None);
}
#[test]
fn lookups_find_the_indices_the_rfc_examples_use() {
assert_eq!(find(":method", "GET"), Some(2));
assert_eq!(find(":path", "/"), Some(4));
assert_eq!(find(":scheme", "http"), Some(6));
assert_eq!(find(":status", "200"), Some(8));
assert_eq!(find_name(":authority"), Some(1));
assert_eq!(find_name("content-type"), Some(31));
assert_eq!(find(":method", "PUT"), None);
}
}