Parse 解析UserAgent
(userAgent string)
| 70 | |
| 71 | // Parse 解析UserAgent |
| 72 | func (this *UserAgentParser) Parse(userAgent string) (result UserAgentParserResult) { |
| 73 | // 限制长度 |
| 74 | if len(userAgent) == 0 || len(userAgent) > 256 { |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | var userAgentKey = fnv.HashString(userAgent) |
| 79 | var shardingIndex = int(userAgentKey % userAgentShardingCount) |
| 80 | |
| 81 | this.mu.RLock(shardingIndex) |
| 82 | cacheResult, ok := this.cacheMaps[shardingIndex][userAgentKey] |
| 83 | if ok { |
| 84 | this.mu.RUnlock(shardingIndex) |
| 85 | return cacheResult |
| 86 | } |
| 87 | this.mu.RUnlock(shardingIndex) |
| 88 | |
| 89 | var parser = this.pool.Get().(*useragent.UserAgent) |
| 90 | parser.Parse(userAgent) |
| 91 | result.OS = parser.OSInfo() |
| 92 | result.BrowserName, result.BrowserVersion = parser.Browser() |
| 93 | result.IsMobile = parser.Mobile() |
| 94 | this.pool.Put(parser) |
| 95 | |
| 96 | // 忽略特殊字符 |
| 97 | if len(result.BrowserName) > 0 { |
| 98 | for _, r := range result.BrowserName { |
| 99 | if r == '$' || r == '"' || r == '\'' || r == '<' || r == '>' || r == ')' { |
| 100 | return |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | this.mu.Lock(shardingIndex) |
| 106 | this.cacheMaps[shardingIndex][userAgentKey] = result |
| 107 | this.mu.Unlock(shardingIndex) |
| 108 | |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | // MaxCacheItems 读取能容纳的缓存最大数量 |
| 113 | func (this *UserAgentParser) MaxCacheItems() int { |