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