2 // System.Net.ServicePoint
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 using System.Net.Sockets;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Threading;
41 public class ServicePoint
46 int currentConnections;
48 Version protocolVersion;
49 X509Certificate certificate;
50 X509Certificate clientCertificate;
54 bool sendContinue = true;
56 object locker = new object ();
57 object hostE = new object ();
64 internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
67 this.connectionLimit = connectionLimit;
68 this.maxIdleTime = maxIdleTime;
69 this.currentConnections = 0;
70 this.idleSince = DateTime.Now;
79 public X509Certificate Certificate {
80 get { return certificate; }
83 public X509Certificate ClientCertificate {
84 get { return clientCertificate; }
87 public int ConnectionLimit {
88 get { return connectionLimit; }
91 throw new ArgumentOutOfRangeException ();
93 connectionLimit = value;
97 public string ConnectionName {
98 get { return uri.Scheme; }
101 public int CurrentConnections {
103 return currentConnections;
107 public DateTime IdleSince {
117 public int MaxIdleTime {
118 get { return maxIdleTime; }
120 if (value < Timeout.Infinite || value > Int32.MaxValue)
121 throw new ArgumentOutOfRangeException ();
122 this.maxIdleTime = value;
126 public virtual Version ProtocolVersion {
127 get { return protocolVersion; }
130 public bool SupportsPipelining {
131 get { return HttpVersion.Version11.Equals (protocolVersion); }
135 public bool Expect100Continue {
136 get { return SendContinue; }
137 set { SendContinue = value; }
140 [MonoTODO ("Use me")]
141 public bool UseNagleAlgorithm {
142 get { return useNagle; }
143 set { useNagle = value; }
147 internal bool SendContinue {
148 get { return sendContinue &&
149 (protocolVersion == null || protocolVersion == HttpVersion.Version11); }
150 set { sendContinue = value; }
154 public override int GetHashCode()
156 return base.GetHashCode ();
161 internal bool UsesProxy {
162 get { return usesProxy; }
163 set { usesProxy = value; }
166 internal bool UseConnect {
167 get { return useConnect; }
168 set { useConnect = value; }
171 internal bool AvailableForRecycling {
173 return CurrentConnections == 0
174 && maxIdleTime != Timeout.Infinite
175 && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
179 internal Hashtable Groups {
182 groups = new Hashtable ();
188 internal IPHostEntry HostEntry
195 string uriHost = uri.Host;
197 // There is no need to do DNS resolution on literal IP addresses
198 if (uri.HostNameType == UriHostNameType.IPv6 ||
199 uri.HostNameType == UriHostNameType.IPv4) {
201 if (uri.HostNameType == UriHostNameType.IPv6) {
202 // Remove square brackets
203 uriHost = uriHost.Substring(1,uriHost.Length-2);
206 // Creates IPHostEntry
207 host = new IPHostEntry();
208 host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
213 // Try DNS resolution on host names
215 host = Dns.GetHostByName (uriHost);
226 internal void SetVersion (Version version)
228 protocolVersion = version;
231 WebConnectionGroup GetConnectionGroup (string name)
236 WebConnectionGroup group = Groups [name] as WebConnectionGroup;
240 group = new WebConnectionGroup (this, name);
241 Groups [name] = group;
245 internal EventHandler SendRequest (HttpWebRequest request, string groupName)
250 WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
251 cnc = cncGroup.GetConnection ();
254 return cnc.SendRequest (request);
257 internal void IncrementConnection ()
260 currentConnections++;
261 idleSince = DateTime.Now.AddMilliseconds (1000000);
265 internal void DecrementConnection ()
268 currentConnections--;
269 if (currentConnections == 0)
270 idleSince = DateTime.Now;
274 internal void SetCertificates (X509Certificate client, X509Certificate server)
276 certificate = server;
277 clientCertificate = client;