2008-11-04 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System / System.Net / ServicePointManager.cs
index 06a7284777e6ed7ab49c0961d29a73deb79bd695..bea42e48cc30dfc838ff743830182e460c2e373c 100644 (file)
@@ -33,6 +33,10 @@ using System.Configuration;
 using System.Net.Configuration;
 using System.Security.Cryptography.X509Certificates;
 
+#if NET_2_0
+using System.Net.Security;
+#endif
+
 //
 // notes:
 // A service point manager manages service points (duh!).
@@ -55,6 +59,37 @@ namespace System.Net
 {
        public class ServicePointManager
        {
+               class SPKey {
+                       Uri uri; // schema/host/port
+                       bool use_connect;
+
+                       public SPKey (Uri uri, bool use_connect) {
+                               this.uri = uri;
+                               this.use_connect = use_connect;
+                       }
+
+                       public Uri Uri {
+                               get { return uri; }
+                       }
+
+                       public bool UseConnect {
+                               get { return use_connect; }
+                       }
+
+                       public override int GetHashCode () {
+                               return uri.GetHashCode () + ((use_connect) ? 1 : 0);
+                       }
+
+                       public override bool Equals (object obj) {
+                               SPKey other = obj as SPKey;
+                               if (obj == null) {
+                                       return false;
+                               }
+
+                               return (uri.Equals (other.uri) && other.use_connect == use_connect);
+                       }
+               }
+
                private static HybridDictionary servicePoints = new HybridDictionary ();
                
                // Static properties
@@ -64,13 +99,14 @@ namespace System.Net
                private static int maxServicePointIdleTime = 900000; // 15 minutes
                private static int maxServicePoints = 0;
                private static bool _checkCRL = false;
-#if (NET_1_0 || NET_1_1)
-               private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Ssl3;
-#else
-               private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Default;
-#endif
+               private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
+
 #if NET_1_1
+#if TARGET_JVM
+               static bool expectContinue = false;
+#else
                static bool expectContinue = true;
+#endif
                static bool useNagle;
 #endif
 
@@ -84,8 +120,24 @@ namespace System.Net
                
                static ServicePointManager ()
                {
+#if NET_2_0 && CONFIGURATION_DEP
+                       object cfg = ConfigurationManager.GetSection (configKey);
+                       ConnectionManagementSection s = cfg as ConnectionManagementSection;
+                       if (s != null) {
+                               manager = new ConnectionManagementData (null);
+                               foreach (ConnectionManagementElement e in s.ConnectionManagement)
+                                       manager.Add (e.Address, e.MaxConnection);
+
+                               defaultConnectionLimit = (int) manager.GetMaxConnections ("*");                         
+                               return;
+                       }
+#endif
                        manager = (ConnectionManagementData) ConfigurationSettings.GetConfig (configKey);
+                       if (manager != null) {
+                               defaultConnectionLimit = (int) manager.GetMaxConnections ("*");                         
+                       }
                }
+
                // Constructors
                private ServicePointManager ()
                {
@@ -93,6 +145,10 @@ namespace System.Net
                
                // Properties
                
+#if NET_2_0
+               [Obsolete ("Use ServerCertificateValidationCallback instead",
+                          false)]
+#endif
                public static ICertificatePolicy CertificatePolicy {
                        get { return policy; }
                        set { policy = value; }
@@ -119,6 +175,35 @@ namespace System.Net
                                defaultConnectionLimit = value; 
                        }
                }
+
+#if NET_2_0
+               static Exception GetMustImplement ()
+               {
+                       return new NotImplementedException ();
+               }
+               
+               [MonoTODO]
+               public static int DnsRefreshTimeout
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+                       set {
+                               throw GetMustImplement ();
+                       }
+               }
+               
+               [MonoTODO]
+               public static bool EnableDnsRoundRobin
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+                       set {
+                               throw GetMustImplement ();
+                       }
+               }
+#endif
                
                public static int MaxServicePointIdleTime {
                        get { 
@@ -155,6 +240,19 @@ namespace System.Net
                        set { _securityProtocol = value; }
                }
 
+#if NET_2_0 && SECURITY_DEP
+               [MonoTODO]
+               public static RemoteCertificateValidationCallback ServerCertificateValidationCallback
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+                       set {
+                               throw GetMustImplement ();
+                       }
+               }
+#endif
+
 #if NET_1_1
                public static bool Expect100Continue {
                        get { return expectContinue; }
@@ -177,7 +275,7 @@ namespace System.Net
                {
                        return FindServicePoint (new Uri(uriString), proxy);
                }
-               
+
                public static ServicePoint FindServicePoint (Uri address, IWebProxy proxy)
                {
                        if (address == null)
@@ -202,7 +300,8 @@ namespace System.Net
                        
                        ServicePoint sp = null;
                        lock (servicePoints) {
-                               sp = servicePoints [address] as ServicePoint;
+                               SPKey key = new SPKey (address, useConnect);
+                               sp = servicePoints [key] as ServicePoint;
                                if (sp != null)
                                        return sp;
 
@@ -218,7 +317,7 @@ namespace System.Net
 #endif
                                sp.UsesProxy = usesProxy;
                                sp.UseConnect = useConnect;
-                               servicePoints.Add (address, sp);
+                               servicePoints.Add (key, sp);
                        }
                        
                        return sp;
@@ -251,7 +350,7 @@ namespace System.Net
                                        ServicePoint sp = (ServicePoint) e.Value;
                                        if (sp.CurrentConnections == 0) {
                                                while (list.ContainsKey (sp.IdleSince))
-                                                       sp.IdleSince.AddMilliseconds (1);
+                                                       sp.IdleSince = sp.IdleSince.AddMilliseconds (1);
                                                list.Add (sp.IdleSince, sp.Address);
                                        }
                                }