This example shows how to use 51Degrees On-premise device detection to determine details about a device based on its User-Agent HTTP header value. To use User-Agent Client Hints as well, call
match_all instead of
match_single.
This example is available in full on GitHub.
This example requires a local data file. The free 'Lite' data file can be downloaded with the
scripts in the data GitHub repository, which
this repository includes at cxx/data. Fetch the submodules by running
git submodule update --init --recursive, then run
sh cxx/data/get-data-files.sh 51Degrees-LiteV4.1.hash from the repository root,
which saves the file as cxx/data/device-detection/51Degrees-LiteV4.1.hash.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
The Enterprise data file has every property.
Contact us about a copy.
Update the data file path before running the example.
In a Linux environment, run the following commands:
$ curl localhost:8080 -I -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
$ curl localhost:8080 -I -A "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114"
Expected output:
HTTP/1.1 200 OK
...
X-IsMobile: False
...
...
HTTP/1.1 200 OK
...
X-IsMobile: True
Code:
vcl 4.0;
import fiftyonedegrees;
backend default {
.host = "127.0.0.1";
.port = "80";
}
sub vcl_recv {
set req.http.X-IsMobile = fiftyonedegrees.match_single(req.http.user-agent, "IsMobile");
}
sub vcl_deliver {
set resp.http.X-IsMobile = fiftyonedegrees.match_single(req.http.user-agent, "IsMobile");
}
sub vcl_init {
fiftyonedegrees.start("/etc/varnish/51Degrees-LiteV4.1.hash");
}