private HttpListener searchListener(Uri uri, out HttpListenerPrefix prefix) { prefix = null; if (uri == null) { return(null); } var host = uri.Host; var dns = Uri.CheckHostName(host) == UriHostNameType.Dns; var port = uri.Port; var path = HttpUtility.UrlDecode(uri.AbsolutePath); var pathSlash = path[path.Length - 1] == '/' ? path : path + "/"; HttpListener bestMatch = null; var bestLen = -1; if (host != null && host.Length > 0) { foreach (var pref in _prefixes.Keys) { var ppath = pref.Path; if (ppath.Length < bestLen) { continue; } if (pref.Port != port) { continue; } if (dns) { var phost = pref.Host; if (Uri.CheckHostName(phost) == UriHostNameType.Dns && phost != host) { continue; } } if (path.StartsWith(ppath) || pathSlash.StartsWith(ppath)) { bestLen = ppath.Length; bestMatch = _prefixes[pref]; prefix = pref; } } if (bestLen != -1) { return(bestMatch); } } var list = _unhandled; bestMatch = matchFromList(host, path, list, out prefix); if (path != pathSlash && bestMatch == null) { bestMatch = matchFromList(host, pathSlash, list, out prefix); } if (bestMatch != null) { return(bestMatch); } list = _all; bestMatch = matchFromList(host, path, list, out prefix); if (path != pathSlash && bestMatch == null) { bestMatch = matchFromList(host, pathSlash, list, out prefix); } if (bestMatch != null) { return(bestMatch); } return(null); }
public void RemovePrefix(HttpListenerPrefix prefix, HttpListener listener) { List <HttpListenerPrefix> current, future; if (prefix.Host == "*") { do { current = _unhandled; if (current == null) { break; } future = new List <HttpListenerPrefix> (current); if (!removeSpecial(future, prefix)) { break; // The prefix wasn't found. } }while (Interlocked.CompareExchange(ref _unhandled, future, current) != current); checkIfRemove(); return; } if (prefix.Host == "+") { do { current = _all; if (current == null) { break; } future = new List <HttpListenerPrefix> (current); if (!removeSpecial(future, prefix)) { break; // The prefix wasn't found. } }while (Interlocked.CompareExchange(ref _all, future, current) != current); checkIfRemove(); return; } Dictionary <HttpListenerPrefix, HttpListener> prefs, prefs2; do { prefs = _prefixes; if (!prefs.ContainsKey(prefix)) { break; } prefs2 = new Dictionary <HttpListenerPrefix, HttpListener> (prefs); prefs2.Remove(prefix); }while (Interlocked.CompareExchange(ref _prefixes, prefs2, prefs) != prefs); checkIfRemove(); }
internal HttpListenerPrefixCollection (HttpListener listener) { _listener = listener; _prefixes = new List<string> (); }