Exemplo n.º 1
0
        private bool RemoveSpecial(List <ListenerPrefix> list, ListenerPrefix prefix)
        {
            if (list == null)
            {
                return(false);
            }

            int c = list.Count;

            for (int i = 0; i < c; i++)
            {
                ListenerPrefix p = list[i];
                if (p.Path == prefix.Path)
                {
                    list.RemoveAt(i);
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        bool RemoveSpecial(List <ListenerPrefix> coll, ListenerPrefix prefix)
        {
            if (coll == null)
            {
                return(false);
            }

            int c = coll.Count;

            for (int i = 0; i < c; i++)
            {
                ListenerPrefix p = (ListenerPrefix)coll[i];
                if (p.Path == prefix.Path)
                {
                    coll.RemoveAt(i);
                    return(true);
                }
            }
            return(false);
        }
        private static void AddPrefixInternal(ILogger logger, string p, HttpListener listener)
        {
            int start = p.IndexOf(':') + 3;
            int colon = p.IndexOf(':', start);

            if (colon != -1)
            {
                // root can't be -1 here, since we've already checked for ending '/' in ListenerPrefix.
                int    root       = p.IndexOf('/', colon, p.Length - colon);
                string portString = p.Substring(colon + 1, root - colon - 1);

                int port;
                if (!int.TryParse(portString, out port) || port <= 0 || port >= 65536)
                {
                    throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_invalid_port");
                }
            }

            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Host != "*" && lp.Host != "+" && Uri.CheckHostName(lp.Host) == UriHostNameType.Unknown)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_listener_host");
            }

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_invalid_path");
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_invalid_path");
            }

            // listens on all the interfaces if host name cannot be parsed by IPAddress.
            HttpEndPointListener epl = GetEPListener(logger, lp.Host, lp.Port, listener, lp.Secure);

            epl.AddPrefix(lp, listener);
        }
Exemplo n.º 4
0
        HttpListener MatchFromList(string host, string path, List <ListenerPrefix> list, out ListenerPrefix prefix)
        {
            prefix = null;
            if (list == null)
            {
                return(null);
            }

            HttpListener best_match  = null;
            int          best_length = -1;

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

                if (path.StartsWith(ppath))
                {
                    best_length = ppath.Length;
                    best_match  = p.Listener;
                    prefix      = p;
                }
            }

            return(best_match);
        }
Exemplo n.º 5
0
        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 path_slash = path[path.Length - 1] == '/' ? path : path + "/";

            HttpListener best_match  = null;
            int          best_length = -1;

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

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

                    if (path.StartsWith(ppath) || path_slash.StartsWith(ppath))
                    {
                        best_length = ppath.Length;
                        best_match  = (HttpListener)p_ro[p];
                        prefix      = p;
                    }
                }
                if (best_length != -1)
                {
                    return(best_match);
                }
            }

            List <ListenerPrefix> list = unhandled;

            best_match = MatchFromList(host, path, list, out prefix);
            if (path != path_slash && best_match == null)
            {
                best_match = MatchFromList(host, path_slash, list, out prefix);
            }
            if (best_match != null)
            {
                return(best_match);
            }

            list       = all;
            best_match = MatchFromList(host, path, list, out prefix);
            if (path != path_slash && best_match == null)
            {
                best_match = MatchFromList(host, path_slash, list, out prefix);
            }
            if (best_match != null)
            {
                return(best_match);
            }

            return(null);
        }
Exemplo n.º 6
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 != "")
            {
                Dictionary <ListenerPrefix, HttpListener> localPrefixes = _prefixes;
                foreach (ListenerPrefix p in localPrefixes.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  = localPrefixes[p];
                        prefix     = p;
                    }
                }
                if (bestLength != -1)
                {
                    return(bestMatch);
                }
            }

            List <ListenerPrefix> list = _unhandledPrefixes;

            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      = _allPrefixes;
            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);
        }