예제 #1
0
        public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
        {
            this.sock = sock;

            this.epl = epl;

            this.secure = secure;

            this.key = key;

            var networkstream = new NetworkStream(sock, false);

            if (secure)
            {
                var sslstream = new System.Net.Security.SslStream(networkstream);

                sslstream.AuthenticateAsServer(cert);

                stream = sslstream;
            }
            else
            {
                stream = networkstream;
            }

            timer = new Timer(OnTimeout, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);

            if (buffer == null)
            {
                buffer = new byte[BufferSize];
            }

            Init();
        }
예제 #2
0
        static void OnAccept(object sender, EventArgs e)
        {
            SocketAsyncEventArgs args = (SocketAsyncEventArgs)e;

            EndPointListener epl = (EndPointListener)args.UserToken;

            Socket accepted = null;

            if (args.SocketError == SocketError.Success)
            {
                accepted = args.AcceptSocket;

                args.AcceptSocket = null;
            }

            try
            {
                if (epl.sock != null)
                {
                    epl.sock.AcceptAsync(args);
                }
            }
            catch
            {
                if (accepted != null)
                {
                    try
                    {
                        accepted.Close();
                    }
                    catch { }

                    accepted = null;
                }
            }

            if (accepted == null)
            {
                return;
            }

            if (epl.secure && (epl.cert == null || epl.key == null))
            {
                accepted.Close();

                return;
            }

            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert, epl.key);

            lock (epl.unregistered)
            {
                epl.unregistered[conn] = conn;
            }

            conn.BeginReadRequest();
        }
예제 #3
0
        static EndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;

            if (host == "*")
            {
                addr = IPAddress.Any;
            }
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try
                {
                    IPHostEntry iphost = System.Net.Dns.GetHostEntry(host);

                    if (iphost != null)
                    {
                        addr = iphost.AddressList[0];
                    }
                    else
                    {
                        addr = IPAddress.Any;
                    }
                }
                catch
                {
                    addr = IPAddress.Any;
                }
            }
            Hashtable p = null;  // Dictionary<int, EndPointListener>

            if (ip_to_endpoints.ContainsKey(addr))
            {
                p = (Hashtable)ip_to_endpoints[addr];
            }
            else
            {
                p = new Hashtable();

                ip_to_endpoints[addr] = p;
            }

            EndPointListener epl = null;

            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p[port];
            }
            else
            {
                epl = new EndPointListener(addr, port, secure);

                p[port] = epl;
            }

            return(epl);
        }
예제 #4
0
        static void RemovePrefixInternal(string prefix, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(prefix);

            if (lp.Path.IndexOf('%') != -1)
            {
                return;
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.RemovePrefix(lp, listener);
        }
예제 #5
0
        static void AddPrefixInternal(string p, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1) // TODO: Code?
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            // listens on all the interfaces if host name cannot be parsed by IPAddress.

            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.AddPrefix(lp, listener);
        }
예제 #6
0
        public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
        {
            lock (ip_to_endpoints)
            {
                // Dictionary<int, EndPointListener> p

                Hashtable p = null;

                p = (Hashtable)ip_to_endpoints[ep.Address];

                p.Remove(ep.Port);

                if (p.Count == 0)
                {
                    ip_to_endpoints.Remove(ep.Address);
                }

                epl.Close();
            }
        }