2 // System.Net.EndPointListener
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Net.Sockets;
31 using System.Collections.Generic;
32 namespace System.Net {
33 sealed class EndPointListener
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?
42 public EndPointListener (IPAddress addr, int port, bool secure)
44 endpoint = new IPEndPoint (addr, port);
45 sock = new Socket (addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
48 sock.BeginAccept (OnAccept, this);
49 prefixes = new Dictionary<ListenerPrefix, HttpListener> ();
53 static void OnAccept (IAsyncResult ares)
55 EndPointListener epl = (EndPointListener) ares.AsyncState;
56 Socket accepted = null;
58 accepted = epl.sock.EndAccept (ares);
60 // Anything to do here?
62 epl.sock.BeginAccept (OnAccept, epl);
65 HttpConnection conn = new HttpConnection (accepted, epl, epl.secure);
66 conn.BeginReadRequest ();
69 public bool BindContext (HttpListenerContext context)
71 HttpListenerRequest req = context.Request;
72 ListenerPrefix prefix;
73 HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
77 context.Listener = listener;
78 context.Connection.Prefix = prefix;
79 listener.RegisterContext (context);
83 public void UnbindContext (HttpListenerContext context)
85 if (context == null || context.Request == null)
88 HttpListenerRequest req = context.Request;
89 ListenerPrefix prefix;
90 HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
92 listener.UnregisterContext (context);
95 HttpListener SearchListener (string host, string raw_url, out ListenerPrefix prefix)
101 //TODO: We should use a ReaderWriterLock between this and the add/remove operations.
103 int colon = host.IndexOf (':');
105 host = host.Substring (0, colon);
108 string path = HttpUtility.UrlDecode (raw_url);
109 HttpListener best_match = null;
110 int best_length = -1;
113 if (host != null && host != "") {
114 foreach (ListenerPrefix p in prefixes.Keys) {
115 string ppath = p.Path;
116 if (ppath.Length < best_length)
119 if (p.Host == host && path.StartsWith (ppath)) {
120 best_length = ppath.Length;
121 best_match = prefixes [p];
125 if (best_length != -1)
129 best_match = MatchFromList (host, path, unhandled, out prefix);
130 if (best_match != null)
133 best_match = MatchFromList (host, path, all, out prefix);
134 if (best_match != null)
140 HttpListener MatchFromList (string host, string path, List<ListenerPrefix> list, out ListenerPrefix prefix)
146 HttpListener best_match = null;
147 int best_length = -1;
149 foreach (ListenerPrefix p in list) {
150 string ppath = p.Path;
151 if (ppath.Length < best_length)
154 if (path.StartsWith (ppath)) {
155 best_length = ppath.Length;
156 best_match = p.Listener;
164 void AddSpecial (List<ListenerPrefix> coll, ListenerPrefix prefix)
169 foreach (ListenerPrefix p in coll) {
170 if (p.Path == prefix.Path) //TODO: code
171 throw new HttpListenerException (400, "Prefix already in use.");
177 void RemoveSpecial (List<ListenerPrefix> coll, ListenerPrefix prefix)
183 for (int i = 0; i < c; i++) {
184 ListenerPrefix p = coll [i];
185 if (p.Path == prefix.Path) {
193 void CheckIfRemove ()
195 if (prefixes.Count > 0)
198 if (unhandled != null && unhandled.Count > 0)
201 if (all != null && all.Count > 0)
204 EndPointManager.RemoveEndPoint (this, endpoint);
212 public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
215 if (prefix.Host == "*") {
216 if (unhandled == null)
217 unhandled = new List<ListenerPrefix> ();
219 prefix.Listener = listener;
220 AddSpecial (unhandled, prefix);
224 if (prefix.Host == "+") {
226 all = new List<ListenerPrefix> ();
227 prefix.Listener = listener;
228 AddSpecial (all, prefix);
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);
239 prefixes [prefix] = listener;
243 public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
246 if (prefix.Host == "*") {
247 RemoveSpecial (unhandled, prefix);
251 if (prefix.Host == "+") {
252 RemoveSpecial (all, prefix);
256 if (prefixes.ContainsKey (prefix)) {
257 prefixes.Remove (prefix);