Merge pull request #2763 from esdrubal/mono-symbolicate-standalone2
[mono.git] / mcs / class / System / System.Net / ServicePoint.cs
index bb47c65c48faa262a6dedeeafc554390e796d4cd..13c06ec493f499c3f5636c35248940ba13b3a6cc 100644 (file)
@@ -47,9 +47,8 @@ namespace System.Net
                int maxIdleTime;
                int currentConnections;
                DateTime idleSince;
+               DateTime lastDnsResolve;
                Version protocolVersion;
-               X509Certificate certificate;
-               X509Certificate clientCertificate;
                IPHostEntry host;
                bool usesProxy;
                Dictionary<string,WebConnectionGroup> groups;
@@ -91,14 +90,6 @@ namespace System.Net
                        set { endPointCallback = value; }
                }
                
-               public X509Certificate Certificate {
-                       get { return certificate; }
-               }
-               
-               public X509Certificate ClientCertificate {
-                       get { return clientCertificate; }
-               }
-
                [MonoTODO]
                public int ConnectionLeaseTimeout
                {
@@ -312,7 +303,7 @@ namespace System.Net
                        lock (this) {
                                idleSince = outIdleSince;
 
-                               if (removeList != null) {
+                               if (removeList != null && groups != null) {
                                        foreach (var group in removeList)
                                                if (groups.ContainsKey (group.Name))
                                                        RemoveConnectionGroup (group);
@@ -339,37 +330,30 @@ namespace System.Net
                        CheckAvailableForRecycling (out dummy);
                }
 
+               private bool HasTimedOut
+               {
+                       get {
+                               int timeout = ServicePointManager.DnsRefreshTimeout;
+                               return timeout != Timeout.Infinite &&
+                                       (lastDnsResolve + TimeSpan.FromMilliseconds (timeout)) < DateTime.UtcNow;
+                       }
+               }
+
                internal IPHostEntry HostEntry
                {
                        get {
                                lock (hostE) {
-                                       if (host != null)
-                                               return host;
-
                                        string uriHost = uri.Host;
 
-                                       // There is no need to do DNS resolution on literal IP addresses
-                                       if (uri.HostNameType == UriHostNameType.IPv6 ||
-                                               uri.HostNameType == UriHostNameType.IPv4) {
+                                       if (host == null || HasTimedOut) {
+                                               lastDnsResolve = DateTime.UtcNow;
 
-                                               if (uri.HostNameType == UriHostNameType.IPv6) {
-                                                       // Remove square brackets
-                                                       uriHost = uriHost.Substring(1,uriHost.Length-2);
+                                               try {
+                                                       host = Dns.GetHostEntry (uriHost);
+                                               }
+                                               catch (Exception) {
+                                                       return null;
                                                }
-
-                                               // Creates IPHostEntry
-                                               host = new IPHostEntry();
-                                               host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
-
-                                               return host;
-                                       }
-
-                                       // Try DNS resolution on host names
-                                       try  {
-                                               host = Dns.GetHostByName (uriHost);
-                                       } 
-                                       catch {
-                                               return null;
                                        }
                                }
 
@@ -419,10 +403,55 @@ namespace System.Net
                        return false;
                }
 
-               internal void SetCertificates (X509Certificate client, X509Certificate server) 
+               //
+               // Copied from the referencesource
+               //
+
+               object m_ServerCertificateOrBytes;
+               object m_ClientCertificateOrBytes;
+
+               /// <devdoc>
+               ///    <para>
+               ///       Gets the certificate received for this <see cref='System.Net.ServicePoint'/>.
+               ///    </para>
+               /// </devdoc>
+               public  X509Certificate Certificate {
+                       get {
+                               object chkCert = m_ServerCertificateOrBytes;
+                               if (chkCert != null && chkCert.GetType() == typeof(byte[]))
+                                       return (X509Certificate)(m_ServerCertificateOrBytes = new X509Certificate((byte[]) chkCert));
+                               else
+                                       return chkCert as X509Certificate;
+                       }
+               }
+               internal void UpdateServerCertificate(X509Certificate certificate)
+               {
+                       if (certificate != null)
+                               m_ServerCertificateOrBytes = certificate.GetRawCertData();
+                       else
+                               m_ServerCertificateOrBytes = null;
+               }
+
+               /// <devdoc>
+               /// <para>
+               /// Gets the Client Certificate sent by us to the Server.
+               /// </para>
+               /// </devdoc>
+               public  X509Certificate ClientCertificate {
+                       get {
+                               object chkCert = m_ClientCertificateOrBytes;
+                               if (chkCert != null && chkCert.GetType() == typeof(byte[]))
+                                       return (X509Certificate)(m_ClientCertificateOrBytes = new X509Certificate((byte[]) chkCert));
+                               else
+                                       return chkCert as X509Certificate;
+                       }
+               }
+               internal void UpdateClientCertificate(X509Certificate certificate)
                {
-                       certificate = server;
-                       clientCertificate = client;
+                       if (certificate != null)
+                               m_ClientCertificateOrBytes = certificate.GetRawCertData();
+                       else
+                               m_ClientCertificateOrBytes = null;
                }
 
                internal bool CallEndPointDelegate (Socket sock, IPEndPoint remote)