ServicePoint.cs 6.0 KB

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