Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / class / System / System.Net / WebConnectionGroup.cs
1 //
2 // System.Net.WebConnectionGroup
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Net.Configuration;
35 using System.Net.Sockets;
36
37 namespace System.Net
38 {
39         class WebConnectionGroup
40         {
41                 ServicePoint sPoint;
42                 string name;
43                 ArrayList connections;
44                 Random rnd;
45                 Queue queue;
46
47                 public WebConnectionGroup (ServicePoint sPoint, string name)
48                 {
49                         this.sPoint = sPoint;
50                         this.name = name;
51                         connections = new ArrayList (1);
52                         queue = new Queue ();
53                 }
54
55                 public void Close ()
56                 {
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
59                         lock (connections) {
60                                 WeakReference cncRef = null;
61
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;
67                                         if (cnc != null) {
68                                                 cnc.Close (false);
69                                         }
70                                 }
71                                 connections.Clear ();
72                         }
73                 }
74
75                 public WebConnection GetConnection (HttpWebRequest request)
76                 {
77                         WebConnection cnc = null;
78                         lock (connections) {
79                                 WeakReference cncRef = null;
80
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;
87                                         if (cnc == null) {
88                                                 if (removed == null)
89                                                         removed = new ArrayList (1);
90
91                                                 removed.Add (i);
92                                         }
93                                 }
94
95                                 if (removed != null) {
96                                         for (int i = removed.Count - 1; i >= 0; i--)
97                                                 connections.RemoveAt ((int) removed [i]);
98                                 }
99
100                                 cnc = CreateOrReuseConnection (request);
101                         }
102
103                         return cnc;
104                 }
105
106                 static void PrepareSharingNtlm (WebConnection cnc, HttpWebRequest request)
107                 {
108                         if (!cnc.NtlmAuthenticated)
109                                 return;
110
111                         bool needs_reset = false;
112                         NetworkCredential cnc_cred = cnc.NtlmCredential;
113
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;
117
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) {
121                                 needs_reset = true;
122                         }
123
124                         if (!needs_reset) {
125                                 bool req_sharing = request.UnsafeAuthenticatedConnectionSharing;
126                                 bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing;
127                                 needs_reset = (req_sharing == false || req_sharing != cnc_sharing);
128                         }
129                         if (needs_reset) {
130                                 cnc.Close (false); // closes the authenticated connection
131                                 cnc.ResetNtlm ();
132                         }
133                 }
134
135                 WebConnection CreateOrReuseConnection (HttpWebRequest request)
136                 {
137                         // lock is up there.
138                         WebConnection cnc;
139                         WeakReference cncRef;
140
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;
145                                 if (cnc == null) {
146                                         connections.RemoveAt (i);
147                                         count--;
148                                         i--;
149                                         continue;
150                                 }
151
152                                 if (cnc.Busy)
153                                         continue;
154
155                                 PrepareSharingNtlm (cnc, request);
156                                 return cnc;
157                         }
158
159                         if (sPoint.ConnectionLimit > count) {
160                                 cnc = new WebConnection (this, sPoint);
161                                 connections.Add (new WeakReference (cnc));
162                                 return cnc;
163                         }
164
165                         if (rnd == null)
166                                 rnd = new Random ();
167
168                         int idx = (count > 1) ? rnd.Next (0, count) : 0;
169                         cncRef = (WeakReference) connections [idx];
170                         cnc = cncRef.Target as WebConnection;
171                         if (cnc == null) {
172                                 cnc = new WebConnection (this, sPoint);
173                                 connections.RemoveAt (idx);
174                                 connections.Add (new WeakReference (cnc));
175                         }
176                         return cnc;
177                 }
178
179                 public string Name {
180                         get { return name; }
181                 }
182
183                 internal Queue Queue {
184                         get { return queue; }
185                 }
186                 
187         }
188 }
189