ServicePoint.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //
  2. // System.Net.ServicePoint
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) 2002 Lawrence Pit
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Net.Sockets;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.Threading;
  36. namespace System.Net
  37. {
  38. public class ServicePoint
  39. {
  40. Uri uri;
  41. int connectionLimit;
  42. int maxIdleTime;
  43. int currentConnections;
  44. DateTime idleSince;
  45. Version protocolVersion;
  46. X509Certificate certificate;
  47. X509Certificate clientCertificate;
  48. IPHostEntry host;
  49. bool usesProxy;
  50. Hashtable groups;
  51. bool sendContinue = true;
  52. bool useConnect;
  53. object locker = new object ();
  54. object hostE = new object ();
  55. #if NET_1_1
  56. bool useNagle;
  57. #endif
  58. #if NET_2_0
  59. BindIPEndPoint endPointCallback = null;
  60. #endif
  61. // Constructors
  62. internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
  63. {
  64. this.uri = uri;
  65. this.connectionLimit = connectionLimit;
  66. this.maxIdleTime = maxIdleTime;
  67. this.currentConnections = 0;
  68. this.idleSince = DateTime.Now;
  69. }
  70. // Properties
  71. public Uri Address {
  72. get { return uri; }
  73. }
  74. #if NET_2_0
  75. static Exception GetMustImplement ()
  76. {
  77. return new NotImplementedException ();
  78. }
  79. public BindIPEndPoint BindIPEndPointDelegate
  80. {
  81. get { return endPointCallback; }
  82. set { endPointCallback = value; }
  83. }
  84. #endif
  85. public X509Certificate Certificate {
  86. get { return certificate; }
  87. }
  88. public X509Certificate ClientCertificate {
  89. get { return clientCertificate; }
  90. }
  91. #if NET_2_0
  92. [MonoTODO]
  93. public int ConnectionLeaseTimeout
  94. {
  95. get {
  96. throw GetMustImplement ();
  97. }
  98. set {
  99. throw GetMustImplement ();
  100. }
  101. }
  102. #endif
  103. public int ConnectionLimit {
  104. get { return connectionLimit; }
  105. set {
  106. if (value <= 0)
  107. throw new ArgumentOutOfRangeException ();
  108. connectionLimit = value;
  109. }
  110. }
  111. public string ConnectionName {
  112. get { return uri.Scheme; }
  113. }
  114. public int CurrentConnections {
  115. get {
  116. return currentConnections;
  117. }
  118. }
  119. public DateTime IdleSince {
  120. get {
  121. return idleSince;
  122. }
  123. internal set {
  124. lock (locker)
  125. idleSince = value;
  126. }
  127. }
  128. public int MaxIdleTime {
  129. get { return maxIdleTime; }
  130. set {
  131. if (value < Timeout.Infinite || value > Int32.MaxValue)
  132. throw new ArgumentOutOfRangeException ();
  133. this.maxIdleTime = value;
  134. }
  135. }
  136. public virtual Version ProtocolVersion {
  137. get { return protocolVersion; }
  138. }
  139. #if NET_2_0
  140. [MonoTODO]
  141. public int ReceiveBufferSize
  142. {
  143. get {
  144. throw GetMustImplement ();
  145. }
  146. set {
  147. throw GetMustImplement ();
  148. }
  149. }
  150. #endif
  151. public bool SupportsPipelining {
  152. get { return HttpVersion.Version11.Equals (protocolVersion); }
  153. }
  154. #if NET_1_1
  155. public bool Expect100Continue {
  156. get { return SendContinue; }
  157. set { SendContinue = value; }
  158. }
  159. [MonoTODO ("Use me")]
  160. public bool UseNagleAlgorithm {
  161. get { return useNagle; }
  162. set { useNagle = value; }
  163. }
  164. #endif
  165. internal bool SendContinue {
  166. get { return sendContinue &&
  167. (protocolVersion == null || protocolVersion == HttpVersion.Version11); }
  168. set { sendContinue = value; }
  169. }
  170. // Methods
  171. #if !NET_2_0
  172. public override int GetHashCode()
  173. {
  174. return base.GetHashCode ();
  175. }
  176. #endif
  177. // Internal Methods
  178. internal bool UsesProxy {
  179. get { return usesProxy; }
  180. set { usesProxy = value; }
  181. }
  182. internal bool UseConnect {
  183. get { return useConnect; }
  184. set { useConnect = value; }
  185. }
  186. internal bool AvailableForRecycling {
  187. get {
  188. return CurrentConnections == 0
  189. && maxIdleTime != Timeout.Infinite
  190. && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
  191. }
  192. }
  193. internal Hashtable Groups {
  194. get {
  195. if (groups == null)
  196. groups = new Hashtable ();
  197. return groups;
  198. }
  199. }
  200. internal IPHostEntry HostEntry
  201. {
  202. get {
  203. lock (hostE) {
  204. if (host != null)
  205. return host;
  206. string uriHost = uri.Host;
  207. // There is no need to do DNS resolution on literal IP addresses
  208. if (uri.HostNameType == UriHostNameType.IPv6 ||
  209. uri.HostNameType == UriHostNameType.IPv4) {
  210. if (uri.HostNameType == UriHostNameType.IPv6) {
  211. // Remove square brackets
  212. uriHost = uriHost.Substring(1,uriHost.Length-2);
  213. }
  214. // Creates IPHostEntry
  215. host = new IPHostEntry();
  216. host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
  217. return host;
  218. }
  219. // Try DNS resolution on host names
  220. try {
  221. host = Dns.GetHostByName (uriHost);
  222. }
  223. catch {
  224. return null;
  225. }
  226. }
  227. return host;
  228. }
  229. }
  230. internal void SetVersion (Version version)
  231. {
  232. protocolVersion = version;
  233. }
  234. #if !TARGET_JVM && !NET_2_1
  235. WebConnectionGroup GetConnectionGroup (string name)
  236. {
  237. if (name == null)
  238. name = "";
  239. WebConnectionGroup group = Groups [name] as WebConnectionGroup;
  240. if (group != null)
  241. return group;
  242. group = new WebConnectionGroup (this, name);
  243. Groups [name] = group;
  244. return group;
  245. }
  246. internal EventHandler SendRequest (HttpWebRequest request, string groupName)
  247. {
  248. WebConnection cnc;
  249. lock (locker) {
  250. WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
  251. cnc = cncGroup.GetConnection (request);
  252. }
  253. return cnc.SendRequest (request);
  254. }
  255. #endif
  256. #if NET_2_0
  257. [MonoNotSupported ("")]
  258. public bool CloseConnectionGroup (string connectionGroupName)
  259. {
  260. throw new NotImplementedException ();
  261. }
  262. #endif
  263. internal void IncrementConnection ()
  264. {
  265. lock (locker) {
  266. currentConnections++;
  267. idleSince = DateTime.Now.AddMilliseconds (1000000);
  268. }
  269. }
  270. internal void DecrementConnection ()
  271. {
  272. lock (locker) {
  273. currentConnections--;
  274. if (currentConnections == 0)
  275. idleSince = DateTime.Now;
  276. }
  277. }
  278. internal void SetCertificates (X509Certificate client, X509Certificate server)
  279. {
  280. certificate = server;
  281. clientCertificate = client;
  282. }
  283. #if NET_2_0
  284. internal bool CallEndPointDelegate (Socket sock, IPEndPoint remote)
  285. {
  286. if (endPointCallback == null)
  287. return true;
  288. int count = 0;
  289. for (;;) {
  290. IPEndPoint local = null;
  291. try {
  292. local = endPointCallback (this,
  293. remote, count);
  294. } catch {
  295. // This is to differentiate from an
  296. // OverflowException, which should propagate.
  297. return false;
  298. }
  299. if (local == null)
  300. return true;
  301. try {
  302. sock.Bind (local);
  303. } catch (SocketException) {
  304. // This is intentional; the docs say
  305. // that if the Bind fails, we keep
  306. // going until there is an
  307. // OverflowException on the retry
  308. // count.
  309. checked { ++count; }
  310. continue;
  311. }
  312. return true;
  313. }
  314. }
  315. #endif
  316. }
  317. }