close #24
This commit is contained in:
@@ -31,6 +31,7 @@ const (
|
|||||||
MacOS = "macOS"
|
MacOS = "macOS"
|
||||||
IOS = "iOS"
|
IOS = "iOS"
|
||||||
Linux = "Linux"
|
Linux = "Linux"
|
||||||
|
FreeBSD = "FreeBSD"
|
||||||
ChromeOS = "ChromeOS"
|
ChromeOS = "ChromeOS"
|
||||||
BlackBerry = "BlackBerry"
|
BlackBerry = "BlackBerry"
|
||||||
|
|
||||||
@@ -74,6 +75,8 @@ func Parse(userAgent string) UserAgent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//fmt.Printf("%+v\n", tokens)
|
||||||
|
|
||||||
// OS lookup
|
// OS lookup
|
||||||
switch {
|
switch {
|
||||||
case tokens.exists("Android"):
|
case tokens.exists("Android"):
|
||||||
@@ -115,6 +118,11 @@ func Parse(userAgent string) UserAgent {
|
|||||||
ua.OSVersion = tokens.get(Linux)
|
ua.OSVersion = tokens.get(Linux)
|
||||||
ua.Desktop = true
|
ua.Desktop = true
|
||||||
|
|
||||||
|
case tokens.exists("FreeBSD"):
|
||||||
|
ua.OS = FreeBSD
|
||||||
|
ua.OSVersion = tokens.get(FreeBSD)
|
||||||
|
ua.Desktop = true
|
||||||
|
|
||||||
case tokens.exists("CrOS"):
|
case tokens.exists("CrOS"):
|
||||||
ua.OS = ChromeOS
|
ua.OS = ChromeOS
|
||||||
ua.OSVersion = tokens.get("CrOS")
|
ua.OSVersion = tokens.get("CrOS")
|
||||||
@@ -277,6 +285,11 @@ func Parse(userAgent string) UserAgent {
|
|||||||
ua.Name = "BlackBerry"
|
ua.Name = "BlackBerry"
|
||||||
ua.Version = tokens.get("Version")
|
ua.Version = tokens.get("Version")
|
||||||
|
|
||||||
|
case tokens.exists("NetFront"):
|
||||||
|
ua.Name = "NetFront"
|
||||||
|
ua.Version = tokens.get("NetFront")
|
||||||
|
ua.Mobile = true
|
||||||
|
|
||||||
// if chrome and Safari defined, find any other token sent descr
|
// if chrome and Safari defined, find any other token sent descr
|
||||||
case tokens.exists(Chrome) && tokens.exists(Safari):
|
case tokens.exists(Chrome) && tokens.exists(Safari):
|
||||||
name := tokens.findBestMatch(true)
|
name := tokens.findBestMatch(true)
|
||||||
@@ -434,7 +447,11 @@ func parse(userAgent string) properties {
|
|||||||
buff.WriteByte(c)
|
buff.WriteByte(c)
|
||||||
isURL = true
|
isURL = true
|
||||||
} else {
|
} else {
|
||||||
slash = true
|
if ignore(buff.String()) {
|
||||||
|
buff.Reset()
|
||||||
|
} else {
|
||||||
|
slash = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -476,7 +493,7 @@ func checkVer(s string) (name, v string) {
|
|||||||
// ignore retursn true if token should be ignored
|
// ignore retursn true if token should be ignored
|
||||||
func ignore(s string) bool {
|
func ignore(s string) bool {
|
||||||
switch s {
|
switch s {
|
||||||
case "KHTML, like Gecko", "U", "compatible", "Mozilla", "WOW64", "en", "en-us", "en-gb", "ru-ru":
|
case "KHTML, like Gecko", "U", "compatible", "Mozilla", "WOW64", "en", "en-us", "en-gb", "ru-ru", "Browser":
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -70,6 +70,9 @@ var testTable = [][]string{
|
|||||||
// Windows phone
|
// Windows phone
|
||||||
{"Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; NOKIA; Lumia 630)", ua.InternetExplorer, "7.0", "mobile", ua.WindowsPhone},
|
{"Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; NOKIA; Lumia 630)", ua.InternetExplorer, "7.0", "mobile", ua.WindowsPhone},
|
||||||
|
|
||||||
|
// FreeBSD
|
||||||
|
{"Mozilla/5.0 (compatible; Konqueror/4.5; FreeBSD) KHTML/4.5.4 (like Gecko)", "Konqueror", "4.5", "desktop", "FreeBSD"},
|
||||||
|
|
||||||
// Bots
|
// Bots
|
||||||
{"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", ua.Googlebot, "2.1", "mobile", "Android", "Nexus 5X"},
|
{"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", ua.Googlebot, "2.1", "mobile", "Android", "Nexus 5X"},
|
||||||
{"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", ua.Googlebot, "2.1", "bot", ""},
|
{"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", ua.Googlebot, "2.1", "bot", ""},
|
||||||
@@ -131,6 +134,7 @@ var testTable = [][]string{
|
|||||||
{"surveyon/2.9.5 (iPhone; CPU iPhone OS 12_5_7 like Mac OS X)", "surveyon", "2.9.5", "mobile", ua.IOS},
|
{"surveyon/2.9.5 (iPhone; CPU iPhone OS 12_5_7 like Mac OS X)", "surveyon", "2.9.5", "mobile", ua.IOS},
|
||||||
{"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.187 Mobile Safari/534.11+", "BlackBerry", "7.0.0.187", "mobile", "BlackBerry"},
|
{"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.187 Mobile Safari/534.11+", "BlackBerry", "7.0.0.187", "mobile", "BlackBerry"},
|
||||||
{"Mozilla/5.0 (X11; CrOS armv7l 13099.110.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.136 Safari/537.36", ua.Chrome, "84.0.4147.136", "desktop", ua.ChromeOS},
|
{"Mozilla/5.0 (X11; CrOS armv7l 13099.110.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.136 Safari/537.36", ua.Chrome, "84.0.4147.136", "desktop", ua.ChromeOS},
|
||||||
|
{"SonyEricssonK310iv/R4DA Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Link/6.3.1.13.0", "NetFront", "3.3", "mobile", ""},
|
||||||
|
|
||||||
// Device names
|
// Device names
|
||||||
{"Mozilla/5.0 (Linux; Android 10; 8092) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36", "Chrome", "112.0.0.0", "mobile", ua.Android, "8092"},
|
{"Mozilla/5.0 (Linux; Android 10; 8092) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36", "Chrome", "112.0.0.0", "mobile", ua.Android, "8092"},
|
||||||
@@ -206,6 +210,11 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSingle(t *testing.T) {
|
||||||
|
agent := ua.Parse("SonyEricssonK310iv/R4DA Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Link/6.3.1.13.0")
|
||||||
|
fmt.Printf("\n%+v\n", agent)
|
||||||
|
}
|
||||||
|
|
||||||
var testUA ua.UserAgent
|
var testUA ua.UserAgent
|
||||||
|
|
||||||
func BenchmarkUserAgent(b *testing.B) {
|
func BenchmarkUserAgent(b *testing.B) {
|
||||||
|
|||||||
Reference in New Issue
Block a user