ServicePoint.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.Generic;
  33. using System.Diagnostics;
  34. using System.Collections;
  35. using System.Net.Sockets;
  36. using System.Security.Cryptography.X509Certificates;
  37. using System.Threading;
  38. namespace System.Net
  39. {
  40. public class ServicePoint
  41. {
  42. readonly Uri uri;
  43. DateTime lastDnsResolve;
  44. Version protocolVersion;
  45. IPHostEntry host;
  46. bool usesProxy;
  47. bool sendContinue = true;
  48. bool useConnect;
  49. object hostE = new object ();
  50. bool useNagle;
  51. BindIPEndPoint endPointCallback = null;
  52. bool tcp_keepalive;
  53. int tcp_keepalive_time;
  54. int tcp_keepalive_interval;
  55. // Constructors
  56. internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
  57. {
  58. this.uri = uri;
  59. Scheduler = new ServicePointScheduler (this, connectionLimit, maxIdleTime);
  60. }
  61. internal ServicePointScheduler Scheduler {
  62. get;
  63. }
  64. // Properties
  65. public Uri Address {
  66. get { return uri; }
  67. }
  68. static Exception GetMustImplement ()
  69. {
  70. return new NotImplementedException ();
  71. }
  72. public BindIPEndPoint BindIPEndPointDelegate {
  73. get { return endPointCallback; }
  74. set { endPointCallback = value; }
  75. }
  76. [MonoTODO]
  77. public int ConnectionLeaseTimeout {
  78. get {
  79. throw GetMustImplement ();
  80. }
  81. set {
  82. throw GetMustImplement ();
  83. }
  84. }
  85. public int ConnectionLimit {
  86. get { return Scheduler.ConnectionLimit; }
  87. set { Scheduler.ConnectionLimit = value; }
  88. }
  89. public string ConnectionName {
  90. get { return uri.Scheme; }
  91. }
  92. public int CurrentConnections {
  93. get {
  94. return Scheduler.CurrentConnections;
  95. }
  96. }
  97. public DateTime IdleSince {
  98. get {
  99. return Scheduler.IdleSince.ToLocalTime ();
  100. }
  101. }
  102. public int MaxIdleTime {
  103. get { return Scheduler.MaxIdleTime; }
  104. set { Scheduler.MaxIdleTime = value; }
  105. }
  106. public virtual Version ProtocolVersion {
  107. get { return protocolVersion; }
  108. }
  109. [MonoTODO]
  110. public int ReceiveBufferSize {
  111. get {
  112. throw GetMustImplement ();
  113. }
  114. set {
  115. throw GetMustImplement ();
  116. }
  117. }
  118. public bool SupportsPipelining {
  119. get { return HttpVersion.Version11.Equals (protocolVersion); }
  120. }
  121. public bool Expect100Continue {
  122. get { return SendContinue; }
  123. set { SendContinue = value; }
  124. }
  125. public bool UseNagleAlgorithm {
  126. get { return useNagle; }
  127. set { useNagle = value; }
  128. }
  129. internal bool SendContinue {
  130. get {
  131. return sendContinue &&
  132. (protocolVersion == null || protocolVersion == HttpVersion.Version11);
  133. }
  134. set { sendContinue = value; }
  135. }
  136. // Methods
  137. public void SetTcpKeepAlive (bool enabled, int keepAliveTime, int keepAliveInterval)
  138. {
  139. if (enabled) {
  140. if (keepAliveTime <= 0)
  141. throw new ArgumentOutOfRangeException ("keepAliveTime", "Must be greater than 0");
  142. if (keepAliveInterval <= 0)
  143. throw new ArgumentOutOfRangeException ("keepAliveInterval", "Must be greater than 0");
  144. }
  145. tcp_keepalive = enabled;
  146. tcp_keepalive_time = keepAliveTime;
  147. tcp_keepalive_interval = keepAliveInterval;
  148. }
  149. internal void KeepAliveSetup (Socket socket)
  150. {
  151. if (!tcp_keepalive)
  152. return;
  153. byte[] bytes = new byte[12];
  154. PutBytes (bytes, (uint)(tcp_keepalive ? 1 : 0), 0);
  155. PutBytes (bytes, (uint)tcp_keepalive_time, 4);
  156. PutBytes (bytes, (uint)tcp_keepalive_interval, 8);
  157. socket.IOControl (IOControlCode.KeepAliveValues, bytes, null);
  158. }
  159. static void PutBytes (byte[] bytes, uint v, int offset)
  160. {
  161. if (BitConverter.IsLittleEndian) {
  162. bytes[offset] = (byte)(v & 0x000000ff);
  163. bytes[offset + 1] = (byte)((v & 0x0000ff00) >> 8);
  164. bytes[offset + 2] = (byte)((v & 0x00ff0000) >> 16);
  165. bytes[offset + 3] = (byte)((v & 0xff000000) >> 24);
  166. } else {
  167. bytes[offset + 3] = (byte)(v & 0x000000ff);
  168. bytes[offset + 2] = (byte)((v & 0x0000ff00) >> 8);
  169. bytes[offset + 1] = (byte)((v & 0x00ff0000) >> 16);
  170. bytes[offset] = (byte)((v & 0xff000000) >> 24);
  171. }
  172. }
  173. // Internal Methods
  174. internal bool UsesProxy {
  175. get { return usesProxy; }
  176. set { usesProxy = value; }
  177. }
  178. internal bool UseConnect {
  179. get { return useConnect; }
  180. set { useConnect = value; }
  181. }
  182. private bool HasTimedOut {
  183. get {
  184. int timeout = ServicePointManager.DnsRefreshTimeout;
  185. return timeout != Timeout.Infinite &&
  186. (lastDnsResolve + TimeSpan.FromMilliseconds (timeout)) < DateTime.UtcNow;
  187. }
  188. }
  189. internal IPHostEntry HostEntry {
  190. get {
  191. lock (hostE) {
  192. string uriHost = uri.Host;
  193. // Cannot do DNS resolution on literal IP addresses
  194. if (uri.HostNameType == UriHostNameType.IPv6 || uri.HostNameType == UriHostNameType.IPv4) {
  195. if (host != null)
  196. return host;
  197. if (uri.HostNameType == UriHostNameType.IPv6) {
  198. // Remove square brackets
  199. uriHost = uriHost.Substring (1, uriHost.Length - 2);
  200. }
  201. // Creates IPHostEntry
  202. host = new IPHostEntry ();
  203. host.AddressList = new IPAddress[] { IPAddress.Parse (uriHost) };
  204. return host;
  205. }
  206. if (!HasTimedOut && host != null)
  207. return host;
  208. lastDnsResolve = DateTime.UtcNow;
  209. try {
  210. host = Dns.GetHostEntry (uriHost);
  211. } catch {
  212. return null;
  213. }
  214. }
  215. return host;
  216. }
  217. }
  218. internal void SetVersion (Version version)
  219. {
  220. protocolVersion = version;
  221. }
  222. internal void SendRequest (WebOperation operation, string groupName)
  223. {
  224. lock (this) {
  225. Scheduler.SendRequest (operation, groupName);
  226. }
  227. }
  228. public bool CloseConnectionGroup (string connectionGroupName)
  229. {
  230. lock (this) {
  231. return Scheduler.CloseConnectionGroup (connectionGroupName);
  232. }
  233. }
  234. //
  235. // Copied from the referencesource
  236. //
  237. object m_ServerCertificateOrBytes;
  238. object m_ClientCertificateOrBytes;
  239. /// <devdoc>
  240. /// <para>
  241. /// Gets the certificate received for this <see cref='System.Net.ServicePoint'/>.
  242. /// </para>
  243. /// </devdoc>
  244. public X509Certificate Certificate {
  245. get {
  246. object chkCert = m_ServerCertificateOrBytes;
  247. if (chkCert != null && chkCert.GetType() == typeof(byte[]))
  248. return (X509Certificate)(m_ServerCertificateOrBytes = new X509Certificate((byte[]) chkCert));
  249. else
  250. return chkCert as X509Certificate;
  251. }
  252. }
  253. internal void UpdateServerCertificate(X509Certificate certificate)
  254. {
  255. if (certificate != null)
  256. m_ServerCertificateOrBytes = certificate.GetRawCertData();
  257. else
  258. m_ServerCertificateOrBytes = null;
  259. }
  260. /// <devdoc>
  261. /// <para>
  262. /// Gets the Client Certificate sent by us to the Server.
  263. /// </para>
  264. /// </devdoc>
  265. public X509Certificate ClientCertificate {
  266. get {
  267. object chkCert = m_ClientCertificateOrBytes;
  268. if (chkCert != null && chkCert.GetType() == typeof(byte[]))
  269. return (X509Certificate)(m_ClientCertificateOrBytes = new X509Certificate((byte[]) chkCert));
  270. else
  271. return chkCert as X509Certificate;
  272. }
  273. }
  274. internal void UpdateClientCertificate(X509Certificate certificate)
  275. {
  276. if (certificate != null)
  277. m_ClientCertificateOrBytes = certificate.GetRawCertData();
  278. else
  279. m_ClientCertificateOrBytes = null;
  280. }
  281. internal bool CallEndPointDelegate (Socket sock, IPEndPoint remote)
  282. {
  283. if (endPointCallback == null)
  284. return true;
  285. int count = 0;
  286. for (;;) {
  287. IPEndPoint local = null;
  288. try {
  289. local = endPointCallback (this,
  290. remote, count);
  291. } catch {
  292. // This is to differentiate from an
  293. // OverflowException, which should propagate.
  294. return false;
  295. }
  296. if (local == null)
  297. return true;
  298. try {
  299. sock.Bind (local);
  300. } catch (SocketException) {
  301. // This is intentional; the docs say
  302. // that if the Bind fails, we keep
  303. // going until there is an
  304. // OverflowException on the retry
  305. // count.
  306. checked { ++count; }
  307. continue;
  308. }
  309. return true;
  310. }
  311. }
  312. internal Socket GetConnection(PooledStream PooledStream, object owner, bool async, out IPAddress address, ref Socket abortSocket, ref Socket abortSocket6)
  313. {
  314. throw new NotImplementedException ();
  315. }
  316. }
  317. }