Esempio n. 1
0
        private static bool RemoveSpecial(IList <ListenerPrefix> coll, ListenerPrefix prefix)
        {
            if (coll == null)
            {
                return(false);
            }

            var c = coll.Count;

            for (var i = 0; i < c; i++)
            {
                if (coll[i].Path != prefix.Path)
                {
                    continue;
                }

                coll.RemoveAt(i);
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        private static HttpListener MatchFromList(string path, List <ListenerPrefix> list, out ListenerPrefix prefix)
        {
            prefix = null;
            if (list == null)
            {
                return(null);
            }

            HttpListener bestMatch  = null;
            var          bestLength = -1;

            foreach (var p in list)
            {
                if (p.Path.Length < bestLength || !path.StartsWith(p.Path))
                {
                    continue;
                }

                bestLength = p.Path.Length;
                bestMatch  = p.Listener;
                prefix     = p;
            }

            return(bestMatch);
        }
Esempio n. 3
0
        private HttpListener SearchListener(Uri uri, out ListenerPrefix prefix)
        {
            prefix = null;
            if (uri == null)
            {
                return(null);
            }

            var host      = uri.Host;
            var port      = uri.Port;
            var path      = WebUtility.UrlDecode(uri.AbsolutePath);
            var pathSlash = path[path.Length - 1] == '/' ? path : path + "/";

            HttpListener bestMatch  = null;
            var          bestLength = -1;

            if (!string.IsNullOrEmpty(host))
            {
                var result = _prefixes;

                foreach (var p in result.Keys)
                {
                    if (p.Path.Length < bestLength)
                    {
                        continue;
                    }

                    if (p.Host != host || p.Port != port)
                    {
                        continue;
                    }

                    if (!path.StartsWith(p.Path) && !pathSlash.StartsWith(p.Path))
                    {
                        continue;
                    }

                    bestLength = p.Path.Length;
                    bestMatch  = result[p];
                    prefix     = p;
                }

                if (bestLength != -1)
                {
                    return(bestMatch);
                }
            }

            var list = _unhandled;

            bestMatch = MatchFromList(path, list, out prefix);
            if (path != pathSlash && bestMatch == null)
            {
                bestMatch = MatchFromList(pathSlash, list, out prefix);
            }
            if (bestMatch != null)
            {
                return(bestMatch);
            }

            list      = _all;
            bestMatch = MatchFromList(path, list, out prefix);
            if (path != pathSlash && bestMatch == null)
            {
                bestMatch = MatchFromList(pathSlash, list, out prefix);
            }

            return(bestMatch);
        }
Esempio n. 4
0
        public void RemovePrefix(ListenerPrefix prefix)
        {
            List <ListenerPrefix>?current;
            List <ListenerPrefix> future;

            if (prefix.Host == "*")
            {
                do
                {
                    current = _unhandled;
                    future  = current?.ToList() ?? new List <ListenerPrefix>();
                    if (!RemoveSpecial(future, prefix))
                    {
                        break; // Prefix not found
                    }
                }while (Interlocked.CompareExchange(ref _unhandled, future, current) != current);

                CheckIfRemove();
                return;
            }

            if (prefix.Host == "+")
            {
                do
                {
                    current = _all;
                    future  = current?.ToList() ?? new List <ListenerPrefix>();
                    if (!RemoveSpecial(future, prefix))
                    {
                        break; // Prefix not found
                    }
                }while (Interlocked.CompareExchange(ref _all, future, current) != current);

                CheckIfRemove();
                return;
            }

            Dictionary <ListenerPrefix, HttpListener> prefs, p2;

            do
            {
                prefs = _prefixes;
                ListenerPrefix lpKey = null;
                foreach (var p in _prefixes.Keys)
                {
                    if (p.Path == prefix.Path)
                    {
                        lpKey = p;
                        break;
                    }
                }

                if (lpKey is null)
                {
                    break;
                }

                p2 = prefs.ToDictionary(x => x.Key, x => x.Value);
                p2.Remove(lpKey);
            }while (Interlocked.CompareExchange(ref _prefixes, p2, prefs) != prefs);

            CheckIfRemove();
        }