2 // System.Net.ServicePointManager
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (c) 2003-2010 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Threading;
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.Collections.Specialized;
37 using System.Configuration;
38 using System.Net.Configuration;
39 using System.Security.Cryptography.X509Certificates;
41 using System.Globalization;
42 using System.Net.Security;
43 using System.Diagnostics;
47 // A service point manager manages service points (duh!).
48 // A service point maintains a list of connections (per scheme + authority).
49 // According to HttpWebRequest.ConnectionGroupName each connection group
50 // creates additional connections. therefor, a service point has a hashtable
51 // of connection groups where each value is a list of connections.
53 // when we need to make an HttpWebRequest, we need to do the following:
54 // 1. find service point, given Uri and Proxy
55 // 2. find connection group, given service point and group name
56 // 3. find free connection in connection group, or create one (if ok due to limits)
57 // 4. lease connection
59 // 6. when finished, return connection
65 public partial class ServicePointManager {
67 Uri uri; // schema/host/port
71 public SPKey (Uri uri, Uri proxy, bool use_connect) {
74 this.use_connect = use_connect;
81 public bool UseConnect {
82 get { return use_connect; }
85 public bool UsesProxy {
86 get { return proxy != null; }
89 public override int GetHashCode () {
91 hash = hash * 31 + ((use_connect) ? 1 : 0);
92 hash = hash * 31 + uri.GetHashCode ();
93 hash = hash * 31 + (proxy != null ? proxy.GetHashCode () : 0);
97 public override bool Equals (object obj) {
98 SPKey other = obj as SPKey;
103 if (!uri.Equals (other.uri))
105 if (use_connect != other.use_connect || UsesProxy != other.UsesProxy)
107 if (UsesProxy && !proxy.Equals (other.proxy))
113 private static HybridDictionary servicePoints = new HybridDictionary ();
117 private static ICertificatePolicy policy;
118 private static int defaultConnectionLimit = DefaultPersistentConnectionLimit;
119 private static int maxServicePointIdleTime = 100000; // 100 seconds
120 private static int maxServicePoints = 0;
121 private static int dnsRefreshTimeout = 2 * 60 * 1000;
122 private static bool _checkCRL = false;
123 private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
125 static bool expectContinue = true;
126 static bool useNagle;
127 static ServerCertValidationCallback server_cert_cb;
128 static bool tcp_keepalive;
129 static int tcp_keepalive_time;
130 static int tcp_keepalive_interval;
134 public const int DefaultNonPersistentConnectionLimit = 4;
136 public const int DefaultPersistentConnectionLimit = 10;
138 public const int DefaultPersistentConnectionLimit = 2;
142 const string configKey = "system.net/connectionManagement";
143 static ConnectionManagementData manager;
146 static ServicePointManager ()
149 #if CONFIGURATION_DEP
150 object cfg = ConfigurationManager.GetSection (configKey);
151 ConnectionManagementSection s = cfg as ConnectionManagementSection;
153 manager = new ConnectionManagementData (null);
154 foreach (ConnectionManagementElement e in s.ConnectionManagement)
155 manager.Add (e.Address, e.MaxConnection);
157 defaultConnectionLimit = (int) manager.GetMaxConnections ("*");
161 manager = (ConnectionManagementData) ConfigurationSettings.GetConfig (configKey);
162 if (manager != null) {
163 defaultConnectionLimit = (int) manager.GetMaxConnections ("*");
169 private ServicePointManager ()
175 [Obsolete ("Use ServerCertificateValidationCallback instead", false)]
176 public static ICertificatePolicy CertificatePolicy {
179 Interlocked.CompareExchange (ref policy, new DefaultCertificatePolicy (), null);
182 set { policy = value; }
185 internal static ICertificatePolicy GetLegacyCertificatePolicy ()
190 [MonoTODO("CRL checks not implemented")]
191 public static bool CheckCertificateRevocationList {
192 get { return _checkCRL; }
193 set { _checkCRL = false; } // TODO - don't yet accept true
196 public static int DefaultConnectionLimit {
197 get { return defaultConnectionLimit; }
200 throw new ArgumentOutOfRangeException ("value");
202 defaultConnectionLimit = value;
205 manager.Add ("*", defaultConnectionLimit);
210 static Exception GetMustImplement ()
212 return new NotImplementedException ();
215 public static int DnsRefreshTimeout
218 return dnsRefreshTimeout;
221 dnsRefreshTimeout = Math.Max (-1, value);
226 public static bool EnableDnsRoundRobin
229 throw GetMustImplement ();
232 throw GetMustImplement ();
236 public static int MaxServicePointIdleTime {
238 return maxServicePointIdleTime;
241 if (value < -2 || value > Int32.MaxValue)
242 throw new ArgumentOutOfRangeException ("value");
243 maxServicePointIdleTime = value;
247 public static int MaxServicePoints {
249 return maxServicePoints;
253 throw new ArgumentException ("value");
255 maxServicePoints = value;
259 public static SecurityProtocolType SecurityProtocol {
260 get { return _securityProtocol; }
261 set { _securityProtocol = value; }
264 internal static ServerCertValidationCallback ServerCertValidationCallback {
265 get { return server_cert_cb; }
268 public static RemoteCertificateValidationCallback ServerCertificateValidationCallback {
270 if (server_cert_cb == null)
272 return server_cert_cb.ValidationCallback;
277 server_cert_cb = null;
279 server_cert_cb = new ServerCertValidationCallback (value);
283 public static bool Expect100Continue {
284 get { return expectContinue; }
285 set { expectContinue = value; }
288 public static bool UseNagleAlgorithm {
289 get { return useNagle; }
290 set { useNagle = value; }
293 internal static bool DisableStrongCrypto {
294 get { return false; }
297 internal static bool DisableSendAuxRecord {
298 get { return false; }
302 public static void SetTcpKeepAlive (bool enabled, int keepAliveTime, int keepAliveInterval)
305 if (keepAliveTime <= 0)
306 throw new ArgumentOutOfRangeException ("keepAliveTime", "Must be greater than 0");
307 if (keepAliveInterval <= 0)
308 throw new ArgumentOutOfRangeException ("keepAliveInterval", "Must be greater than 0");
311 tcp_keepalive = enabled;
312 tcp_keepalive_time = keepAliveTime;
313 tcp_keepalive_interval = keepAliveInterval;
316 public static ServicePoint FindServicePoint (Uri address)
318 return FindServicePoint (address, null);
321 public static ServicePoint FindServicePoint (string uriString, IWebProxy proxy)
323 return FindServicePoint (new Uri(uriString), proxy);
326 public static ServicePoint FindServicePoint (Uri address, IWebProxy proxy)
329 throw new ArgumentNullException ("address");
331 var origAddress = new Uri (address.Scheme + "://" + address.Authority);
333 bool usesProxy = false;
334 bool useConnect = false;
335 if (proxy != null && !proxy.IsBypassed(address)) {
337 bool isSecure = address.Scheme == "https";
338 address = proxy.GetProxy (address);
339 if (address.Scheme != "http")
340 throw new NotSupportedException ("Proxy scheme not supported.");
342 if (isSecure && address.Scheme == "http")
346 address = new Uri (address.Scheme + "://" + address.Authority);
348 ServicePoint sp = null;
349 SPKey key = new SPKey (origAddress, usesProxy ? address : null, useConnect);
350 lock (servicePoints) {
351 sp = servicePoints [key] as ServicePoint;
355 if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
356 throw new InvalidOperationException ("maximum number of service points reached");
360 limit = defaultConnectionLimit;
362 string addr = address.ToString ();
363 limit = (int) manager.GetMaxConnections (addr);
365 sp = new ServicePoint (address, limit, maxServicePointIdleTime);
366 sp.Expect100Continue = expectContinue;
367 sp.UseNagleAlgorithm = useNagle;
368 sp.UsesProxy = usesProxy;
369 sp.UseConnect = useConnect;
370 sp.SetTcpKeepAlive (tcp_keepalive, tcp_keepalive_time, tcp_keepalive_interval);
371 servicePoints.Add (key, sp);
377 internal static void CloseConnectionGroup (string connectionGroupName)
379 lock (servicePoints) {
380 foreach (ServicePoint sp in servicePoints.Values) {
381 sp.CloseConnectionGroup (connectionGroupName);