-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdevice_detection.cpp
More file actions
50 lines (48 loc) · 2.12 KB
/
device_detection.cpp
File metadata and controls
50 lines (48 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! @example device_detection.cpp
#include "fastly/sdk.h"
#include <iostream>
#include <optional>
#include <string>
using namespace std::string_literals;
int main() {
auto req{fastly::Request::from_client()};
fastly::Body body;
auto ua{req.get_header("User-Agent")};
if (!ua.has_value()) {
body << "Failed to get User-Agent header: " << ua.error().error_msg()
<< std::endl;
} else if (ua == std::nullopt) {
body << "No user agent. Can't detect device." << std::endl;
} else {
body << "Trying to detect device using UA `" << **ua << "`..." << std::endl;
auto maybe_dev{fastly::device_detection::lookup(**ua)};
if (!maybe_dev.has_value()) {
body << "Error while looking up device: " << maybe_dev.error().error_msg()
<< std::endl;
} else if (*maybe_dev == std::nullopt) {
body << "Failed to detect device based on User Agent string."
<< std::endl;
} else {
auto dev{std::move(**maybe_dev)};
body << "Device name: " << dev.device_name().value_or("UNKNOWN")
<< std::endl
<< "Brand: " << dev.brand().value_or("UNKNOWN") << std::endl
<< "Model: " << dev.model().value_or("UNKNOWN") << std::endl
<< "Hardware Type: " << dev.hwtype().value_or("UNKNOWN") << std::endl
<< "eReader?: " << dev.is_ereader().value_or("UNKNOWN") << std::endl
<< "Game console?: " << dev.is_gameconsole().value_or("UNKNOWN")
<< std::endl
<< "Media player?: " << dev.is_mediaplayer().value_or("UNKNOWN")
<< std::endl
<< "Mobile?: " << dev.is_mobile().value_or("UNKNOWN") << std::endl
<< "martTV?: " << dev.is_smarttv().value_or("UNKNOWN") << std::endl
<< "Tablet?: " << dev.is_tablet().value_or("UNKNOWN") << std::endl
<< "TV Player?: " << dev.is_tvplayer().value_or("UNKNOWN")
<< std::endl
<< "Desktop?: " << dev.is_desktop().value_or("UNKNOWN") << std::endl
<< "Has Touchsreen?: " << dev.is_touchscreen().value_or("UNKNOWN")
<< std::endl;
}
}
fastly::Response::from_body(std::move(body)).send_to_client();
}