Exemplo n.º 1
0
        private static void processAccepted(Socket socket, EndPointListener listener)
        {
            HttpConnection conn = null;

            try {
                conn = new HttpConnection(socket, listener);
                lock (listener._unregisteredSync)
                    listener._unregistered[conn] = conn;

                conn.BeginReadRequest();
            }
            catch {
                if (conn != null)
                {
                    conn.Close(true);
                    return;
                }

                socket.Close();
            }
        }
Exemplo n.º 2
0
        internal HttpConnection(Socket socket, EndPointListener listener)
        {
            _socket   = socket;
            _listener = listener;

            var netStream = new NetworkStream(socket, false);

            if (listener.IsSecure)
            {
                var sslConf   = listener.SslConfiguration;
                var sslStream = new SslStream(
                    netStream,
                    false,
                    sslConf.ClientCertificateValidationCallback
                    );

                sslStream.AuthenticateAsServer(
                    sslConf.ServerCertificate,
                    sslConf.ClientCertificateRequired,
                    sslConf.EnabledSslProtocols,
                    sslConf.CheckCertificateRevocation
                    );

                _secure = true;
                _stream = sslStream;
            }
            else
            {
                _stream = netStream;
            }

            _localEndPoint   = socket.LocalEndPoint;
            _remoteEndPoint  = socket.RemoteEndPoint;
            _sync            = new object();
            _timeout         = 90000; // 90k ms for first request, 15k ms from then on.
            _timeoutCanceled = new Dictionary <int, bool> ();
            _timer           = new Timer(onTimeout, this, Timeout.Infinite, Timeout.Infinite);

            init();
        }
Exemplo n.º 3
0
        private static void addPrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (addr == null)
            {
                throw new HttpListenerException(87, "Includes an invalid host.");
            }

            if (!addr.IsLocal())
            {
                throw new HttpListenerException(87, "Includes an invalid host.");
            }

            int port;

            if (!Int32.TryParse(pref.Port, out port))
            {
                throw new HttpListenerException(87, "Includes an invalid port.");
            }

            if (!port.IsPortNumber())
            {
                throw new HttpListenerException(87, "Includes an invalid port.");
            }

            var path = pref.Path;

            if (path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;

            if (_endpoints.TryGetValue(endpoint, out lsnr))
            {
                if (lsnr.IsSecure ^ pref.IsSecure)
                {
                    throw new HttpListenerException(87, "Includes an invalid scheme.");
                }
            }
            else
            {
                lsnr =
                    new EndPointListener(
                        endpoint,
                        pref.IsSecure,
                        listener.CertificateFolderPath,
                        listener.SslConfiguration,
                        listener.ReuseAddress
                        );

                _endpoints.Add(endpoint, lsnr);
            }

            lsnr.AddPrefix(pref, listener);
        }