Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / class / System / System.Net / WebRequest.cs
index ac2612ccb8f7422302865b534c015b8c780244c9..bb1f13c06643604115e022fb60abccb4f73c4d48 100644 (file)
@@ -1,9 +1,9 @@
-//\r
-// System.Net.WebRequest\r
-//\r
-// Authors:\r
-//   Lawrence Pit (loz@cable.a2000.nl)\r
-//\r
+//
+// System.Net.WebRequest
+//
+// Authors:
+//   Lawrence Pit (loz@cable.a2000.nl)
+//
 
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-\r
-using System;\r
-using System.Collections;\r
-using System.Collections.Specialized;\r
-using System.Configuration;\r
-using System.IO;\r
-using System.Runtime.Serialization;\r
-#if NET_2_0\r
-using System.Net.Configuration;\r
-#endif\r
-\r
-namespace System.Net \r
-{\r
-       [Serializable]\r
-       public abstract class WebRequest : MarshalByRefObject, ISerializable\r
-       {\r
-               static HybridDictionary prefixes = new HybridDictionary ();\r
-               \r
-               // Constructors\r
-               \r
-               static WebRequest ()\r
-               {\r
-#if NET_2_0 && CONFIGURATION_DEP\r
-                       object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");\r
-                       WebRequestModulesSection s = cfg as WebRequestModulesSection;\r
-                       if (s != null) {\r
-                               foreach (WebRequestModuleElement el in\r
-                                        s.WebRequestModules)\r
-                                       AddPrefix (el.Prefix, el.Type);\r
-                               return;\r
-                       }\r
-#endif\r
-                       ConfigurationSettings.GetConfig ("system.net/webRequestModules");\r
-               }\r
-               \r
+
+using System;
+using System.Collections;
+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
+
+namespace System.Net 
+{
+       [Serializable]
+       public abstract class WebRequest : MarshalByRefObject, ISerializable
+       {
+               static HybridDictionary prefixes = new HybridDictionary ();
+#if NET_2_0
+               static bool isDefaultWebProxySet;
+               static IWebProxy defaultWebProxy;
+#endif
+               
+               // Constructors
+               
+               static WebRequest ()
+               {
+#if NET_2_0 && CONFIGURATION_DEP
+                       object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
+                       WebRequestModulesSection s = cfg as WebRequestModulesSection;
+                       if (s != null) {
+                               foreach (WebRequestModuleElement el in
+                                        s.WebRequestModules)
+                                       AddPrefix (el.Prefix, el.Type);
+                               return;
+                       }
+#endif
+                       ConfigurationSettings.GetConfig ("system.net/webRequestModules");
+               }
+               
                protected WebRequest () 
                {
-               }\r
-               \r
-               protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) \r
-               {\r
-               }\r
-               \r
-               // Properties\r
-               \r
-               public virtual string ConnectionGroupName { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual long ContentLength { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual string ContentType { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual ICredentials Credentials { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual WebHeaderCollection Headers { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual string Method { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual bool PreAuthenticate { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual IWebProxy Proxy { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual Uri RequestUri { \r
-                       get { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               public virtual int Timeout { \r
-                       get { throw new NotImplementedException (); }\r
-                       set { throw new NotImplementedException (); }\r
-               }\r
-               \r
-               // Methods\r
-               \r
-               public virtual void Abort()\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               \r
-               public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) \r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               \r
-               public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-\r
-               public static WebRequest Create (string requestUriString) \r
-               {\r
-                       if (requestUriString == null)\r
-                               throw new ArgumentNullException ("requestUriString");\r
-                       return Create (new Uri (requestUriString));\r
-               }\r
-                               \r
-               public static WebRequest Create (Uri requestUri) \r
-               {\r
-                       if (requestUri == null)\r
-                               throw new ArgumentNullException ("requestUri");\r
-                       return GetCreator (requestUri.AbsoluteUri).Create (requestUri);\r
-               }\r
-               \r
-               public static WebRequest CreateDefault (Uri requestUri)\r
-               {\r
-                       if (requestUri == null)\r
-                               throw new ArgumentNullException ("requestUri");\r
-                       return GetCreator (requestUri.Scheme).Create (requestUri);\r
-               }\r
-\r
-               public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               \r
-               public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               \r
-               public virtual Stream GetRequestStream()\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               \r
-               public virtual WebResponse GetResponse()\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               \r
-               void ISerializable.GetObjectData (SerializationInfo serializationInfo,\r
-                                                 StreamingContext streamingContext)\r
-               {\r
-                       throw new NotSupportedException ();\r
-               }\r
-\r
-               public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)\r
-               {\r
-                       if (prefix == null)\r
-                               throw new ArgumentNullException("prefix");\r
-                       if (creator == null)\r
-                               throw new ArgumentNullException("creator");                     \r
-                       \r
-                       lock (prefixes.SyncRoot) {\r
-                               string lowerCasePrefix = prefix.ToLower ();\r
-                               if (prefixes.Contains (lowerCasePrefix))\r
-                                       return false;\r
-                               prefixes.Add (lowerCasePrefix, creator);\r
-                       }\r
-                       return true;\r
-               }\r
-               \r
-               private static IWebRequestCreate GetCreator (string prefix)\r
-               {\r
-                       int longestPrefix = -1;\r
-                       IWebRequestCreate creator = null;\r
-\r
-                       prefix = prefix.ToLower ();\r
-\r
-                       IDictionaryEnumerator e = prefixes.GetEnumerator ();\r
-                       while (e.MoveNext ()) {\r
-                               string key = e.Key as string;\r
-\r
-                               if (key.Length <= longestPrefix) \r
-                                       continue;\r
-                               \r
-                               if (!prefix.StartsWith (key))\r
-                                       continue;                                       \r
-                                       \r
-                               longestPrefix = key.Length;\r
-                               creator = (IWebRequestCreate) e.Value;\r
-                       }\r
-                       \r
-                       if (creator == null) \r
-                               throw new NotSupportedException (prefix);\r
-                               \r
-                       return creator;\r
-               }\r
-\r
-               internal static void ClearPrefixes ()\r
-               {\r
-                       prefixes.Clear ();\r
-               }\r
-\r
-               internal static void RemovePrefix (string prefix)\r
-               {\r
-                       prefixes.Remove (prefix);\r
-               }\r
-\r
-               internal static void AddPrefix (string prefix, string typeName)\r
-               {\r
-                       Type type = Type.GetType (typeName);\r
-                       if (type == null)\r
-                               throw new ConfigurationException (String.Format ("Type {0} not found", typeName));\r
-                       AddPrefix (prefix, type);\r
-               }\r
-\r
-               internal static void AddPrefix (string prefix, Type type)\r
-               {\r
-                       object o = Activator.CreateInstance (type, true);\r
-                       prefixes [prefix] = o;\r
-               }\r
-       }\r
-}\r
-\r
+               }
+               
+               protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
+               {
+#if ONLY_1_1
+                       throw GetMustImplement ();
+#endif
+               }
+
+               static Exception GetMustImplement ()
+               {
+                       return new NotImplementedException ("This method must be implemented in derived classes");
+               }
+               
+               // Properties
+
+#if NET_2_0
+               private AuthenticationLevel authentication_level = AuthenticationLevel.MutualAuthRequested;
+               
+               public AuthenticationLevel AuthenticationLevel
+               {
+                       get {
+                               return(authentication_level);
+                       }
+                       set {
+                               authentication_level = value;
+                       }
+               }
+
+               public virtual RequestCachePolicy CachePolicy
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+                       set {
+                       }
+               }
+#endif
+               
+               public virtual string ConnectionGroupName {
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+               public virtual long ContentLength { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+               public virtual string ContentType { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+               public virtual ICredentials Credentials { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+
+#if NET_2_0
+               public static RequestCachePolicy DefaultCachePolicy
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+                       set {
+                               throw GetMustImplement ();
+                       }
+               }
+#endif
+               
+               public virtual WebHeaderCollection Headers { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+#if NET_2_0
+               public TokenImpersonationLevel ImpersonationLevel {
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+#endif
+               public virtual string Method { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+               public virtual bool PreAuthenticate { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+               public virtual IWebProxy Proxy { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+               public virtual Uri RequestUri { 
+                       get { throw GetMustImplement (); }
+               }
+               
+               public virtual int Timeout { 
+                       get { throw GetMustImplement (); }
+                       set { throw GetMustImplement (); }
+               }
+               
+#if NET_2_0
+               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 {
+                               if (!isDefaultWebProxySet) {
+                                       lock (lockobj) {
+                                               if (defaultWebProxy == null)
+                                                       defaultWebProxy = GetDefaultWebProxy ();
+                                       }
+                               }
+                               return defaultWebProxy;
+                       }
+                       set {
+                               /* 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;
+                       }
+               }
+               
+               [MonoTODO("Needs to respect Module, Proxy.AutoDetect, and Proxy.ScriptLocation config settings")]
+               static IWebProxy GetDefaultWebProxy ()
+               {
+                       WebProxy p = null;
+                       
+#if CONFIGURATION_DEP
+                       DefaultProxySection sec = ConfigurationManager.GetSection ("system.net/defaultProxy") as DefaultProxySection;
+                       if (sec == null)
+                               return GetSystemWebProxy ();
+                       
+                       ProxyElement pe = sec.Proxy;
+                       
+                       if ((pe.UseSystemDefault != ProxyElement.UseSystemDefaultValues.False) && (pe.ProxyAddress == null))
+                               p = (WebProxy) GetSystemWebProxy ();
+                       else
+                               p = new WebProxy ();
+                       
+                       if (pe.ProxyAddress != null)
+                               p.Address = pe.ProxyAddress;
+                       
+                       if (pe.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified)
+                               p.BypassProxyOnLocal = (pe.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);
+#endif
+                       return p;
+               }
+#endif
+
+               // Methods
+               
+               public virtual void Abort()
+               {
+                       throw GetMustImplement ();
+               }
+               
+               public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
+               {
+                       throw GetMustImplement ();
+               }
+               
+               public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
+               {
+                       throw GetMustImplement ();
+               }
+
+               public static WebRequest Create (string requestUriString) 
+               {
+                       if (requestUriString == null)
+                               throw new ArgumentNullException ("requestUriString");
+                       return Create (new Uri (requestUriString));
+               }
+                               
+               public static WebRequest Create (Uri requestUri) 
+               {
+                       if (requestUri == null)
+                               throw new ArgumentNullException ("requestUri");
+                       return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
+               }
+               
+               public static WebRequest CreateDefault (Uri requestUri)
+               {
+                       if (requestUri == null)
+                               throw new ArgumentNullException ("requestUri");
+                       return GetCreator (requestUri.Scheme).Create (requestUri);
+               }
+
+               public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
+               {
+                       throw GetMustImplement ();
+               }
+               
+               public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
+               {
+                       throw GetMustImplement ();
+               }
+               
+               public virtual Stream GetRequestStream()
+               {
+                       throw GetMustImplement ();
+               }
+               
+               public virtual WebResponse GetResponse()
+               {
+                       throw GetMustImplement ();
+               }
+               
+#if NET_2_0
+               [MonoTODO("Look in other places for proxy config info")]
+               public static IWebProxy GetSystemWebProxy ()
+               {
+                       string address = Environment.GetEnvironmentVariable ("http_proxy");
+                       if (address != null) {
+                               try {
+                                       WebProxy p = new WebProxy (address);
+                                       return p;
+                               } catch (UriFormatException) {}
+                       }
+                       return new WebProxy ();
+               }
+#endif
+
+               void ISerializable.GetObjectData
+               (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");
+                       if (creator == null)
+                               throw new ArgumentNullException ("creator");
+                       
+                       lock (prefixes.SyncRoot) {
+                               string lowerCasePrefix = prefix.ToLower (CultureInfo.InvariantCulture);
+                               if (prefixes.Contains (lowerCasePrefix))
+                                       return false;
+                               prefixes.Add (lowerCasePrefix, creator);
+                       }
+                       return true;
+               }
+               
+               private static IWebRequestCreate GetCreator (string prefix)
+               {
+                       int longestPrefix = -1;
+                       IWebRequestCreate creator = null;
+
+                       prefix = prefix.ToLower (CultureInfo.InvariantCulture);
+
+                       IDictionaryEnumerator e = prefixes.GetEnumerator ();
+                       while (e.MoveNext ()) {
+                               string key = e.Key as string;
+
+                               if (key.Length <= longestPrefix) 
+                                       continue;
+                               
+                               if (!prefix.StartsWith (key))
+                                       continue;
+                                       
+                               longestPrefix = key.Length;
+                               creator = (IWebRequestCreate) e.Value;
+                       }
+                       
+                       if (creator == null) 
+                               throw new NotSupportedException (prefix);
+                               
+                       return creator;
+               }
+
+               internal static void ClearPrefixes ()
+               {
+                       prefixes.Clear ();
+               }
+
+               internal static void RemovePrefix (string prefix)
+               {
+                       prefixes.Remove (prefix);
+               }
+
+               internal static void AddPrefix (string prefix, string typeName)
+               {
+                       Type type = Type.GetType (typeName);
+                       if (type == null)
+                               throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
+                       AddPrefix (prefix, type);
+               }
+
+               internal static void AddPrefix (string prefix, Type type)
+               {
+                       object o = Activator.CreateInstance (type, true);
+                       prefixes [prefix] = o;
+               }
+       }
+}
+