Merge pull request #2020 from tomjepp/master
[mono.git] / mcs / class / System / System.Net / WebRequest.cs
index ad5a8e298d3bd609c5e781d080c5274df3b83231..efb06fe9aa82385b6a9df86d0c63fb8c121111b2 100644 (file)
@@ -39,9 +39,7 @@ using System.Net.Configuration;
 using System.Net.Security;
 using System.Net.Cache;
 using System.Security.Principal;
-#if NET_4_5
 using System.Threading.Tasks;
-#endif
 
 #if NET_2_1
 using ConfigurationException = System.ArgumentException;
@@ -53,31 +51,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;
-               
-               // Constructors
-               
+
                static WebRequest ()
                {
-#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;
@@ -118,14 +107,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 (); }
@@ -147,9 +128,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 ();
                        }
@@ -160,12 +150,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 (); }
@@ -199,7 +184,9 @@ namespace System.Net
                                throw GetMustImplement ();
                        }
                }
-               
+
+               public TokenImpersonationLevel ImpersonationLevel { get; set; }
+
 //             volatile static IWebProxy proxy;
                static readonly object lockobj = new object ();
                
@@ -222,6 +209,13 @@ namespace System.Net
                                isDefaultWebProxySet = true;
                        }
                }
+
+               internal static IWebProxy InternalDefaultWebProxy {
+                       get {
+                               return DefaultWebProxy;
+                       }
+               }
+
                
                [MonoTODO("Needs to respect Module, Proxy.AutoDetect, and Proxy.ScriptLocation config settings")]
                static IWebProxy GetDefaultWebProxy ()
@@ -297,7 +291,27 @@ namespace System.Net
                                throw new ArgumentNullException ("requestUri");
                        return GetCreator (requestUri.Scheme).Create (requestUri);
                }
+               static HttpWebRequest SharedCreateHttp (Uri uri)
+               {
+                       if (uri.Scheme != "http" && uri.Scheme != "https")
+                               throw new NotSupportedException ("The uri should start with http or https");
+
+                       return new HttpWebRequest (uri);
+               }
 
+               public static HttpWebRequest CreateHttp (string requestUriString)
+               {
+                       if (requestUriString == null)
+                               throw new ArgumentNullException ("requestUriString");
+                       return SharedCreateHttp (new Uri (requestUriString));
+               }
+                       
+               public static HttpWebRequest CreateHttp (Uri requestUri)
+               {
+                       if (requestUri == null)
+                               throw new ArgumentNullException ("requestUri");
+                       return SharedCreateHttp (requestUri);
+               }
                public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
                {
                        throw GetMustImplement ();
@@ -321,6 +335,15 @@ 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);
@@ -411,6 +434,7 @@ namespace System.Net
 #endif
                        
                        return new WebProxy ();
+#endif // MONOTOUCH
                }
 
                void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
@@ -495,7 +519,6 @@ namespace System.Net
                        prefixes [prefix] = o;
                }
 
-#if NET_4_5
                public virtual Task<Stream> GetRequestStreamAsync ()
                {
                        return Task<Stream>.Factory.FromAsync (BeginGetRequestStream, EndGetRequestStream, null);
@@ -505,7 +528,6 @@ namespace System.Net
                {
                        return Task<WebResponse>.Factory.FromAsync (BeginGetResponse, EndGetResponse, null);
                }
-#endif
 
        }
 }