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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* Copyright (C) 2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "UserAgentStringParser.h"
#include "RFC7230.h"
#include "UserAgentStringData.h"
#include <optional>
#include <wtf/ASCIICType.h>
#include <wtf/StdLibExtras.h>
#include <wtf/WeakPtr.h>
#include <wtf/text/MakeString.h>
#include <wtf/text/StringCommon.h>
#include <wtf/text/StringImpl.h>
#include <wtf/text/WTFString.h>
/*
* GRAMMAR:
* https://www.rfc-editor.org/rfc/rfc9110#name-user-agent
* User-Agent = product *( RWS ( product / comment ) )
* product = token ["/" product-version]
* product-version = token
* token = 1*tchar
* tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA ; any VCHAR, except delimiters
* RWS = 1*( SP / HTAB ); required whitespace
* comment = "(" *( ctext / quoted-pair / comment ) ")"
* ctext = HTAB / SP / %x21-27 / %x2A-5B / %x5D-7E / obs-text
* quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
* obs-text = %x80-FF
* HTAB = <ASCII horizontal tab %x09, aka '\t'>
* SP = <ASCII space, i.e. " ">
* VCHAR = <any visible US-ASCII character>
*
* REFERENCE:
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/User-Agent#syntax
*
* NOTE:
* User agent strings come in many different forms, but most browsers conform to a common pattern.
* This class is attempting to determine attributes about the user agent by expecting common forms
* of user agent strings. There is a link below that contains a list of of these strings grouped
* by platform, browser, layout engine, etc. I tried to pick the most frequent ones to parse out
* viable information.
*
* https://explore.whatismybrowser.com/useragents/explore/
*
* Some user agent strings, while valid grammatically, list their info in odd locations.
* This parser will not be able to pick out the correct information from those.
*/
namespace WebCore {
UserAgentStringParser::UserAgentStringParser(const String& userAgentString)
: m_userAgentString(userAgentString)
, data(UserAgentStringData::create()) { };
Ref<UserAgentStringParser> UserAgentStringParser::create(const String& userAgentString)
{
return adoptRef(*new UserAgentStringParser(userAgentString));
}
std::optional<Ref<UserAgentStringData>> UserAgentStringParser::parse()
{
data = UserAgentStringData::create();
if (atEnd())
return { };
consumeProduct();
while (!atEnd()) {
if (!isTabOrSpace(peek()))
return { };
consumeRWS();
start = pos;
if (peek() == '(')
consumeComment();
else
consumeProduct();
if (malformed)
return { };
}
populateUserAgentData();
return data;
}
void UserAgentStringParser::consumeProduct()
{
consumeToken();
if (malformed)
return;
auto product = Product { .name = getSubstring(), .version = { } };
if (!atEnd() && peek() == '/') {
increment();
start = pos;
consumeToken();
if (malformed)
return;
product.version = getSubstring();
}
start = pos;
segments.append(product);
}
void UserAgentStringParser::consumeRWS()
{
while (!atEnd() && isTabOrSpace(peek()))
increment();
}
void UserAgentStringParser::consumeComment()
{
ASSERT(peek() == '(');
increment(); // pass first '('
start = pos;
if (atEnd()) {
malformed = true;
return;
}
auto c = peek();
while (!atEnd() && c != ')') {
if (c == '(')
consumeComment();
else if (c == '\\')
consumeQuotedPair();
else if (RFC7230::isCommentText(c))
increment();
if (malformed)
return;
c = peek();
}
if (atEnd()) {
malformed = true;
return;
}
auto s = getSubstring();
if (!s.isEmpty()) {
auto comment = Comment { .parts = s.split(';') };
segments.append(comment);
}
increment();
start = pos;
// malformed user agent string
}
void UserAgentStringParser::consumeToken()
{
if (!RFC7230::isTokenCharacter(peek())) {
malformed = true;
return;
}
do {
increment();
} while (!atEnd() && RFC7230::isTokenCharacter(peek()));
}
void UserAgentStringParser::consumeQuotedPair()
{
ASSERT(peek() == '\\');
increment(); // pass '\'
if (RFC7230::isQuotedPairSecondOctet(peek())) {
increment();
return;
}
malformed = true;
}
inline char16_t UserAgentStringParser::peek()
{
return m_userAgentString[this->pos];
}
inline void UserAgentStringParser::increment()
{
this->pos++;
}
inline bool UserAgentStringParser::atEnd()
{
return this->pos >= this->m_userAgentString.length();
}
inline String UserAgentStringParser::getSubstring()
{
return m_userAgentString.substring(start, pos - start);
}
struct BrowsersSeen {
bool brave : 1 { false };
bool firefox : 1 { false };
bool chrome : 1 { false };
bool safari : 1 { false };
bool opera : 1 { false };
bool edge : 1 { false };
String braveVersion;
String firefoxVersion;
String chromeVersion;
String safariVersion;
String operaVersion;
String edgeVersion;
};
void UserAgentStringParser::populateUserAgentData()
{
BrowsersSeen browsersSeen;
auto weakThis = WeakPtr { *this };
bool linuxSeen { false };
for (const auto& segment : segments) {
WTF::switchOn(segment, [&browsersSeen, weakThis](const Product& p) {
if (p.name == "Mobile") {
weakThis->data->mobile = true;
return;
}
if (p.name == "Brave") {
browsersSeen.braveVersion = p.version;
browsersSeen.brave = true;
return;
}
if (p.name == "Firefox" || p.name == "fxiOS") {
browsersSeen.firefoxVersion = p.version;
browsersSeen.firefox = true;
return;
}
if (p.name == "Chrome") {
browsersSeen.chromeVersion = p.version;
browsersSeen.chrome = true;
return;
}
if (p.name == "Safari") {
browsersSeen.firefoxVersion = p.version;
browsersSeen.safari = true;
return;
}
if (p.name == "OPR") {
browsersSeen.operaVersion = p.version;
browsersSeen.opera = true;
return;
}
if (p.name.contains("Edg")) {
browsersSeen.edgeVersion = p.version;
browsersSeen.edge = true;
return;
} }, [weakThis, &linuxSeen](const Comment& c) {
for (const auto& part : c.parts) {
if (part.contains("Windows")) {
weakThis->data->platform = "Windows"_s;
return;
}
if (part == "Macintosh") {
weakThis->data->platform = "macOS"_s;
return;
}
if (part == "iPhone") {
weakThis->data->platform = "iOS"_s;
return;
}
if (part == "iPad") {
weakThis->data->platform = "iOS"_s;
return;
}
if (part.contains("Android")) {
weakThis->data->platform = "Android"_s;
return;
}
if (part.contains("Linux")) {
linuxSeen = true;
return;
}
if (part.contains("CrOS")) {
weakThis->data->platform = "ChromeOS"_s;
return;
}
} });
}
// android user agents sometimes list linux and android, but linux user agents don't list androids
if (linuxSeen && data->platform.isEmpty())
data->platform = "Linux"_s;
// both chrome and firefox sometimes list safari in their user agent strings
if (browsersSeen.safari && !browsersSeen.chrome && !browsersSeen.firefox) {
data->browserName = "Safari"_s;
data->browserVersion = browsersSeen.safariVersion;
return;
}
// no other browser typically list firefox
if (browsersSeen.firefox) {
data->browserName = "Firefox"_s;
data->browserVersion = browsersSeen.firefoxVersion;
}
// chrome based browsers typically list chrome
if (browsersSeen.chrome) {
if (browsersSeen.edge) {
data->browserName = "Microsoft Edge"_s;
data->browserVersion = browsersSeen.edgeVersion;
} else if (browsersSeen.brave) {
data->browserName = "Brave"_s;
data->browserVersion = browsersSeen.braveVersion;
} else if (browsersSeen.opera) {
data->browserName = "Opera"_s;
data->browserVersion = browsersSeen.operaVersion;
} else {
data->browserName = "Google Chrome"_s;
data->browserVersion = browsersSeen.chromeVersion;
}
}
}
};
|