use crate::head::ListElements;
const HOP_BY_HOP: &[&str] = &[
"connection",
"keep-alive",
"proxy-connection",
"te",
"transfer-encoding",
"upgrade",
"trailer",
];
#[must_use]
pub fn is_hop_by_hop(name: &str, mut connection_options: ListElements<'_, '_>) -> bool {
HOP_BY_HOP.iter().any(|h| h.eq_ignore_ascii_case(name))
|| connection_options.any(|o| o.eq_ignore_ascii_case(name.as_bytes()))
}
#[cfg(test)]
mod tests {
use crate::{ServerConn, ServerEvent};
use super::*;
#[test]
fn test_is_hop_by_hop_fixed_set_and_connection_options() {
let mut conn = ServerConn::new();
let _ = conn.feed(
b"GET / HTTP/1.1\r\nHost: a\r\nConnection: close, X-A\r\nConnection: x-b\r\n\r\n",
);
let Ok(ServerEvent::Request(req)) = conn.poll() else {
panic!("request");
};
for name in HOP_BY_HOP {
assert!(is_hop_by_hop(name, req.connection_options()), "{name}");
let upper = name.to_ascii_uppercase();
assert!(is_hop_by_hop(&upper, req.connection_options()), "{upper}");
}
assert!(is_hop_by_hop("X-A", req.connection_options()));
assert!(is_hop_by_hop("x-b", req.connection_options()));
assert!(is_hop_by_hop("close", req.connection_options()));
for name in ["host", "accept", "x-c", "content-length", "trailers"] {
assert!(!is_hop_by_hop(name, req.connection_options()), "{name}");
}
}
}