2 // System.Web.HttpBrowserCapabilities
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2003 Novell, Inc. (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Globalization;
34 using System.Web.Configuration;
36 using System.Security.Permissions;
39 namespace System.Web.Configuration {
40 public partial class HttpCapabilitiesBase
43 namespace System.Web {
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 public class HttpBrowserCapabilities : HttpCapabilitiesBase
49 const int HaveActiveXControls = 1; // 1;
50 const int HaveAdapters = 2;
51 const int HaveAOL = 3; // 2;
52 const int HaveBackGroundSounds = 4; // 3;
53 const int HaveBeta = 5; // 4;
54 const int HaveBrowser = 6; // 5;
55 const int HaveBrowsers = 7;
56 const int HaveCanCombineFormsInDeck = 8;
57 const int HaveCanInitiateVoiceCall = 9;
58 const int HaveCanRenderAfterInputOrSelectElement = 10;
59 const int HaveCanRenderEmptySelects = 11;
60 const int HaveCanRenderInputAndSelectElementsTogether = 12;
61 const int HaveCanRenderMixedSelects = 13;
62 const int HaveCanRenderOneventAndPrevElementsTogether = 14;
63 const int HaveCanRenderPostBackCards = 15;
64 const int HaveCanRenderSetvarZeroWithMultiSelectionList = 16;
65 const int HaveCanSendMail = 17;
66 const int HaveCDF = 18; // 6;
67 //const int HaveClrVersion = 19; // 7;
68 const int HaveCookies = 20; // 8;
69 const int HaveCrawler = 21; // 9;
70 const int HaveDefaultSubmitButtonLimit = 22;
71 const int HaveEcmaScriptVersion = 23;
72 const int HaveFrames = 24; // 11;
73 const int HaveGatewayMajorVersion = 25;
74 const int HaveGatewayMinorVersion = 26;
75 const int HaveGatewayVersion = 27;
76 const int HaveHasBackButton = 28;
77 const int HaveHidesRightAlignedMultiselectScrollbars = 29;
78 const int HaveHtmlTextWriter = 30;
79 const int HaveId = 31;
80 const int HaveInputType = 32;
81 const int HaveIsColor = 33;
82 const int HaveIsMobileDevice = 34;
83 const int HaveJavaApplets = 35; // 12;
84 const int HaveJavaScript = 36; // 13;
85 const int HaveJScriptVersion = 37;
86 const int HaveMajorVersion = 38; // 14;
87 const int HaveMaximumHrefLength = 39;
88 const int HaveMaximumRenderedPageSize = 40;
89 const int HaveMaximumSoftkeyLabelLength = 41;
90 const int HaveMinorVersion = 42; // 15;
91 const int HaveMinorVersionString = 43;
92 const int HaveMobileDeviceManufacturer = 44;
93 const int HaveMobileDeviceModel = 45;
94 const int HaveMSDomVersion = 46; // 16;
95 const int HaveNumberOfSoftkeys = 47;
96 const int HavePlatform = 48; // 17;
97 const int HavePreferredImageMime = 49;
98 const int HavePreferredRenderingMime = 50;
99 const int HavePreferredRenderingType = 51;
100 const int HavePreferredRequestEncoding = 52;
101 const int HavePreferredResponseEncoding = 53;
102 const int HaveRendersBreakBeforeWmlSelectAndInput = 54;
103 const int HaveRendersBreaksAfterHtmlLists = 55;
104 const int HaveRendersBreaksAfterWmlAnchor = 56;
105 const int HaveRendersBreaksAfterWmlInput = 57;
106 const int HaveRendersWmlDoAcceptsInline = 58;
107 const int HaveRendersWmlSelectsAsMenuCards = 59;
108 const int HaveRequiredMetaTagNameValue = 60;
109 const int HaveRequiresAttributeColonSubstitution = 61;
110 const int HaveRequiresContentTypeMetaTag = 62;
111 const int HaveRequiresControlStateInSession = 63;
112 const int HaveRequiresDBCSCharacter = 64;
113 const int HaveRequiresHtmlAdaptiveErrorReporting = 65;
114 const int HaveRequiresLeadingPageBreak = 66;
115 const int HaveRequiresNoBreakInFormatting = 67;
116 const int HaveRequiresOutputOptimization = 68;
117 const int HaveRequiresPhoneNumbersAsPlainText = 69;
118 const int HaveRequiresSpecialViewStateEncoding = 70;
119 const int HaveRequiresUniqueFilePathSuffix = 71;
120 const int HaveRequiresUniqueHtmlCheckboxNames = 72;
121 const int HaveRequiresUniqueHtmlInputNames = 73;
122 const int HaveRequiresUrlEncodedPostfieldValues = 74;
123 const int HaveScreenBitDepth = 75;
124 const int HaveScreenCharactersHeight = 76;
125 const int HaveScreenCharactersWidth = 77;
126 const int HaveScreenPixelsHeight = 78;
127 const int HaveScreenPixelsWidth = 79;
128 const int HaveSupportsAccesskeyAttribute = 80;
129 const int HaveSupportsBodyColor = 81;
130 const int HaveSupportsBold = 82;
131 const int HaveSupportsCacheControlMetaTag = 83;
132 const int HaveSupportsCallback = 84;
133 const int HaveSupportsCss = 85;
134 const int HaveSupportsDivAlign = 86;
135 const int HaveSupportsDivNoWrap = 87;
136 const int HaveSupportsEmptyStringInCookieValue = 88;
137 const int HaveSupportsFontColor = 89;
138 const int HaveSupportsFontName = 90;
139 const int HaveSupportsFontSize = 91;
140 const int HaveSupportsImageSubmit = 92;
141 const int HaveSupportsIModeSymbols = 93;
142 const int HaveSupportsInputIStyle = 94;
143 const int HaveSupportsInputMode = 95;
144 const int HaveSupportsItalic = 96;
145 const int HaveSupportsJPhoneMultiMediaAttributes = 97;
146 const int HaveSupportsJPhoneSymbols = 98;
147 const int HaveSupportsQueryStringInFormAction = 99;
148 const int HaveSupportsRedirectWithCookie = 100;
149 const int HaveSupportsSelectMultiple = 101;
150 const int HaveSupportsUncheck = 102;
151 const int HaveSupportsXmlHttp = 103;
152 const int HaveTables = 104; // 18;
153 const int HaveTagWriter = 105; // 19;
154 const int HaveType = 106;
155 const int HaveUseOptimizedCacheKey = 107;
156 const int HaveVBScript = 108; // 20;
157 const int HaveVersion = 109; // 21;
158 const int HaveW3CDomVersion = 110; // 22;
159 const int HaveWin16 = 111; // 23;
160 const int HaveWin32 = 112; // 24;
161 const int LastHaveFlag = 113;
163 BitArray flags = new BitArray (LastHaveFlag);
164 bool activeXControls;
166 bool backgroundSounds;
173 Version ecmaScriptVersion;
179 Version msDomVersion;
185 Version w3CDomVersion;
188 Version [] clrVersions;
189 internal string useragent;
192 public HttpBrowserCapabilities ()
197 public bool ActiveXControls {
199 if (!Get (HaveActiveXControls)) {
200 activeXControls = ReadBoolean ("activexcontrols");
201 Set (HaveActiveXControls);
204 return activeXControls;
210 if (!Get (HaveAOL)) {
211 aol = ReadBoolean ("aol");
219 public bool BackgroundSounds {
221 if (!Get (HaveBackGroundSounds)) {
222 backgroundSounds = ReadBoolean ("backgroundsounds");
223 Set (HaveBackGroundSounds);
226 return backgroundSounds;
232 if (!Get (HaveBeta)) {
233 beta = ReadBoolean ("beta");
241 public string Browser {
243 if (!Get (HaveBrowser)) {
244 browser = this ["browser"];
245 if (browser == null || browser.Length == 0)
254 ArrayList browsers = null;
255 public ArrayList Browsers {
257 if (!Get (HaveBrowsers)) {
258 browsers = ReadArrayList ("browsers");
266 public bool IsBrowser (string browserName)
268 foreach (string browser in Browsers) {
269 if (0 == String.Compare (browser, "Unknown", StringComparison.OrdinalIgnoreCase)) {
272 if (0 == String.Compare (browserName, browser, StringComparison.OrdinalIgnoreCase)) {
281 if (!Get (HaveCDF)) {
282 cdf = ReadBoolean ("cdf");
290 public Version ClrVersion {
292 if (clrVersion == null)
293 InternalGetClrVersions ();
299 public bool Cookies {
301 if (!Get (HaveCookies)) {
302 cookies = ReadBoolean ("cookies");
310 public bool Crawler {
312 if (!Get (HaveCrawler)) {
313 crawler = ReadBoolean ("crawler");
321 public Version EcmaScriptVersion {
323 if (!Get (HaveEcmaScriptVersion)) {
324 ecmaScriptVersion = ReadVersion ("ecmascriptversion");
325 Set (HaveEcmaScriptVersion);
328 return ecmaScriptVersion;
334 if (!Get (HaveFrames)) {
335 frames = ReadBoolean ("frames");
343 public bool JavaApplets {
345 if (!Get (HaveJavaApplets)) {
346 javaApplets = ReadBoolean ("javaapplets");
347 Set (HaveJavaApplets);
357 public bool JavaScript {
359 if (!Get (HaveJavaScript)) {
360 javaScript = ReadBoolean ("javascript");
361 Set (HaveJavaScript);
368 public int MajorVersion {
370 if (!Get (HaveMajorVersion)) {
371 majorVersion = ReadInt32 ("majorver");
372 Set (HaveMajorVersion);
379 public double MinorVersion {
381 if (!Get (HaveMinorVersion)) {
382 minorVersion = ReadDouble ("minorver");
383 Set (HaveMinorVersion);
390 public Version MSDomVersion {
392 if (!Get (HaveMSDomVersion)) {
393 msDomVersion = ReadVersion ("msdomversion");
394 Set (HaveMSDomVersion);
401 public string Platform {
403 if (!Get (HavePlatform)) {
404 platform = ReadString ("platform");
414 if (!Get (HaveTables)) {
415 tables = ReadBoolean ("tables");
423 public Type TagWriter {
425 if (!Get (HaveTagWriter)) {
426 tagWriter = GetTagWriter ();
433 internal virtual Type GetTagWriter ()
435 return typeof (HtmlTextWriter);
440 return Browser + MajorVersion;
444 public bool VBScript {
446 if (!Get (HaveVBScript)) {
447 vbscript = ReadBoolean ("vbscript");
455 public string Version {
457 if (!Get (HaveVersion)) {
458 version = ReadString ("version");
466 public Version W3CDomVersion {
468 if (!Get (HaveW3CDomVersion)) {
469 w3CDomVersion = ReadVersion ("w3cdomversion");
470 Set (HaveW3CDomVersion);
473 return w3CDomVersion;
479 if (!Get (HaveWin16)) {
480 win16 = ReadBoolean ("win16");
490 // This is the list of different windows platforms that browscap.ini has.
491 // Win16 Win2000 Win2003 Win32 Win95 Win98 WinME WinNT WinVI WinXP
492 if (!Get (HaveWin32)) {
493 string platform = Platform;
494 win32 = (platform != "Win16" && platform.StartsWith ("Win"));
502 public Version [] GetClrVersions ()
504 if (clrVersions == null)
505 InternalGetClrVersions ();
511 void InternalGetClrVersions ()
513 char [] anychars = new char [] { ';', ')' };
514 string s = useragent;
515 ArrayList list = null;
517 while ((s != null) && (idx = s.IndexOf (".NET CLR ")) != -1) {
518 int end = s.IndexOfAny (anychars, idx + 9);
522 string ver = s.Substring (idx + 9, end - idx - 9);
525 v = new Version (ver);
526 if (clrVersion == null || v > clrVersion)
530 list = new ArrayList (4);
534 s = s.Substring (idx + 9);
537 if (list == null || list.Count == 0) {
538 clrVersion = new Version ();
542 clrVersions = new Version [1] { clrVersion };
546 clrVersions = (Version []) list.ToArray (typeof (Version));
550 bool ReadBoolean (string key)
552 string v = this [key];
554 throw CreateCapabilityNotFoundException (key);
557 return (String.Compare (v, "True", true, CultureInfo.InvariantCulture) == 0);
560 int ReadInt32 (string key)
562 string v = this [key];
564 throw CreateCapabilityNotFoundException (key);
568 return Int32.Parse (v);
570 throw CreateCapabilityNotFoundException (key);
574 double ReadDouble (string key)
576 string v = this [key];
578 throw CreateCapabilityNotFoundException (key);
582 return Double.Parse (v);
584 throw CreateCapabilityNotFoundException (key);
588 private string ReadString (string key)
590 string v = this [key];
592 throw CreateCapabilityNotFoundException (key);
598 Version ReadVersion (string key)
600 string v = this [key];
602 throw CreateCapabilityNotFoundException (key);
606 return new Version (v);
609 throw CreateCapabilityNotFoundException (key);
614 private ArrayList ReadArrayList (string key)
616 ArrayList v = (ArrayList)this.capabilities [key];
618 throw CreateCapabilityNotFoundException (key);
624 Exception CreateCapabilityNotFoundException (string key) {
625 return new ArgumentNullException (String.Format ("browscaps.ini does not contain a definition for capability {0} for userAgent {1}", key, Browser));
630 return flags.Get (idx);
635 flags.Set (idx, true);