ServicePoint.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Constructors
  59. internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
  60. {
  61. this.uri = uri;
  62. this.connectionLimit = connectionLimit;
  63. this.maxIdleTime = maxIdleTime;
  64. this.currentConnections = 0;
  65. this.idleSince = DateTime.Now;
  66. }
  67. // Properties
  68. public Uri Address {
  69. get { return uri; }
  70. }
  71. public X509Certificate Certificate {
  72. get { return certificate; }
  73. }
  74. public X509Certificate ClientCertificate {
  75. get { return clientCertificate; }
  76. }
  77. public int ConnectionLimit {
  78. get { return connectionLimit; }
  79. set {
  80. if (value <= 0)
  81. throw new ArgumentOutOfRangeException ();
  82. connectionLimit = value;
  83. }
  84. }
  85. public string ConnectionName {
  86. get { return uri.Scheme; }
  87. }
  88. public int CurrentConnections {
  89. get {
  90. lock (locker) {
  91. return currentConnections;
  92. }
  93. }
  94. }
  95. public DateTime IdleSince {
  96. get {
  97. lock (locker) {
  98. return idleSince;
  99. }
  100. }
  101. }
  102. public int MaxIdleTime {
  103. get { return maxIdleTime; }
  104. set {
  105. if (value < Timeout.Infinite || value > Int32.MaxValue)
  106. throw new ArgumentOutOfRangeException ();
  107. this.maxIdleTime = value;
  108. }
  109. }
  110. public virtual Version ProtocolVersion {
  111. get { return protocolVersion; }
  112. }
  113. public bool SupportsPipelining {
  114. get { return HttpVersion.Version11.Equals (protocolVersion); }
  115. }
  116. #if NET_1_1
  117. public bool Expect100Continue {
  118. get { return SendContinue; }
  119. set { SendContinue = value; }
  120. }
  121. [MonoTODO ("Use me")]
  122. public bool UseNagleAlgorithm {
  123. get { return useNagle; }
  124. set { useNagle = value; }
  125. }
  126. #endif
  127. internal bool SendContinue {
  128. get { return sendContinue &&
  129. (protocolVersion == null || protocolVersion == HttpVersion.Version11); }
  130. set { sendContinue = value; }
  131. }
  132. // Methods
  133. public override int GetHashCode()
  134. {
  135. return base.GetHashCode ();
  136. }
  137. // Internal Methods
  138. internal bool UsesProxy {
  139. get { return usesProxy; }
  140. set { usesProxy = value; }
  141. }
  142. internal bool UseConnect {
  143. get { return useConnect; }
  144. set { useConnect = value; }
  145. }
  146. internal bool AvailableForRecycling {
  147. get {
  148. return CurrentConnections == 0
  149. && maxIdleTime != Timeout.Infinite
  150. && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
  151. }
  152. }
  153. internal Hashtable Groups {
  154. get {
  155. if (groups == null)
  156. groups = new Hashtable ();
  157. return groups;
  158. }
  159. }
  160. internal IPHostEntry HostEntry
  161. {
  162. get {
  163. lock (hostE) {
  164. if (host != null)
  165. return host;
  166. string uriHost = uri.Host;
  167. // There is no need to do DNS resolution on literal IP addresses
  168. if (uri.HostNameType == UriHostNameType.IPv6 ||
  169. uri.HostNameType == UriHostNameType.IPv4) {
  170. if (uri.HostNameType == UriHostNameType.IPv6) {
  171. // Remove square brackets
  172. uriHost = uriHost.Substring(1,uriHost.Length-2);
  173. }
  174. // Creates IPHostEntry
  175. host = new IPHostEntry();
  176. host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
  177. return host;
  178. }
  179. // Try DNS resolution on host names
  180. try {
  181. host = Dns.GetHostByName (uriHost);
  182. }
  183. catch {
  184. return null;
  185. }
  186. }
  187. return host;
  188. }
  189. }
  190. internal void SetVersion (Version version)
  191. {
  192. protocolVersion = version;
  193. }
  194. WebConnectionGroup GetConnectionGroup (string name)
  195. {
  196. if (name == null)
  197. name = "";
  198. WebConnectionGroup group = Groups [name] as WebConnectionGroup;
  199. if (group != null)
  200. return group;
  201. group = new WebConnectionGroup (this, name);
  202. Groups [name] = group;
  203. return group;
  204. }
  205. internal EventHandler SendRequest (HttpWebRequest request, string groupName)
  206. {
  207. WebConnection cnc;
  208. lock (locker) {
  209. WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
  210. cnc = cncGroup.GetConnection ();
  211. }
  212. return cnc.SendRequest (request);
  213. }
  214. internal void IncrementConnection ()
  215. {
  216. lock (locker) {
  217. currentConnections++;
  218. idleSince = DateTime.Now.AddMilliseconds (1000000);
  219. }
  220. }
  221. internal void DecrementConnection ()
  222. {
  223. lock (locker) {
  224. currentConnections--;
  225. if (currentConnections == 0)
  226. idleSince = DateTime.Now;
  227. }
  228. }
  229. internal void SetCertificates (X509Certificate client, X509Certificate server)
  230. {
  231. certificate = server;
  232. clientCertificate = client;
  233. }
  234. }
  235. }