summaryrefslogtreecommitdiff
path: root/node_modules/ua-parser/cpp/ua_parser.cpp
blob: 6b506814048215ea85d1f8b0a3eafb7fbfc815d2 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
# Copyright 2013 Andrew Punch
#
# Licensed under the Apache License, Version 2.0 (the 'License')
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/


#include <fstream>
#include <boost/algorithm/string/replace.hpp>
#include <yaml-cpp/yaml.h>
#include "ua_parser.h"



namespace ua_parser {
BrowserParser::BrowserParser(const YAML::Node &yaml_attributes) {
  yaml_attributes["regex"] >> pattern;
  regex = boost::regex(pattern);

  if (const YAML::Node *pName =yaml_attributes.FindValue("family_replacement"))
    overrides.family = pName->to<std::string>();
  if (const YAML::Node *pName =yaml_attributes.FindValue("v1_replacement"))
    *pName >> overrides.major;
  if (const YAML::Node *pName =yaml_attributes.FindValue("v2_replacement"))
    *pName >> overrides.minor;
}



Browser BrowserParser::Parse(const std::string &user_agent_string) {
  Browser browser;
  using namespace std;

  boost::smatch result;
  if (boost::regex_search(user_agent_string.begin(), user_agent_string.end(), result, regex)) {
    if (overrides.family.empty() && result.size()>1)
      browser.family = result[1].str();
    else {
      browser.family = overrides.family;
      boost::replace_all(browser.family, "$1", result[1].str());
    }

    // If the group is not found, use the override value even if it is blank

    if (overrides.major.empty()&&result.size()>2)
      browser.major = result[2].str();
    else
      browser.major = overrides.major;

    if (overrides.minor.empty()&&result.size()>3)
      browser.minor = result[3].str();
    else
      browser.minor = overrides.minor;

    if (overrides.patch.empty()&&result.size()>4)
      browser.patch = result[4].str();
    else
      browser.patch = overrides.patch;

    return browser;
  }

  return browser;

}

OperatingSystemParser::OperatingSystemParser(const YAML::Node &yaml_attributes) {
  yaml_attributes["regex"] >> pattern;
  regex = boost::regex(pattern);

  if (const YAML::Node *pName =yaml_attributes.FindValue("os_replacement"))
    *pName >> overrides.os;
  if (const YAML::Node *pName =yaml_attributes.FindValue("os_v1_replacement"))
    *pName >> overrides.major;
  if (const YAML::Node *pName =yaml_attributes.FindValue("os_v2_replacement"))
    *pName >> overrides.minor;
}

OperatingSystem OperatingSystemParser::Parse(const std::string &user_agent_string) {
  OperatingSystem os;
  using namespace std;

  boost::smatch result;
  if (boost::regex_search(user_agent_string.begin(), user_agent_string.end(), result, regex)) {
    if (overrides.os.empty() && result.size()>1)
      os.os = result[1].str();
    else {
      os.os = overrides.os;
      boost::replace_all(os.os, "$1", result[1].str());
    }

    // If the group is not found, use the override value even if it is blank

    if (overrides.major.empty()&&result.size()>2)
      os.major = result[2].str();
    else
      os.major = overrides.major;

    if (overrides.minor.empty()&&result.size()>3)
      os.minor = result[3].str();
    else
      os.minor = overrides.minor;

    if (overrides.patch.empty()&&result.size()>4)
      os.patch = result[4].str();
    else
      os.patch = overrides.patch;

    if (overrides.patch_minor.empty()&&result.size()>5)
      os.patch_minor = result[5].str();
    else
      os.patch_minor = overrides.patch_minor;

    return os;
  }
  return os;
}

DeviceParser::DeviceParser(const YAML::Node &yaml_attributes) {
  yaml_attributes["regex"] >> pattern;
  regex = boost::regex(pattern);

  if (const YAML::Node *pName=yaml_attributes.FindValue("device_replacement"))
    *pName >> overrides;
}

Device DeviceParser::Parse(const std::string &user_agent_string) {
  Device device;
  using namespace std;

  boost::smatch result;
  if (boost::regex_search(user_agent_string.begin(), user_agent_string.end(), result, regex)) {
    if (overrides.empty() && result.size()>1)
      device = result[1].str();
    else {
      device = overrides;
      boost::replace_all(device, "$1", result[1].str());
    }

    return device;
  }

  return device;
}



template<class ParserType>
static std::vector<ParserType> ParseYaml(const YAML::Node &yaml_regexes) {
  std::vector<ParserType> parsers;

  for (YAML::Iterator i=yaml_regexes.begin(); i!=yaml_regexes.end(); i++) {
    ParserType parser(*i);

    parsers.push_back(parser);
  }

  return parsers;
}


Parser::Parser(const std::string &yaml_file) {
  std::ifstream in(yaml_file.c_str());
  if (!in.good()) {
    std::cerr << "Could not open YAML file" << yaml_file << std::endl;
    return;
  }

  YAML::Parser parser(in);

  YAML::Node doc;
  parser.GetNextDocument(doc);

  _browser_parsers = ParseYaml<BrowserParser>(doc["user_agent_parsers"]);
  _os_parsers = ParseYaml<OperatingSystemParser>(doc["os_parsers"]);
  _device_parsers = ParseYaml<DeviceParser>(doc["device_parsers"]);

}

UserAgent Parser::Parse(const std::string &user_agent_string) {
  UserAgent user_agent;

  user_agent.browser.family = "Other";
  user_agent.os.os = "Other";
  user_agent.device = "Other";

  for (std::vector<BrowserParser>::iterator i=_browser_parsers.begin(); i!=_browser_parsers.end(); i++) {
    Browser browser = i->Parse(user_agent_string);
    if (!browser.family.empty()) {
      user_agent.browser = browser;
      break;
    }

  }

  for (std::vector<OperatingSystemParser>::iterator i=_os_parsers.begin(); i!=_os_parsers.end(); i++) {
    OperatingSystem os = i->Parse(user_agent_string);
    if (!os.os.empty()) {
      user_agent.os = os;
      break;
    }

  }

  for (std::vector<DeviceParser>::iterator i=_device_parsers.begin(); i!=_device_parsers.end(); i++) {
    Device device = i->Parse(user_agent_string);
    if (!device.empty()) {
      user_agent.device = device;
      break;
    }

  }



  return user_agent;
}

}