use super::*;
impl Default for CompressionPolicy {
fn default() -> Self {
Self {
min_size: DEFAULT_MIN_SIZE,
encodings: vec![ENCODING_GZIP.to_owned(), ENCODING_BROTLI.to_owned()],
}
}
}
impl CompressionPolicy {
pub fn pick(&self, accept_encoding: &str) -> Option<String> {
let tokens: Vec<String> = self.accepted(accept_encoding);
for encoding in self.get_encodings().iter() {
if encoding.is_empty() {
continue;
}
if tokens
.iter()
.any(|token: &String| token == TOKEN_ANY || token == encoding)
{
return Some(encoding.clone());
}
}
None
}
pub fn accepted(&self, accept_encoding: &str) -> Vec<String> {
let mut tokens: Vec<String> = Vec::new();
for part in accept_encoding.split(SEPARATOR_COMMA) {
let token: String = part.split(';').next().unwrap_or("").trim().to_lowercase();
if token.is_empty() {
continue;
}
let refused: bool = part.split(';').skip(1).any(|parameter: &str| {
let quality: Option<&str> = parameter.trim().strip_prefix(PARAMETER_QUALITY);
match quality {
Some(quality) => quality
.parse::<f32>()
.map(|value: f32| value <= 0.0)
.unwrap_or(false),
None => false,
}
});
if !refused {
tokens.push(token);
}
}
tokens
}
pub fn is_compressible(content_type: &str) -> bool {
let content_type: String = content_type.trim().to_lowercase();
if content_type.is_empty() {
return true;
}
if content_type == TEXT_EVENT_STREAM {
return false;
}
if CONTENT_TYPES_COMPRESSED.contains(&content_type.as_str()) {
return false;
}
!CONTENT_TYPE_PREFIXES_COMPRESSED
.iter()
.any(|prefix: &&str| content_type.starts_with(*prefix))
}
}