Exemplo n.º 1
0
        /// <summary>
        ///     Establish secured connection with client and receive HTTP request using it.
        /// </summary>
        /// <param name="context">current request context</param>
        protected override void ReceiveRequest(ProcessingContext context)
        {
            base.ReceiveRequest(context);

            if (context.RequestHeader == null)
            {
                throw new InvalidOperationException("Not SSL request");
            }

            if (context.RequestHeader.MethodType != RequestMethodTypes.CONNECT)
            {
                throw new InvalidContextException("RequestHeader");
            }

            if (context.ClientStream == null)
            {
                throw new InvalidContextException("ClientStream");
            }

            var responseWriter = new HttpResponseWriter(context.ClientStream);

            var sslStream = new SslStream(context.ClientStream, false, _certificateValidationCallback);

            try
            {
                responseWriter.WriteConnectionEstablished();

                sslStream.AuthenticateAsServer(_certificate, false, SslProtocols.Tls, false);

                context.ClientStream = sslStream;

                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Client SSL connection established");
                }

                base.ReceiveRequest(context);
            }
            catch (IOException ex)
            {
                context.StopProcessing();

                if (SocketUtils.IsSocketException(ex, SocketError.ConnectionReset, SocketError.ConnectionAborted))
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Request aborted");
                    }
                }
                else
                {
                    Logger.Error("Failed to read request", ex);
                    Logger.Error(context.RequestHeader);

                    throw;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Establish secured connection with client and receive HTTP request using it.
        /// </summary>
        /// <param name="context">current request context</param>
        protected override void ReceiveRequest(ProcessingContext context)
        {
            base.ReceiveRequest(context);

            if (context.Processed)
            {
                return;
            }

            if (context.RequestHeader == null)
            {
                throw new InvalidContextException("RequestHeader");
            }

            if (context.RequestHeader.MethodType != RequestMethodTypes.CONNECT)
            {
                throw new InvalidOperationException("Not SSL request");
            }

            if (context.ClientStream == null)
            {
                throw new InvalidContextException("ClientStream");
            }

            var responseWriter = new HttpResponseWriter(context.ClientStream);

            var sslStream = new SslStream(context.ClientStream, false, _certificateValidationCallback);

            try
            {
                responseWriter.WriteConnectionEstablished();
                sslStream.AuthenticateAsServer(_certificate, false, SslProtocols.Tls, false);
                context.ClientStream = sslStream;

                base.ReceiveRequest(context);
            }
            catch (IOException ex)
            {
                if (ex.IsSocketException(SocketError.ConnectionReset, SocketError.ConnectionAborted))
                {
                    Logger.WarnFormat("Request Aborted. {0}", TraceUtils.GetHttpTrace(context.RequestHeader));
                }
                else if(ex.IsSocketException(SocketError.TimedOut))
                {
                    Logger.WarnFormat("Client request time out. {0}", TraceUtils.GetHttpTrace(context.RequestHeader));
                }
                else
                {
                    throw;
                }

                context.StopProcessing();
            }
        }