use crate::modules::pxutils;
use crate::px_debug;
use crate::pxconfig::PXConfig;
use fastly::Request;
pub fn should_filter_request(req: &Request, conf: &PXConfig) -> bool {
for ext in &conf.filter_by_extension {
if req.get_method_str() == "GET"
&& req.get_path().trim().to_lowercase().ends_with(ext.as_str())
{
px_debug!("Skipping verification for whitelist ext: {}", ext);
return true;
}
}
for ua in &conf.filter_by_user_agent {
if let Some(header_value) = req.get_header_str_lossy("user-agent") {
if header_value.as_ref() == ua.trim() {
px_debug!("Skipping verification for filtered user agent: {}", ua);
return true;
}
}
}
for whitelist_prefix in &conf.filter_by_route {
if req.get_path().trim().starts_with(whitelist_prefix) {
px_debug!(
"Skipping verification for filtered prefix: {}",
whitelist_prefix
);
return true;
}
}
if let Some(client_ip) = pxutils::extract_ip_from_configured_headers(req, &conf.ip_headers)
.or_else(|| req.get_client_ip_addr().map(|ip| ip.to_string()))
{
for ip in &conf.filter_by_ip {
if pxutils::ip_matches_filter(client_ip.as_str(), ip.as_str()) {
px_debug!("Skipping verification for filtered IP: {}", ip);
return true;
}
}
}
for method in &conf.filter_by_http_method {
if req.get_method_str().eq_ignore_ascii_case(method.trim()) {
px_debug!("Skipping verification for filtered method: {}", method);
return true;
}
}
false
}