Documentation
¶
Overview ¶
Package uasurfer provides fast and reliable abstraction of HTTP User-Agent strings. The philosophy is to identify technologies that holds >1% market share, and to avoid expending resources and accuracy on guessing at esoteric UA strings.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseUserAgent ¶
ParseUserAgent is the same as Parse, but populates the supplied UserAgent. It is the caller's responsibility to call Reset() on the UserAgent before passing it to this function.
func ParseUserAgentWithHints ¶
ParseUserAgentWithHints is the same as ParseUserAgent, but accepts a Hints struct.
Types ¶
type Browser ¶
type Browser struct {
Name BrowserName
Version Version
}
Browser contains the name of the browser and its version. Browsers are grouped without consideration for device: Chrome (Chrome/43.0) and Chrome for iOS (CriOS/43.0) both report as BrowserChrome with version 43.0, and Internet Explorer 11 and Edge 12 both report as BrowserIE with version 11 or 12.
type BrowserName ¶
type BrowserName int
BrowserName (int) returns a constant.
const ( BrowserUnknown BrowserName = iota BrowserChrome BrowserIE BrowserSafari BrowserFirefox BrowserAndroid BrowserOpera BrowserBlackberry BrowserUCBrowser BrowserSilk BrowserNokia BrowserNetFront BrowserQQ BrowserMaxthon BrowserSogouExplorer BrowserSpotify BrowserNintendo BrowserSamsung BrowserYandex BrowserCocCoc BrowserBot // Bot list begins here BrowserAppleBot BrowserBaiduBot BrowserBingBot BrowserDuckDuckGoBot BrowserFacebookBot BrowserGoogleBot BrowserLinkedInBot BrowserMsnBot BrowserPingdomBot BrowserTwitterBot BrowserYandexBot BrowserCocCocBot BrowserYahooBot BrowserOpenAIBot BrowserAnthropicBot BrowserPerplexityBot BrowserAmazonBot BrowserBytedanceBot BrowserCommonCrawlBot BrowserAhrefsBot BrowserSemrushBot BrowserPetalBot // In-app webviews. A tap on a link inside one of these apps renders in a // frame the app controls, with no address bar and its own idea of a viewport, // which is a different surface to measure than the browser it is built on. BrowserFacebook BrowserInstagram BrowserWeChat BrowserTikTok BrowserSnapchat BrowserLine // Chromium with another name on it. The engine is Chrome's; the vendor, // release cadence and default settings are not. BrowserVivaldi BrowserWhale BrowserMIUI BrowserHuawei BrowserDuckDuckGo )
A complete list of supported web browsers in the form of constants.
func (BrowserName) String ¶
func (b BrowserName) String() string
func (BrowserName) StringTrimPrefix ¶
func (b BrowserName) StringTrimPrefix() string
StringTrimPrefix is like String() but trims the "Browser" prefix
type DeviceType ¶
type DeviceType int
DeviceType (int) returns a constant.
const ( DeviceUnknown DeviceType = iota DeviceComputer DeviceTablet DevicePhone DeviceConsole DeviceWearable DeviceTV )
A complete list of supported devices in the form of constants.
func (DeviceType) String ¶
func (d DeviceType) String() string
func (DeviceType) StringTrimPrefix ¶
func (d DeviceType) StringTrimPrefix() string
StringTrimPrefix is like String() but trims the "Device" prefix
type Hints ¶
type Hints struct {
// ScreenSize identifies an iPad running in desktop mode, which since
// iPadOS 13 is indistinguishable from a Mac in the agent alone.
ScreenSize *ScreenSize
// Mobile is Sec-CH-UA-Mobile: "?1" on a phone, "?0" on anything else.
// Chromium sends it by default, unasked.
Mobile string
// Platform is Sec-CH-UA-Platform: "Android", "Chrome OS", "iOS", "Linux",
// "macOS", "Windows", "Unknown". Also sent by default.
Platform string
// PlatformVersion is Sec-CH-UA-Platform-Version, and is the only way to
// recover an OS version that the agent no longer tells the truth about:
// Chromium reports macOS frozen at 10.15.7, and Android as 10, whatever the
// device runs.
//
// Windows is deliberately left alone. OS.Version is the NT version there,
// and this hint counts differently - 13 or more means Windows 11, which NT
// numbering cannot express, so a caller wanting that distinction has to read
// the header itself.
PlatformVersion string
// FormFactors is Sec-CH-UA-Form-Factors: one or more of "Desktop",
// "Automotive", "Mobile", "Tablet", "XR", "EInk", "Watch". The most direct
// answer to a question the agent has never answered well, and the only one
// that settles an Android tablet outright.
FormFactors string
// AppBundle is the ID of the app the request came from, as an ad request
// states it: "tv.vidaa.ui.plus", "com.roku.xyz". A bundle that only exists
// on televisions settles the device outright, which matters because a TV
// app's requests often carry no agent worth reading - the video player's
// own "GStreamer curlhttpsrc libcurl/7.78.0", or a desktop agent.
AppBundle string
}
Hints carries what the agent cannot say for itself.
The client hint fields take the raw header value, quotes and all, so that a caller can pass what it already has:
uasurfer.ParseWithHints(r.UserAgent(), &uasurfer.Hints{
Mobile: r.Header.Get("Sec-CH-UA-Mobile"),
Platform: r.Header.Get("Sec-CH-UA-Platform"),
PlatformVersion: r.Header.Get("Sec-CH-UA-Platform-Version"),
FormFactors: r.Header.Get("Sec-CH-UA-Form-Factors"),
})
Only Chromium browsers send these, and only for the hints a site has asked for with Accept-CH. Every field is optional: an empty one changes nothing, so there is no need to check before setting.
type OSName ¶
type OSName int
OSName (int) returns a constant.
const ( OSUnknown OSName = iota OSWindowsPhone OSWindows OSMacOSX OSiOS OSiPadOS OSAndroid OSBlackberry OSChromeOS OSKindle OSWebOS OSLinux OSPlaystation OSXbox OSNintendo OSBot OSTizen // Samsung smart TVs, and the handful of Tizen phones OSRoku OSTvOS )
A complete list of supported OSes in the form of constants. For handling particular versions of operating systems (e.g. Windows 2000), see the README.md file.
func (OSName) StringTrimPrefix ¶
StringTrimPrefix is like String() but trims the "OS" prefix
type Platform ¶
type Platform int
Platform (int) returns a constant.
const ( PlatformUnknown Platform = iota PlatformWindows PlatformMac PlatformLinux PlatformiPad PlatformiPhone PlatformiPod PlatformBlackberry PlatformWindowsPhone PlatformPlaystation PlatformXbox PlatformNintendo PlatformBot PlatformAppleTV )
A complete list of supported platforms in the form of constants. Many OSes report their true platform, such as Android OS being Linux platform.
func (Platform) StringTrimPrefix ¶
StringTrimPrefix is like String() but trims the "Platform" prefix
type ScreenSize ¶
type UserAgent ¶
type UserAgent struct {
Browser Browser
OS OS
DeviceType DeviceType
}
func ParseWithHints ¶
ParseWithHints is the same as Parse, but accepts a Hints struct.
