-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathBrowserCapabilitiesFactory.cs
More file actions
728 lines (639 loc) · 29.1 KB
/
Copy pathBrowserCapabilitiesFactory.cs
File metadata and controls
728 lines (639 loc) · 29.1 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.SystemWebAdapters.Features;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
namespace System.Web.Configuration;
/// <summary>
/// This implementation draws inspiration from <see href="https://referencesource.microsoft.com/#System.Web/Configuration/BrowserCapabilitiesFactory.cs"/>, but
/// fixes up some patterns that were inefficient:
///
/// - Regex instances were not cached and created with each process
/// - Matches were copied to a separate array instead of accessing directly (new Regex APIs help here)
/// - The default process method listed a ton of methods to process and called them individually - now we put it in a list and iterate through
/// - The useragent was accessed from the header list each time but was the only thing used
/// - This class was public and had user-overridable functions for before and after each process.
/// For instance, CrawlerProcess would do an initial check, then call CrawlerProcessGateways and CrawlerProcessBrowsers
/// to do custom stuff. These are not implemented in the framework and are potentially added by uses. For now, we don't
/// support that pattern.
/// </summary>
internal sealed class BrowserCapabilitiesFactory : IBrowserCapabilitiesFactory
{
// Func will return true if we want to end the processing of further funcs
private readonly Func<string, ParsedBrowserResult, bool>[] _processList;
public BrowserCapabilitiesFactory()
{
_processList = new[]
{
DefaultProcess,
DefaultProcessGateways,
CrawlerProcess,
PlatformProcess,
WinProcess,
BlackberryProcess,
OperaProcess,
GenericdownlevelProcess,
MozillaProcess,
UcbrowserProcess,
};
}
IHttpBrowserCapabilityFeature IBrowserCapabilitiesFactory.Create(HttpRequestCore request)
{
var userAgent = request.Headers.UserAgent.ToString();
if (string.IsNullOrWhiteSpace(userAgent))
{
return EmptyBrowserFeatures.Instance;
}
else if (request.HttpContext.RequestServices.GetService<IMemoryCache>() is { } cache)
{
return cache.GetOrCreate(userAgent, entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(2);
return Parse(userAgent);
}) ?? EmptyBrowserFeatures.Instance;
}
else
{
return Parse(userAgent);
}
}
public IHttpBrowserCapabilityFeature Parse(string userAgent)
{
var dictionary = new ParsedBrowserResult();
foreach (var process in _processList)
{
if (process(userAgent, dictionary))
{
break;
}
}
return dictionary;
}
private bool DefaultProcess(string userAgent, ParsedBrowserResult result)
{
result["activexcontrols"] = "false";
result["aol"] = "false";
result["backgroundsounds"] = "false";
result["beta"] = "false";
result["browser"] = "Unknown";
result["canCombineFormsInDeck"] = "true";
result["canInitiateVoiceCall"] = "false";
result["canRenderAfterInputOrSelectElement"] = "true";
result["canRenderEmptySelects"] = "true";
result["canRenderInputAndSelectElementsTogether"] = "true";
result["canRenderMixedSelects"] = "true";
result["canRenderOneventAndPrevElementsTogether"] = "true";
result["canRenderPostBackCards"] = "true";
result["canRenderSetvarZeroWithMultiSelectionList"] = "true";
result["canSendMail"] = "true";
result["cdf"] = "false";
result["cookies"] = "true";
result["crawler"] = "false";
result["defaultSubmitButtonLimit"] = "1";
result["ecmascriptversion"] = "0.0";
result["frames"] = "false";
result["gatewayMajorVersion"] = "0";
result["gatewayMinorVersion"] = "0";
result["gatewayVersion"] = "None";
result["hasBackButton"] = "true";
result["hidesRightAlignedMultiselectScrollbars"] = "false";
result["inputType"] = "telephoneKeypad";
result["isColor"] = "false";
result["isMobileDevice"] = "false";
result["javaapplets"] = "false";
result["javascript"] = "false";
result["jscriptversion"] = "0.0";
result["majorversion"] = "0";
result["maximumHrefLength"] = "10000";
result["maximumRenderedPageSize"] = "2000";
result["maximumSoftkeyLabelLength"] = "5";
result["minorversion"] = "0";
result["mobileDeviceManufacturer"] = "Unknown";
result["mobileDeviceModel"] = "Unknown";
result["msdomversion"] = "0.0";
result["numberOfSoftkeys"] = "0";
result["platform"] = "Unknown";
result["preferredImageMime"] = "image/gif";
result["preferredRenderingMime"] = "text/html";
result["preferredRenderingType"] = "html32";
result["rendersBreakBeforeWmlSelectAndInput"] = "false";
result["rendersBreaksAfterHtmlLists"] = "true";
result["rendersBreaksAfterWmlAnchor"] = "false";
result["rendersBreaksAfterWmlInput"] = "false";
result["rendersWmlDoAcceptsInline"] = "true";
result["rendersWmlSelectsAsMenuCards"] = "false";
result["requiredMetaTagNameValue"] = "";
result["requiresAbsolutePostbackUrl"] = "false";
result["requiresAdaptiveErrorReporting"] = "false";
result["requiresAttributeColonSubstitution"] = "false";
result["requiresContentTypeMetaTag"] = "false";
result["requiresControlStateInSession"] = "false";
result["requiresDBCSCharacter"] = "false";
result["requiresFullyQualifiedRedirectUrl"] = "false";
result["requiresLeadingPageBreak"] = "false";
result["requiresNoBreakInFormatting"] = "false";
result["requiresOutputOptimization"] = "false";
result["requiresPhoneNumbersAsPlainText"] = "false";
result["requiresPostRedirectionHandling"] = "false";
result["requiresSpecialViewStateEncoding"] = "false";
result["requiresUniqueFilePathSuffix"] = "false";
result["requiresUniqueHtmlCheckboxNames"] = "false";
result["requiresUniqueHtmlInputNames"] = "false";
result["requiresUrlEncodedPostfieldValues"] = "false";
result["requiresXhtmlCssSuppression"] = "false";
result["screenBitDepth"] = "1";
result["supportsAccesskeyAttribute"] = "false";
result["supportsBodyColor"] = "true";
result["supportsBold"] = "false";
result["supportsCallback"] = "false";
result["supportsCacheControlMetaTag"] = "true";
result["supportsCss"] = "false";
result["supportsDivAlign"] = "true";
result["supportsDivNoWrap"] = "false";
result["supportsEmptyStringInCookieValue"] = "true";
result["supportsFileUpload"] = "false";
result["supportsFontColor"] = "true";
result["supportsFontName"] = "false";
result["supportsFontSize"] = "false";
result["supportsImageSubmit"] = "false";
result["supportsIModeSymbols"] = "false";
result["supportsInputIStyle"] = "false";
result["supportsInputMode"] = "false";
result["supportsItalic"] = "false";
result["supportsJPhoneMultiMediaAttributes"] = "false";
result["supportsJPhoneSymbols"] = "false";
result["SupportsMaintainScrollPositionOnPostback"] = "false";
result["supportsMultilineTextBoxDisplay"] = "false";
result["supportsQueryStringInFormAction"] = "true";
result["supportsRedirectWithCookie"] = "true";
result["supportsSelectMultiple"] = "true";
result["supportsUncheck"] = "true";
result["supportsVCard"] = "false";
result["tables"] = "false";
result["tagwriter"] = "System.Web.UI.Html32TextWriter";
result["type"] = "Unknown";
result["vbscript"] = "false";
result["version"] = "0.0";
result["w3cdomversion"] = "0.0";
result["win16"] = "false";
result["win32"] = "false";
result.AddBrowser("Default");
return false;
}
private bool DefaultProcessGateways(string userAgent, ParsedBrowserResult dictionary)
{
return false;
}
private readonly RegexWorker _crawler = new("crawler|Crawler|Googlebot|bingbot");
private bool CrawlerProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_crawler.Process(userAgent) is { HasMatches: true })
{
dictionary["crawler"] = "true";
}
return false;
}
private readonly RegexWorker _win = new("Windows NT|WinNT|Windows XP");
private bool PlatformProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_win.Process(userAgent) is { HasMatches: true })
{
dictionary["platform"] = "WinNT";
}
return false;
}
private bool WinProcess(string userAgent, ParsedBrowserResult dictionary) => false;
private readonly RegexWorker _blackberry = new(@"BlackBerry(?'deviceName'\w+)/(?'version'(?'major'\d+)(\.(?'minor'\d+)?)\w*)");
private bool BlackberryProcess(string userAgent, ParsedBrowserResult result)
{
if (_blackberry.Process(userAgent) is { HasMatches: true } match)
{
result["layoutEngine"] = "BlackBerry";
result["browser"] = "BlackBerry";
result["majorversion"] = match["major"];
result["minorversion"] = match["minor"];
result["type"] = match["BlackBerry${major"];
result["mobileDeviceModel"] = match["deviceName"];
result["isMobileDevice"] = "true";
result["version"] = match["version"];
result["ecmascriptversion"] = "3.0";
result["javascript"] = "true";
result["javascriptversion"] = "1.3";
result["w3cdomversion"] = "1.0";
result["supportsAccesskeyAttribute"] = "true";
result["tagwriter"] = "System.Web.UI.HtmlTextWriter";
result["cookies"] = "true";
result["frames"] = "true";
result["javaapplets"] = "true";
result["supportsCallback"] = "true";
result["supportsDivNoWrap"] = "false";
result["supportsFileUpload"] = "true";
result["supportsMultilineTextBoxDisplay"] = "true";
result["supportsXmlHttp"] = "true";
result["tables"] = "true";
result["canInitiateVoiceCall"] = "true";
result.AddBrowser("BlackBerry");
return true;
}
return false;
}
private readonly RegexWorker _opera = new("Opera[ /](?'version'(?'major'\\d+)(\\.(?'minor'\\d+)?)(?'letters'\\w*))", "OPR/(?'version'(?'major'\\d+)(\\.(?'minor'\\d+)?)(?'letters'\\w*))");
private bool OperaProcess(string userAgent, ParsedBrowserResult capabilities)
{
if (_opera.Process(userAgent) is { HasMatches: true } match)
{
capabilities.AddBrowser("Opera");
capabilities["browser"] = "Opera";
capabilities["majorversion"] = match["major"];
capabilities["minorversion"] = match["minor"];
capabilities["type"] = match["Opera${major"];
capabilities["version"] = match["version"];
capabilities["layoutEngine"] = "Presto";
capabilities["layoutEngineVersion"] = match["layoutVersion"];
capabilities["ecmascriptversion"] = "3.0";
capabilities["javascript"] = "true";
capabilities["javascriptversion"] = "1.5";
capabilities["letters"] = match["letters"];
capabilities["w3cdomversion"] = "1.0";
capabilities["tagwriter"] = "System.Web.UI.HtmlTextWriter";
capabilities["cookies"] = "true";
capabilities["frames"] = "true";
capabilities["javaapplets"] = "true";
capabilities["supportsAccesskeyAttribute"] = "true";
capabilities["supportsCallback"] = "true";
capabilities["supportsFileUpload"] = "true";
capabilities["supportsMultilineTextBoxDisplay"] = "true";
capabilities["supportsXmlHttp"] = "true";
capabilities["tables"] = "true";
capabilities["inputType"] = "keyboard";
capabilities["isColor"] = "true";
capabilities["isMobileDevice"] = "false";
capabilities["maximumRenderedPageSize"] = "300000";
capabilities["screenBitDepth"] = "8";
capabilities["supportsBold"] = "true";
capabilities["supportsCss"] = "true";
capabilities["supportsDivNoWrap"] = "true";
capabilities["supportsFontName"] = "true";
capabilities["supportsFontSize"] = "true";
capabilities["supportsImageSubmit"] = "true";
capabilities["supportsItalic"] = "true";
return true;
}
return false;
}
private bool GenericdownlevelProcess(string userAgent, ParsedBrowserResult dictionary) => false;
private readonly RegexWorker _mozilla = new("Mozilla");
private bool MozillaProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_mozilla.Process(userAgent) is { HasMatches: true })
{
dictionary.AddBrowser("Mozilla");
dictionary["browser"] = "Mozilla";
dictionary["cookies"] = "true";
dictionary["ecmascriptversion"] = "3.0";
dictionary["frames"] = "true";
dictionary["inputType"] = "keyboard";
dictionary["isColor"] = "true";
dictionary["isMobileDevice"] = "false";
dictionary["javascript"] = "true";
dictionary["javascriptversion"] = "1.5";
dictionary["maximumRenderedPageSize"] = "300000";
dictionary["screenBitDepth"] = "8";
dictionary["supportsBold"] = "true";
dictionary["supportsCallback"] = "true";
dictionary["supportsCss"] = "true";
dictionary["supportsDivNoWrap"] = "true";
dictionary["supportsFileUpload"] = "true";
dictionary["supportsFontName"] = "true";
dictionary["supportsFontSize"] = "true";
dictionary["supportsImageSubmit"] = "true";
dictionary["supportsItalic"] = "true";
dictionary["supportsMaintainScrollPositionOnPostback"] = "true";
dictionary["supportsMultilineTextBoxDisplay"] = "true";
dictionary["supportsXmlHttp"] = "true";
dictionary["tables"] = "true";
dictionary["tagwriter"] = "System.Web.UI.HtmlTextWriter";
dictionary["type"] = "Mozilla";
dictionary["w3cdomversion"] = "1.0";
if (!IeProcess(userAgent, dictionary) &&
!FirefoxProcess(userAgent, dictionary) &&
!WebkitProcess(userAgent, dictionary) &&
!IemobileProcess(userAgent, dictionary))
{
return false;
}
return true;
}
return false;
}
private readonly RegexWorker _ie = new(@"MSIE (?'version'(?'major'\d+)(\.(?'minor'\d+)?)(?'letters'\w*))(?'extra'[^)]*)", @"Trident/(?'layoutVersion'\d+)");
private readonly RegexWorker _ieMobileBasic = new(@"IEMobile");
private bool IeProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_ie.Process(userAgent) is { HasMatches: true } regexWorker && _ieMobileBasic.Process(userAgent) is { HasMatches: false })
{
dictionary["browser"] = "IE";
dictionary["layoutEngine"] = "Trident";
dictionary["layoutEngineVersion"] = regexWorker["layoutVersion"];
dictionary["extra"] = regexWorker["extra"];
dictionary["isColor"] = "true";
dictionary["letters"] = regexWorker["letters"];
dictionary["majorversion"] = regexWorker["major"];
dictionary["minorversion"] = regexWorker["minor"];
dictionary["screenBitDepth"] = "8";
dictionary["type"] = regexWorker["IE${major"];
dictionary["version"] = regexWorker["version"];
dictionary.AddBrowser("IE");
return true;
}
return false;
}
private readonly RegexWorker _firefox = new(@"Firefox/(?'version'(?'major'\d+)(\.(?'minor'\d+)))", @"Gecko/(?'layoutVersion'\d+)");
private bool FirefoxProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_firefox.Process(userAgent) is { HasMatches: true } regexWorker)
{
dictionary["browser"] = "Firefox";
dictionary["majorversion"] = regexWorker["major"];
dictionary["minorversion"] = regexWorker["minor"];
dictionary["version"] = regexWorker["version"];
dictionary["type"] = regexWorker["Firefox${major"];
dictionary["layoutEngine"] = "Gecko";
dictionary["layoutEngineVersion"] = regexWorker["layoutVersion"];
dictionary["supportsAccesskeyAttribute"] = "true";
dictionary["javaapplets"] = "true";
dictionary["supportsDivNoWrap"] = "false";
dictionary.AddBrowser("Firefox");
return true;
}
return false;
}
private readonly RegexWorker _ieMobile = new(@"IEMobile.(?'version'(?'major'\d+)(\.(?'minor'\d+)?)\w*)", @"MSIE (?'msieMajorVersion'\d+)");
private bool IemobileProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_ieMobile.Process(userAgent) is { HasMatches: true } regexWorker)
{
dictionary["layoutEngine"] = "Trident";
dictionary["browser"] = "IEMobile";
dictionary["majorversion"] = regexWorker["major"];
dictionary["minorversion"] = regexWorker["minor"];
dictionary["type"] = regexWorker["IEMobile${msieMajorVersion"];
dictionary["isMobileDevice"] = "true";
dictionary["version"] = regexWorker["version"];
dictionary["jscriptversion"] = "5.6";
dictionary["msdomversion"] = regexWorker["majorversion}.${minorversion"];
dictionary["supportsAccesskeyAttribute"] = "true";
dictionary["javaapplets"] = "true";
dictionary["supportsDivNoWrap"] = "false";
dictionary["vbscript"] = "true";
dictionary["inputType"] = "virtualKeyboard";
dictionary["numberOfSoftkeys"] = "2";
dictionary.AddBrowser("IEMobile");
return true;
}
return false;
}
private readonly RegexWorker _webkit = new(@"AppleWebKit/(?'layoutVersion'\d+)");
private bool WebkitProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_webkit.Process(userAgent) is { HasMatches: true } regexWorker)
{
dictionary["layoutEngine"] = "WebKit";
dictionary["layoutEngineVersion"] = regexWorker["layoutVersion"];
dictionary.AddBrowser("WebKit");
WebkitmobileProcess(userAgent, dictionary);
if (ChromeProcess(userAgent, dictionary))
{
}
else if (SafariProcess(userAgent, dictionary))
{
}
return true;
}
return false;
}
private readonly RegexWorker _webkitMobile = new(@"Mobile( Safari)?/(?'iOSVersion'[^ ]+)", @"Mozilla/5.0 \((?'deviceName'[^;]+)");
private void WebkitmobileProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_webkitMobile.Process(userAgent) is { HasMatches: true } regexWorker)
{
dictionary["mobileDeviceModel"] = regexWorker["deviceName"];
dictionary["isMobileDevice"] = "true";
dictionary["ecmascriptversion"] = "3.0";
dictionary["javascript"] = "true";
dictionary["javascriptversion"] = "1.6";
dictionary["w3cdomversion"] = "1.0";
dictionary["supportsAccesskeyAttribute"] = "true";
dictionary["tagwriter"] = "System.Web.UI.HtmlTextWriter";
dictionary["cookies"] = "true";
dictionary["frames"] = "true";
dictionary["supportsCallback"] = "true";
dictionary["supportsDivNoWrap"] = "false";
dictionary["supportsFileUpload"] = "true";
dictionary["supportsMaintainScrollPositionOnPostback"] = "true";
dictionary["supportsMultilineTextBoxDisplay"] = "true";
dictionary["supportsXmlHttp"] = "true";
dictionary["tables"] = "true";
}
}
private readonly RegexWorker _chrome = new(@"Chrome/(?'version'(?'major'\d+)(\.(?'minor'\d+)?)\w*)");
private bool ChromeProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_chrome.Process(userAgent) is { HasMatches: true } regexWorker)
{
dictionary["browser"] = "Chrome";
dictionary["majorversion"] = regexWorker["major"];
dictionary["minorversion"] = regexWorker["minor"];
dictionary["type"] = regexWorker["Chrome${major"];
dictionary["version"] = regexWorker["version"];
dictionary["ecmascriptversion"] = "3.0";
dictionary["javascript"] = "true";
dictionary["javascriptversion"] = "1.7";
dictionary["w3cdomversion"] = "1.0";
dictionary["supportsAccesskeyAttribute"] = "true";
dictionary["tagwriter"] = "System.Web.UI.HtmlTextWriter";
dictionary["cookies"] = "true";
dictionary["frames"] = "true";
dictionary["javaapplets"] = "true";
dictionary["supportsCallback"] = "true";
dictionary["supportsDivNoWrap"] = "false";
dictionary["supportsFileUpload"] = "true";
dictionary["supportsMaintainScrollPositionOnPostback"] = "true";
dictionary["supportsMultilineTextBoxDisplay"] = "true";
dictionary["supportsXmlHttp"] = "true";
dictionary["tables"] = "true";
dictionary.AddBrowser("Chrome");
return true;
}
return false;
}
private readonly RegexWorker _safari = new("Safari");
private readonly RegexWorker _notSafari = new("Chrome|Android");
private bool SafariProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_safari.Process(userAgent) is { HasMatches: true } && _notSafari.Process(userAgent) is { HasMatches: false })
{
dictionary["browser"] = "Safari";
dictionary["type"] = "Safari";
dictionary.AddBrowser("Safari");
IphoneProcess(userAgent, dictionary);
IpadProcess(userAgent, dictionary);
IpodProcess(userAgent, dictionary);
Safari3plusProcess(userAgent, dictionary);
return true;
}
return false;
}
private readonly RegexWorker _iphone = new("iPhone");
private void IphoneProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_iphone.Process(userAgent) is { HasMatches: true })
{
dictionary["isMobileDevice"] = "true";
dictionary["mobileDeviceManufacturer"] = "Apple";
dictionary["mobileDeviceModel"] = "IPhone";
dictionary["canInitiateVoiceCall"] = "true";
}
}
private readonly RegexWorker _ipad = new("iPad");
private void IpadProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_ipad.Process(userAgent) is { HasMatches: true })
{
dictionary["isMobileDevice"] = "true";
dictionary["mobileDeviceManufacturer"] = "Apple";
dictionary["mobileDeviceModel"] = "IPad";
}
}
private readonly RegexWorker _ipod = new("iPod");
private void IpodProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_ipod.Process(userAgent) is { HasMatches: true })
{
dictionary["isMobileDevice"] = "true";
dictionary["mobileDeviceManufacturer"] = "Apple";
dictionary["mobileDeviceModel"] = "IPod";
}
}
private readonly RegexWorker _safari3plus = new(@"Version/(?'version'(?'major'[3-9]|\d{2,})(\.(?'minor'\d+)?)\w*)");
private bool Safari3plusProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_safari3plus.Process(userAgent) is { HasMatches: true } regexWorker)
{
dictionary["version"] = regexWorker["version"];
dictionary["majorversion"] = regexWorker["major"];
dictionary["minorversion"] = regexWorker["minor"];
dictionary["type"] = regexWorker["Safari${major"];
dictionary["ecmascriptversion"] = "3.0";
dictionary["javascript"] = "true";
dictionary["javascriptversion"] = "1.6";
dictionary["w3cdomversion"] = "1.0";
dictionary["tagwriter"] = "System.Web.UI.HtmlTextWriter";
dictionary["cookies"] = "true";
dictionary["frames"] = "true";
dictionary["javaapplets"] = "true";
dictionary["supportsAccesskeyAttribute"] = "true";
dictionary["supportsCallback"] = "true";
dictionary["supportsDivNoWrap"] = "false";
dictionary["supportsFileUpload"] = "true";
dictionary["supportsMaintainScrollPositionOnPostback"] = "true";
dictionary["supportsMultilineTextBoxDisplay"] = "true";
dictionary["supportsXmlHttp"] = "true";
dictionary["tables"] = "true";
dictionary.AddBrowser("Safari3Plus");
return true;
}
return false;
}
private readonly RegexWorker _ucBrowser = new(@"(UC Browser |UCWEB)(?'version'(?'major'\d+)(\.(?'minor'[\d\.]+)?)\w*)");
private bool UcbrowserProcess(string userAgent, ParsedBrowserResult dictionary)
{
if (_ucBrowser.Process(userAgent) is { HasMatches: true } regexWorker)
{
dictionary["browser"] = "UCBrowser";
dictionary["majorversion"] = regexWorker["major"];
dictionary["minorversion"] = regexWorker["minor"];
dictionary["isMobileDevice"] = "true";
dictionary["version"] = regexWorker["version"];
dictionary["ecmascriptversion"] = "3.0";
dictionary["javascript"] = "true";
dictionary["javascriptversion"] = "1.5";
dictionary["tagwriter"] = "System.Web.UI.HtmlTextWriter";
dictionary["cookies"] = "true";
dictionary["frames"] = "true";
dictionary["supportsCallback"] = "true";
dictionary["supportsFileUpload"] = "true";
dictionary["supportsMultilineTextBoxDisplay"] = "true";
dictionary["supportsXmlHttp"] = "true";
dictionary["tables"] = "true";
dictionary.AddBrowser("UCBrowser");
return true;
}
return false;
}
private sealed class ParsedBrowserResult : Dictionary<string, string?>, IHttpBrowserCapabilityFeature
{
public ParsedBrowserResult()
: base(StringComparer.OrdinalIgnoreCase)
{
}
[Conditional("NotNeededYet")]
[Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Retained in case we need the data later")]
public void AddBrowser(string browser)
{
}
string? IHttpBrowserCapabilityFeature.this[string key] => TryGetValue(key, out var value) ? value : null;
}
private readonly struct RegexResult
{
private readonly Match? _match;
private readonly Match? _match2;
public RegexResult(Match? match, Match? match2)
{
if (match is { Success: true })
{
_match = match;
_match2 = match2;
}
else
{
_match = null;
_match2 = null;
}
}
public bool HasMatches => _match is not null;
public string? this[string key] => Get(_match, key) ?? Get(_match2, key);
private static string? Get(Match? match, string key)
{
if (match is not null && match.Groups.TryGetValue(key, out var group))
{
return group.Value;
}
return null;
}
}
private sealed class RegexWorker
{
private readonly Regex _regex;
private readonly Regex? _regex2;
public RegexWorker(string pattern, string? pattern2 = null)
{
_regex = new Regex(pattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
if (pattern2 is not null)
{
_regex2 = new Regex(pattern2, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
}
}
public RegexResult Process(string userAgent)
=> new(_regex?.Match(userAgent), _regex2?.Match(userAgent));
}
private sealed class EmptyBrowserFeatures : IHttpBrowserCapabilityFeature
{
public static IHttpBrowserCapabilityFeature Instance { get; } = new EmptyBrowserFeatures();
public string? this[string key] => null;
}
}