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

Commit

Permalink
Leave early if the user agent belongs to a bot.
Browse files Browse the repository at this point in the history
  • Loading branch information
mssola committed Sep 25, 2014
1 parent f991723 commit c0c2e55
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ func (p *UserAgent) Parse(ua string) {
p.mozilla = sections[0].version
if !p.bot {
for _, v := range sections {
p.checkBot(v.comment)
if p.isBot(v.comment) {
p.bot = true
break
}
}
if !p.bot {
p.detectBrowser(sections)
Expand All @@ -136,21 +139,21 @@ func (p *UserAgent) Parse(ua string) {
}
}

// Check if we're dealing with a Bot.
func (p *UserAgent) checkBot(comment []string) {
// Returns true if we're dealing with a bot, false otherwise.
func (p *UserAgent) isBot(comment []string) bool {
// Regular bots (Google, Bing, ...).
reg, _ := regexp.Compile("(?i)bot")
for _, v := range comment {
if reg.Match([]byte(v)) {
p.bot = true
return
return true
}
}

// Special case for the Baidu bot.
if len(comment) > 1 && strings.HasPrefix(comment[1], "Baidu") {
p.bot = true
return true
}
return false
}

// Returns the mozilla version (it's how the User Agent string begins:
Expand Down

0 comments on commit c0c2e55

Please sign in to comment.