2 // System.Net.EndPointListener
5 // Gonzalo Paniagua Javier (gonzalo.mono@gmail.com)
7 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
8 // Copyright (c) 2012 Xamarin, Inc. (http://xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #if MONO_SECURITY_ALIAS
33 extern alias MonoSecurity;
34 using MonoSecurity::Mono.Security.Authenticode;
36 using Mono.Security.Authenticode;
40 using System.Net.Sockets;
41 using System.Collections;
42 using System.Collections.Generic;
43 using System.Security.Cryptography;
44 using System.Security.Cryptography.X509Certificates;
45 using System.Threading;
47 namespace System.Net {
48 sealed class EndPointListener
50 HttpListener listener;
53 Hashtable prefixes; // Dictionary <ListenerPrefix, HttpListener>
54 ArrayList unhandled; // List<ListenerPrefix> unhandled; host = '*'
55 ArrayList all; // List<ListenerPrefix> all; host = '+'
58 Dictionary<HttpConnection, HttpConnection> unregistered;
60 public EndPointListener (HttpListener listener, IPAddress addr, int port, bool secure)
62 this.listener = listener;
66 cert = listener.LoadCertificateAndKey (addr, port);
69 endpoint = new IPEndPoint (addr, port);
70 sock = new Socket (addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
73 SocketAsyncEventArgs args = new SocketAsyncEventArgs ();
74 args.UserToken = this;
75 args.Completed += OnAccept;
76 sock.AcceptAsync (args);
77 prefixes = new Hashtable ();
78 unregistered = new Dictionary<HttpConnection, HttpConnection> ();
81 internal HttpListener Listener {
82 get { return listener; }
85 static void OnAccept (object sender, EventArgs e)
87 SocketAsyncEventArgs args = (SocketAsyncEventArgs) e;
88 EndPointListener epl = (EndPointListener) args.UserToken;
89 Socket accepted = null;
90 if (args.SocketError == SocketError.Success) {
91 accepted = args.AcceptSocket;
92 args.AcceptSocket = null;
97 epl.sock.AcceptAsync (args);
99 if (accepted != null) {
107 if (accepted == null)
110 if (epl.secure && epl.cert == null) {
114 HttpConnection conn = new HttpConnection (accepted, epl, epl.secure, epl.cert);
115 lock (epl.unregistered) {
116 epl.unregistered [conn] = conn;
118 conn.BeginReadRequest ();
121 internal void RemoveConnection (HttpConnection conn)
123 lock (unregistered) {
124 unregistered.Remove (conn);
128 public bool BindContext (HttpListenerContext context)
130 HttpListenerRequest req = context.Request;
131 ListenerPrefix prefix;
132 HttpListener listener = SearchListener (req.Url, out prefix);
133 if (listener == null)
136 context.Listener = listener;
137 context.Connection.Prefix = prefix;
141 public void UnbindContext (HttpListenerContext context)
143 if (context == null || context.Request == null)
146 context.Listener.UnregisterContext (context);
149 HttpListener SearchListener (Uri uri, out ListenerPrefix prefix)
155 string host = uri.Host;
157 string path = WebUtility.UrlDecode (uri.AbsolutePath);
158 string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
160 HttpListener best_match = null;
161 int best_length = -1;
163 if (host != null && host != "") {
164 Hashtable p_ro = prefixes;
165 foreach (ListenerPrefix p in p_ro.Keys) {
166 string ppath = p.Path;
167 if (ppath.Length < best_length)
170 if (p.Host != host || p.Port != port)
173 if (path.StartsWith (ppath) || path_slash.StartsWith (ppath)) {
174 best_length = ppath.Length;
175 best_match = (HttpListener) p_ro [p];
179 if (best_length != -1)
183 ArrayList list = unhandled;
184 best_match = MatchFromList (host, path, list, out prefix);
185 if (path != path_slash && best_match == null)
186 best_match = MatchFromList (host, path_slash, list, out prefix);
187 if (best_match != null)
191 best_match = MatchFromList (host, path, list, out prefix);
192 if (path != path_slash && best_match == null)
193 best_match = MatchFromList (host, path_slash, list, out prefix);
194 if (best_match != null)
200 HttpListener MatchFromList (string host, string path, ArrayList list, out ListenerPrefix prefix)
206 HttpListener best_match = null;
207 int best_length = -1;
209 foreach (ListenerPrefix p in list) {
210 string ppath = p.Path;
211 if (ppath.Length < best_length)
214 if (path.StartsWith (ppath)) {
215 best_length = ppath.Length;
216 best_match = p.Listener;
224 void AddSpecial (ArrayList coll, ListenerPrefix prefix)
229 foreach (ListenerPrefix p in coll) {
230 if (p.Path == prefix.Path) //TODO: code
231 throw new HttpListenerException (400, "Prefix already in use.");
236 bool RemoveSpecial (ArrayList coll, ListenerPrefix prefix)
242 for (int i = 0; i < c; i++) {
243 ListenerPrefix p = (ListenerPrefix) coll [i];
244 if (p.Path == prefix.Path) {
252 void CheckIfRemove ()
254 if (prefixes.Count > 0)
257 ArrayList list = unhandled;
258 if (list != null && list.Count > 0)
262 if (list != null && list.Count > 0)
265 EndPointManager.RemoveEndPoint (this, endpoint);
271 lock (unregistered) {
273 // Clone the list because RemoveConnection can be called from Close
275 var connections = new List<HttpConnection> (unregistered.Keys);
277 foreach (HttpConnection c in connections)
279 unregistered.Clear ();
283 public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
287 if (prefix.Host == "*") {
290 future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
291 prefix.Listener = listener;
292 AddSpecial (future, prefix);
293 } while (Interlocked.CompareExchange (ref unhandled, future, current) != current);
297 if (prefix.Host == "+") {
300 future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
301 prefix.Listener = listener;
302 AddSpecial (future, prefix);
303 } while (Interlocked.CompareExchange (ref all, future, current) != current);
310 if (prefs.ContainsKey (prefix)) {
311 HttpListener other = (HttpListener) prefs [prefix];
312 if (other != listener) // TODO: code.
313 throw new HttpListenerException (400, "There's another listener for " + prefix);
316 p2 = (Hashtable) prefs.Clone ();
317 p2 [prefix] = listener;
318 } while (Interlocked.CompareExchange (ref prefixes, p2, prefs) != prefs);
321 public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
325 if (prefix.Host == "*") {
328 future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
329 if (!RemoveSpecial (future, prefix))
330 break; // Prefix not found
331 } while (Interlocked.CompareExchange (ref unhandled, future, current) != current);
336 if (prefix.Host == "+") {
339 future = (current != null) ? (ArrayList) current.Clone () : new ArrayList ();
340 if (!RemoveSpecial (future, prefix))
341 break; // Prefix not found
342 } while (Interlocked.CompareExchange (ref all, future, current) != current);
350 if (!prefs.ContainsKey (prefix))
353 p2 = (Hashtable) prefs.Clone ();
355 } while (Interlocked.CompareExchange (ref prefixes, p2, prefs) != prefs);