-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathheaders.go
More file actions
134 lines (118 loc) · 3.16 KB
/
headers.go
File metadata and controls
134 lines (118 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"net/http"
"strings"
)
// platformStrings is a list of platform names to look for in User-Agent
// strings. The order matters; the first one found will be considered the
// correct result.
var platformStrings = []string{
"Android",
"Linux",
"Macintosh",
"iPad",
"iPod",
"iPhone",
"iOS",
"Darwin",
"dataaccessd",
"Windows",
"Blackberry",
"BlackBerry",
"BB10",
"MAC",
"Mac",
"Microsoft",
"MICROSOFT",
"MS Web Service",
"WIN",
"Win",
"GoogleAuth",
"Gms-Backup",
"GmsCore",
"okhttp",
"CaptiveNetworkSupport",
"CloudKit",
"CFNetwork",
"com.apple",
}
var platformAliases = map[string]string{
"BB10": "Blackberry",
"BlackBerry": "Blackberry",
"MAC": "Macintosh",
"Mac": "Macintosh",
"macOS": "Macintosh",
"MICROSOFT": "Windows",
"Microsoft": "Windows",
"MS Web Service": "Windows",
"WIN": "Windows",
"Win": "Windows",
"GoogleAuth": "Android",
"Gms-Backup": "Android",
"Gms-Core": "Android",
"okhttp": "Android",
"CaptiveNetworkSupport": "Darwin",
"CloudKit": "Darwin",
"CFNetwork": "Darwin",
"dataaccessd": "Darwin",
"iOS": "Darwin",
"com.apple": "Darwin",
"Chrome OS": "Linux",
"Chromium OS": "Linux",
}
var darwinPlatforms = map[string]bool{
"Macintosh": true,
"iPhone": true,
"iPad": true,
"iPod": true,
"Darwin": true,
}
// platform examines a Sec-CH-UA-Platform or User-Agent string and attempts to return
// the platform that the client is running on. If it can't detect the platform,
// it returns the empty string. Apple products are distinguished if possible
// (Macintosh, iPad, etc.), but often will be just Darwin.
func platform(header http.Header) string {
chPlatform := platformFromClientHint(header.Get("Sec-CH-UA-Platform"))
// If the UA value is "iOS", check the UA string, to
// determine the form factor (iPhone, iPad, iPod).
if chPlatform != "" && chPlatform != "iOS" {
return chPlatform
}
if uaPlatform := platformFromUA(header.Get("User-Agent")); uaPlatform != "" {
return uaPlatform
}
return chPlatform
}
// platformFromClientHint examines a Sec-CH-UA-Platform string for platform value.
// If the header is not present or is "Unknown", fall back to parsing the User-Agent string.
func platformFromClientHint(ch string) string {
if ch == "" {
return ""
}
ch = strings.Trim(ch, `"`)
if ch == "Unknown" {
return ""
}
if chAlias, ok := platformAliases[ch]; ok {
return chAlias
}
return ch
}
// platformFromUA examines a User-Agent string and attempts to return
// the platform that the client is running on. If it can't detect the platform,
// it returns the empty string. Apple products are distinguished if possible
// (Macintosh, iPad, etc.), but often will be just Darwin.
func platformFromUA(ua string) string {
if ua == "" {
return ""
}
for _, p := range platformStrings {
if strings.Contains(ua, p) {
if p2, ok := platformAliases[p]; ok {
p = p2
}
return p
}
}
return ""
}