Implementation of the Woothee HTTP user agent parser
To use this package, run the following command in your project's root directory:
Put the following dependency into your project's dependences section:
A D implementation of Project Woothee, the multi-language HTTP user agent string parser.
import woothee; const agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"; assert (!isCrawler(agent)); const result = parse(agent); assert (result.category == Category.pc); assert (result.name == "Chrome"); assert (result.client_type == ClientType.browser); assert (result.client_version == "86.0.4240.111"); assert (result.os == "Linux"); assert (result.os_version is null); assert (result.vendor == "Google");
Regexes and data tables can be precomputed at compile time. That makes compilation significantly slower, so it's controlled by the
WootheePrecompute version flag. You can enable it in DMD (and compatible compilers) by adding
-version=WootheePrecompute to the compiler command line.
This code just follows the Project Woothee spec. If you want to add support for more user agents, please add it upstream, first.
If you disagree with any parser output, please check if it's different from the Ruby version. If it is, file a bug report here, otherwise discuss it with the upstream project maintainers.
To update this implementation to a newer version of the Woothee spec, you need to 1) pull in the new Woothee data, and 2) update the code so that tests pass.
Upstream Woothee data is tracked using git submodules. It's converted from YAML to JSON to avoid adding a YAML parser as a dependency (which may change if Phobos ever includes a YAML parser). The YAML-to-JSON conversion requires the remarshal tool.
git submodule init git submodule update cd woothee-data git tag # to list tags git checkout vX.X.X cd .. ./mkjson.sh # Check all changes in
You can run the tests using
dub test, or using a D compiler directly:
dmd -J=data -main -unittest source/woothee.d ./woothee
A major goal is to keep the D version up-to-date with the Project Woothee spec by making it easy to update by anyone who knows D and can read the Ruby implementation.
- Treat the Ruby implementation as the definitive version. It's right, even if you have a different opinion. Keep the other guidelines in mind, even if you aren't translating directly from the Ruby version
- D and Ruby are different, but please try to keep the structure of the D code similar to the Ruby code
- Keep naming as consistent (or at least predictable) where possible
- If the Ruby code has comments (except for ones that are specific to the Ruby implementation), add them verbatim to the D code
- Boring is good. The code has a lot of simple logic and string matching because that's how Woothee works. That's okay.
- Registered by Simon Arneaud
- 1.11.1 released 10 months ago
- Apache 2.0
- Copyright © 2020, Simon Arneaud, TAGOMORI Satoshi, Hideo Hattori