コード例 #1
0
ファイル: HttpConnection.cs プロジェクト: zhouweiaccp/reactor
        void Init()
        {
            context_bound = false;

            i_stream = null;

            o_stream = null;

            prefix = null;

            chunked = false;

            ms = new MemoryStream();

            position = 0;

            input_state = InputState.RequestLine;

            line_state = LineState.None;

            context = new HttpListenerContext(this);
        }
コード例 #2
0
        bool RemoveSpecial(ArrayList 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);
        }
コード例 #3
0
        public void RemovePrefix(ListenerPrefix prefix, HttpListener listener)
        {
            ArrayList current;

            ArrayList future;

            if (prefix.Host == "*")
            {
                do
                {
                    current = unhandled;

                    future = (current != null) ? (ArrayList)current.Clone() : new ArrayList();

                    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 != null) ? (ArrayList)current.Clone() : new ArrayList();

                    if (!RemoveSpecial(future, prefix))
                    {
                        break; // Prefix not found
                    }
                } while (Interlocked.CompareExchange(ref all, future, current) != current);

                CheckIfRemove();

                return;
            }

            Hashtable prefs, p2;

            do
            {
                prefs = prefixes;

                if (!prefs.ContainsKey(prefix))
                {
                    break;
                }

                p2 = (Hashtable)prefs.Clone();

                p2.Remove(prefix);
            } while (Interlocked.CompareExchange(ref prefixes, p2, prefs) != prefs);

            CheckIfRemove();
        }
コード例 #4
0
        public void AddPrefix(ListenerPrefix prefix, HttpListener listener)
        {
            ArrayList current;

            ArrayList future;

            if (prefix.Host == "*")
            {
                do
                {
                    current = unhandled;

                    future = (current != null) ? (ArrayList)current.Clone() : new ArrayList();

                    prefix.Listener = listener;

                    AddSpecial(future, prefix);
                } while (Interlocked.CompareExchange(ref unhandled, future, current) != current);

                return;
            }

            if (prefix.Host == "+")
            {
                do
                {
                    current = all;

                    future = (current != null) ? (ArrayList)current.Clone() : new ArrayList();

                    prefix.Listener = listener;

                    AddSpecial(future, prefix);
                } while (Interlocked.CompareExchange(ref all, future, current) != current);

                return;
            }

            Hashtable prefs, p2;

            do
            {
                prefs = prefixes;

                if (prefs.ContainsKey(prefix))
                {
                    HttpListener other = (HttpListener)prefs[prefix];

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

                    return;
                }

                p2 = (Hashtable)prefs.Clone();

                p2[prefix] = listener;
            } while (Interlocked.CompareExchange(ref prefixes, p2, prefs) != prefs);
        }
コード例 #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 = HttpUtility.UrlDecode(uri.AbsolutePath);

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

            HttpListener best_match = null;

            int best_length = -1;

            if (host != null && host != "")
            {
                Hashtable 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);
                }
            }

            ArrayList 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);
        }