use crate::error::{AppError, AppResult, ErrorKind};
use super::constants::{
POOL_IDLE_TIMEOUT_SECS, POOL_MAX_IDLE_PER_HOST, TCP_KEEPALIVE_IDLE_SECS,
TCP_KEEPALIVE_INTERVAL_SECS, TCP_KEEPALIVE_RETRIES,
};
pub(super) fn ascii_contains_ignore_case(haystack: &str, needle: &str) -> bool {
if needle.is_empty() {
return true;
}
let n = needle.as_bytes();
haystack
.as_bytes()
.windows(n.len())
.any(|w| w.eq_ignore_ascii_case(n))
}
pub fn content_type_looks_json(ct: Option<&str>) -> bool {
ct.is_some_and(|c| ascii_contains_ignore_case(c, "json"))
}
pub fn content_type_looks_html(ct: Option<&str>) -> bool {
ct.is_some_and(|c| {
ascii_contains_ignore_case(c, "html") || ascii_contains_ignore_case(c, "xhtml")
})
}
pub fn require_content_type_json(ct: Option<&str>) -> AppResult<()> {
match ct {
None => Ok(()),
Some(c) if content_type_looks_json(Some(c)) => Ok(()),
Some(c) => Err(AppError::new(
ErrorKind::Parse,
format!("unexpected Content-Type for JSON response: {c}"),
)),
}
}
pub fn require_content_type_html(ct: Option<&str>) -> AppResult<()> {
match ct {
None => Ok(()),
Some(c) if content_type_looks_html(Some(c)) => Ok(()),
Some(c) => Err(AppError::new(
ErrorKind::Parse,
format!("unexpected Content-Type for HTML response: {c}"),
)),
}
}
pub const RUSTLS_VERSION_FLOOR: &str = "0.23.18";
pub const TLS_CRYPTO_PROVIDER: &str = "ring";
pub fn client_posture_detail() -> String {
format!(
"rustls≥{RUSTLS_VERSION_FLOOR} provider={TLS_CRYPTO_PROVIDER} TLS≥1.2 webpki-roots http2 system-proxy tcp_nodelay keepalive={TCP_KEEPALIVE_IDLE_SECS}s/{TCP_KEEPALIVE_INTERVAL_SECS}s/r{TCP_KEEPALIVE_RETRIES} pool_idle={POOL_MAX_IDLE_PER_HOST}@{POOL_IDLE_TIMEOUT_SECS}s host-allowlist no-danger-tls"
)
}