[System] WebRequest from referencesource
[mono.git] / mcs / class / System / System.Net / WebProxy.cs
index 91eef062a30b4842c2929183320c81a2440d0714..a0f422b4c6a4a28002fa1c27a71a0a17e26661d8 100644 (file)
@@ -32,6 +32,7 @@ using System.Collections;
 using System.Globalization;
 using System.Runtime.Serialization;
 using System.Text.RegularExpressions;
+using Mono.Net;
 
 namespace System.Net 
 {
@@ -42,9 +43,7 @@ namespace System.Net
                bool bypassOnLocal;
                ArrayList bypassList;
                ICredentials credentials;
-#if NET_2_0
                bool useDefaultCredentials;
-#endif
 
                // Constructors
 
@@ -92,9 +91,7 @@ namespace System.Net
                        this.address = (Uri) serializationInfo.GetValue ("_ProxyAddress", typeof (Uri));
                        this.bypassOnLocal = serializationInfo.GetBoolean ("_BypassOnLocal");
                        this.bypassList = (ArrayList) serializationInfo.GetValue ("_BypassList", typeof (ArrayList));
-#if NET_2_0
                        this.useDefaultCredentials =  serializationInfo.GetBoolean ("_UseDefaultCredentials");
-#endif
                        this.credentials = null;
                        CheckBypassList ();
                }
@@ -133,15 +130,14 @@ namespace System.Net
                        set { credentials = value; }
                }
 
-#if NET_2_0
                [MonoTODO ("Does not affect Credentials, since CredentialCache.DefaultCredentials is not implemented.")]
                public bool UseDefaultCredentials {
                        get { return useDefaultCredentials; }
                        set { useDefaultCredentials = value; }
                }
-#endif
 
                // Methods
+               [Obsolete ("This method has been deprecated", false)]
                [MonoTODO("Can we get this info under windows from the system?")]
                public static WebProxy GetDefaultProxy ()
                {
@@ -163,10 +159,8 @@ namespace System.Net
 
                public bool IsBypassed (Uri host)
                {
-#if NET_2_0
                        if (host == null)
                                throw new ArgumentNullException ("host");
-#endif
 
                        if (host.IsLoopback && bypassOnLocal)
                                return true;
@@ -185,14 +179,12 @@ namespace System.Net
                                if (String.Compare (server, "loopback", true, CultureInfo.InvariantCulture) == 0)
                                        return true;
 
-                               try {
-                                       IPAddress addr = IPAddress.Parse (server);
-                                       if (IPAddress.IsLoopback (addr))
-                                               return true;
-                               } catch {}
+                               IPAddress addr = null;
+                               if (IPAddress.TryParse (server, out addr) && IPAddress.IsLoopback (addr))
+                                       return true;
                        }
 
-                       if (bypassList == null)
+                       if (bypassList == null || bypassList.Count == 0)
                                return false;
 
                        try {
@@ -222,15 +214,18 @@ namespace System.Net
                        }
                }
 
-               void ISerializable.GetObjectData (SerializationInfo serializationInfo,
-                                                 StreamingContext streamingContext)
+               protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
                {
                        serializationInfo.AddValue ("_BypassOnLocal", bypassOnLocal);
                        serializationInfo.AddValue ("_ProxyAddress", address);
                        serializationInfo.AddValue ("_BypassList", bypassList);
-#if NET_2_0
                        serializationInfo.AddValue ("_UseDefaultCredentials", UseDefaultCredentials);
-#endif
+               }
+
+               void ISerializable.GetObjectData (SerializationInfo serializationInfo,
+                                                 StreamingContext streamingContext)
+               {
+                       GetObjectData (serializationInfo, streamingContext);
                }
 
                // Private Methods
@@ -249,10 +244,142 @@ namespace System.Net
                        if (address == null)
                                return null;
                                
-                       if (address.IndexOf ("://") == -1) 
+                       if (address.IndexOf ("://", StringComparison.Ordinal) == -1) 
                                address = "http://" + address;
 
                        return new Uri (address);
                }
+
+               internal static IWebProxy CreateDefaultProxy ()
+               {
+#if MONOTOUCH
+                       return CFNetwork.GetDefaultProxy ();
+#elif MONODROID
+                       // Return the system web proxy.  This only works for ICS+.
+                       var androidProxy = AndroidPlatform.GetDefaultProxy ();
+                       if (androidProxy != null)
+                               return androidProxy;
+
+                       return new WebProxy (false);
+#else
+                       if (Platform.IsMacOS)
+                               return CFNetwork.GetDefaultProxy ();
+
+                       return new WebProxy (false);
+#endif
+               }
+
+               // Global settings initialization
+               private WebProxy (bool enableAutoproxy)
+               {
+#if !MOBILE                    
+                       if (IsWindows () && InitializeRegistryGlobalProxy ())
+                               return;
+#endif
+                       string address = Environment.GetEnvironmentVariable ("http_proxy") ?? Environment.GetEnvironmentVariable ("HTTP_PROXY");
+
+                       if (address != null) {
+                               try {
+                                       if (!address.StartsWith ("http://"))
+                                               address = "http://" + address;
+
+                                       Uri uri = new Uri (address);
+                                       IPAddress ip;
+                                       
+                                       if (IPAddress.TryParse (uri.Host, out ip)) {
+                                               if (IPAddress.Any.Equals (ip)) {
+                                                       UriBuilder builder = new UriBuilder (uri);
+                                                       builder.Host = "127.0.0.1";
+                                                       uri = builder.Uri;
+                                               } else if (IPAddress.IPv6Any.Equals (ip)) {
+                                                       UriBuilder builder = new UriBuilder (uri);
+                                                       builder.Host = "[::1]";
+                                                       uri = builder.Uri;
+                                               }
+                                       }
+
+                                       bool bBypassOnLocal = false;
+                                       ArrayList al = new ArrayList ();
+                                       string bypass = Environment.GetEnvironmentVariable ("no_proxy") ?? Environment.GetEnvironmentVariable ("NO_PROXY");
+                                       
+                                       if (bypass != null) {
+                                               string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
+                                       
+                                               foreach (string str in bypassList) {
+                                                       if (str != "*.local")
+                                                               al.Add (str);
+                                                       else
+                                                               bBypassOnLocal = true;
+                                               }
+                                       }
+
+                                       this.address = uri;
+                                       this.bypassOnLocal = bBypassOnLocal;
+                                       this.bypassList = CreateBypassList (al);
+                                       return;
+                               } catch (UriFormatException) {
+                               }
+                       }
+               }
+
+#if !MOBILE
+               static bool IsWindows ()
+               {
+                       return (int) Environment.OSVersion.Platform < 4;
+               }
+                               
+               bool InitializeRegistryGlobalProxy ()
+               {
+                       int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
+
+                       if (iProxyEnable > 0) {
+                               string strHttpProxy = "";
+                               bool bBypassOnLocal = false;
+                               ArrayList al = new ArrayList ();
+                               
+                               string strProxyServer = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
+                               string strProxyOverrride = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);
+                               
+                               if (strProxyServer.Contains ("=")) {
+                                       foreach (string strEntry in strProxyServer.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
+                                               if (strEntry.StartsWith ("http=")) {
+                                                       strHttpProxy = strEntry.Substring (5);
+                                                       break;
+                                               }
+                               } else strHttpProxy = strProxyServer;
+                               
+                               if (strProxyOverrride != null) {
+                                       string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
+                               
+                                       foreach (string str in bypassList) {
+                                               if (str != "<local>")
+                                                       al.Add (str);
+                                               else
+                                                       bBypassOnLocal = true;
+                                       }
+                               }
+
+                               this.address = ToUri (strHttpProxy);
+                               this.bypassOnLocal = bBypassOnLocal;
+                               this.bypassList = CreateBypassList (al);
+                               return true;
+                       }
+
+                       return false;
+               }
+#endif
+
+               // Takes an ArrayList of fileglob-formatted strings and returns an array of Regex-formatted strings
+               static ArrayList CreateBypassList (ArrayList al)
+               {
+                       string[] result = al.ToArray (typeof (string)) as string[];
+                       for (int c = 0; c < result.Length; c++)
+                       {
+                               result [c] = "^" +
+                                       Regex.Escape (result [c]).Replace (@"\*", ".*").Replace (@"\?", ".") +
+                                       "$";
+                       }
+                       return new ArrayList (result);
+               }
        }
 }