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

            HttpListener bestMatch  = null;
            int          bestLength = -1;

            foreach (ListenerPrefix p in list)
            {
                string ppath = p.Path;
                if (ppath.Length < bestLength)
                {
                    continue;
                }

                if (path.StartsWith(ppath))
                {
                    bestLength = ppath.Length;
                    bestMatch  = p.Listener;
                    prefix     = p;
                }
            }

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

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

            HttpListener bestMatch  = null;
            int          bestLength = -1;

            if (host != null && host != "")
            {
                var pRo = _prefixes;
                foreach (ListenerPrefix p in pRo.Keys)
                {
                    string ppath = p.Path;
                    if (ppath.Length < bestLength)
                    {
                        continue;
                    }

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

                    if (path.StartsWith(ppath) || pathSlash.StartsWith(ppath))
                    {
                        bestLength = ppath.Length;
                        bestMatch  = (HttpListener)pRo[p];
                        prefix     = p;
                    }
                }
                if (bestLength != -1)
                {
                    return(bestMatch);
                }
            }

            List <ListenerPrefix> 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);
        }