ServicePoint.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. using System;
  12. using System.Collections;
  13. using System.Net.Sockets;
  14. using System.Security.Cryptography.X509Certificates;
  15. using System.Threading;
  16. namespace System.Net
  17. {
  18. public class ServicePoint
  19. {
  20. Uri uri;
  21. int connectionLimit;
  22. int maxIdleTime;
  23. int currentConnections;
  24. DateTime idleSince;
  25. Version protocolVersion;
  26. X509Certificate certificate;
  27. X509Certificate clientCertificate;
  28. IPHostEntry host;
  29. bool usesProxy;
  30. Hashtable groups;
  31. bool sendContinue = true;
  32. // Constructors
  33. internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
  34. {
  35. this.uri = uri;
  36. this.connectionLimit = connectionLimit;
  37. this.maxIdleTime = maxIdleTime;
  38. this.currentConnections = 0;
  39. this.idleSince = DateTime.Now;
  40. }
  41. // Properties
  42. public Uri Address {
  43. get { return uri; }
  44. }
  45. public X509Certificate Certificate {
  46. get { return certificate; }
  47. }
  48. public X509Certificate ClientCertificate {
  49. get { return clientCertificate; }
  50. }
  51. public int ConnectionLimit {
  52. get { return connectionLimit; }
  53. set {
  54. if (value <= 0)
  55. throw new ArgumentOutOfRangeException ();
  56. connectionLimit = value;
  57. }
  58. }
  59. public string ConnectionName {
  60. get { return uri.Scheme; }
  61. }
  62. public int CurrentConnections {
  63. get { return currentConnections; }
  64. }
  65. public DateTime IdleSince {
  66. get { return idleSince; }
  67. }
  68. public int MaxIdleTime {
  69. get { return maxIdleTime; }
  70. set {
  71. if (value < Timeout.Infinite || value > Int32.MaxValue)
  72. throw new ArgumentOutOfRangeException ();
  73. this.maxIdleTime = value;
  74. }
  75. }
  76. public virtual Version ProtocolVersion {
  77. get { return protocolVersion; }
  78. }
  79. public bool SupportsPipelining {
  80. get { return HttpVersion.Version11.Equals (protocolVersion); }
  81. }
  82. internal bool SendContinue {
  83. get { return sendContinue; }
  84. set { sendContinue = value; }
  85. }
  86. // Methods
  87. public override int GetHashCode()
  88. {
  89. return base.GetHashCode ();
  90. }
  91. // Internal Methods
  92. internal bool UsesProxy {
  93. get { return usesProxy; }
  94. set { usesProxy = value; }
  95. }
  96. internal bool AvailableForRecycling {
  97. get {
  98. return CurrentConnections == 0
  99. && maxIdleTime != Timeout.Infinite
  100. && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
  101. }
  102. }
  103. internal Hashtable Groups {
  104. get {
  105. if (groups == null)
  106. groups = new Hashtable ();
  107. return groups;
  108. }
  109. }
  110. internal IPHostEntry HostEntry
  111. {
  112. get {
  113. if (host == null) {
  114. string uriHost = uri.Host;
  115. // There is no need to do DNS resolution on literal IP addresses
  116. if (uri.HostNameType == UriHostNameType.IPv6 ||
  117. uri.HostNameType == UriHostNameType.IPv4) {
  118. if (uri.HostNameType == UriHostNameType.IPv6) {
  119. // Remove square brackets
  120. uriHost = uriHost.Substring(1,uriHost.Length-2);
  121. }
  122. // Creates IPHostEntry
  123. host = new IPHostEntry();
  124. host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
  125. return host;
  126. }
  127. // Try DNS resolution on host names
  128. try {
  129. host = Dns.GetHostByName (uriHost);
  130. }
  131. catch {
  132. return null;
  133. }
  134. }
  135. return host;
  136. }
  137. }
  138. internal WebConnectionGroup GetConnectionGroup (string name)
  139. {
  140. if (name == null)
  141. name = "";
  142. WebConnectionGroup group = Groups [name] as WebConnectionGroup;
  143. if (group != null)
  144. return group;
  145. group = new WebConnectionGroup (this, name);
  146. Groups [name] = group;
  147. return group;
  148. }
  149. internal EventHandler SendRequest (HttpWebRequest request, string groupName)
  150. {
  151. WebConnection cnc;
  152. lock (this) {
  153. WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
  154. cnc = cncGroup.GetConnection (groupName);
  155. }
  156. return cnc.SendRequest (request);
  157. }
  158. }
  159. }