[System] Remove any CFNetwork usage from the watchOS profile. Fixes #45847.
[mono-project.git] / mcs / class / System / ReferenceSources / AutoWebProxyScriptEngine.cs
blobf3b840e8f7f56533be087a32001bfdabaacd9a82
1 using System.Collections;
2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
4 #if !MONOTOUCH_WATCH
5 using Mono.Net;
6 #endif
8 namespace System.Net
10 class AutoWebProxyScriptEngine
12 public AutoWebProxyScriptEngine (WebProxy proxy, bool useRegistry)
16 public Uri AutomaticConfigurationScript { get; set; }
17 public bool AutomaticallyDetectSettings { get; set; }
19 public bool GetProxies (Uri destination, out IList<string> proxyList)
21 int i = 0;
22 return GetProxies (destination, out proxyList, ref i);
25 public bool GetProxies(Uri destination, out IList<string> proxyList, ref int syncStatus)
27 proxyList = null;
28 return false;
31 public void Close ()
35 public void Abort (ref int syncStatus)
39 public void CheckForChanges ()
43 #if !MOBILE
44 public WebProxyData GetWebProxyData ()
46 WebProxyData data;
48 // TODO: Could re-use some pieces from _AutoWebProxyScriptEngine.cs
49 if (IsWindows ()) {
50 data = InitializeRegistryGlobalProxy ();
51 if (data != null)
52 return data;
55 data = ReadEnvVariables ();
56 return data ?? new WebProxyData ();
59 WebProxyData ReadEnvVariables ()
61 string address = Environment.GetEnvironmentVariable ("http_proxy") ?? Environment.GetEnvironmentVariable ("HTTP_PROXY");
63 if (address != null) {
64 try {
65 if (!address.StartsWith ("http://"))
66 address = "http://" + address;
68 Uri uri = new Uri (address);
69 IPAddress ip;
71 if (IPAddress.TryParse (uri.Host, out ip)) {
72 if (IPAddress.Any.Equals (ip)) {
73 UriBuilder builder = new UriBuilder (uri);
74 builder.Host = "127.0.0.1";
75 uri = builder.Uri;
76 } else if (IPAddress.IPv6Any.Equals (ip)) {
77 UriBuilder builder = new UriBuilder (uri);
78 builder.Host = "[::1]";
79 uri = builder.Uri;
83 bool bBypassOnLocal = false;
84 ArrayList al = new ArrayList ();
85 string bypass = Environment.GetEnvironmentVariable ("no_proxy") ?? Environment.GetEnvironmentVariable ("NO_PROXY");
87 if (bypass != null) {
88 string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
90 foreach (string str in bypassList) {
91 if (str != "*.local")
92 al.Add (str);
93 else
94 bBypassOnLocal = true;
98 return new WebProxyData {
99 proxyAddress = uri,
100 bypassOnLocal = bBypassOnLocal,
101 bypassList = CreateBypassList (al)
103 } catch (UriFormatException) {
107 return null;
110 static bool IsWindows ()
112 return (int) Environment.OSVersion.Platform < 4;
115 WebProxyData InitializeRegistryGlobalProxy ()
117 int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
119 if (iProxyEnable > 0) {
120 string strHttpProxy = "";
121 bool bBypassOnLocal = false;
122 ArrayList al = new ArrayList ();
124 string strProxyServer = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
125 string strProxyOverrride = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);
127 if (strProxyServer.Contains ("=")) {
128 foreach (string strEntry in strProxyServer.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
129 if (strEntry.StartsWith ("http=")) {
130 strHttpProxy = strEntry.Substring (5);
131 break;
133 } else strHttpProxy = strProxyServer;
135 if (strProxyOverrride != null) {
136 string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
138 foreach (string str in bypassList) {
139 if (str != "<local>")
140 al.Add (str);
141 else
142 bBypassOnLocal = true;
146 return new WebProxyData {
147 proxyAddress = ToUri (strHttpProxy),
148 bypassOnLocal = bBypassOnLocal,
149 bypassList = CreateBypassList (al)
153 return null;
156 static Uri ToUri (string address)
158 if (address == null)
159 return null;
161 if (address.IndexOf ("://", StringComparison.Ordinal) == -1)
162 address = "http://" + address;
164 return new Uri (address);
167 // Takes an ArrayList of fileglob-formatted strings and returns an array of Regex-formatted strings
168 static ArrayList CreateBypassList (ArrayList al)
170 string[] result = al.ToArray (typeof (string)) as string[];
171 for (int c = 0; c < result.Length; c++)
173 result [c] = "^" +
174 Regex.Escape (result [c]).Replace (@"\*", ".*").Replace (@"\?", ".") +
175 "$";
177 return new ArrayList (result);
179 #endif