avct / uasurfer

Go package for fast and reliable abstraction of browser user agent strings.
Other
337 stars 76 forks source link

Fix the bug: mobile bots being recognized as a real browser #38

Closed Dorokhov closed 5 years ago

Dorokhov commented 5 years ago

Changed browser detection implementation so that it tries to recognize the bot first and if no luck - then browser type.

It should have more sense, because bots mimic the real browsers and the string contains both kind of attributes: bot and real browser. The previous implementation didn't try to recognize a bot if there are any browser attributes in the user agent string.

The bug reproducing the problem is covered by the unit tests. @iand @tealeg @ryanslade @vavrusa

ryanslade commented 5 years ago

Thanks for this.

My concern is that this moves a lot of checks before webkit which slows down those cases a lot:

benchmark                         old ns/op     new ns/op     delta
BenchmarkAgentSurfer-4            7004          8673          +23.83%
BenchmarkAgentSurferReuse-4       6976          8601          +23.29%
BenchmarkEvalSystem-4             4535          4557          +0.49%
BenchmarkEvalBrowserName-4        2317          2483          +7.16%
BenchmarkEvalBrowserVersion-4     133           132           -0.75%
BenchmarkEvalDevice-4             1336          1418          +6.14%
BenchmarkParseChromeMac-4         2223          4252          +91.27%
BenchmarkParseChromeWin-4         2086          3856          +84.85%
BenchmarkParseChromeAndroid-4     7550          9787          +29.63%
BenchmarkParseSafariMac-4         3732          5809          +55.65%
BenchmarkParseSafariiPad-4        3835          6216          +62.09%

Instead, we can add the check for GoogleBot to the top of the WebKit switch and keep the ordering as it was before:

if strings.Contains(ua, "applewebkit") {
        switch {
        case strings.Contains(ua, "googlebot"):
            u.Browser.Name = BrowserGoogleBot

   ... 
Dorokhov commented 5 years ago

@ryanslade Thanks for your feedback, fixed now

ryanslade commented 5 years ago

Great, thanks!