implemented DnsRefreshTimeout to ServicePointManager and adjusted behavior of ServicePoint.HostEntry property.

simplified some calls as requested
This commit is contained in:
Simon Hartmann committed 2015-09-08 11:08:50 +02:00
1 parent e97227a53d
commit 932359f3d6
3 files changed
+51 -26

No files matched your search

+17 -23
View File
@@ -47,6 +47,7 @@ namespace System.Net
int maxIdleTime;
int currentConnections;
DateTime idleSince;
DateTime lastDnsResolve;
Version protocolVersion;
X509Certificate certificate;
X509Certificate clientCertificate;
@@ -339,37 +340,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;
}
}
@@ -134,6 +134,7 @@ namespace System.Net
private static int defaultConnectionLimit = DefaultPersistentConnectionLimit;
private static int maxServicePointIdleTime = 100000; // 100 seconds
private static int maxServicePoints = 0;
private static int dnsRefreshTimeout = 2 * 60 * 1000;
private static bool _checkCRL = false;
private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
@@ -218,14 +219,13 @@ namespace System.Net
return new NotImplementedException ();
}
[MonoTODO]
public static int DnsRefreshTimeout
{
get {
throw GetMustImplement ();
return dnsRefreshTimeout;
}
set {
throw GetMustImplement ();
dnsRefreshTimeout = Math.Max (-1, value);
}
}
@@ -13,6 +13,7 @@ using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
namespace MonoTests.System.Net
@@ -205,6 +206,36 @@ public class ServicePointTest
}
}
[Test]
public void DnsRefreshTimeout ()
{
const int dnsRefreshTimeout = 2;
ServicePoint sp;
IPHostEntry host0, host1, host2;
Uri uri;
PropertyInfo hostEntryProperty;
ServicePointManager.DnsRefreshTimeout = dnsRefreshTimeout;
uri = new Uri ("http://ww.google.com/");
sp = ServicePointManager.FindServicePoint (uri);
hostEntryProperty = typeof (ServicePoint).GetProperty ("HostEntry", BindingFlags.NonPublic | BindingFlags.Instance);
host0 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
host1 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
Assert.AreEqual (host0, host1, "HostEntry should result in the same IPHostEntry object.");
Thread.Sleep (dnsRefreshTimeout);
host2 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
Assert.AreNotEqual(host0, host2, "HostEntry should result in a new IPHostEntry " +
"object when DnsRefreshTimeout is reached.");
}
// Debug code not used now, but could be useful later
/*
private void WriteServicePoint (string label, ServicePoint sp)