useragent: xojoc.pw/useragent Index | Examples | Files

package useragent

import "xojoc.pw/useragent"

Package useragent parses a user agent string.

Index

Examples

Package Files

browser.go crawler.go lex.go parse.go

Constants

const (
    OSAndroid = "Android"
    OSMacOS   = "Mac OS X"
    OSiOS     = "iOS"
    OSLinux   = "GNU/Linux"
    OSWindows = "Windows"
)

type Security Uses

type Security int

Some browsers may put security level information in their user agent string.

const (
    SecurityUnknown Security = iota
    SecurityNone
    SecurityWeak
    SecurityStrong
)

func (Security) String Uses

func (s Security) String() string

type Type Uses

type Type int
const (
    Unknown Type = iota
    Browser
    Crawler
    LinkChecker
    Validator
    FeedReader
    Library
)

func (Type) String Uses

func (a Type) String() string

type UserAgent Uses

type UserAgent struct {
    // The original user agent string.
    Original string
    Type     Type
    // The browser/crawler/etc. name. For example:
    //  Firefox, IceCat, Iceweasel
    //  Dillo
    //  Chrome
    //  MSIE
    //  Googlebot
    //   etc.
    // If the name is not known, Name will be `unknown'.
    Name    string
    Version semver.Version
    // The OS name. Can be one of:
    //  GNU/Linux
    //  FreeBSD
    //  OpenBSD
    //  NetBSD
    //  Windows
    //  Mac OS X
    //  Android
    //  Firefox OS
    //  CrOS
    //   etc.
    // If the os is not known, OS will be `unknown'.
    OS        string
    OSVersion semver.Version
    Security  Security
    // URL with more information about the user agent (in most cases it's the home page).
    // If unknown is nil.
    URL *url.URL
    // Is it a phone device?
    Mobile bool
    // Is it a tablet device?
    Tablet bool
}

func Parse Uses

func Parse(uas string) *UserAgent

Try to extract information about an user agent from uas. Since user agent strings don't have a standard, this function uses heuristics.

Code:

ua := Parse("Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0")
fmt.Print(ua)

Output:

Type: Browser
Name: Firefox
Version: 38.0.0
OS: GNU/Linux
OSVersion: 0.0.0
Security: Unknown security
Mobile: false
Tablet: false

Code:

ua := Parse("Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0")
if ua == nil {
    log.Fatal("cannot parse user agent string")
}
fmt.Println(ua.Type)
fmt.Println(ua.Name)
fmt.Println(ua.Version)
fmt.Println(ua.OS)
if ua.Security != SecurityUnknown {
    fmt.Println(ua.Security)
}

Output:

Browser
Firefox
38.0.0
GNU/Linux

func (*UserAgent) String Uses

func (ua *UserAgent) String() string

Package useragent imports 6 packages (graph) and is imported by 3 packages. Updated 2018-11-19. Refresh now. Tools for package owners.