User Agent Parser

Build Status

A parser to get the product, OS, device, cpu, and engine information from a user agent, inspired by https://github.com/faisalman/ua-parser-js and https://github.com/ua-parser/uap-core

Usage

You can make a regexes.yaml file or copy one from https://github.com/ua-parser/uap-core

This is a simple example of regexes.yaml.

```yaml useragentparsers: - regex: '(ESPN)[%20| ]+Radio/(\d+).(\d+).(\d+) CFNetwork' - regex: '(Namoroka|Shiretoko|Minefield)/(\d+).(\d+).(\d+(?:pre|))' familyreplacement: 'Firefox ($1)' - regex: '(Android) Eclair' v1replacement: '2' v2_replacement: '1'

osparsers: - regex: 'Win(?:dows)? ?(95|98|3.1|NT|ME|2000|XP|Vista|7|CE)' osreplacement: 'Windows' osv1replacement: '$1'

deviceparsers: - regex: '\bSmartWatch *( *([^;]+) *; *([^;]+) *;' devicereplacement: '$1 $2' brandreplacement: '$1' modelreplacement: '$2' ```

Then, use the from_path (or from_str if your YAML data is in-memory) associated function to create a UserAgentParser instance.

```rust extern crate useragentparser;

use useragentparser::UserAgentParser;

let uaparser = UserAgentParser::frompath("/path/to/regexes.yaml").unwrap(); ```

Use the parse_* methods and input a user-agent string to get information.

```rust extern crate useragentparser;

use useragentparser::UserAgentParser;

let uaparser = UserAgentParser::frompath("/path/to/regexes.yaml").unwrap();

let useragent = "Mozilla/5.0 (X11; Linux x8664; rv:10.0) Gecko/20100101 Firefox/10.0 [FBAN/FBIOS;FBAV/8.0.0.28.18;FBBV/1665515;FBDV/iPhone4,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/7.0.4;FBSS/2; FBCR/Telekom.de;FBID/phone;FBLC/de_DE;FBOP/5]";

let product = uaparser.parseproduct(user_agent);

println!("{:#?}", product);

// Product { // name: Some( // "Facebook", // ), // major: Some( // "8", // ), // minor: Some( // "0", // ), // patch: Some( // "0", // ), // }

let os = uaparser.parseos(user_agent);

println!("{:#?}", os);

// OS { // name: Some( // "iOS", // ), // major: None, // minor: None, // patch: None, // patch_minor: None, // }

let device = uaparser.parsedevice(user_agent);

println!("{:#?}", device);

// Device { // name: Some( // "iPhone", // ), // brand: Some( // "Apple", // ), // model: Some( // "iPhone4,1", // ), // }

let cpu = uaparser.parsecpu(user_agent);

println!("{:#?}", cpu);

// CPU { // architecture: Some( // "amd64", // ), // }

let engine = uaparser.parseengine(user_agent);

println!("{:#?}", engine);

// Engine { // name: Some( // "Gecko", // ), // major: Some( // "10", // ), // minor: Some( // "0", // ), // patch: None, // } ```

The lifetime of result instances of the parse_* methods depends on the user-agent string and the UserAgentParser instance. To make it independent, call the into_owned method.

```rust extern crate useragentparser;

use useragentparser::UserAgentParser;

let uaparser = UserAgentParser::frompath("/path/to/regexes.yaml").unwrap();

let product = uaparser.parseproduct("Mozilla/5.0 (X11; U; Linux x8664; en-US; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12").intoowned(); ```

Rocket Support

This crate supports the Rocket framework. All you have to do is enabling the rocket feature for this crate.

toml [dependencies.user-agent-parser] version = "*" features = ["rocket"]

Let Rocket manage a UserAgentParser instance, and the Product, OS, Device, CPU, Engine models of this crate (plus the UserAgent model) can be used as Request Guards.

```rust

![feature(procmacrohygiene, decl_macro)]

[macro_use]

extern crate rocket;

extern crate useragentparser;

use useragentparser::{UserAgentParser, UserAgent, Product, OS, Device, CPU, Engine};

[get("/")]

fn index(useragent: UserAgent, product: Product, os: OS, device: Device, cpu: CPU, engine: Engine) -> String { format!("{useragent:#?}\n{product:#?}\n{os:#?}\n{device:#?}\n{cpu:#?}\n{engine:#?}", useragent = useragent, product = product, os = os, device = device, cpu = cpu, engine = engine, ) }

fn main() { rocket::ignite() .manage(UserAgentParser::from_path("/path/to/regexes.yaml").unwrap()) .mount("/", routes![index]) .launch(); } ```

Testing

```bash git clone --recurse-submodules git://github.com/magiclen/user-agent-parser.git

cd user-agent-parser

cargo test ```

Crates.io

https://crates.io/crates/user-agent-parser

Documentation

https://docs.rs/user-agent-parser

License

MIT