User Agent Parser
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.
user_agent_parsers:
- regex: '(ESPN)[%20| ]+Radio/(\d+)\.(\d+)\.(\d+) CFNetwork'
- regex: '(Namoroka|Shiretoko|Minefield)/(\d+)\.(\d+)\.(\d+(?:pre|))'
family_replacement: 'Firefox ($1)'
- regex: '(Android) Eclair'
v1_replacement: '2'
v2_replacement: '1'
os_parsers:
- regex: 'Win(?:dows)? ?(95|98|3.1|NT|ME|2000|XP|Vista|7|CE)'
os_replacement: 'Windows'
os_v1_replacement: '$1'
device_parsers:
- regex: '\bSmartWatch *\( *([^;]+) *; *([^;]+) *;'
device_replacement: '$1 $2'
brand_replacement: '$1'
model_replacement: '$2'
Then, use the from_path
(or from_str
if your YAML data is in-memory) associated function to create a UserAgentParser
instance.
use UserAgentParser;
let ua_parser = from_path.unwrap;
Use the parse_*
methods and input a user-agent string to get information.
use UserAgentParser;
let ua_parser = from_path.unwrap;
let user_agent = "Mozilla/5.0 (X11; Linux x86_64; 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 = ua_parser.parse_product;
println!;
// Product {
// name: Some(
// "Facebook",
// ),
// major: Some(
// "8",
// ),
// minor: Some(
// "0",
// ),
// patch: Some(
// "0",
// ),
// }
let os = ua_parser.parse_os;
println!;
// OS {
// name: Some(
// "iOS",
// ),
// major: None,
// minor: None,
// patch: None,
// patch_minor: None,
// }
let device = ua_parser.parse_device;
println!;
// Device {
// name: Some(
// "iPhone",
// ),
// brand: Some(
// "Apple",
// ),
// model: Some(
// "iPhone4,1",
// ),
// }
let cpu = ua_parser.parse_cpu;
println!;
// CPU {
// architecture: Some(
// "amd64",
// ),
// }
let engine = ua_parser.parse_engine;
println!;
// 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.
use UserAgentParser;
let ua_parser = from_path.unwrap;
let product = ua_parser.parse_product.into_owned;
Rocket Support
This crate supports the Rocket framework. All you have to do is enabling the rocket
feature for this crate.
[]
= "*"
= ["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.
extern crate rocket;
use ;
Testing
# git clone --recurse-submodules git://github.com/magiclen/user-agent-parser.git
Crates.io
https://crates.io/crates/user-agent-parser
Documentation
https://docs.rs/user-agent-parser