use super::*;
#[inline(always)]
pub(crate) fn status_has_no_body(status: usize) -> bool {
(100..200).contains(&status) || status == 204 || status == 304
}
#[inline]
pub(crate) fn is_mime_excluded(content_type: &str, excluded_prefixes: &[String]) -> bool {
let media: &str = match content_type.split_once(';') {
Some((head, _)) => head.trim(),
None => content_type.trim(),
};
for prefix in excluded_prefixes {
if media.len() >= prefix.len()
&& media.as_bytes()[..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
{
return true;
}
}
false
}
#[inline]
pub(crate) fn has_no_transform(cache_control: &str) -> bool {
cache_control
.split(',')
.any(|dir: &str| dir.trim().eq_ignore_ascii_case(CACHE_CONTROL_NO_TRANSFORM))
}
#[inline]
pub(crate) fn contains_ascii_icase(haystack: &[u8], needle: &[u8]) -> bool {
let n_len: usize = needle.len();
if n_len == 0 || haystack.len() < n_len {
return false;
}
haystack
.windows(n_len)
.any(|win: &[u8]| win.eq_ignore_ascii_case(needle))
}
#[inline]
pub(crate) fn negotiate_response_coding(
request: &Request,
enable_br: bool,
enable_gzip: bool,
enable_deflate: bool,
) -> Option<Compress> {
let values: &VecDeque<String> = request.get_headers().get(ACCEPT_ENCODING)?;
let raw: &str = values.back()?;
let negotiated: Compress = Compress::from_accept_encoding(raw)?;
match negotiated {
Compress::Br if enable_br => Some(Compress::Br),
Compress::Gzip if enable_gzip => Some(Compress::Gzip),
Compress::Deflate if enable_deflate => Some(Compress::Deflate),
_ => {
let raw_bytes: &[u8] = raw.as_bytes();
if enable_gzip && contains_ascii_icase(raw_bytes, GZIP_TOKEN_BYTES) {
Some(Compress::Gzip)
} else if enable_deflate && contains_ascii_icase(raw_bytes, DEFLATE_TOKEN_BYTES) {
Some(Compress::Deflate)
} else {
None
}
}
}
}
#[inline]
pub(crate) fn ensure_vary_accept_encoding(response: &mut Response) {
if let Some(slot) = response.get_headers().get(VARY) {
let already: bool = slot.iter().any(|v: &String| {
v.split(',').any(|tok: &str| {
let t: &str = tok.trim();
t == VARY_WILDCARD || t.eq_ignore_ascii_case(VARY_ACCEPT_ENCODING)
})
});
if !already {
response.add_header(VARY, VARY_ACCEPT_ENCODING);
}
} else {
response.set_header(VARY, VARY_ACCEPT_ENCODING);
}
}