apis/utils/src/is_human.rs
2023-08-12 13:58:28 -07:00

17 lines
591 B
Rust

use actix_web::{http::header, HttpRequest};
pub fn is_human(request: &HttpRequest) -> bool {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
const HUMANS: &[&str] = &[
"Firefox", "Chrome", // Chrome and Chromium browsers
"Opera", // Old Presto-based Opera
"Mobile", // Safari
"Trident", // Internet Explorer
];
request
.headers()
.get(header::USER_AGENT)
.and_then(|header| header.to_str().ok())
.map(|ua| HUMANS.iter().any(|&human| ua.contains(human)))
.unwrap_or(false)
}