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