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;
113 NetworkCredential req_cred = request.Credentials.GetCredential (request.RequestUri, "NTLM");
114 if (cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName ||
115 cnc_cred.Password != req_cred.Password) {
120 bool req_sharing = request.UnsafeAuthenticatedConnectionSharing;
121 bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing;
122 needs_reset = (req_sharing == false || req_sharing != cnc_sharing);
126 cnc.Close (false); // closes the authenticated connection
131 WebConnection CreateOrReuseConnection (HttpWebRequest request)
135 WeakReference cncRef;
137 int count = connections.Count;
138 for (int i = 0; i < count; i++) {
139 WeakReference wr = connections [i] as WeakReference;
140 cnc = wr.Target as WebConnection;
142 connections.RemoveAt (i);
151 PrepareSharingNtlm (cnc, request);
155 if (sPoint.ConnectionLimit > count) {
156 cnc = new WebConnection (this, sPoint);
157 connections.Add (new WeakReference (cnc));
164 int idx = (count > 1) ? rnd.Next (0, count) : 0;
165 cncRef = (WeakReference) connections [idx];
166 cnc = cncRef.Target as WebConnection;
168 cnc = new WebConnection (this, sPoint);
169 connections.RemoveAt (idx);
170 connections.Add (new WeakReference (cnc));
179 internal Queue Queue {
180 get { return queue; }