| 1 | /* |
| 2 | * Copyright (C) 2012, 2014, 2016 Igalia S.L. |
| 3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "UserAgent.h" |
| 29 | |
| 30 | #include "UserAgentQuirks.h" |
| 31 | #include <wtf/NeverDestroyed.h> |
| 32 | #include <wtf/URL.h> |
| 33 | #include <wtf/text/StringBuilder.h> |
| 34 | |
| 35 | #if OS(UNIX) |
| 36 | #include <sys/utsname.h> |
| 37 | #endif |
| 38 | |
| 39 | // WARNING! WARNING! WARNING! |
| 40 | // |
| 41 | // The user agent is ludicrously fragile. The most innocent change can |
| 42 | // and will break websites. Read the git log for this file carefully |
| 43 | // before changing user agent construction. You have been warned. |
| 44 | |
| 45 | namespace WebCore { |
| 46 | |
| 47 | static const char* platformForUAString() |
| 48 | { |
| 49 | #if OS(MAC_OS_X) |
| 50 | return "Macintosh" ; |
| 51 | #else |
| 52 | return "X11" ; |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | static const String platformVersionForUAString() |
| 57 | { |
| 58 | #if OS(UNIX) |
| 59 | struct utsname name; |
| 60 | uname(&name); |
| 61 | static NeverDestroyed<const String> uaOSVersion(makeString(name.sysname, ' ', name.machine)); |
| 62 | return uaOSVersion; |
| 63 | #else |
| 64 | // We will always claim to be Safari in Intel Mac OS X, since Safari without |
| 65 | // OS X or anything on ARM triggers mobile versions of some websites. |
| 66 | // Version is frozen per https://bugs.webkit.org/show_bug.cgi?id=180365 |
| 67 | static NeverDestroyed<const String> uaOSVersion(MAKE_STATIC_STRING_IMPL("Intel Mac OS X 10_13_4" )); |
| 68 | return uaOSVersion; |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | static inline const char* versionForUAString() |
| 73 | { |
| 74 | // https://bugs.webkit.org/show_bug.cgi?id=180365 |
| 75 | return "605.1.15" ; |
| 76 | } |
| 77 | |
| 78 | static String buildUserAgentString(const UserAgentQuirks& quirks) |
| 79 | { |
| 80 | StringBuilder uaString; |
| 81 | uaString.appendLiteral("Mozilla/5.0 " ); |
| 82 | uaString.append('('); |
| 83 | |
| 84 | if (quirks.contains(UserAgentQuirks::NeedsMacintoshPlatform)) |
| 85 | uaString.append(UserAgentQuirks::stringForQuirk(UserAgentQuirks::NeedsMacintoshPlatform)); |
| 86 | else if (quirks.contains(UserAgentQuirks::NeedsLinuxDesktopPlatform)) |
| 87 | uaString.append(UserAgentQuirks::stringForQuirk(UserAgentQuirks::NeedsLinuxDesktopPlatform)); |
| 88 | else { |
| 89 | uaString.append(platformForUAString()); |
| 90 | uaString.appendLiteral("; " ); |
| 91 | uaString.append(platformVersionForUAString()); |
| 92 | } |
| 93 | |
| 94 | uaString.appendLiteral(") AppleWebKit/" ); |
| 95 | uaString.append(versionForUAString()); |
| 96 | uaString.appendLiteral(" (KHTML, like Gecko) " ); |
| 97 | |
| 98 | // Note that Chrome UAs advertise *both* Chrome and Safari. |
| 99 | if (quirks.contains(UserAgentQuirks::NeedsChromeBrowser)) { |
| 100 | uaString.append(UserAgentQuirks::stringForQuirk(UserAgentQuirks::NeedsChromeBrowser)); |
| 101 | uaString.appendLiteral(" " ); |
| 102 | } |
| 103 | |
| 104 | // Version/X is mandatory *before* Safari/X to be a valid Safari UA. See |
| 105 | // https://bugs.webkit.org/show_bug.cgi?id=133403 for details. |
| 106 | uaString.appendLiteral("Version/11.0 Safari/" ); |
| 107 | uaString.append(versionForUAString()); |
| 108 | |
| 109 | return uaString.toString(); |
| 110 | } |
| 111 | |
| 112 | static const String standardUserAgentStatic() |
| 113 | { |
| 114 | static NeverDestroyed<const String> uaStatic(buildUserAgentString(UserAgentQuirks())); |
| 115 | return uaStatic; |
| 116 | } |
| 117 | |
| 118 | String standardUserAgent(const String& applicationName, const String& applicationVersion) |
| 119 | { |
| 120 | // Create a default user agent string with a liberal interpretation of |
| 121 | // https://developer.mozilla.org/en-US/docs/User_Agent_Strings_Reference |
| 122 | // |
| 123 | // Forming a functional user agent is really difficult. We must mention Safari, because some |
| 124 | // sites check for that when detecting WebKit browsers. Additionally some sites assume that |
| 125 | // browsers that are "Safari" but not running on OS X are the Safari iOS browser. Getting this |
| 126 | // wrong can cause sites to load the wrong JavaScript, CSS, or custom fonts. In some cases |
| 127 | // sites won't load resources at all. |
| 128 | if (applicationName.isEmpty()) |
| 129 | return standardUserAgentStatic(); |
| 130 | |
| 131 | String finalApplicationVersion = applicationVersion; |
| 132 | if (finalApplicationVersion.isEmpty()) |
| 133 | finalApplicationVersion = versionForUAString(); |
| 134 | |
| 135 | return standardUserAgentStatic() + ' ' + applicationName + '/' + finalApplicationVersion; |
| 136 | } |
| 137 | |
| 138 | String standardUserAgentForURL(const URL& url) |
| 139 | { |
| 140 | auto quirks = UserAgentQuirks::quirksForURL(url); |
| 141 | // The null string means we don't need a specific UA for the given URL. |
| 142 | return quirks.isEmpty() ? String() : buildUserAgentString(quirks); |
| 143 | } |
| 144 | |
| 145 | } // namespace WebCore |
| 146 | |
| 147 | |