Enable the System build for monodroid
[mono.git] / mcs / class / System / System.Net / WebRequest.cs
index c1383652ecd72de5b742291c0aee0ff0de1259fb..533dd17f7a6475f2cca1ddd73b8cddf81724bd22 100644 (file)
@@ -32,22 +32,53 @@ using System.Collections.Specialized;
 using System.Configuration;
 using System.IO;
 using System.Runtime.Serialization;
+using System.Globalization;
 #if NET_2_0
 using System.Net.Configuration;
+using System.Net.Security;
+using System.Net.Cache;
+using System.Security.Principal;
+#endif
+
+#if NET_2_1
+using ConfigurationException = System.ArgumentException;
+
+namespace System.Net.Configuration {
+       class Dummy {}
+}
 #endif
 
 namespace System.Net 
 {
+#if MOONLIGHT
+       internal abstract class WebRequest : ISerializable {
+#else
        [Serializable]
-       public abstract class WebRequest : MarshalByRefObject, ISerializable
-       {
+       public abstract class WebRequest : MarshalByRefObject, ISerializable {
+#endif
                static HybridDictionary prefixes = new HybridDictionary ();
+#if NET_2_0
+               static bool isDefaultWebProxySet;
+               static IWebProxy defaultWebProxy;
+               static RequestCachePolicy defaultCachePolicy;
+#endif
                
                // Constructors
                
                static WebRequest ()
                {
-#if NET_2_0 && CONFIGURATION_DEP
+#if NET_2_1
+                       AddPrefix ("http", typeof (HttpRequestCreator));
+                       AddPrefix ("https", typeof (HttpRequestCreator));
+       #if MOBILE
+                       AddPrefix ("file", typeof (FileWebRequestCreator));
+                       AddPrefix ("ftp", typeof (FtpRequestCreator));
+       #endif
+#else
+       #if NET_2_0
+                       defaultCachePolicy = new HttpRequestCachePolicy (HttpRequestCacheLevel.NoCacheNoStore);
+       #endif
+       #if NET_2_0 && CONFIGURATION_DEP
                        object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
                        WebRequestModulesSection s = cfg as WebRequestModulesSection;
                        if (s != null) {
@@ -56,8 +87,9 @@ namespace System.Net
                                        AddPrefix (el.Prefix, el.Type);
                                return;
                        }
-#endif
+       #endif
                        ConfigurationSettings.GetConfig ("system.net/webRequestModules");
+#endif
                }
                
                protected WebRequest () 
@@ -66,6 +98,9 @@ namespace System.Net
                
                protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
                {
+#if ONLY_1_1
+                       throw GetMustImplement ();
+#endif
                }
 
                static Exception GetMustImplement ()
@@ -74,8 +109,30 @@ namespace System.Net
                }
                
                // Properties
+
+#if NET_2_0
+               private AuthenticationLevel authentication_level = AuthenticationLevel.MutualAuthRequested;
                
-               public virtual string ConnectionGroupName { 
+               public AuthenticationLevel AuthenticationLevel
+               {
+                       get {
+                               return(authentication_level);
+                       }
+                       set {
+                               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 {
+                       }
+               }
+#endif
+               
+               public virtual string ConnectionGroupName {
                        get { throw GetMustImplement (); }
                        set { throw GetMustImplement (); }
                }
@@ -94,12 +151,28 @@ namespace System.Net
                        get { throw GetMustImplement (); }
                        set { throw GetMustImplement (); }
                }
+
+#if NET_2_0
+               public static RequestCachePolicy DefaultCachePolicy
+               {
+                       get { return defaultCachePolicy; }
+                       set {
+                               throw GetMustImplement ();
+                       }
+               }
+#endif
                
                public virtual WebHeaderCollection Headers { 
                        get { throw GetMustImplement (); }
                        set { throw GetMustImplement (); }
                }
                
+#if NET_2_0 && !MOONLIGHT
+               public TokenImpersonationLevel ImpersonationLevel {
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+#endif
                public virtual string Method { 
                        get { throw GetMustImplement (); }
                        set { throw GetMustImplement (); }
@@ -125,22 +198,36 @@ namespace System.Net
                }
                
 #if NET_2_0
-               volatile static IWebProxy proxy;
+               public virtual bool UseDefaultCredentials
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+                       set {
+                               throw GetMustImplement ();
+                       }
+               }
+               
+//             volatile static IWebProxy proxy;
                static readonly object lockobj = new object ();
                
                public static IWebProxy DefaultWebProxy {
                        get {
-                               lock (lockobj) {
-                                       if (proxy == null)
-                                               proxy = GetDefaultWebProxy ();
-                                       return proxy;
+                               if (!isDefaultWebProxySet) {
+                                       lock (lockobj) {
+                                               if (defaultWebProxy == null)
+                                                       defaultWebProxy = GetDefaultWebProxy ();
+                                       }
                                }
+                               return defaultWebProxy;
                        }
                        set {
-                               if (value == null)
-                                       throw new ArgumentNullException ("WebRequest.DefaultWebProxy",
-                                                       "null IWebProxy not allowed.");
-                               proxy = value;
+                               /* MS documentation states that a null value would cause an ArgumentNullException
+                                * but that's not the way it behaves:
+                                * https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304724
+                                */
+                               defaultWebProxy = value;
+                               isDefaultWebProxySet = true;
                        }
                }
                
@@ -168,7 +255,7 @@ namespace System.Net
                                p.BypassProxyOnLocal = (pe.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);
 #endif
                        return p;
-               }               
+               }
 #endif
 
                // Methods
@@ -234,35 +321,57 @@ namespace System.Net
                public static IWebProxy GetSystemWebProxy ()
                {
                        string address = Environment.GetEnvironmentVariable ("http_proxy");
+                       if (address == null)
+                               address = Environment.GetEnvironmentVariable ("HTTP_PROXY");
+
                        if (address != null) {
                                try {
-                                       WebProxy p = new WebProxy (address);
-                                       return p;
-                               } catch (UriFormatException) {}
+                                       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;
+                                               }
+                                       }
+                                       return new WebProxy (uri);
+                               } catch (UriFormatException) { }
                        }
                        return new WebProxy ();
                }
 #endif
-#if TARGET_JVM
-               public virtual void GetObjectData
-#else
+
                void ISerializable.GetObjectData
-#endif
                (SerializationInfo serializationInfo,
                                                  StreamingContext streamingContext)
                {
                        throw new NotSupportedException ();
                }
 
+
+#if NET_2_0
+               protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
+               {
+                       throw GetMustImplement ();
+               }
+#endif
+
                public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
                {
                        if (prefix == null)
-                               throw new ArgumentNullException("prefix");
+                               throw new ArgumentNullException ("prefix");
                        if (creator == null)
-                               throw new ArgumentNullException("creator");                     
+                               throw new ArgumentNullException ("creator");
                        
                        lock (prefixes.SyncRoot) {
-                               string lowerCasePrefix = prefix.ToLower ();
+                               string lowerCasePrefix = prefix.ToLower (CultureInfo.InvariantCulture);
                                if (prefixes.Contains (lowerCasePrefix))
                                        return false;
                                prefixes.Add (lowerCasePrefix, creator);
@@ -275,7 +384,7 @@ namespace System.Net
                        int longestPrefix = -1;
                        IWebRequestCreate creator = null;
 
-                       prefix = prefix.ToLower ();
+                       prefix = prefix.ToLower (CultureInfo.InvariantCulture);
 
                        IDictionaryEnumerator e = prefixes.GetEnumerator ();
                        while (e.MoveNext ()) {
@@ -285,7 +394,7 @@ namespace System.Net
                                        continue;
                                
                                if (!prefix.StartsWith (key))
-                                       continue;                                       
+                                       continue;
                                        
                                longestPrefix = key.Length;
                                creator = (IWebRequestCreate) e.Value;