Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / System / System.Net / WebRequest.cs
index 71d3b675e23aa168b2e463366f9df5e742e918ee..eeed685db3af763ec2a84aa1f0b8ec177c79d5b2 100644 (file)
@@ -53,42 +53,22 @@ namespace System.Net.Configuration {
 
 namespace System.Net 
 {
-#if MOONLIGHT
-       internal abstract class WebRequest : ISerializable {
-#else
        [Serializable]
        public abstract class WebRequest : MarshalByRefObject, ISerializable {
-#endif
                static HybridDictionary prefixes = new HybridDictionary ();
                static bool isDefaultWebProxySet;
                static IWebProxy defaultWebProxy;
                static RequestCachePolicy defaultCachePolicy;
-               static MethodInfo cfGetDefaultProxy;
-               
-               // Constructors
-               
+
                static WebRequest ()
                {
-                       if (Platform.IsMacOS) {
-#if MONOTOUCH
-                               Type type = Type.GetType ("MonoTouch.CoreFoundation.CFNetwork, monotouch");
-#else
-                               Type type = Type.GetType ("MonoMac.CoreFoundation.CFNetwork, monomac");
-#endif
-                               if (type != null)
-                                       cfGetDefaultProxy = type.GetMethod ("GetDefaultProxy");
-                       }
-                       
-#if NET_2_1
+#if MOBILE
                        IWebRequestCreate http = new HttpRequestCreator ();
                        RegisterPrefix ("http", http);
                        RegisterPrefix ("https", http);
-       #if MOBILE
                        RegisterPrefix ("file", new FileWebRequestCreator ());
                        RegisterPrefix ("ftp", new FtpRequestCreator ());
-       #endif
 #else
-                       defaultCachePolicy = new HttpRequestCachePolicy (HttpRequestCacheLevel.NoCacheNoStore);
        #if CONFIGURATION_DEP
                        object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
                        WebRequestModulesSection s = cfg as WebRequestModulesSection;
@@ -129,14 +109,6 @@ namespace System.Net
                                authentication_level = value;
                        }
                }
-
-               [MonoTODO ("Implement the caching system. Currently always returns a policy with the NoCacheNoStore level")]
-               public virtual RequestCachePolicy CachePolicy
-               {
-                       get { return DefaultCachePolicy; }
-                       set {
-                       }
-               }
                
                public virtual string ConnectionGroupName {
                        get { throw GetMustImplement (); }
@@ -158,9 +130,18 @@ namespace System.Net
                        set { throw GetMustImplement (); }
                }
 
-               public static RequestCachePolicy DefaultCachePolicy
+               [MonoTODO ("Implement the caching system. Currently always returns a policy with the NoCacheNoStore level")]
+               public virtual RequestCachePolicy CachePolicy
                {
-                       get { return defaultCachePolicy; }
+                       get { return DefaultCachePolicy; }
+                       set {
+                       }
+               }
+               
+               public static RequestCachePolicy DefaultCachePolicy {
+                       get {
+                               return defaultCachePolicy ?? (defaultCachePolicy = new HttpRequestCachePolicy (HttpRequestCacheLevel.NoCacheNoStore));
+                       }
                        set {
                                throw GetMustImplement ();
                        }
@@ -171,12 +152,7 @@ namespace System.Net
                        set { throw GetMustImplement (); }
                }
                
-#if !MOONLIGHT
-               public TokenImpersonationLevel ImpersonationLevel {
-                       get { throw GetMustImplement (); }
-                       set { throw GetMustImplement (); }
-               }
-#endif
+
                public virtual string Method { 
                        get { throw GetMustImplement (); }
                        set { throw GetMustImplement (); }
@@ -210,7 +186,9 @@ namespace System.Net
                                throw GetMustImplement ();
                        }
                }
-               
+
+               public TokenImpersonationLevel ImpersonationLevel { get; set; }
+
 //             volatile static IWebProxy proxy;
                static readonly object lockobj = new object ();
                
@@ -246,9 +224,14 @@ namespace System.Net
                        
                        ProxyElement pe = sec.Proxy;
                        
-                       if ((pe.UseSystemDefault != ProxyElement.UseSystemDefaultValues.False) && (pe.ProxyAddress == null))
-                               p = (WebProxy) GetSystemWebProxy ();
-                       else
+                       if ((pe.UseSystemDefault != ProxyElement.UseSystemDefaultValues.False) && (pe.ProxyAddress == null)) {
+                               IWebProxy proxy = GetSystemWebProxy ();
+                               
+                               if (!(proxy is WebProxy))
+                                       return proxy;
+                               
+                               p = (WebProxy) proxy;
+                       } else
                                p = new WebProxy ();
                        
                        if (pe.ProxyAddress != null)
@@ -303,7 +286,19 @@ namespace System.Net
                                throw new ArgumentNullException ("requestUri");
                        return GetCreator (requestUri.Scheme).Create (requestUri);
                }
-
+#if NET_4_0
+               [MonoTODO ("for portable library support")]
+               public static HttpWebRequest CreateHttp (string requestUriString)
+               {
+                       throw new NotImplementedException ();
+               }
+                       
+               [MonoTODO ("for portable library support")]
+               public static HttpWebRequest CreateHttp (Uri requestUri)
+               {
+                       throw new NotImplementedException ();
+               }
+#endif
                public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
                {
                        throw GetMustImplement ();
@@ -327,14 +322,23 @@ namespace System.Net
                [MonoTODO("Look in other places for proxy config info")]
                public static IWebProxy GetSystemWebProxy ()
                {
+#if MONOTOUCH
+                       return CFNetwork.GetDefaultProxy ();
+#else
+#if MONODROID
+                       // Return the system web proxy.  This only works for ICS+.
+                       var androidProxy = AndroidPlatform.GetDefaultProxy ();
+                       if (androidProxy != null)
+                               return androidProxy;
+#endif
 #if !NET_2_1
                        if (IsWindows ()) {
                                int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
 
                                if (iProxyEnable > 0) {
-                                       string strHttpProxy = "";
-                                       string[] bypassList = null;
+                                       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);
@@ -347,20 +351,24 @@ namespace System.Net
                                                        }
                                        } else strHttpProxy = strProxyServer;
                                        
-                                       if (strProxyOverrride != null)
-                                               bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
-                                       
-                                       ArrayList al = new ArrayList ();
+                                       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;
+                                               foreach (string str in bypassList) {
+                                                       if (str != "<local>")
+                                                               al.Add (str);
+                                                       else
+                                                               bBypassOnLocal = true;
+                                               }
                                        }
                                        
                                        return new WebProxy (strHttpProxy, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
                                }
                        } else {
 #endif
+                               if (Platform.IsMacOS)
+                                       return CFNetwork.GetDefaultProxy ();
+                               
                                string address = Environment.GetEnvironmentVariable ("http_proxy");
 
                                if (address == null)
@@ -386,19 +394,22 @@ namespace System.Net
                                                        }
                                                }
                                                
-                                               bool bBypassOnLocal = false;
-                                               string[] bypassList = null;
+                                               bool bBypassOnLocal = false;                                            
                                                ArrayList al = new ArrayList ();
                                                string bypass = Environment.GetEnvironmentVariable ("no_proxy");
                                                
                                                if (bypass == null)
                                                        bypass = Environment.GetEnvironmentVariable ("NO_PROXY");
                                                
-                                               bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
+                                               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;
+                                                       foreach (string str in bypassList) {
+                                                               if (str != "*.local")
+                                                                       al.Add (str);
+                                                               else
+                                                                       bBypassOnLocal = true;
+                                                       }
                                                }
                                                
                                                return new WebProxy (uri, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
@@ -409,10 +420,8 @@ namespace System.Net
                        }
 #endif
                        
-                       if (cfGetDefaultProxy != null)
-                               return (IWebProxy) cfGetDefaultProxy.Invoke (null, null);
-                       
                        return new WebProxy ();
+#endif // MONOTOUCH
                }
 
                void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)