GithubHelp home page GithubHelp logo

useragent's Introduction

UserAgent

Build Status Gem Version

UserAgent is a Ruby library that parses and compares HTTP User Agents.

Installation

gem install useragent

Examples

Reporting

string = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5'
user_agent = UserAgent.parse(string)
user_agent.browser
# => 'Chrome'
user_agent.version
# => '19.0.1084.56'
user_agent.platform
# => 'Macintosh'

Comparison

Browser = Struct.new(:browser, :version)

SupportedBrowsers = [
  Browser.new("Safari", "3.1.1"),
  Browser.new("Firefox", "2.0.0.14"),
  Browser.new("Internet Explorer", "7.0")
]

user_agent = UserAgent.parse(request.user_agent)
SupportedBrowsers.detect { |browser| user_agent >= browser }

Copyright (c) 2015-2019 Garry Shutler, released under the MIT license

useragent's People

Contributors

actsasflinn avatar anaisbetts avatar andrewwillis avatar arches avatar assembler avatar baldrailers avatar bartimaeus avatar bmorrall avatar bookwyrm avatar bowsersenior avatar craic avatar getabetterpic avatar gshutler avatar jeremy avatar josh avatar kevinzen avatar michieldewit avatar nikz avatar pencil avatar phocke avatar qrush avatar raycohen avatar sapientpants avatar spacechurro avatar tay avatar tekkub avatar terryfinn avatar vanderhoorn avatar ziggythehamster avatar zlx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

useragent's Issues

changelog

Please, could you provide a changelog file or a changelog in github release.

iPad in-app browser not properly detected

The iPad in-app browser from Feedly (UA string Mozilla/5.0 (iPad; CPU OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F69) is not properly recognized:

ipad = UserAgent.parse 'Mozilla/5.0 (iPad; CPU OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F69'
=> [#<UserAgent:0x007f8f449c0ce0 @comment=["iPad", "CPU OS 8_3 like Mac OS X"], @product="Mozilla", @version=#<UserAgent::Version 5.0>>,
 #<UserAgent:0x007f8f449c0240 @comment=["KHTML, like Gecko"], @product="AppleWebKit", @version=#<UserAgent::Version 600.1.4>>,
 #<UserAgent:0x007f8f449cb8e8 @comment=nil, @product="Mobile", @version=#<UserAgent::Version 12F69>>]

ipad.version
=> #<UserAgent::Version >

I'm not 100% sure if this is an issue with Feedly's in-app browser (it's missing both the Version/<> and Safari/<> strings), or if this is a bug in useragent. I did some debugging, and there seem to be some weird things going on with the UA parsing:

https://github.com/gshutler/useragent/blob/master/lib/user_agent/operating_systems.rb#L23, where
if os =~ /CPU OS\s*([0-9_\.]+)?\slike Mac OS X/ matches the iPad in-app UA. This means that os is set by https://github.com/gshutler/useragent/blob/master/lib/user_agent/browsers/webkit.rb#L96 to something that does not match https://github.com/gshutler/useragent/blob/master/lib/user_agent/browsers/webkit.rb#L57, which, if not a bug, is at least a bit smelly

Below a number of UA strings from iPad and iPhone browsers, both in-app (Feedly) and in the Safari app

description UA
iPad Feedly in-app Mozilla/5.0 (iPad; CPU OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F69
iPad Safari Mozilla/5.0 (iPad; CPU OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F69 Safari/600.1.4
iPhone Feedly in-app Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F70
iPhone Safari Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4

Microsoft Edge User Agent String

Related to #16

According to the official Microsoft documentation.

The User-Agent of Microsoft Edge is:
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43

Microsoft selected the Edg token to avoid compatibility issues that may be caused by using the string Edge.
In this case, it will parse Microsoft Edge as Chrome.

NoMethodError: undefined method `join' for nil:NilClass

$ irb
2.2.2 :001 > require 'useragent'
 => true
2.2.2 :002 > ua = UserAgent.parse("Mozilla/5.0 SHC; SHC-Unit-04973; SHC-HTS; SHC-KIOSK; SHC-MAC-0000 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586")
 => [#<UserAgent:0x007fbf5ca19960 @product="Mozilla", @version=#<UserAgent::Version 5.0>, @comment=nil>, #<UserAgent:0x007fbf5ca194d8 @product="SHC;", @version=#<UserAgent::Version >, @comment=nil>, #<UserAgent:0x007fbf5ca19230 @product="SHC-Unit-04973;", @version=#<UserAgent::Version >, @comment=nil>, #<UserAgent:0x007fbf5ca18f88 @product="SHC-HTS;", @version=#<UserAgent::Version >, @comment=nil>, #<UserAgent:0x007fbf5ca18ce0 @product="SHC-KIOSK;", @version=#<UserAgent::Version >, @comment=nil>, #<UserAgent:0x007fbf5ca18a10 @product="SHC-MAC-0000", @version=#<UserAgent::Version >, @comment=["Windows NT 10.0", "Win64", "x64"]>, #<UserAgent:0x007fbf5ca18678 @product="AppleWebKit", @version=#<UserAgent::Version 537.36>, @comment=["KHTML, like Gecko"]>, #<UserAgent:0x007fbf5ca18218 @product="Chrome", @version=#<UserAgent::Version 46.0.2486.0>, @comment=nil>, #<UserAgent:0x007fbf5ca23cf8 @product="Safari", @version=#<UserAgent::Version 537.36>, @comment=nil>, #<UserAgent:0x007fbf5ca23960 @product="Edge", @version=#<UserAgent::Version 13.10586>, @comment=nil>]
2.2.2 :003 > ua.os
NoMethodError: undefined method `join' for nil:NilClass
    from /Users/rohit/.rvm/gems/ruby-2.2.2/gems/useragent-0.16.5/lib/user_agent/browsers/edge.rb:21:in `os'

#platform is not normalized for Windows user on QQ Browser

[2] pry(main)> ua = UserAgent.parse('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.691.400 QQBrowser/9.0.2524.400')
[3] pry(main)> ua.os
=> "Windows 7"
[4] pry(main)> ua.platform
=> "Windows NT 6.1"

It seems to parse the OS correctly, but the platform is not normalized.
I'd expect #platform to return Windows as it does for other Win7 browsers

Gem version: 0.16.8

Unsupported user-agent

I found a request in my app coming from the following user-agent:
HUAWEI_MT7-TL00_TD/5.0 Android/4.4.2 (Linux; U; Android 4.4.2; zh-cn) Release/01.18.2014 Browser/WAP2.0 (AppleWebKit/537.36) Mobile Safari/537.36

The only useful information I was able to retrieve using the gem were:

  • browser: HUAWEI_MT7-TL00_TD
  • version: 5.0
  • mobile?: true

http://www.useragentstring.com/ was able to return more information from that UA string

Version comparing bug

(dev)> a = UserAgent.parse('Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko (Internet Explorer 11.0)')
=> [#<UserAgent:0x0055cb05854e10 @product="Mozilla", @version=#<UserAgent::Version 5.0>, comment["Windows NT 10.0", "WOW64", "Trident/7.0", "rv:11.0"], #<UserAgent:0x0055cb058546b8 @product="like", @version=#<UserAgent::Version >, commentnil, #<UserAgent:0x0055cb05854370 @product="Gecko", @version=#<UserAgent::Version >, comment["Internet Explorer 11.0"]]
(dev)> a.version > '9.0'
=> true
(dev)> '9.0' > a.version
=> true
(dev)> 

Android versions are incorrect

It always shows up like
->browser: Android
->version: 4.0
from Android devices. And it is wrong.

in such cases:
Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9
Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; HTC_IncredibleS_S710e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.4; fr-fr; HTC Desire Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile myTouch 3G Slide Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; zh-tw; HTC_Pyramid Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; zh-tw; HTC_Pyramid Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari
Mozilla/5.0 (Linux; U; Android 2.3.3; zh-tw; HTC Pyramid Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; ko-kr; LG-LU3000 Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; HTC_DesireS_S510e Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; HTC_DesireS_S510e Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile
Mozilla/5.0 (Linux; U; Android 2.3.3; de-de; HTC Desire Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; de-ch; HTC Desire Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2; fr-lu; HTC Legend Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2; en-sa; HTC_DesireHD_A9191 Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2.1; fr-fr; HTC_DesireZ_A7272 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2.1; en-gb; HTC_DesireZ_A7272 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2.1; en-ca; LG-P505R Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

I expected
browser: Android
version: 4.0.3
or
browser: Android
version: 2.3.3
or
browser: Android
version: 2.2.1
....
etc

Feature Request: #to_h

It would be interesting if you could generate a hash with symbolized keys and string values. This would fit in well with the functionality of the addressable and cgi gems. Using those two and your useragent gem we could parse out everything of interest in the URL and UserAgent strings.

Consider adding #real_version to Base

We are using Errbit for application error tracking and noticed that our error reports were erroneously saying "Internet Explorer 7" for some of our internal users who are actually using IE 11.

Errbit uses this gem for user agent identification and it turns out that our corporate settings for IE are flagging our site as being in the Intranet zone, which automatically turns on "compatibility mode" (see here for the gory details). Our actual site is using <meta http-equiv="x-ua-compatible" content="ie=edge"> to force the browser back out of compatibility mode, but by the time the server sees the User-Agent header it is too late for that to matter.

I would like to make a fix to Errbit to have it call #real_version to get the actual version of IE being used in compatibility mode, but I would rather not have to add a call to #respond_to?(:real_version) and bifurcate the code.

Would it make sense to add this to UserAgent::Browsers::Base:

def real_version
  version
end

If that were done, I could simply change the Errbit code to call #real_version instead of #version and avoid this issue with compatibility mode.

Why <=> returns false for invalid comparison?

If <=> returns nil for invalid comparison, we can reuse the standard Comparable module, which has been available since at least 1.9.3. Any use case for that? I think most people would just use other derived operators, instead of directly use <=>.

Opera browser 28 is reported as version 5.0

The chromium-based Opera browser is correctly detected, but the reported version is 5.0 from the Mozilla/5.0 product, instead of the actual version from OPR/28.0.1750.48. The actual user-agent string is:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36 OPR/28.0.1750.48

Microsoft Edge Parsed as Chrome

Example of an edge useragent = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600

The gem is parsing this as Chrome 42, however it is Edge 12.

Error in playstation.rb

sometimes I get an error in playstation.rb:
NoMethodError: undefined method `include?' for nil:NilClass
in line 11
so probably 'agent.application.comment.first' is sometimes nil

regards,
Henk

Feature Request: Register a custom 'browser'

Hi,

I'm looking for the best way to register a custom browser to parse against. Say for instance I'd like to parse a native App's user agent and get similar output as a common browser.

Thanks in advance,
-Spencer

Parsing does not work correctly for OS with a downcased string

example

eg_string = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5"
UserAgent.parse(eg_string).os
=> "OS X 10.6.8"
UserAgent.parse(eg_string.downcase).os
=> "intel mac os x 10_6_8"

It's seemed the RubyGems version (0.16.0) is missing some files in browsers folder.

When I upgrade useragent gem to 0.16.0, I catch the error:
LoadError: cannot load such file -- user_agent/browsers/playstation
...
/Users/Sean/.rvm/gems/ruby-2.2.2@xxx/gems/useragent-0.16.0/lib/user_agent/browsers.rb:12:in `<top (required)>'
...

I found that the browsers.rb is new version which it contains " require 'user_agent/browsers/playstation' ",
but there is no file named playstation.rb in gem's lib/user_agent/browsers/ folder.

the source is https://rubygems.org.

Inconsistent iOS Detection

I found some of my iOS identification strings returning 'iOS 3.2' and others returning 'CPU iPhone OS 3_1_3 like Mac OS X' which was a little jarring.

I've added a pull request that fixes this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.