ServicePointManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // System.Net.ServicePointManager
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Specialized;
  30. using System.Configuration;
  31. using System.Net.Configuration;
  32. using System.Security.Cryptography.X509Certificates;
  33. //
  34. // notes:
  35. // A service point manager manages service points (duh!).
  36. // A service point maintains a list of connections (per scheme + authority).
  37. // According to HttpWebRequest.ConnectionGroupName each connection group
  38. // creates additional connections. therefor, a service point has a hashtable
  39. // of connection groups where each value is a list of connections.
  40. //
  41. // when we need to make an HttpWebRequest, we need to do the following:
  42. // 1. find service point, given Uri and Proxy
  43. // 2. find connection group, given service point and group name
  44. // 3. find free connection in connection group, or create one (if ok due to limits)
  45. // 4. lease connection
  46. // 5. execute request
  47. // 6. when finished, return connection
  48. //
  49. namespace System.Net
  50. {
  51. public class ServicePointManager
  52. {
  53. private static HybridDictionary servicePoints = new HybridDictionary ();
  54. // Static properties
  55. private static ICertificatePolicy policy = new DefaultCertificatePolicy ();
  56. private static int defaultConnectionLimit = DefaultPersistentConnectionLimit;
  57. private static int maxServicePointIdleTime = 900000; // 15 minutes
  58. private static int maxServicePoints = 0;
  59. private static bool _checkCRL = false;
  60. #if (NET_1_0 || NET_1_1)
  61. private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Ssl3;
  62. #else
  63. private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Default;
  64. #endif
  65. #if NET_1_1
  66. static bool expectContinue = true;
  67. static bool useNagle;
  68. #endif
  69. // Fields
  70. public const int DefaultNonPersistentConnectionLimit = 4;
  71. public const int DefaultPersistentConnectionLimit = 2;
  72. const string configKey = "system.net/connectionManagement";
  73. static ConnectionManagementData manager;
  74. static ServicePointManager ()
  75. {
  76. manager = (ConnectionManagementData) ConfigurationSettings.GetConfig (configKey);
  77. }
  78. // Constructors
  79. private ServicePointManager ()
  80. {
  81. }
  82. // Properties
  83. public static ICertificatePolicy CertificatePolicy {
  84. get { return policy; }
  85. set { policy = value; }
  86. }
  87. #if NET_1_0
  88. // we need it for SslClientStream
  89. internal
  90. #else
  91. [MonoTODO("CRL checks not implemented")]
  92. public
  93. #endif
  94. static bool CheckCertificateRevocationList {
  95. get { return _checkCRL; }
  96. set { _checkCRL = false; } // TODO - don't yet accept true
  97. }
  98. public static int DefaultConnectionLimit {
  99. get { return defaultConnectionLimit; }
  100. set {
  101. if (value <= 0)
  102. throw new ArgumentOutOfRangeException ("value");
  103. defaultConnectionLimit = value;
  104. }
  105. }
  106. public static int MaxServicePointIdleTime {
  107. get {
  108. return maxServicePointIdleTime;
  109. }
  110. set {
  111. if (value < -2 || value > Int32.MaxValue)
  112. throw new ArgumentOutOfRangeException ("value");
  113. maxServicePointIdleTime = value;
  114. }
  115. }
  116. public static int MaxServicePoints {
  117. get {
  118. return maxServicePoints;
  119. }
  120. set {
  121. if (value < 0)
  122. throw new ArgumentException ("value");
  123. maxServicePoints = value;
  124. RecycleServicePoints ();
  125. }
  126. }
  127. #if NET_1_0
  128. // we need it for SslClientStream
  129. internal
  130. #else
  131. public
  132. #endif
  133. static SecurityProtocolType SecurityProtocol {
  134. get { return _securityProtocol; }
  135. set { _securityProtocol = value; }
  136. }
  137. #if NET_1_1
  138. public static bool Expect100Continue {
  139. get { return expectContinue; }
  140. set { expectContinue = value; }
  141. }
  142. public static bool UseNagleAlgorithm {
  143. get { return useNagle; }
  144. set { useNagle = value; }
  145. }
  146. #endif
  147. // Methods
  148. public static ServicePoint FindServicePoint (Uri address)
  149. {
  150. return FindServicePoint (address, GlobalProxySelection.Select);
  151. }
  152. public static ServicePoint FindServicePoint (string uriString, IWebProxy proxy)
  153. {
  154. return FindServicePoint (new Uri(uriString), proxy);
  155. }
  156. public static ServicePoint FindServicePoint (Uri address, IWebProxy proxy)
  157. {
  158. if (address == null)
  159. throw new ArgumentNullException ("address");
  160. RecycleServicePoints ();
  161. bool usesProxy = false;
  162. bool useConnect = false;
  163. if (proxy != null && !proxy.IsBypassed(address)) {
  164. usesProxy = true;
  165. bool isSecure = address.Scheme == "https";
  166. address = proxy.GetProxy (address);
  167. if (address.Scheme != "http" && !isSecure)
  168. throw new NotSupportedException ("Proxy scheme not supported.");
  169. if (isSecure && address.Scheme == "http")
  170. useConnect = true;
  171. }
  172. address = new Uri (address.Scheme + "://" + address.Authority);
  173. ServicePoint sp = null;
  174. lock (servicePoints) {
  175. sp = servicePoints [address] as ServicePoint;
  176. if (sp != null)
  177. return sp;
  178. if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
  179. throw new InvalidOperationException ("maximum number of service points reached");
  180. string addr = address.ToString ();
  181. int limit = (int) manager.GetMaxConnections (addr);
  182. sp = new ServicePoint (address, limit, maxServicePointIdleTime);
  183. #if NET_1_1
  184. sp.Expect100Continue = expectContinue;
  185. sp.UseNagleAlgorithm = useNagle;
  186. #endif
  187. sp.UsesProxy = usesProxy;
  188. sp.UseConnect = useConnect;
  189. servicePoints.Add (address, sp);
  190. }
  191. return sp;
  192. }
  193. // Internal Methods
  194. internal static void RecycleServicePoints ()
  195. {
  196. ArrayList toRemove = new ArrayList ();
  197. lock (servicePoints) {
  198. IDictionaryEnumerator e = servicePoints.GetEnumerator ();
  199. while (e.MoveNext ()) {
  200. ServicePoint sp = (ServicePoint) e.Value;
  201. if (sp.AvailableForRecycling) {
  202. toRemove.Add (e.Key);
  203. }
  204. }
  205. for (int i = 0; i < toRemove.Count; i++)
  206. servicePoints.Remove (toRemove [i]);
  207. if (maxServicePoints == 0 || servicePoints.Count <= maxServicePoints)
  208. return;
  209. // get rid of the ones with the longest idle time
  210. SortedList list = new SortedList (servicePoints.Count);
  211. e = servicePoints.GetEnumerator ();
  212. while (e.MoveNext ()) {
  213. ServicePoint sp = (ServicePoint) e.Value;
  214. if (sp.CurrentConnections == 0) {
  215. while (list.ContainsKey (sp.IdleSince))
  216. sp.IdleSince.AddMilliseconds (1);
  217. list.Add (sp.IdleSince, sp.Address);
  218. }
  219. }
  220. for (int i = 0; i < list.Count && servicePoints.Count > maxServicePoints; i++)
  221. servicePoints.Remove (list.GetByIndex (i));
  222. }
  223. }
  224. }
  225. }