Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / class / System / System.Net / ServicePoint.cs
1 //
2 // System.Net.ServicePoint
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Net.Sockets;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Threading;
38
39 namespace System.Net 
40 {
41         public class ServicePoint
42         {
43                 Uri uri;
44                 int connectionLimit;
45                 int maxIdleTime;
46                 int currentConnections;
47                 DateTime idleSince;
48                 Version protocolVersion;
49                 X509Certificate certificate;
50                 X509Certificate clientCertificate;
51                 IPHostEntry host;
52                 bool usesProxy;
53                 Hashtable groups;
54                 bool sendContinue = true;
55                 bool useConnect;
56                 object locker = new object ();
57                 object hostE = new object ();
58 #if NET_1_1
59                 bool useNagle;
60 #endif
61 #if NET_2_0
62                 BindIPEndPoint endPointCallback = null;
63 #endif
64                 
65                 // Constructors
66
67                 internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
68                 {
69                         this.uri = uri;  
70                         this.connectionLimit = connectionLimit;
71                         this.maxIdleTime = maxIdleTime;                 
72                         this.currentConnections = 0;
73                         this.idleSince = DateTime.Now;
74                 }
75                 
76                 // Properties
77                 
78                 public Uri Address {
79                         get { return uri; }
80                 }
81
82 #if NET_2_0
83                 static Exception GetMustImplement ()
84                 {
85                         return new NotImplementedException ();
86                 }
87
88                 public BindIPEndPoint BindIPEndPointDelegate
89                 {
90                         get { return endPointCallback; }
91                         set { endPointCallback = value; }
92                 }
93 #endif
94                 
95                 public X509Certificate Certificate {
96                         get { return certificate; }
97                 }
98                 
99                 public X509Certificate ClientCertificate {
100                         get { return clientCertificate; }
101                 }
102
103 #if NET_2_0
104                 [MonoTODO]
105                 public int ConnectionLeaseTimeout
106                 {
107                         get {
108                                 throw GetMustImplement ();
109                         }
110                         set {
111                                 throw GetMustImplement ();
112                         }
113                 }
114 #endif
115                 
116                 public int ConnectionLimit {
117                         get { return connectionLimit; }
118                         set {
119                                 if (value <= 0)
120                                         throw new ArgumentOutOfRangeException ();
121
122                                 connectionLimit = value;
123                         }
124                 }
125                 
126                 public string ConnectionName {
127                         get { return uri.Scheme; }
128                 }
129
130                 public int CurrentConnections {
131                         get {
132                                 return currentConnections;
133                         }
134                 }
135
136                 public DateTime IdleSince {
137                         get {
138                                 return idleSince;
139                         }
140                         internal set {
141                                 lock (locker)
142                                         idleSince = value;
143                         }
144                 }
145                 
146                 public int MaxIdleTime {
147                         get { return maxIdleTime; }
148                         set { 
149                                 if (value < Timeout.Infinite || value > Int32.MaxValue)
150                                         throw new ArgumentOutOfRangeException ();
151                                 this.maxIdleTime = value; 
152                         }
153                 }
154                 
155                 public virtual Version ProtocolVersion {
156                         get { return protocolVersion; }
157                 }
158
159 #if NET_2_0
160                 [MonoTODO]
161                 public int ReceiveBufferSize
162                 {
163                         get {
164                                 throw GetMustImplement ();
165                         }
166                         set {
167                                 throw GetMustImplement ();
168                         }
169                 }
170 #endif
171                 
172                 public bool SupportsPipelining {
173                         get { return HttpVersion.Version11.Equals (protocolVersion); }
174                 }
175
176 #if NET_1_1
177                 public bool Expect100Continue {
178                         get { return SendContinue; }
179                         set { SendContinue = value; }
180                 }
181
182                 [MonoTODO ("Use me")]
183                 public bool UseNagleAlgorithm {
184                         get { return useNagle; }
185                         set { useNagle = value; }
186                 }
187 #endif
188
189                 internal bool SendContinue {
190                         get { return sendContinue &&
191                                      (protocolVersion == null || protocolVersion == HttpVersion.Version11); }
192                         set { sendContinue = value; }
193                 }
194                 // Methods
195                 
196 #if !NET_2_0
197                 public override int GetHashCode() 
198                 {
199                         return base.GetHashCode ();
200                 }
201 #endif
202                 
203                 // Internal Methods
204
205                 internal bool UsesProxy {
206                         get { return usesProxy; }
207                         set { usesProxy = value; }
208                 }
209
210                 internal bool UseConnect {
211                         get { return useConnect; }
212                         set { useConnect = value; }
213                 }
214
215                 internal bool AvailableForRecycling {
216                         get { 
217                                 return CurrentConnections == 0
218                                     && maxIdleTime != Timeout.Infinite
219                                     && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
220                         }
221                 }
222
223                 internal Hashtable Groups {
224                         get {
225                                 if (groups == null)
226                                         groups = new Hashtable ();
227
228                                 return groups;
229                         }
230                 }
231
232                 internal IPHostEntry HostEntry
233                 {
234                         get {
235                                 lock (hostE) {
236                                         if (host != null)
237                                                 return host;
238
239                                         string uriHost = uri.Host;
240
241                                         // There is no need to do DNS resolution on literal IP addresses
242                                         if (uri.HostNameType == UriHostNameType.IPv6 ||
243                                                 uri.HostNameType == UriHostNameType.IPv4) {
244
245                                                 if (uri.HostNameType == UriHostNameType.IPv6) {
246                                                         // Remove square brackets
247                                                         uriHost = uriHost.Substring(1,uriHost.Length-2);
248                                                 }
249
250                                                 // Creates IPHostEntry
251                                                 host = new IPHostEntry();
252                                                 host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
253
254                                                 return host;
255                                         }
256
257                                         // Try DNS resolution on host names
258                                         try  {
259                                                 host = Dns.GetHostByName (uriHost);
260                                         } 
261                                         catch {
262                                                 return null;
263                                         }
264                                 }
265
266                                 return host;
267                         }
268                 }
269
270                 internal void SetVersion (Version version)
271                 {
272                         protocolVersion = version;
273                 }
274 #if !TARGET_JVM && !NET_2_1
275                 WebConnectionGroup GetConnectionGroup (string name)
276                 {
277                         if (name == null)
278                                 name = "";
279
280                         WebConnectionGroup group = Groups [name] as WebConnectionGroup;
281                         if (group != null)
282                                 return group;
283
284                         group = new WebConnectionGroup (this, name);
285                         Groups [name] = group;
286                         return group;
287                 }
288
289                 internal EventHandler SendRequest (HttpWebRequest request, string groupName)
290                 {
291                         WebConnection cnc;
292                         
293                         lock (locker) {
294                                 WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
295                                 cnc = cncGroup.GetConnection (request);
296                         }
297                         
298                         return cnc.SendRequest (request);
299                 }
300 #endif
301 #if NET_2_0
302                 [MonoNotSupported ("")]
303                 public bool CloseConnectionGroup (string connectionGroupName)
304                 {
305                         throw new NotImplementedException ();
306                 }
307 #endif
308
309                 internal void IncrementConnection ()
310                 {
311                         lock (locker) {
312                                 currentConnections++;
313                                 idleSince = DateTime.Now.AddMilliseconds (1000000);
314                         }
315                 }
316
317                 internal void DecrementConnection ()
318                 {
319                         lock (locker) {
320                                 currentConnections--;
321                                 if (currentConnections == 0)
322                                         idleSince = DateTime.Now;
323                         }
324                 }
325
326                 internal void SetCertificates (X509Certificate client, X509Certificate server) 
327                 {
328                         certificate = server;
329                         clientCertificate = client;
330                 }
331
332 #if NET_2_0
333                 internal bool CallEndPointDelegate (Socket sock, IPEndPoint remote)
334                 {
335                         if (endPointCallback == null)
336                                 return true;
337
338                         int count = 0;
339                         for (;;) {
340                                 IPEndPoint local = null;
341                                 try {
342                                         local = endPointCallback (this,
343                                                 remote, count);
344                                 } catch {
345                                         // This is to differentiate from an
346                                         // OverflowException, which should propagate.
347                                         return false;
348                                 }
349
350                                 if (local == null)
351                                         return true;
352
353                                 try {
354                                         sock.Bind (local);
355                                 } catch (SocketException) {
356                                         // This is intentional; the docs say
357                                         // that if the Bind fails, we keep
358                                         // going until there is an
359                                         // OverflowException on the retry
360                                         // count.
361                                         checked { ++count; }
362                                         continue;
363                                 }
364
365                                 return true;
366                         }
367                 }
368 #endif
369         }
370 }
371
372