-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathClientHints.cs
More file actions
391 lines (351 loc) · 14.3 KB
/
ClientHints.cs
File metadata and controls
391 lines (351 loc) · 14.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using YamlDotNet.Core.Tokens;
namespace DeviceDetectorNET
{
/// <summary>
/// @todo: use IRegexEngine
/// </summary>
public class ClientHints
{
/// <summary>
/// Represents `Sec-CH-UA-Arch` header field: The underlying architecture's instruction
/// </summary>
public string Architecture { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Bitness` header field: The underlying architecture's bitness
/// </summary>
public string Bitness { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Mobile` header field: whether the user agent should receive a specifically "mobile" UX
/// </summary>
public bool Mobile { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Model` header field: the user agent's underlying device model
/// </summary>
public string Model { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Platform` header field: the platform's brand
/// </summary>
public string Platform { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Platform-Version` header field: the platform's major version
/// </summary>
public string PlatformVersion { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Full-Version` header field: the platform's major version
/// </summary>
public string UaFullVersion { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Full-Version-List` header field: the full version for each brand in its brand list
/// </summary>
public IReadOnlyDictionary<string, string> FullVersionList { get; set; }
/// <summary>
/// Represents `x-requested-with` header field: Android app id
/// </summary>
public string App { get; set; }
/// <summary>
/// Represents `Sec-CH-UA-Form-Factors` header field: form factor device type name
/// </summary>
public List<string> FormFactors { get; set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="model">`Sec-CH-UA-Model` header field</param>
/// <param name="platform">`Sec-CH-UA-Platform` header field</param>
/// <param name="platformVersion">`Sec-CH-UA-Platform-Version` header field</param>
/// <param name="uaFullVersion">`Sec-CH-UA-Full-Version` header field</param>
/// <param name="fullVersionList">`Sec-CH-UA-Full-Version-List` header field</param>
/// <param name="mobile">`Sec-CH-UA-Mobile` header field</param>
/// <param name="architecture">`Sec-CH-UA-Arch` header field</param>
/// <param name="bitness">`Sec-CH-UA-Bitness`</param>
/// <param name="app">`HTTP_X-REQUESTED-WITH`</param>
/// <param name="formFactors">`Sec-CH-UA-Form-Factors` header field</param>
public ClientHints(string model, string platform, string platformVersion, string uaFullVersion,
IReadOnlyDictionary<string,string> fullVersionList, bool mobile, string architecture, string bitness, string app, List<string> formFactors)
{
Model = model;
Platform = platform;
PlatformVersion = platformVersion;
UaFullVersion = uaFullVersion;
FullVersionList = fullVersionList;
Mobile = mobile;
Architecture = architecture;
Bitness = bitness;
App = app;
FormFactors = formFactors;
}
/// <summary>
/// Returns if the client hints
/// </summary>
/// <returns></returns>
public bool IsMobile()
{
return this.Mobile;
}
/// <summary>
/// Returns the Architecture
/// </summary>
/// <returns></returns>
public string GetArchitecture()
{
return this.Architecture;
}
/// <summary>
/// Returns the Bitness
/// </summary>
/// <returns></returns>
public string GetBitness()
{
return this.Bitness;
}
/// <summary>
/// Returns the device model
/// </summary>
/// <returns></returns>
public string GetModel()
{
return this.Model;
}
/// <summary>
/// Returns the Operating System
/// </summary>
/// <returns></returns>
public string GetOperatingSystem()
{
return this.Platform;
}
/// <summary>
/// Returns the Operating System version
/// </summary>
/// <returns></returns>
public string GetOperatingSystemVersion()
{
return this.PlatformVersion;
}
/// <summary>
/// Returns the Browser name
/// </summary>
/// <returns></returns>
public IReadOnlyDictionary<string, string> GetBrandList()
{
if (FullVersionList.Count > 0)
{
return FullVersionList;
//$brands = \array_column($this->fullVersionList, 'brand');
//$versions = \array_column($this->fullVersionList, 'version');
//if (\count($brands) === \count($versions)) {
// return \array_combine($brands, $versions);
//}
}
return new Dictionary<string, string>();
}
/// <summary>
/// Returns the Browser version
/// </summary>
/// <returns></returns>
public string GetBrandVersion()
{
if (!string.IsNullOrEmpty(UaFullVersion))
{
return UaFullVersion;
}
return null;
}
/// <summary>
/// Returns the Android app id
/// </summary>
/// <returns></returns>
public string GetApp()
{
return App;
}
/// <summary>
/// Returns the formFactor device type name
/// </summary>
/// <returns></returns>
public List<string> GetFormFactors()
{
return FormFactors;
}
/// <summary>
/// Factory method to easily instantiate this class using an array containing all available (client hint) header
/// </summary>
/// <param name="headers"></param>
/// <returns></returns>
public static ClientHints Factory(Dictionary<string,string> headers)
{
var model = string.Empty;
var platform = string.Empty;
var platformVersion = string.Empty;
var uaFullVersion = string.Empty;
var architecture = string.Empty;
var bitness = string.Empty;
var app = string.Empty;
var mobile = false;
var fullVersionList = new Dictionary<string, string>();
var formFactors = new List<string>();
// any kind of html tag is unexpected as part of those headers, so discard values that contain some.
//if (\is_string($value) && \strip_tags($value) !== $value) {
foreach (var header in headers)
{
if (string.IsNullOrEmpty(header.Value))
continue;
var headerNormalized = header.Key.ToLower().Replace("_", "-");
switch (headerNormalized)
{
case "http-sec-ch-ua-arch":
case "sec-ch-ua-arch":
case "arch":
case "architecture":
architecture = header.Value.Trim('"');
break;
case "http-sec-ch-ua-bitness":
case "sec-ch-ua-bitness":
case "bitness":
bitness = header.Value.Trim('"');
break;
case "http-sec-ch-ua-mobile":
case "sec-ch-ua-mobile":
case "mobile":
//if (header.Value.GetType() == typeof(bool))
//{
// mobile = (bool)header.Value;
//}
//else
{
mobile = header.Value.Equals("1") || header.Value.Equals("?1");
}
break;
case "http-sec-ch-ua-model":
case "sec-ch-ua-model":
case "model":
model = header.Value.Trim('"');
break;
case "http-sec-ch-ua-full-version":
case "sec-ch-ua-full-version":
case "uafullversion":
uaFullVersion = header.Value.Trim('"');
break;
case "http-sec-ch-ua-platform":
case "sec-ch-ua-platform":
case "platform":
platform = header.Value.Trim('"');
break;
case "http-sec-ch-ua-platform-version":
case "sec-ch-ua-platform-version":
case "platformversion":
platformVersion = header.Value.Trim('"');
break;
case "brands":
//if (fullVersionList.Count > 0)
//{
// break;
//}
// use this only if no other header already set the list
case "fullversionlist":
//fullVersionList = (header.Value.GetType() == typeof(List<string>))
// ? (List<string>)header.Value
// : fullVersionList;
break;
case "http-sec-ch-ua":
case "sec-ch-ua":
//if (fullVersionList.Count > 0)
//{
// break;
//}
// use this only if no other header already set the list
case "http-sec-ch-ua-full-version-list":
case "sec-ch-ua-full-version-list":
//from up case
if (headerNormalized.Equals("fullversionlist"))
{
//header
//fullVersionList
//headerNormalized
break;
}
//from up case
if (headerNormalized.Equals("brands") &&
fullVersionList.Count > 0)
{
break;
}
//from up case
if (headerNormalized.Equals("http-sec-ch-ua") ||
headerNormalized.Equals("sec-ch-ua") &&
fullVersionList.Count > 0)
{
break;
}
const string reg = "^\"([^\"]+)\"; ?v=\"([^\"]+)\"(?:, )?";
var list = new Dictionary<string,string>();
var value = header.Value;
while (!string.IsNullOrEmpty(value))
{
var r = new Regex(reg, RegexOptions.IgnoreCase);
var match = r.Match(value);
if (match.Success)
{
while (match.Success)
{
var substr = match.Groups[0].Value;
var brand = match.Groups[1].Value;
var version = match.Groups[2].Value;
if (!list.ContainsKey(brand))
{
list.Add(brand, version);
}
value = value.Substring(substr.Length);
match = match.NextMatch();
}
}
else
{
value = string.Empty;
}
}
if (list.Count > 0)
{
fullVersionList = list;
}
break;
case "http-x-requested-with":
case "x-requested-with":
if (!header.Value.Equals("xmlhttprequest", StringComparison.InvariantCultureIgnoreCase)) {
app = header.Value;
}
break;
case "formfactors":
case "http-sec-ch-ua-form-factors":
case "sec-ch-ua-form-factors":
{
if (header.Key.Length > 1)
{
formFactors.Add(header.Value);
}
else
{
const string reg_frm = "~\"([a-z]+)\"~i";
var r = new Regex(reg_frm, RegexOptions.IgnoreCase);
var matchs = r.Matches(header.Value);
if (matchs.Count > 0)
{
foreach (Match match in matchs)
{
formFactors.Add(match.Value);
}
}
}
break;
}
default:
break;
}
}
var clientHints = new ClientHints(model, platform, platformVersion, uaFullVersion, fullVersionList, mobile, architecture, bitness, app, formFactors);
return clientHints;
}
}
}