Update Reference Sources to .NET Framework 4.6
[mono.git] / mcs / class / referencesource / System / net / System / Net / HttpSysSettings.cs
1 using System;
2 using System.Diagnostics;
3 using System.Text;
4 using System.Collections.Generic;
5 using System.Globalization;
6 using System.Security.Permissions;
7 using Microsoft.Win32;
8 using System.IO;
9 using System.Security;
10
11 namespace System.Net
12 {
13     internal static class HttpSysSettings
14     {
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";
20
21         private static volatile bool enableNonUtf8;
22         private static volatile bool favorUtf8;
23
24         static HttpSysSettings()
25         {
26             enableNonUtf8 = enableNonUtf8Default;
27             favorUtf8 = favorUtf8Default;
28             ReadHttpSysRegistrySettings();
29         }
30
31         public static bool EnableNonUtf8
32         {
33             get { return enableNonUtf8; }
34         }
35
36         public static bool FavorUtf8
37         {
38             get { return favorUtf8; }
39         }
40
41         [RegistryPermission(SecurityAction.Assert, Read = @"HKEY_LOCAL_MACHINE\" + httpSysParametersKey)]
42         private static void ReadHttpSysRegistrySettings()
43         {
44             try
45             {
46                 RegistryKey httpSysParameters = Registry.LocalMachine.OpenSubKey(httpSysParametersKey);
47
48                 if (httpSysParameters == null)
49                 {
50                     LogWarning("ReadHttpSysRegistrySettings", SR.net_log_listener_httpsys_registry_null,
51                         httpSysParametersKey);
52                 }
53                 else
54                 {
55                     using (httpSysParameters)
56                     {
57                         enableNonUtf8 = ReadRegistryValue(httpSysParameters, enableNonUtf8Name, enableNonUtf8Default);
58                         favorUtf8 = ReadRegistryValue(httpSysParameters, favorUtf8Name, favorUtf8Default);
59                     }
60                 }
61             }
62             catch (SecurityException e)
63             {
64                 LogRegistryException("ReadHttpSysRegistrySettings", e);
65             }
66             catch (ObjectDisposedException e)
67             {
68                 LogRegistryException("ReadHttpSysRegistrySettings", e);
69             }
70         }
71
72         private static bool ReadRegistryValue(RegistryKey key, string valueName, bool defaultValue)
73         {
74             Debug.Assert(key != null, "'key' must not be null");
75
76             try
77             {
78                 // This check will throw an IOException if keyName doesn't exist. That's OK, we return the
79                 // default value.
80                 if (key.GetValueKind(valueName) == RegistryValueKind.DWord)
81                 {
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);
85                 }
86             }
87             catch (UnauthorizedAccessException e)
88             {
89                 LogRegistryException("ReadRegistryValue", e);
90             }
91             catch (IOException e)
92             {
93                 LogRegistryException("ReadRegistryValue", e);
94             }
95             catch (SecurityException e)
96             {
97                 LogRegistryException("ReadRegistryValue", e);
98             }
99             catch (ObjectDisposedException e)
100             {
101                 LogRegistryException("ReadRegistryValue", e);
102             }
103
104             return defaultValue;
105         }
106
107         private static void LogRegistryException(string methodName, Exception e)
108         {
109             LogWarning(methodName, SR.net_log_listener_httpsys_registry_error, httpSysParametersKey, e);
110         }
111
112         private static void LogWarning(string methodName, string message, params object[] args)
113         {
114             if (Logging.On)
115             {
116                 Logging.PrintWarning(Logging.HttpListener, typeof(HttpSysSettings), methodName,
117                     SR.GetString(message, args));
118             }
119         }
120     }
121 }