ServicePoint.cs 5.9 KB

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