use super::*;
#[inline]
pub(crate) fn gateway_now_ms() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|dur: Duration| dur.as_millis() as u64)
.unwrap_or(0)
}
#[inline(always)]
pub(crate) fn request_header_back<'a>(req: &'a Request, lower_key: &str) -> Option<&'a str> {
req.get_headers()
.get(lower_key)
.and_then(|values: &RequestHeadersValue| values.back())
.map(String::as_str)
}
pub(crate) fn normalize_prefix(prefix: &str) -> String {
let trimmed: &str = prefix.trim();
if trimmed.is_empty() || trimmed == "/" {
return "/".to_owned();
}
let with_slash: String = if trimmed.starts_with('/') {
trimmed.to_owned()
} else {
format!("/{trimmed}")
};
with_slash.trim_end_matches('/').to_owned()
}
#[inline(always)]
pub fn path_matches_prefix(path: &str, prefix: &str) -> bool {
if prefix == "/" {
return true;
}
let Some(rest) = path.strip_prefix(prefix) else {
return false;
};
rest.is_empty() || rest.as_bytes()[0] == b'/'
}
#[inline]
pub fn build_upstream_url(
base_url: &str,
path: &str,
query: &str,
prefix: &str,
strip_prefix: bool,
) -> String {
let forward_path: &str = if strip_prefix && prefix != "/" {
let stripped: &str = path.strip_prefix(prefix).unwrap_or(path);
if stripped.is_empty() { "/" } else { stripped }
} else if path.is_empty() {
"/"
} else {
path
};
let extra: usize = if query.is_empty() { 0 } else { 1 + query.len() };
let mut url: String = String::with_capacity(base_url.len() + forward_path.len() + extra);
url.push_str(base_url);
url.push_str(forward_path);
if !query.is_empty() {
url.push('?');
url.push_str(query);
}
url
}
#[inline]
pub(crate) fn build_upstream_url_from_request(
base_url: &str,
req: &Request,
prefix: &str,
strip_prefix: bool,
) -> String {
let path: &str = req.get_path();
let forward_path: &str = if strip_prefix && prefix != "/" {
let stripped: &str = path.strip_prefix(prefix).unwrap_or(path);
if stripped.is_empty() { "/" } else { stripped }
} else if path.is_empty() {
"/"
} else {
path
};
let mut query_len: usize = 0;
for (k, vals) in req.get_querys() {
for v in vals {
query_len += 1 + k.len() + if v.is_empty() { 0 } else { 1 + v.len() };
}
}
let mut url: String = String::with_capacity(base_url.len() + forward_path.len() + query_len);
url.push_str(base_url);
url.push_str(forward_path);
let mut first: bool = true;
for (k, vals) in req.get_querys() {
for v in vals {
url.push(if first { '?' } else { '&' });
first = false;
url.push_str(k);
if !v.is_empty() {
url.push('=');
url.push_str(v);
}
}
}
url
}
#[inline(always)]
pub fn is_hop_by_hop_header(name: &str, extra_connection_headers: &HashSet<String>) -> bool {
let is_standard: bool = match name.len() {
2 => name.eq_ignore_ascii_case(TE),
7 => name.eq_ignore_ascii_case(UPGRADE) || name.eq_ignore_ascii_case(HEADER_TRAILER),
10 => name.eq_ignore_ascii_case(CONNECTION) || name.eq_ignore_ascii_case(KEEP_ALIVE),
16 => name.eq_ignore_ascii_case(HEADER_PROXY_CONNECTION),
17 => name.eq_ignore_ascii_case(TRANSFER_ENCODING),
18 => name.eq_ignore_ascii_case(PROXY_AUTHENTICATE),
19 => name.eq_ignore_ascii_case(PROXY_AUTHORIZATION),
_ => false,
};
if is_standard {
return true;
}
if extra_connection_headers.is_empty() {
return false;
}
if extra_connection_headers.contains(name) {
return true;
}
if !name.bytes().any(|b: u8| b.is_ascii_uppercase()) {
return false;
}
extra_connection_headers.contains(&name.to_ascii_lowercase())
}
#[inline]
pub(crate) fn collect_custom_connection_tokens(val: &str, tokens: &mut HashSet<String>) {
for part in val.split(',') {
let trimmed: &str = part.trim();
if trimmed.is_empty()
|| trimmed.eq_ignore_ascii_case(KEEP_ALIVE)
|| trimmed.eq_ignore_ascii_case(HEADER_VALUE_CLOSE)
{
continue;
}
tokens.insert(trimmed.to_ascii_lowercase());
}
}
#[inline]
pub(crate) fn extract_request_connection_tokens(req: &Request) -> HashSet<String> {
let mut tokens: HashSet<String> = HashSet::new();
if let Some(values) = req.get_headers().get(CONNECTION) {
for val in values {
collect_custom_connection_tokens(val, &mut tokens);
}
}
tokens
}
#[inline]
pub(crate) fn extract_response_connection_tokens(
headers: &HashMapXxHash3_64<String, String>,
) -> HashSet<String> {
let mut tokens: HashSet<String> = HashSet::new();
for (k, val) in headers {
if k.len() == CONNECTION.len() && k.eq_ignore_ascii_case(CONNECTION) {
collect_custom_connection_tokens(val, &mut tokens);
}
}
tokens
}