Пример #1
0
        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);

                leaveIfNoPrefix();
                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);

                leaveIfNoPrefix();
                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);

            leaveIfNoPrefix();
        }
Пример #2
0
        internal bool TrySearchHttpListener(Uri uri, out HttpListener listener)
        {
            listener = null;

            if (uri == null)
            {
                return(false);
            }

            var host      = uri.Host;
            var dns       = Uri.CheckHostName(host) == UriHostNameType.Dns;
            var port      = uri.Port.ToString();
            var path      = HttpUtility.UrlDecode(uri.AbsolutePath);
            var pathSlash = path[path.Length - 1] != '/' ? path + "/" : path;

            if (host != null && host.Length > 0)
            {
                var bestLen = -1;
                foreach (var pref in _prefixes.Keys)
                {
                    if (dns)
                    {
                        var prefHost = pref.Host;
                        if (Uri.CheckHostName(prefHost) == UriHostNameType.Dns && prefHost != host)
                        {
                            continue;
                        }
                    }

                    if (pref.Port != port)
                    {
                        continue;
                    }

                    var prefPath = pref.Path;

                    var len = prefPath.Length;
                    if (len < bestLen)
                    {
                        continue;
                    }

                    if (path.StartsWith(prefPath) || pathSlash.StartsWith(prefPath))
                    {
                        bestLen  = len;
                        listener = _prefixes[pref];
                    }
                }

                if (bestLen != -1)
                {
                    return(true);
                }
            }

            var prefs = _unhandled;

            listener = searchHttpListenerFromSpecial(path, prefs);
            if (listener == null && pathSlash != path)
            {
                listener = searchHttpListenerFromSpecial(pathSlash, prefs);
            }

            if (listener != null)
            {
                return(true);
            }

            prefs    = _all;
            listener = searchHttpListenerFromSpecial(path, prefs);
            if (listener == null && pathSlash != path)
            {
                listener = searchHttpListenerFromSpecial(pathSlash, prefs);
            }

            return(listener != null);
        }