public void RemovePrefix(ListenerPrefix prefix, HttpListener listener) { lock (prefixes) { if (prefix.Host == "*") { RemoveSpecial(unhandled, prefix); return; } if (prefix.Host == "+") { RemoveSpecial(all, prefix); return; } if (prefixes.ContainsKey(prefix)) { prefixes.Remove(prefix); } } }
public void AddPrefix(ListenerPrefix prefix, HttpListener listener) { lock (prefixes) { if (prefix.Host == "*") { if (unhandled == null) { unhandled = new ArrayList(); } prefix.Listener = listener; AddSpecial(unhandled, prefix); return; } if (prefix.Host == "+") { if (all == null) { all = new ArrayList(); } prefix.Listener = listener; AddSpecial(all, prefix); return; } if (prefixes.ContainsKey(prefix)) { HttpListener other = (HttpListener)prefixes [prefix]; if (other != listener) // TODO: code. { throw new HttpListenerException(400, "There's another listener for " + prefix); } return; } prefixes [prefix] = listener; } }
HttpListener SearchListener(string host, string raw_url, out ListenerPrefix prefix) { prefix = null; if (raw_url == null) { return(null); } //TODO: We should use a ReaderWriterLock between this and the add/remove operations. if (host != null) { int colon = host.IndexOf(':'); if (colon >= 0) { host = host.Substring(0, colon); } } string path; Uri raw_uri; #if NET_2_0 if (MonoHttp.Utility.MaybeUri(raw_url) && Uri.TryCreate(raw_url, UriKind.Absolute, out raw_uri)) #else try { raw_uri = new Uri(raw_url); } catch { raw_uri = null; } if (raw_uri != null) #endif { path = raw_uri.PathAndQuery; } else { path = HttpUtility.UrlDecode(raw_url); } string path_slash = path [path.Length - 1] == '/' ? path : path + "/"; HttpListener best_match = null; int best_length = -1; lock (prefixes) { if (host != null && host != "") { foreach (ListenerPrefix p in prefixes.Keys) { string ppath = p.Path; if (ppath.Length < best_length) { continue; } if (p.Host == host && (path.StartsWith(ppath) || path_slash.StartsWith(ppath))) { best_length = ppath.Length; best_match = (HttpListener)prefixes [p]; prefix = p; } } if (best_length != -1) { return(best_match); } } best_match = MatchFromList(host, path, unhandled, out prefix); if (best_match != null) { return(best_match); } best_match = MatchFromList(host, path, all, out prefix); if (best_match != null) { return(best_match); } } return(null); }