2 using System.Diagnostics;
4 using System.Collections.Generic;
5 using System.Globalization;
6 using System.Security.Permissions;
13 internal static class HttpSysSettings
15 private const string httpSysParametersKey = @"System\CurrentControlSet\Services\HTTP\Parameters";
16 private const bool enableNonUtf8Default = true;
17 private const bool favorUtf8Default = true;
18 private const string enableNonUtf8Name = "EnableNonUtf8";
19 private const string favorUtf8Name = "FavorUtf8";
21 private static volatile bool enableNonUtf8;
22 private static volatile bool favorUtf8;
24 static HttpSysSettings()
26 enableNonUtf8 = enableNonUtf8Default;
27 favorUtf8 = favorUtf8Default;
28 ReadHttpSysRegistrySettings();
31 public static bool EnableNonUtf8
33 get { return enableNonUtf8; }
36 public static bool FavorUtf8
38 get { return favorUtf8; }
41 [RegistryPermission(SecurityAction.Assert, Read = @"HKEY_LOCAL_MACHINE\" + httpSysParametersKey)]
42 private static void ReadHttpSysRegistrySettings()
46 RegistryKey httpSysParameters = Registry.LocalMachine.OpenSubKey(httpSysParametersKey);
48 if (httpSysParameters == null)
50 LogWarning("ReadHttpSysRegistrySettings", SR.net_log_listener_httpsys_registry_null,
51 httpSysParametersKey);
55 using (httpSysParameters)
57 enableNonUtf8 = ReadRegistryValue(httpSysParameters, enableNonUtf8Name, enableNonUtf8Default);
58 favorUtf8 = ReadRegistryValue(httpSysParameters, favorUtf8Name, favorUtf8Default);
62 catch (SecurityException e)
64 LogRegistryException("ReadHttpSysRegistrySettings", e);
66 catch (ObjectDisposedException e)
68 LogRegistryException("ReadHttpSysRegistrySettings", e);
72 private static bool ReadRegistryValue(RegistryKey key, string valueName, bool defaultValue)
74 Debug.Assert(key != null, "'key' must not be null");
78 // This check will throw an IOException if keyName doesn't exist. That's OK, we return the
80 if (key.GetValueKind(valueName) == RegistryValueKind.DWord)
82 // At this point we know the Registry value exists and it must be valid (any DWORD value
83 // can be converted to a bool).
84 return Convert.ToBoolean(key.GetValue(valueName), CultureInfo.InvariantCulture);
87 catch (UnauthorizedAccessException e)
89 LogRegistryException("ReadRegistryValue", e);
93 LogRegistryException("ReadRegistryValue", e);
95 catch (SecurityException e)
97 LogRegistryException("ReadRegistryValue", e);
99 catch (ObjectDisposedException e)
101 LogRegistryException("ReadRegistryValue", e);
107 private static void LogRegistryException(string methodName, Exception e)
109 LogWarning(methodName, SR.net_log_listener_httpsys_registry_error, httpSysParametersKey, e);
112 private static void LogWarning(string methodName, string message, params object[] args)
116 Logging.PrintWarning(Logging.HttpListener, typeof(HttpSysSettings), methodName,
117 SR.GetString(message, args));