Exemple #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.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();
            }
        }
Exemple #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.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;
                }
            }
        }
Exemple #3
0
        /// <summary>
        ///     Send respose to <see cref="ProcessingContext.ClientStream" /> containing
        ///     <see cref="ProcessingContext.ResponseHeader" />
        ///     and rest of<see cref="ProcessingContext.ServerStream" />.
        ///     Expect <see cref="ProcessingContext.ServerStream" />, <see cref="ProcessingContext.ClientStream" /> and
        ///     <see cref="ProcessingContext.ResponseHeader" /> to be defined.
        /// </summary>
        /// <param name="context">current request context</param>
        protected virtual void SendResponse(ProcessingContext context)
        {
            Contract.Requires<ArgumentNullException>(context != null, "context");
            Contract.Requires<InvalidContextException>(context.ServerStream != null, "ServerStream");
            Contract.Requires<InvalidContextException>(context.ResponseHeader != null, "ResponseHeader");
            Contract.Requires<InvalidContextException>(context.ClientStream != null, "ClientStream");
            Contract.Requires<InvalidContextException>(context.ServerSocket != null, "ServerSocket");

            var responseWriter = new HttpResponseWriter(context.ClientStream);

            try
            {
                responseWriter.Write(context.ResponseHeader, context.ServerStream, context.ServerSocket.Available);

                if (Logger.IsDebugEnabled)
                {
                    Logger.DebugFormat("Response Sent. {0}", TraceUtils.GetHttpTrace(context.ResponseHeader));
                }
            }
            catch (IOException ex)
            {
                if (ex.IsSocketException(SocketError.TimedOut))
                {
                    Logger.WarnFormat("Request to remote server has timed out. {0}", TraceUtils.GetHttpTrace(context.RequestHeader));

                    responseWriter.WriteGatewayTimeout();
                }
                else if (ex.IsSocketException(SocketError.ConnectionReset, SocketError.ConnectionAborted))
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.DebugFormat("Request Aborted. {0}", TraceUtils.GetHttpTrace(context.RequestHeader));
                    }
                }
                else
                {
                    throw;
                }

                context.StopProcessing();
            }
        }