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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#include "UaParser.h"
#include <fstream>
#include <string>
#include <vector>
#include <unordered_set>
#include <boost/regex.hpp>
#include <glog/logging.h>
#include <yaml-cpp/yaml.h>
namespace {
struct DeviceStore {
std::string replacement;
boost::regex regExpr;
};
struct AgentStore : DeviceStore {
std::string majorVersionReplacement;
std::string minorVersionReplacement;
};
typedef AgentStore OsStore;
typedef AgentStore BrowserStore;
#define FILL_AGENT_STORE(node, agent_store, repl, maj_repl, min_repl) \
CHECK(node.Type() == YAML::NodeType::Map); \
for (auto it = node.begin(); it != node.end(); ++it) { \
const std::string key = it.first().to<std::string>(); \
const std::string value = it.second().to<std::string>(); \
if (key == "regex") { \
agent_store.regExpr = value; \
} else if (key == repl) { \
agent_store.replacement = value; \
} else if (key == maj_repl && !value.empty()) { \
agent_store.majorVersionReplacement = value; \
} else if (key == min_repl && !value.empty()) { \
try { \
agent_store.minorVersionReplacement = value; \
} catch (...) {} \
} else { \
CHECK(false); \
} \
}
struct UAStore {
explicit UAStore(const std::string& regexes_file_path) {
std::ifstream in_stream(regexes_file_path);
CHECK(in_stream.good());
YAML::Parser yaml_parser(in_stream);
YAML::Node regexes;
CHECK(yaml_parser.GetNextDocument(regexes));
const auto& user_agent_parsers = regexes["user_agent_parsers"];
for (const auto& user_agent : user_agent_parsers) {
BrowserStore browser;
FILL_AGENT_STORE(user_agent, browser, "family_replacement",
"v1_replacement", "v2_replacement");
browserStore.push_back(browser);
}
const auto& os_parsers = regexes["os_parsers"];
for (const auto& o : os_parsers) {
OsStore os;
FILL_AGENT_STORE(o, os, "os_replacement", "os_v1_replacement",
"os_v2_replacement");
osStore.push_back(os);
}
const auto& device_parsers = regexes["device_parsers"];
for (const auto& d : device_parsers) {
DeviceStore device;
for (auto it = d.begin(); it != d.end(); ++it) {
const std::string key = it.first().to<std::string>();
const std::string value = it.second().to<std::string>();
if (key == "regex") {
device.regExpr = value;
} else if (key == "device_replacement") {
device.replacement = value;
} else {
CHECK(false);
}
}
deviceStore.push_back(device);
}
}
std::vector<DeviceStore> deviceStore;
std::vector<OsStore> osStore;
std::vector<BrowserStore> browserStore;
};
template<class AGENT, class AGENT_STORE>
void fillAgent(AGENT& agent, const AGENT_STORE& store, const boost::smatch& m) {
CHECK(!m.empty());
if (m.size() > 1) {
agent.family = !store.replacement.empty()
? boost::regex_replace(store.replacement, boost::regex("\\$1"), m[1].str())
: m[1];
} else {
agent.family = !store.replacement.empty()
? boost::regex_replace(store.replacement, boost::regex("\\$1"), m[0].str())
: m[0];
}
if (!store.majorVersionReplacement.empty()) {
agent.major = store.majorVersionReplacement;
} else if (m.size() > 2) {
const auto s = m[2].str();
if (!s.empty()) {
agent.major = s;
}
}
if (!store.minorVersionReplacement.empty()) {
agent.minor = store.minorVersionReplacement;
} else if (m.size() > 3) {
const auto s = m[3].str();
if (!s.empty()) {
agent.minor = s;
}
}
if (m.size() > 4) {
const auto s = m[4].str();
if (!s.empty()) {
agent.patch = s;
}
}
}
UserAgent parseImpl(const std::string& ua, const UAStore* ua_store) {
UserAgent uagent;
for (const auto& b : ua_store->browserStore) {
auto& browser = uagent.browser;
boost::smatch m;
if (boost::regex_search(ua, m, b.regExpr)) {
fillAgent(browser, b, m);
break;
} else {
browser.family = "Other";
}
}
for (const auto& o : ua_store->osStore) {
auto& os = uagent.os;
boost::smatch m;
if (boost::regex_search(ua, m, o.regExpr)) {
fillAgent(os, o, m);
break;
} else {
os.family = "Other";
}
}
for (const auto& d : ua_store->deviceStore) {
auto& device = uagent.device;
boost::smatch m;
if (boost::regex_search(ua, m, d.regExpr)) {
if (m.size() > 1) {
device.family = !d.replacement.empty()
? boost::regex_replace(d.replacement, boost::regex("\\$1"), m[1].str())
: m[1].str();
} else if (m.size() == 1) {
device.family = !d.replacement.empty()
? boost::regex_replace(d.replacement, boost::regex("\\$1"), m[0].str())
: m[0].str();
}
break;
} else {
device.family = "Other";
}
}
return uagent;
}
} // namespace
UserAgentParser::UserAgentParser(const std::string& regexes_file_path)
: regexes_file_path_ { regexes_file_path } {
ua_store_ = new UAStore(regexes_file_path);
}
UserAgentParser::~UserAgentParser() {
delete static_cast<const UAStore*>(ua_store_);
}
UserAgent UserAgentParser::parse(const std::string& ua) const {
return parseImpl(ua, static_cast<const UAStore*>(ua_store_));
}
|