Example #1
0
        internal HttpConnection(Socket socket, EndPointListener listener)
        {
            _socket   = socket;
            _listener = listener;
            _secure   = listener.IsSecure;

            var netStream = new NetworkStream(socket, false);

            if (_secure)
            {
                var conf      = listener.SslConfiguration;
                var sslStream = new SslStream(netStream, false, conf.ClientCertificateValidationCallback);
                sslStream.AuthenticateAsServer(
                    conf.ServerCertificate,
                    conf.ClientCertificateRequired,
                    conf.EnabledSslProtocols,
                    conf.CheckCertificateRevocation
                    );

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

            _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();
        }
        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();
            }
        }
Example #3
0
        private static void addPrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.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);
        }