Exemplo n.º 1
0
        void RemoveSpecial(ArrayList coll, ListenerPrefix prefix)
        {
            if (coll == null)
            {
                return;
            }

            int c = coll.Count;

            for (int i = 0; i < c; i++)
            {
                ListenerPrefix p = (ListenerPrefix)coll [i];
                if (p.Path == prefix.Path)
                {
                    coll.RemoveAt(i);
                    CheckIfRemove();
                    return;
                }
            }
        }
Exemplo n.º 2
0
        public void RemovePrefix(ListenerPrefix prefix, HttpListener listener)
        {
            lock (prefixes) {
                if (prefix.Host == "*")
                {
                    RemoveSpecial(unhandled, prefix);
                    return;
                }

                if (prefix.Host == "+")
                {
                    RemoveSpecial(all, prefix);
                    return;
                }

                if (prefixes.ContainsKey(prefix))
                {
                    prefixes.Remove(prefix);
                }
            }
        }
Exemplo n.º 3
0
        public void AddPrefix(ListenerPrefix prefix, HttpListener listener)
        {
            lock (prefixes) {
                if (prefix.Host == "*")
                {
                    if (unhandled == null)
                    {
                        unhandled = new ArrayList();
                    }

                    prefix.Listener = listener;
                    AddSpecial(unhandled, prefix);
                    return;
                }

                if (prefix.Host == "+")
                {
                    if (all == null)
                    {
                        all = new ArrayList();
                    }
                    prefix.Listener = listener;
                    AddSpecial(all, prefix);
                    return;
                }

                if (prefixes.ContainsKey(prefix))
                {
                    HttpListener other = (HttpListener)prefixes [prefix];
                    if (other != listener)                     // TODO: code.
                    {
                        throw new HttpListenerException(400, "There's another listener for " + prefix);
                    }
                    return;
                }

                prefixes [prefix] = listener;
            }
        }
Exemplo n.º 4
0
        HttpListener SearchListener(string host, string raw_url, out ListenerPrefix prefix)
        {
            prefix = null;
            if (raw_url == null)
            {
                return(null);
            }

            //TODO: We should use a ReaderWriterLock between this and the add/remove operations.
            if (host != null)
            {
                int colon = host.IndexOf(':');
                if (colon >= 0)
                {
                    host = host.Substring(0, colon);
                }
            }

            string path;
            Uri    raw_uri;

            if (UriHelper.MaybeUri(raw_url) && Uri.TryCreate(raw_url, UriKind.Absolute, out raw_uri))
            {
                path = raw_uri.PathAndQuery;
            }
            else
            {
                path = HttpUtility.UrlDecode(raw_url);
            }

            string path_slash = path [path.Length - 1] == '/' ? path : path + "/";

            HttpListener best_match  = null;
            int          best_length = -1;

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

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

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

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