Rust

User agent

Partly Rust
User-agent parsing: operating system, client, device and bot.

In Appwrite#

Appwrite runs some of its classes from the Rust crate; the rest are still PHP.

1 PHP classes served from Rust4 still PHP
Rust 2,220 lines (1,670 code)· PHP 136 lines (100 code)split
PartCodeCommentsBlankTotalFiles
Rust source1,6703821682,22012
PHP source10012241364

Comments include doc comments. Each side counts its src/; the PHP side is what the package still has in PHP.

Served from Rust (1)
Utopia\UserAgent\UserAgent
Still PHP (4)
Utopia\UserAgent\BotUtopia\UserAgent\ClientUtopia\UserAgent\DeviceUtopia\UserAgent\OperatingSystem

Install#

Shell
cargo add utopia-user-agent

Getting started#

Common tasks with User agent, each a complete program you can copy and run.

A user-agent string tells a server what made a request: the browser, its operating system, the device it runs on, or the bot or HTTP library behind it. This library reads those facts from the User-Agent header without data files or network calls. Each category is detected only when you ask for it, and unknown fields are None, so any header is safe to parse. This guide covers the common tasks.

Identify the browser

Parse the header, then read the client. Text values are bytes, because a header can carry any byte.

Rust
use utopia_user_agent::UserAgent;

let agent = UserAgent::parse(
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.127 Safari/537.36",
);
let client = agent.client();

assert!(client.is_browser());
assert_eq!(client.name.as_deref(), Some(&b"Chrome"[..]));
assert_eq!(client.version.as_deref(), Some(&b"126.0"[..]));
assert_eq!(client.engine.as_deref(), Some(&b"Blink"[..]));
assert_eq!(client.engine_version.as_deref(), Some(&b"126.0.6478.127"[..]));
PHP
use Utopia\UserAgent\UserAgent;

$agent = UserAgent::parse('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.127 Safari/537.36');
$client = $agent->client();

var_dump($client->isBrowser());        // bool(true)
echo $client->name, "\n";              // Chrome
echo $client->version, "\n";           // 126.0
echo $client->engine, "\n";            // Blink
echo $client->engineVersion, "\n";     // 126.0.6478.127

Identify the operating system

The operating system has a short code, a name and a version. Versions written with underscores, like 10_15_7, come back with dots.

Rust
use utopia_user_agent::UserAgent;

let agent = UserAgent::parse(
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15",
);
let os = agent.operating_system();

assert_eq!(os.code.as_deref(), Some(&b"MAC"[..]));
assert_eq!(os.name.as_deref(), Some(&b"Mac"[..]));
assert_eq!(os.version.as_deref(), Some(&b"10.15"[..]));
PHP
use Utopia\UserAgent\UserAgent;

$agent = UserAgent::parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15');
$os = $agent->operatingSystem();

echo $os->code, "\n";    // MAC
echo $os->name, "\n";    // Mac
echo $os->version, "\n"; // 10.15

Tell phones, tablets and desktops apart

The device type is the class of hardware: smartphone, tablet, desktop, tv and so on. Use it to pick a layout or to group traffic.

Rust
use utopia_user_agent::UserAgent;

let headers = [
    "Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro Build/TQ3A.230805.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36",
    "Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1",
    "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0",
];

let layouts: Vec<&str> = headers
    .iter()
    .map(|header| match UserAgent::parse(header).device().kind.as_deref() {
        Some(b"smartphone") => "mobile",
        Some(b"tablet") => "tablet",
        _ => "desktop",
    })
    .collect();
assert_eq!(layouts, ["mobile", "tablet", "desktop"]);
PHP
use Utopia\UserAgent\UserAgent;

$headers = [
    'Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro Build/TQ3A.230805.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36',
    'Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0',
];

foreach ($headers as $header) {
    echo match (UserAgent::parse($header)->device()->type) {
        'smartphone' => 'mobile',
        'tablet' => 'tablet',
        default => 'desktop',
    }, "\n";
}
// mobile
// tablet
// desktop

Detect bots and crawlers

bot() returns the bot's name and category, such as search crawler, ai crawler or social preview. Bot detection does not hide the other results: a crawler that copies a phone's header still reports that phone.

Rust
use utopia_user_agent::UserAgent;

let agent = UserAgent::parse(
    "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.2; +https://openai.com/gptbot)",
);
let bot = agent.bot().expect("a bot");
assert_eq!(&*bot.name, b"GPTBot");
assert_eq!(&*bot.category, b"ai crawler");

let agent = UserAgent::parse(
    "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
);
assert!(agent.is_bot());
assert_eq!(agent.bot().map(|bot| &*bot.category), Some(&b"search crawler"[..]));
assert_eq!(agent.device().model.as_deref(), Some(&b"Nexus 5X"[..]));
PHP
use Utopia\UserAgent\UserAgent;

$agent = UserAgent::parse('Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.2; +https://openai.com/gptbot)');
echo $agent->bot()?->name, "\n";     // GPTBot
echo $agent->bot()?->category, "\n"; // ai crawler

$agent = UserAgent::parse('Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
var_dump($agent->isBot());           // bool(true)
echo $agent->bot()?->category, "\n"; // search crawler
echo $agent->device()->model, "\n";  // Nexus 5X

Recognize API clients and HTTP libraries

Requests from scripts and servers often come from an HTTP library. Its client type is library, so you can tell it from a browser without a list of names.

Rust
use utopia_user_agent::UserAgent;

for (header, name) in [("curl/8.4.0", "curl"), ("python-requests/2.31.0", "Python Requests")] {
    let agent = UserAgent::parse(header);
    let client = agent.client();
    assert_eq!(client.kind.as_deref(), Some(&b"library"[..]));
    assert_eq!(client.name.as_deref(), Some(name.as_bytes()));
    assert!(!client.is_browser());
}
PHP
use Utopia\UserAgent\UserAgent;

foreach (['curl/8.4.0', 'python-requests/2.31.0'] as $header) {
    $client = UserAgent::parse($header)->client();
    echo $client->type, ': ', $client->name, "\n";
}
// library: curl
// library: Python Requests

Handle missing or unknown headers

An empty or unfamiliar header never fails. Every field is None, and is_known tells you whether anything was found.

Rust
use utopia_user_agent::UserAgent;

for header in ["", "not a browser"] {
    let agent = UserAgent::parse(header);
    let known = agent.operating_system().is_known()
        || agent.client().is_known()
        || agent.device().is_known()
        || agent.is_bot();
    assert!(!known);
    assert_eq!(agent.client().name, None);
}
PHP
use Utopia\UserAgent\UserAgent;

foreach (['', 'not a browser'] as $header) {
    $agent = UserAgent::parse($header);
    $known = $agent->operatingSystem()->isKnown()
        || $agent->client()->isKnown()
        || $agent->device()->isKnown()
        || $agent->isBot();
    var_dump($known, $agent->client()->name);
}
// bool(false)
// NULL
// bool(false)
// NULL

Show a session's device in an account page

Account pages list active sessions with a short label like "Mobile Safari on iOS 17.4". Build it from the client and the operating system, with a fallback for unknown parts.

Rust
use utopia_user_agent::{Text, UserAgent};

fn label(header: &str) -> String {
    let agent = UserAgent::parse(header);
    let text = |value: &Option<Text>| value.as_deref().map(String::from_utf8_lossy).unwrap_or_default().into_owned();

    let client = agent.client();
    let os = agent.operating_system();
    let browser = if client.is_known() { text(&client.name) } else { "Unknown client".to_owned() };
    let system = format!("{} {}", text(&os.name), text(&os.version));
    match system.trim() {
        "" => browser,
        system => format!("{browser} on {system}"),
    }
}

assert_eq!(
    label("Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1"),
    "Mobile Safari on iOS 17.4",
);
assert_eq!(label("okhttp/4.12.0"), "OkHttp");
assert_eq!(label(""), "Unknown client");
PHP
use Utopia\UserAgent\UserAgent;

function label(string $header): string
{
    $agent = UserAgent::parse($header);
    $client = $agent->client();
    $os = $agent->operatingSystem();

    $browser = $client->isKnown() ? $client->name : 'Unknown client';
    $system = trim($os->name . ' ' . $os->version);

    return $system === '' ? $browser : "{$browser} on {$system}";
}

echo label('Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1'), "\n"; // Mobile Safari on iOS 17.4
echo label('okhttp/4.12.0'), "\n"; // OkHttp
echo label(''), "\n";              // Unknown client

Reference#

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.

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.

PHPRust
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.

Rust
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());

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

API#

What utopia_user_agent exports at its root.