Expand description
User-agent detection: the operating system, client, device and bot behind a user-agent string.
Any bytes are accepted, including bytes that are not UTF-8, and what is read from the string is borrowed from it.
use utopia_user_agent::UserAgent;
let agent = UserAgent::parse("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
assert_eq!(agent.operating_system().version.as_deref(), Some(b"7".as_slice()));
assert_eq!(agent.client().name.as_deref(), Some(b"Firefox".as_slice()));
assert!(!agent.is_bot());Detection runs on every request, so it avoids regular expressions where
native code does the same job: case-insensitive substring, word and
<token>/<version> scans that never allocate, and one pass over the
string for the whole bot table. The few patterns left (device models
and brands, Internet Explorer, the generic bot rule) are compiled once
per process, and run only when the string holds a literal they need.
PHP: the port of utopia-php/user-agent (packages/user-agent), with
identical results for every input. The remaining patterns are the PHP
library’s, run by php_std::pcre (a port of the PCRE2 PHP uses); each
native scan names the pattern it replaces, and the crate’s tests check
every one, and every prefilter, against that pattern.
| PHP | Rust |
|---|---|
UserAgent::parse($value) | UserAgent::parse |
UserAgent::raw() | UserAgent::raw |
UserAgent::operatingSystem() / client() / device() / bot() | UserAgent::operating_system, UserAgent::client, UserAgent::device, UserAgent::bot |
UserAgent::isBot() | UserAgent::is_bot |
UserAgent::toArray(), *::toArray() | the public fields of OperatingSystem, Client, Device, Bot |
OperatingSystem (code, name, version, isKnown()) | OperatingSystem, OperatingSystem::is_known |
Client (type, …, engineVersion, isKnown(), isBrowser()) | Client (kind, …, engine_version), Client::is_known, Client::is_browser |
Device (type, brand, model, isKnown()) | Device (kind, …), Device::is_known |
new Bot($name, $category = 'crawler') | Bot::new, Bot::with_category |
Detection\OperatingSystemDetector::detect() | detection::operating_system |
Detection\ClientDetector::detect() | detection::client |
Detection\DeviceDetector::detect() | detection::device |
Detection\BotDetector::detect() | detection::bot |
PHP: Appwrite’s session attributes (Appwrite\Detector\Detector) are in
appwrite_core::detector.
Modules§
- detection
- The detectors: pure functions from a user-agent string to what it
reveals.
UserAgentruns them lazily and keeps their results.
Structs§
- Bot
- A bot a user-agent string names: a crawler, preview fetcher, monitor or automation tool.
- Client
- The client a user-agent string names: a browser, an HTTP library or a native app.
- Device
- The device a user-agent string names.
- Operating
System - The operating system a user-agent string names; every field is
Nonewhen it names none. - User
Agent - A lazily evaluated analysis of one user-agent string.
Type Aliases§
- Text
- A text value: borrowed from the user-agent or from a static table, owned
when detection rewrites it (
17_4becomes17.4).