39253cbad22a8cb2e1880edc5eb6a97a2ece66ee
[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                         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) {
116                                 needs_reset = true;
117                         }
118
119                         if (!needs_reset) {
120                                 bool req_sharing = request.UnsafeAuthenticatedConnectionSharing;
121                                 bool cnc_sharing = cnc.UnsafeAuthenticatedConnectionSharing;
122                                 needs_reset = (req_sharing == false || req_sharing != cnc_sharing);
123                         }
124                         if (needs_reset) {
125                                 cnc.Close (false); // closes the authenticated connection
126                                 cnc.ResetNtlm ();
127                         }
128                 }
129
130                 WebConnection CreateOrReuseConnection (HttpWebRequest request)
131                 {
132                         // lock is up there.
133                         WebConnection cnc;
134                         WeakReference cncRef;
135
136                         int count = connections.Count;
137                         for (int i = 0; i < count; i++) {
138                                 WeakReference wr = connections [i] as WeakReference;
139                                 cnc = wr.Target as WebConnection;
140                                 if (cnc == null) {
141                                         connections.RemoveAt (i);
142                                         count--;
143                                         i--;
144                                         continue;
145                                 }
146
147                                 if (cnc.Busy)
148                                         continue;
149
150                                 PrepareSharingNtlm (cnc, request);
151                                 return cnc;
152                         }
153
154                         if (sPoint.ConnectionLimit > count) {
155                                 cnc = new WebConnection (this, sPoint);
156                                 connections.Add (new WeakReference (cnc));
157                                 return cnc;
158                         }
159
160                         if (rnd == null)
161                                 rnd = new Random ();
162
163                         int idx = (count > 1) ? rnd.Next (0, count) : 0;
164                         cncRef = (WeakReference) connections [idx];
165                         cnc = cncRef.Target as WebConnection;
166                         if (cnc == null) {
167                                 cnc = new WebConnection (this, sPoint);
168                                 connections.RemoveAt (idx);
169                                 connections.Add (new WeakReference (cnc));
170                         }
171                         return cnc;
172                 }
173
174                 public string Name {
175                         get { return name; }
176                 }
177
178                 internal Queue Queue {
179                         get { return queue; }
180                 }
181                 
182         }
183 }
184