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

Commit 7841362

Browse files
committed
Added support for Dalvik user agent parsing
Dalvik user agent is sent in a request header from a browser running in Android (which runs within Dalvik). Resolves: #22
1 parent 63395b1 commit 7841362

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

all_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,38 @@ var uastrings = []struct {
409409
ua: "Mozilla/5.0 (SymbianOS/9.1; U; [en-us]) AppleWebKit/413 (KHTML, like Gecko) Safari/413",
410410
expected: "Mozilla:5.0 Platform:Symbian OS:SymbianOS/9.1 Browser:Symbian-413 Engine:AppleWebKit-413 Bot:false Mobile:true",
411411
},
412+
413+
// Dalvik
414+
{
415+
title: "Dalvik - Dell:001DL",
416+
ua: "Dalvik/1.2.0 (Linux; U; Android 2.2.2; 001DL Build/FRG83G)",
417+
expected: "Mozilla:5.0 Platform:Linux OS:Android 2.2.2 Bot:false Mobile:true",
418+
},
419+
{
420+
title: "Dalvik - HTC:001HT",
421+
ua: "Dalvik/1.4.0 (Linux; U; Android 2.3.3; 001HT Build/GRI40)",
422+
expected: "Mozilla:5.0 Platform:Linux OS:Android 2.3.3 Bot:false Mobile:true",
423+
},
424+
{
425+
title: "Dalvik - ZTE:009Z",
426+
ua: "Dalvik/1.4.0 (Linux; U; Android 2.3.4; 009Z Build/GINGERBREAD)",
427+
expected: "Mozilla:5.0 Platform:Linux OS:Android 2.3.4 Bot:false Mobile:true",
428+
},
429+
{
430+
title: "Dalvik - A850",
431+
ua: "Dalvik/1.6.0 (Linux; U; Android 4.2.2; A850 Build/JDQ39) Configuration/CLDC-1.1; Opera Mini/att/4.2",
432+
expected: "Mozilla:5.0 Platform:Linux OS:Android 4.2.2 Bot:false Mobile:true",
433+
},
434+
{
435+
title: "Dalvik - Asus:T00Q",
436+
ua: "Dalvik/1.6.0 (Linux; U; Android 4.4.2; ASUS_T00Q Build/KVT49L)/CLDC-1.1",
437+
expected: "Mozilla:5.0 Platform:Linux OS:Android 4.4.2 Bot:false Mobile:true",
438+
},
439+
{
440+
title: "Dalvik - W2430",
441+
ua: "Dalvik/1.6.0 (Linux; U; Android 4.0.4; W2430 Build/IMM76D)014; Profile/MIDP-2.1 Configuration/CLDC-1",
442+
expected: "Mozilla:5.0 Platform:Linux OS:Android 4.0.4 Bot:false Mobile:true",
443+
},
412444
}
413445

414446
// Internal: beautify the UserAgent reference into a string so it can be

browser.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ func (p *UserAgent) detectBrowser(sections []section) {
3636
slen := len(sections)
3737

3838
if sections[0].name == "Opera" {
39-
p.mozilla = ""
4039
p.browser.Name = "Opera"
4140
p.browser.Version = sections[0].version
4241
p.browser.Engine = "Presto"
4342
if slen > 1 {
4443
p.browser.EngineVersion = sections[1].version
4544
}
45+
} else if sections[0].name == "Dalvik" {
46+
// When Dalvik VM is in use, there is no browser info attached to ua.
47+
// Although browser is still a Mozilla/5.0 compatible.
48+
p.mozilla = "5.0"
4649
} else if slen > 1 {
4750
engine := sections[1]
4851
p.browser.Engine = engine.name

operating_systems.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ func opera(p *UserAgent, comment []string) {
193193
}
194194
}
195195

196+
// Guess the OS. Android browsers send Dalvik as the user agent in the
197+
// request header.
198+
//
199+
// The first argument p is a reference to the current UserAgent and the second
200+
// argument is a slice of strings containing the comment.
201+
func dalvik(p *UserAgent, comment []string) {
202+
slen := len(comment)
203+
204+
if strings.HasPrefix(comment[0], "Linux") {
205+
p.platform = comment[0]
206+
if slen > 2 {
207+
p.os = comment[2]
208+
}
209+
p.mobile = true
210+
}
211+
}
212+
196213
// Given the comment of the first section of the UserAgent string,
197214
// get the platform.
198215
func getPlatform(comment []string) string {
@@ -238,6 +255,10 @@ func (p *UserAgent) detectOS(s section) {
238255
if len(s.comment) > 0 {
239256
opera(p, s.comment)
240257
}
258+
} else if s.name == "Dalvik" {
259+
if len(s.comment) > 0 {
260+
dalvik(p, s.comment)
261+
}
241262
} else {
242263
// Check whether this is a bot or just a weird browser.
243264
p.undecided = true

user_agent.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
// information that has been extracted from a parsed User Agent string.
99
package user_agent
1010

11-
import (
12-
"strings"
13-
)
11+
import "strings"
1412

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

143141
if len(sections) > 0 {
144-
p.mozilla = sections[0].version
142+
if sections[0].name == "Mozilla" {
143+
p.mozilla = sections[0].version
144+
}
145145

146146
p.detectBrowser(sections)
147147
p.detectOS(sections[0])
@@ -167,3 +167,8 @@ func (p *UserAgent) Bot() bool {
167167
func (p *UserAgent) Mobile() bool {
168168
return p.mobile
169169
}
170+
171+
// Returns the original given user agent.
172+
func (p *UserAgent) UA() string {
173+
return p.ua
174+
}

0 commit comments

Comments
 (0)