File: ServicePoint.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (398 lines) | stat: -rw-r--r-- 9,785 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// System.Net.ServicePoint
//
// Authors:
// 	Lawrence Pit (loz@cable.a2000.nl)
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2002 Lawrence Pit
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace System.Net
{
	public class ServicePoint
	{
		readonly Uri uri;
		DateTime lastDnsResolve;
		Version protocolVersion;
		IPHostEntry host;
		bool usesProxy;
		bool sendContinue = true;
		bool useConnect;
		object hostE = new object ();
		bool useNagle;
		BindIPEndPoint endPointCallback = null;
		bool tcp_keepalive;
		int tcp_keepalive_time;
		int tcp_keepalive_interval;
		bool disposed;
		int connectionLeaseTimeout = -1;
		int receiveBufferSize = -1;

		// Constructors

		internal ServicePoint (ServicePointManager.SPKey key, Uri uri, int connectionLimit, int maxIdleTime)
		{
			Key = key;
			this.uri = uri;
			this.connectionLimit = connectionLimit;
			this.maxIdleTime = maxIdleTime;

			Scheduler = new ServicePointScheduler (this, connectionLimit, maxIdleTime);
		}

		internal ServicePointManager.SPKey Key {
			get;
		}

		ServicePointScheduler Scheduler {
			get; set;
		}

		// Properties

		public Uri Address {
			get { return uri; }
		}

		public BindIPEndPoint BindIPEndPointDelegate {
			get { return endPointCallback; }
			set { endPointCallback = value; }
		}

		public int ConnectionLeaseTimeout {
			get { return connectionLeaseTimeout; }
			set
			{
				if (value < Timeout.Infinite)
					throw new ArgumentOutOfRangeException (nameof (value));

				connectionLeaseTimeout = value;
			}
		}

		int connectionLimit;
		int maxIdleTime;

		public int ConnectionLimit {
			get { return connectionLimit; }
			set {
				connectionLimit = value;
				if (!disposed)
					Scheduler.ConnectionLimit = value;
			}
		}

		public string ConnectionName {
			get { return uri.Scheme; }
		}

		public int CurrentConnections {
			get {
				return disposed ? 0 : Scheduler.CurrentConnections;
			}
		}

		public DateTime IdleSince {
			get {
				if (disposed)
					return DateTime.MinValue;
				return Scheduler.IdleSince.ToLocalTime ();
			}
		}

		public int MaxIdleTime {
			get { return maxIdleTime; }
			set {
				maxIdleTime = value;
				if (!disposed)
					Scheduler.MaxIdleTime = value;
			}
		}

		public virtual Version ProtocolVersion {
			get { return protocolVersion; }
		}

		public int ReceiveBufferSize {
			get { return receiveBufferSize; }
			set
			{
				if (value < -1)
					throw new ArgumentOutOfRangeException (nameof (value));

				receiveBufferSize = value;
			}
		}

		public bool SupportsPipelining {
			get { return HttpVersion.Version11.Equals (protocolVersion); }
		}


		public bool Expect100Continue {
			get { return SendContinue; }
			set { SendContinue = value; }
		}

		public bool UseNagleAlgorithm {
			get { return useNagle; }
			set { useNagle = value; }
		}

		internal bool SendContinue {
			get {
				return sendContinue &&
				       (protocolVersion == null || protocolVersion == HttpVersion.Version11);
			}
			set { sendContinue = value; }
		}
		// Methods

		public void SetTcpKeepAlive (bool enabled, int keepAliveTime, int keepAliveInterval)
		{
			if (enabled) {
				if (keepAliveTime <= 0)
					throw new ArgumentOutOfRangeException ("keepAliveTime", "Must be greater than 0");
				if (keepAliveInterval <= 0)
					throw new ArgumentOutOfRangeException ("keepAliveInterval", "Must be greater than 0");
			}

			tcp_keepalive = enabled;
			tcp_keepalive_time = keepAliveTime;
			tcp_keepalive_interval = keepAliveInterval;
		}

		internal void KeepAliveSetup (Socket socket)
		{
			if (!tcp_keepalive)
				return;

			byte[] bytes = new byte[12];
			PutBytes (bytes, (uint)(tcp_keepalive ? 1 : 0), 0);
			PutBytes (bytes, (uint)tcp_keepalive_time, 4);
			PutBytes (bytes, (uint)tcp_keepalive_interval, 8);
			socket.IOControl (IOControlCode.KeepAliveValues, bytes, null);
		}

		static void PutBytes (byte[] bytes, uint v, int offset)
		{
			if (BitConverter.IsLittleEndian) {
				bytes[offset] = (byte)(v & 0x000000ff);
				bytes[offset + 1] = (byte)((v & 0x0000ff00) >> 8);
				bytes[offset + 2] = (byte)((v & 0x00ff0000) >> 16);
				bytes[offset + 3] = (byte)((v & 0xff000000) >> 24);
			} else {
				bytes[offset + 3] = (byte)(v & 0x000000ff);
				bytes[offset + 2] = (byte)((v & 0x0000ff00) >> 8);
				bytes[offset + 1] = (byte)((v & 0x00ff0000) >> 16);
				bytes[offset] = (byte)((v & 0xff000000) >> 24);
			}
		}

		// Internal Methods

		internal bool UsesProxy {
			get { return usesProxy; }
			set { usesProxy = value; }
		}

		internal bool UseConnect {
			get { return useConnect; }
			set { useConnect = value; }
		}

		private bool HasTimedOut {
			get {
				int timeout = ServicePointManager.DnsRefreshTimeout;
				return timeout != Timeout.Infinite &&
					(lastDnsResolve + TimeSpan.FromMilliseconds (timeout)) < DateTime.UtcNow;
			}
		}

		internal IPHostEntry HostEntry {
			get {
				lock (hostE) {
					string uriHost = uri.Host;

					// Cannot do DNS resolution on literal IP addresses
					if (uri.HostNameType == UriHostNameType.IPv6 || uri.HostNameType == UriHostNameType.IPv4) {
						if (host != null)
							return host;

						if (uri.HostNameType == UriHostNameType.IPv6) {
							// Remove square brackets
							uriHost = uriHost.Substring (1, uriHost.Length - 2);
						}

						// Creates IPHostEntry
						host = new IPHostEntry ();
						host.AddressList = new IPAddress[] { IPAddress.Parse (uriHost) };
						return host;
					}

					if (!HasTimedOut && host != null)
						return host;

					lastDnsResolve = DateTime.UtcNow;

					try {
						host = Dns.GetHostEntry (uriHost);
					} catch {
						return null;
					}
				}

				return host;
			}
		}

		internal void SetVersion (Version version)
		{
			protocolVersion = version;
		}

		internal void SendRequest (WebOperation operation, string groupName)
		{
			lock (this) {
				if (disposed)
					throw new ObjectDisposedException (typeof (ServicePoint).FullName);
				Scheduler.SendRequest (operation, groupName);
			}
		}

		public bool CloseConnectionGroup (string connectionGroupName)
		{
			lock (this) {
				if (disposed)
					return true;
				return Scheduler.CloseConnectionGroup (connectionGroupName);
			}
		}

		internal void FreeServicePoint ()
		{
			disposed = true;
			Scheduler = null;
		}

		//
		// Copied from the referencesource
		//

		object m_ServerCertificateOrBytes;
		object m_ClientCertificateOrBytes;

		/// <devdoc>
		///    <para>
		///       Gets the certificate received for this <see cref='System.Net.ServicePoint'/>.
		///    </para>
		/// </devdoc>
		public  X509Certificate Certificate {
			get {
				object chkCert = m_ServerCertificateOrBytes;
				if (chkCert != null && chkCert.GetType() == typeof(byte[]))
					return (X509Certificate)(m_ServerCertificateOrBytes = new X509Certificate((byte[]) chkCert));
				else
					return chkCert as X509Certificate;
			}
		}
		internal void UpdateServerCertificate(X509Certificate certificate)
		{
			if (certificate != null)
				m_ServerCertificateOrBytes = certificate.GetRawCertData();
			else
				m_ServerCertificateOrBytes = null;
		}

		/// <devdoc>
		/// <para>
		/// Gets the Client Certificate sent by us to the Server.
		/// </para>
		/// </devdoc>
		public  X509Certificate ClientCertificate {
			get {
				object chkCert = m_ClientCertificateOrBytes;
				if (chkCert != null && chkCert.GetType() == typeof(byte[]))
					return (X509Certificate)(m_ClientCertificateOrBytes = new X509Certificate((byte[]) chkCert));
				else
					return chkCert as X509Certificate;
			}
		}
		internal void UpdateClientCertificate(X509Certificate certificate)
		{
			if (certificate != null)
				m_ClientCertificateOrBytes = certificate.GetRawCertData();
			else
				m_ClientCertificateOrBytes = null;
		}

		internal bool CallEndPointDelegate (Socket sock, IPEndPoint remote)
		{
			if (endPointCallback == null)
				return true;

			int count = 0;
			for (;;) {
				IPEndPoint local = null;
				try {
					local = endPointCallback (this,
						remote, count);
				} catch {
					// This is to differentiate from an
					// OverflowException, which should propagate.
					return false;
				}

				if (local == null)
					return true;

				try {
					sock.Bind (local);
				} catch (SocketException) {
					// This is intentional; the docs say
					// that if the Bind fails, we keep
					// going until there is an
					// OverflowException on the retry
					// count.
					checked { ++count; }
					continue;
				}

				return true;
			}
		}
	}
}