2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / System.Net / ServicePointManager.cs
1 //
2 // System.Net.ServicePointManager
3 //
4 // Author:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Configuration;
33 using System.Net.Configuration;
34 using System.Security.Cryptography.X509Certificates;
35
36 //
37 // notes:
38 // A service point manager manages service points (duh!).
39 // A service point maintains a list of connections (per scheme + authority).
40 // According to HttpWebRequest.ConnectionGroupName each connection group
41 // creates additional connections. therefor, a service point has a hashtable
42 // of connection groups where each value is a list of connections.
43 // 
44 // when we need to make an HttpWebRequest, we need to do the following:
45 // 1. find service point, given Uri and Proxy 
46 // 2. find connection group, given service point and group name
47 // 3. find free connection in connection group, or create one (if ok due to limits)
48 // 4. lease connection
49 // 5. execute request
50 // 6. when finished, return connection
51 //
52
53
54 namespace System.Net 
55 {
56         public class ServicePointManager
57         {
58                 private static HybridDictionary servicePoints = new HybridDictionary ();
59                 
60                 // Static properties
61                 
62                 private static ICertificatePolicy policy = new DefaultCertificatePolicy ();
63                 private static int defaultConnectionLimit = DefaultPersistentConnectionLimit;
64                 private static int maxServicePointIdleTime = 900000; // 15 minutes
65                 private static int maxServicePoints = 0;
66                 private static bool _checkCRL = false;
67 #if (NET_1_0 || NET_1_1)
68                 private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Ssl3;
69 #else
70                 private static SecurityProtocolType _securityProtocol = SecurityProtocolType.Default;
71 #endif
72 #if NET_1_1
73                 static bool expectContinue = true;
74                 static bool useNagle;
75 #endif
76
77                 // Fields
78                 
79                 public const int DefaultNonPersistentConnectionLimit = 4;
80                 public const int DefaultPersistentConnectionLimit = 2;
81
82                 const string configKey = "system.net/connectionManagement";
83                 static ConnectionManagementData manager;
84                 
85                 static ServicePointManager ()
86                 {
87                         manager = (ConnectionManagementData) ConfigurationSettings.GetConfig (configKey);
88                 }
89                 // Constructors
90                 private ServicePointManager ()
91                 {
92                 }               
93                 
94                 // Properties
95                 
96                 public static ICertificatePolicy CertificatePolicy {
97                         get { return policy; }
98                         set { policy = value; }
99                 }
100
101 #if NET_1_0
102                 // we need it for SslClientStream
103                 internal
104 #else
105                 [MonoTODO("CRL checks not implemented")]
106                 public
107 #endif
108                 static bool CheckCertificateRevocationList {
109                         get { return _checkCRL; }
110                         set { _checkCRL = false; }      // TODO - don't yet accept true
111                 }
112                 
113                 public static int DefaultConnectionLimit {
114                         get { return defaultConnectionLimit; }
115                         set { 
116                                 if (value <= 0)
117                                         throw new ArgumentOutOfRangeException ("value");
118
119                                 defaultConnectionLimit = value; 
120                         }
121                 }
122                 
123                 public static int MaxServicePointIdleTime {
124                         get { 
125                                 return maxServicePointIdleTime;
126                         }
127                         set { 
128                                 if (value < -2 || value > Int32.MaxValue)
129                                         throw new ArgumentOutOfRangeException ("value");
130                                 maxServicePointIdleTime = value;
131                         }
132                 }
133                 
134                 public static int MaxServicePoints {
135                         get { 
136                                 return maxServicePoints; 
137                         }
138                         set {  
139                                 if (value < 0)
140                                         throw new ArgumentException ("value");                          
141
142                                 maxServicePoints = value;
143                                 RecycleServicePoints ();
144                         }
145                 }
146
147 #if NET_1_0
148                 // we need it for SslClientStream
149                 internal
150 #else
151                 public
152 #endif
153                 static SecurityProtocolType SecurityProtocol {
154                         get { return _securityProtocol; }
155                         set { _securityProtocol = value; }
156                 }
157
158 #if NET_1_1
159                 public static bool Expect100Continue {
160                         get { return expectContinue; }
161                         set { expectContinue = value; }
162                 }
163
164                 public static bool UseNagleAlgorithm {
165                         get { return useNagle; }
166                         set { useNagle = value; }
167                 }
168 #endif
169                 // Methods
170                 
171                 public static ServicePoint FindServicePoint (Uri address) 
172                 {
173                         return FindServicePoint (address, GlobalProxySelection.Select);
174                 }
175                 
176                 public static ServicePoint FindServicePoint (string uriString, IWebProxy proxy)
177                 {
178                         return FindServicePoint (new Uri(uriString), proxy);
179                 }
180                 
181                 public static ServicePoint FindServicePoint (Uri address, IWebProxy proxy)
182                 {
183                         if (address == null)
184                                 throw new ArgumentNullException ("address");
185
186                         RecycleServicePoints ();
187                         
188                         bool usesProxy = false;
189                         bool useConnect = false;
190                         if (proxy != null && !proxy.IsBypassed(address)) {
191                                 usesProxy = true;
192                                 bool isSecure = address.Scheme == "https";
193                                 address = proxy.GetProxy (address);
194                                 if (address.Scheme != "http" && !isSecure)
195                                         throw new NotSupportedException ("Proxy scheme not supported.");
196
197                                 if (isSecure && address.Scheme == "http")
198                                         useConnect = true;
199                         } 
200
201                         address = new Uri (address.Scheme + "://" + address.Authority);
202                         
203                         ServicePoint sp = null;
204                         lock (servicePoints) {
205                                 sp = servicePoints [address] as ServicePoint;
206                                 if (sp != null)
207                                         return sp;
208
209                                 if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
210                                         throw new InvalidOperationException ("maximum number of service points reached");
211
212                                 string addr = address.ToString ();
213                                 int limit = (int) manager.GetMaxConnections (addr);
214                                 sp = new ServicePoint (address, limit, maxServicePointIdleTime);
215 #if NET_1_1
216                                 sp.Expect100Continue = expectContinue;
217                                 sp.UseNagleAlgorithm = useNagle;
218 #endif
219                                 sp.UsesProxy = usesProxy;
220                                 sp.UseConnect = useConnect;
221                                 servicePoints.Add (address, sp);
222                         }
223                         
224                         return sp;
225                 }
226                 
227                 // Internal Methods
228
229                 internal static void RecycleServicePoints ()
230                 {
231                         ArrayList toRemove = new ArrayList ();
232                         lock (servicePoints) {
233                                 IDictionaryEnumerator e = servicePoints.GetEnumerator ();
234                                 while (e.MoveNext ()) {
235                                         ServicePoint sp = (ServicePoint) e.Value;
236                                         if (sp.AvailableForRecycling) {
237                                                 toRemove.Add (e.Key);
238                                         }
239                                 }
240                                 
241                                 for (int i = 0; i < toRemove.Count; i++) 
242                                         servicePoints.Remove (toRemove [i]);
243
244                                 if (maxServicePoints == 0 || servicePoints.Count <= maxServicePoints)
245                                         return;
246
247                                 // get rid of the ones with the longest idle time
248                                 SortedList list = new SortedList (servicePoints.Count);
249                                 e = servicePoints.GetEnumerator ();
250                                 while (e.MoveNext ()) {
251                                         ServicePoint sp = (ServicePoint) e.Value;
252                                         if (sp.CurrentConnections == 0) {
253                                                 while (list.ContainsKey (sp.IdleSince))
254                                                         sp.IdleSince.AddMilliseconds (1);
255                                                 list.Add (sp.IdleSince, sp.Address);
256                                         }
257                                 }
258                                 
259                                 for (int i = 0; i < list.Count && servicePoints.Count > maxServicePoints; i++)
260                                         servicePoints.Remove (list.GetByIndex (i));
261                         }
262                 }
263         }
264 }