Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
Added support for Dalvik user agent parsing
Browse files Browse the repository at this point in the history
Dalvik user agent is sent in a request header from a browser
running in Android (which runs within Dalvik).

Resolves: #22
  • Loading branch information
nivnadler committed Jan 5, 2016
1 parent 63395b1 commit 7841362
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
32 changes: 32 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,38 @@ var uastrings = []struct {
ua: "Mozilla/5.0 (SymbianOS/9.1; U; [en-us]) AppleWebKit/413 (KHTML, like Gecko) Safari/413",
expected: "Mozilla:5.0 Platform:Symbian OS:SymbianOS/9.1 Browser:Symbian-413 Engine:AppleWebKit-413 Bot:false Mobile:true",
},

// Dalvik
{
title: "Dalvik - Dell:001DL",
ua: "Dalvik/1.2.0 (Linux; U; Android 2.2.2; 001DL Build/FRG83G)",
expected: "Mozilla:5.0 Platform:Linux OS:Android 2.2.2 Bot:false Mobile:true",
},
{
title: "Dalvik - HTC:001HT",
ua: "Dalvik/1.4.0 (Linux; U; Android 2.3.3; 001HT Build/GRI40)",
expected: "Mozilla:5.0 Platform:Linux OS:Android 2.3.3 Bot:false Mobile:true",
},
{
title: "Dalvik - ZTE:009Z",
ua: "Dalvik/1.4.0 (Linux; U; Android 2.3.4; 009Z Build/GINGERBREAD)",
expected: "Mozilla:5.0 Platform:Linux OS:Android 2.3.4 Bot:false Mobile:true",
},
{
title: "Dalvik - A850",
ua: "Dalvik/1.6.0 (Linux; U; Android 4.2.2; A850 Build/JDQ39) Configuration/CLDC-1.1; Opera Mini/att/4.2",
expected: "Mozilla:5.0 Platform:Linux OS:Android 4.2.2 Bot:false Mobile:true",
},
{
title: "Dalvik - Asus:T00Q",
ua: "Dalvik/1.6.0 (Linux; U; Android 4.4.2; ASUS_T00Q Build/KVT49L)/CLDC-1.1",
expected: "Mozilla:5.0 Platform:Linux OS:Android 4.4.2 Bot:false Mobile:true",
},
{
title: "Dalvik - W2430",
ua: "Dalvik/1.6.0 (Linux; U; Android 4.0.4; W2430 Build/IMM76D)014; Profile/MIDP-2.1 Configuration/CLDC-1",
expected: "Mozilla:5.0 Platform:Linux OS:Android 4.0.4 Bot:false Mobile:true",
},
}

// Internal: beautify the UserAgent reference into a string so it can be
Expand Down
5 changes: 4 additions & 1 deletion browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ func (p *UserAgent) detectBrowser(sections []section) {
slen := len(sections)

if sections[0].name == "Opera" {
p.mozilla = ""
p.browser.Name = "Opera"
p.browser.Version = sections[0].version
p.browser.Engine = "Presto"
if slen > 1 {
p.browser.EngineVersion = sections[1].version
}
} else if sections[0].name == "Dalvik" {
// When Dalvik VM is in use, there is no browser info attached to ua.
// Although browser is still a Mozilla/5.0 compatible.
p.mozilla = "5.0"
} else if slen > 1 {
engine := sections[1]
p.browser.Engine = engine.name
Expand Down
21 changes: 21 additions & 0 deletions operating_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,23 @@ func opera(p *UserAgent, comment []string) {
}
}

// Guess the OS. Android browsers send Dalvik as the user agent in the
// request header.
//
// The first argument p is a reference to the current UserAgent and the second
// argument is a slice of strings containing the comment.
func dalvik(p *UserAgent, comment []string) {
slen := len(comment)

if strings.HasPrefix(comment[0], "Linux") {
p.platform = comment[0]
if slen > 2 {
p.os = comment[2]
}
p.mobile = true
}
}

// Given the comment of the first section of the UserAgent string,
// get the platform.
func getPlatform(comment []string) string {
Expand Down Expand Up @@ -238,6 +255,10 @@ func (p *UserAgent) detectOS(s section) {
if len(s.comment) > 0 {
opera(p, s.comment)
}
} else if s.name == "Dalvik" {
if len(s.comment) > 0 {
dalvik(p, s.comment)
}
} else {
// Check whether this is a bot or just a weird browser.
p.undecided = true
Expand Down
13 changes: 9 additions & 4 deletions user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
// information that has been extracted from a parsed User Agent string.
package user_agent

import (
"strings"
)
import "strings"

// A section contains the name of the product, its version and
// an optional comment.
Expand Down Expand Up @@ -141,7 +139,9 @@ func (p *UserAgent) Parse(ua string) {
}

if len(sections) > 0 {
p.mozilla = sections[0].version
if sections[0].name == "Mozilla" {
p.mozilla = sections[0].version
}

p.detectBrowser(sections)
p.detectOS(sections[0])
Expand All @@ -167,3 +167,8 @@ func (p *UserAgent) Bot() bool {
func (p *UserAgent) Mobile() bool {
return p.mobile
}

// Returns the original given user agent.
func (p *UserAgent) UA() string {
return p.ua
}

0 comments on commit 7841362

Please sign in to comment.