2 // System.Net.WebConnectionGroup
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Net.Configuration;
35 using System.Net.Sockets;
39 class WebConnectionGroup
43 ArrayList connections;
47 public WebConnectionGroup (ServicePoint sPoint, string name)
51 connections = new ArrayList (1);
57 //TODO: what do we do with the queue? Empty it out and abort the requests?
58 //TODO: abort requests or wait for them to finish
60 WeakReference cncRef = null;
62 int end = connections.Count;
63 // ArrayList removed = null;
64 for (int i = 0; i < end; i++) {
65 cncRef = (WeakReference) connections [i];
66 WebConnection cnc = cncRef.Target as WebConnection;
75 public WebConnection GetConnection (HttpWebRequest request)
77 WebConnection cnc = null;
79 WeakReference cncRef = null;
81 // Remove disposed connections
82 int end = connections.Count;
83 ArrayList removed = null;
84 for (int i = 0; i < end; i++) {
85 cncRef = (WeakReference) connections [i];
86 cnc = cncRef.Target as WebConnection;
89 removed = new ArrayList (1);
95 if (removed != null) {
96 for (int i = removed.Count - 1; i >= 0; i--)
97 connections.RemoveAt ((int) removed [i]);
100 cnc = CreateOrReuseConnection (request);
106 static void PrepareSharingNtlm (WebConnection cnc, HttpWebRequest request)
108 if (!cnc.NtlmAuthenticated)
111 bool needs_reset = false;
112 NetworkCredential cnc_cred = cnc.NtlmCredential;
114 bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.RequestUri));
115 ICredentials req_icreds = (!isProxy) ? request.Credentials : request.Proxy.Credentials;
116 NetworkCredential req_cred = (req_icreds != null) ? req_icreds.GetCredential (request.RequestUri, "NTLM") : null;
118 if (cnc_cred == null || req_cred == null ||
119 cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName ||
120 cnc_cred.Password != req_cred.Password) {
125 bool req_sharing = request.UnsafeAuthenticatedConnectionSharing;
126 bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing;
127 needs_reset = (req_sharing == false || req_sharing != cnc_sharing);
130 cnc.Close (false); // closes the authenticated connection
135 WebConnection CreateOrReuseConnection (HttpWebRequest request)
139 WeakReference cncRef;
141 int count = connections.Count;
142 for (int i = 0; i < count; i++) {
143 WeakReference wr = connections [i] as WeakReference;
144 cnc = wr.Target as WebConnection;
146 connections.RemoveAt (i);
155 PrepareSharingNtlm (cnc, request);
159 if (sPoint.ConnectionLimit > count) {
160 cnc = new WebConnection (this, sPoint);
161 connections.Add (new WeakReference (cnc));
168 int idx = (count > 1) ? rnd.Next (0, count) : 0;
169 cncRef = (WeakReference) connections [idx];
170 cnc = cncRef.Target as WebConnection;
172 cnc = new WebConnection (this, sPoint);
173 connections.RemoveAt (idx);
174 connections.Add (new WeakReference (cnc));
183 internal Queue Queue {
184 get { return queue; }