Merge pull request #3800 from madewokherd/mingwbuild
[mono.git] / mcs / class / System / ReferenceSources / AutoWebProxyScriptEngine.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
4 #if !MONOTOUCH_WATCH
5 using Mono.Net;
6 #endif
7
8 namespace System.Net
9 {
10         class AutoWebProxyScriptEngine
11         {
12                 public AutoWebProxyScriptEngine (WebProxy proxy, bool useRegistry)
13                 {
14                 }
15
16                 public Uri AutomaticConfigurationScript { get; set; }
17                 public bool AutomaticallyDetectSettings { get; set; }
18
19                 public bool GetProxies (Uri destination, out IList<string> proxyList)
20                 {
21                         int i = 0;
22                         return GetProxies (destination, out proxyList, ref i);
23                 }
24
25                 public bool GetProxies(Uri destination, out IList<string> proxyList, ref int syncStatus)
26                 {
27                         proxyList = null;
28                         return false;
29                 }
30
31                 public void Close ()
32                 {
33                 }
34
35                 public void Abort (ref int syncStatus)
36                 {
37                 }
38
39                 public void CheckForChanges ()
40                 {
41                 }
42
43 #if !MOBILE
44                 public WebProxyData GetWebProxyData ()
45                 {
46                         WebProxyData data;
47
48                         // TODO: Could re-use some pieces from _AutoWebProxyScriptEngine.cs
49                         if (IsWindows ()) {
50                                 data = InitializeRegistryGlobalProxy ();
51                                 if (data != null)
52                                         return data;
53                         }
54
55                         data = ReadEnvVariables ();
56                         return data ?? new WebProxyData ();
57                 }
58
59                 WebProxyData ReadEnvVariables ()
60                 {
61                         string address = Environment.GetEnvironmentVariable ("http_proxy") ?? Environment.GetEnvironmentVariable ("HTTP_PROXY");
62
63                         if (address != null) {
64                                 try {
65                                         if (!address.StartsWith ("http://"))
66                                                 address = "http://" + address;
67
68                                         Uri uri = new Uri (address);
69                                         IPAddress ip;
70                                         
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;
80                                                 }
81                                         }
82
83                                         bool bBypassOnLocal = false;
84                                         ArrayList al = new ArrayList ();
85                                         string bypass = Environment.GetEnvironmentVariable ("no_proxy") ?? Environment.GetEnvironmentVariable ("NO_PROXY");
86                                         
87                                         if (bypass != null) {
88                                                 string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
89                                         
90                                                 foreach (string str in bypassList) {
91                                                         if (str != "*.local")
92                                                                 al.Add (str);
93                                                         else
94                                                                 bBypassOnLocal = true;
95                                                 }
96                                         }
97
98                                         return new WebProxyData {
99                                                 proxyAddress = uri,
100                                                 bypassOnLocal = bBypassOnLocal,
101                                                 bypassList = CreateBypassList (al)
102                                         };
103                                 } catch (UriFormatException) {
104                                 }
105                         }
106
107                         return null;
108                 }
109
110                 static bool IsWindows ()
111                 {
112                         return (int) Environment.OSVersion.Platform < 4;
113                 }
114                                 
115                 WebProxyData InitializeRegistryGlobalProxy ()
116                 {
117                         int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
118
119                         if (iProxyEnable > 0) {
120                                 string strHttpProxy = "";
121                                 bool bBypassOnLocal = false;
122                                 ArrayList al = new ArrayList ();
123                                 
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);
126                                 
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;
132                                                 }
133                                 } else strHttpProxy = strProxyServer;
134                                 
135                                 if (strProxyOverrride != null) {
136                                         string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
137                                 
138                                         foreach (string str in bypassList) {
139                                                 if (str != "<local>")
140                                                         al.Add (str);
141                                                 else
142                                                         bBypassOnLocal = true;
143                                         }
144                                 }
145
146                                 return new WebProxyData {
147                                         proxyAddress = ToUri (strHttpProxy),
148                                         bypassOnLocal = bBypassOnLocal,
149                                         bypassList = CreateBypassList (al)
150                                 };
151                         }
152
153                         return null;
154                 }
155
156                 static Uri ToUri (string address)
157                 {
158                         if (address == null)
159                                 return null;
160                                 
161                         if (address.IndexOf ("://", StringComparison.Ordinal) == -1) 
162                                 address = "http://" + address;
163
164                         return new Uri (address);
165                 }
166
167                 // Takes an ArrayList of fileglob-formatted strings and returns an array of Regex-formatted strings
168                 static ArrayList CreateBypassList (ArrayList al)
169                 {
170                         string[] result = al.ToArray (typeof (string)) as string[];
171                         for (int c = 0; c < result.Length; c++)
172                         {
173                                 result [c] = "^" +
174                                         Regex.Escape (result [c]).Replace (@"\*", ".*").Replace (@"\?", ".") +
175                                         "$";
176                         }
177                         return new ArrayList (result);
178                 }
179 #endif
180         }
181 }