* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / System.Net / EndPointListener.cs
1 //
2 // System.Net.EndPointListener
3 //
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if NET_2_0
29 using System.IO;
30 using System.Net.Sockets;
31 using System.Collections.Generic;
32 namespace System.Net {
33         sealed class EndPointListener
34         {
35                 IPEndPoint endpoint;
36                 Socket sock;
37                 Dictionary<ListenerPrefix, HttpListener> prefixes;
38                 List<ListenerPrefix> unhandled; // host = '*'
39                 List<ListenerPrefix> all; // host = '+'
40                 bool secure; // Can a port have listeners for secure and not secure at the same time?
41
42                 public EndPointListener (IPAddress addr, int port, bool secure)
43                 {
44                         endpoint = new IPEndPoint (addr, port);
45                         sock = new Socket (addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
46                         sock.Bind (endpoint);
47                         sock.Listen (500);
48                         sock.BeginAccept (OnAccept, this);
49                         prefixes = new Dictionary<ListenerPrefix, HttpListener> ();
50                         this.secure = secure;
51                 }
52
53                 static void OnAccept (IAsyncResult ares)
54                 {
55                         EndPointListener epl = (EndPointListener) ares.AsyncState;
56                         Socket accepted = null;
57                         try {
58                                 accepted = epl.sock.EndAccept (ares);
59                         } catch {
60                                 // Anything to do here?
61                         } finally {
62                                 epl.sock.BeginAccept (OnAccept, epl);
63                         }
64
65                         HttpConnection conn = new HttpConnection (accepted, epl, epl.secure);
66                         conn.BeginReadRequest ();
67                 }
68
69                 public bool BindContext (HttpListenerContext context)
70                 {
71                         HttpListenerRequest req = context.Request;
72                         ListenerPrefix prefix;
73                         HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
74                         if (listener == null)
75                                 return false;
76
77                         context.Listener = listener;
78                         context.Connection.Prefix = prefix;
79                         listener.RegisterContext (context);
80                         return true;
81                 }
82
83                 public void UnbindContext (HttpListenerContext context)
84                 {
85                         if (context == null || context.Request == null)
86                                 return;
87
88                         HttpListenerRequest req = context.Request;
89                         ListenerPrefix prefix;
90                         HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
91                         if (listener != null)
92                                 listener.UnregisterContext (context);
93                 }
94
95                 HttpListener SearchListener (string host, string raw_url, out ListenerPrefix prefix)
96                 {
97                         prefix = null;
98                         if (raw_url == null)
99                                 return null;
100
101                         //TODO: We should use a ReaderWriterLock between this and the add/remove operations.
102                         if (host != null) {
103                                 int colon = host.IndexOf (':');
104                                 if (colon >= 0)
105                                         host = host.Substring (0, colon);
106                         }
107
108                         string path = HttpUtility.UrlDecode (raw_url);
109                         HttpListener best_match = null;
110                         int best_length = -1;
111
112                         lock (prefixes) {
113                                 if (host != null && host != "") {
114                                         foreach (ListenerPrefix p in prefixes.Keys) {
115                                                 string ppath = p.Path;
116                                                 if (ppath.Length < best_length)
117                                                         continue;
118
119                                                 if (p.Host == host && path.StartsWith (ppath)) {
120                                                         best_length = ppath.Length;
121                                                         best_match = prefixes [p];
122                                                         prefix = p;
123                                                 }
124                                         }
125                                         if (best_length != -1)
126                                                 return best_match;
127                                 }
128
129                                 best_match = MatchFromList (host, path, unhandled, out prefix);
130                                 if (best_match != null)
131                                         return best_match;
132
133                                 best_match = MatchFromList (host, path, all, out prefix);
134                                 if (best_match != null)
135                                         return best_match;
136                         }
137                         return null;
138                 }
139
140                 HttpListener MatchFromList (string host, string path, List<ListenerPrefix> list, out ListenerPrefix prefix)
141                 {
142                         prefix = null;
143                         if (list == null)
144                                 return null;
145
146                         HttpListener best_match = null;
147                         int best_length = -1;
148                         
149                         foreach (ListenerPrefix p in list) {
150                                 string ppath = p.Path;
151                                 if (ppath.Length < best_length)
152                                         continue;
153
154                                 if (path.StartsWith (ppath)) {
155                                         best_length = ppath.Length;
156                                         best_match = p.Listener;
157                                         prefix = p;
158                                 }
159                         }
160
161                         return best_match;
162                 }
163
164                 void AddSpecial (List<ListenerPrefix> coll, ListenerPrefix prefix)
165                 {
166                         if (coll == null)
167                                 return;
168
169                         foreach (ListenerPrefix p in coll) {
170                                 if (p.Path == prefix.Path) //TODO: code
171                                         throw new HttpListenerException (400, "Prefix already in use.");
172                         }
173
174                         coll.Add (prefix);
175                 }
176
177                 void RemoveSpecial (List<ListenerPrefix> coll, ListenerPrefix prefix)
178                 {
179                         if (coll == null)
180                                 return;
181
182                         int c = coll.Count;
183                         for (int i = 0; i < c; i++) {
184                                 ListenerPrefix p = coll [i];
185                                 if (p.Path == prefix.Path) {
186                                         coll.RemoveAt (i);
187                                         CheckIfRemove ();
188                                         return;
189                                 }
190                         }
191                 }
192
193                 void CheckIfRemove ()
194                 {
195                         if (prefixes.Count > 0)
196                                 return;
197
198                         if (unhandled != null && unhandled.Count > 0)
199                                 return;
200
201                         if (all != null && all.Count > 0)
202                                 return;
203
204                         EndPointManager.RemoveEndPoint (this, endpoint);
205                 }
206
207                 public void Close ()
208                 {
209                         sock.Close ();
210                 }
211
212                 public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
213                 {
214                         lock (prefixes) {
215                                 if (prefix.Host == "*") {
216                                         if (unhandled == null)
217                                                 unhandled = new List<ListenerPrefix> ();
218
219                                         prefix.Listener = listener;
220                                         AddSpecial (unhandled, prefix);
221                                         return;
222                                 }
223
224                                 if (prefix.Host == "+") {
225                                         if (all == null)
226                                                 all = new List<ListenerPrefix> ();
227                                         prefix.Listener = listener;
228                                         AddSpecial (all, prefix);
229                                         return;
230                                 }
231
232                                 if (prefixes.ContainsKey (prefix)) {
233                                         HttpListener other = prefixes [prefix];
234                                         if (other != listener) // TODO: code.
235                                                 throw new HttpListenerException (400, "There's another listener for " + prefix);
236                                         return;
237                                 }
238
239                                 prefixes [prefix] = listener;
240                         }
241                 }
242
243                 public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
244                 {
245                         lock (prefixes) {
246                                 if (prefix.Host == "*") {
247                                         RemoveSpecial (unhandled, prefix);
248                                         return;
249                                 }
250
251                                 if (prefix.Host == "+") {
252                                         RemoveSpecial (all, prefix);
253                                         return;
254                                 }
255
256                                 if (prefixes.ContainsKey (prefix)) {
257                                         prefixes.Remove (prefix);
258                                 }
259                         }
260                 }
261         }
262 }
263 #endif
264