Exemple #1
0
        /// <summary>
        /// Process result (check if it should be sent back or not)
        /// </summary>
        /// <param name="result"></param>
        /// <param name="e"></param>
        /// <returns><c>true</c> if request was processed properly.; otherwise <c>false</c>.</returns>
        protected virtual bool ProcessResult(ProcessingResult result, RequestEventArgs e)
        {
            if (result == ProcessingResult.Abort)
            {
                e.IsHandled = true;
                return(true);
            }

            if (result == ProcessingResult.SendResponse)
            {
                SendResponse(e.Context, e.Request, e.Response);
                e.IsHandled = true;
                return(true);
            }

            return(false);
        }
Exemple #2
0
        private void OnRequest(object sender, RequestEventArgs e)
        {
            _server = this;

            Exception exception;

            try
            {
                ProcessingResult result = HandleRequest(e);
                if (result != ProcessingResult.Continue)
                {
                    return;
                }

                exception = null;
            }
            catch (HttpException err)
            {
                _logger.Error("Got an HTTP exception.", err);
                e.Response.Status = err.Code;
                e.Response.Reason = err.Message;
                exception         = err;
            }
            catch (Exception err)
            {
                _logger.Error("Got an unhandled exception.", err);
                exception         = err;
                e.Response.Status = HttpStatusCode.InternalServerError;
                e.Response.Reason = "Failed to process request.";
            }


            if (exception == null)
            {
                e.Response.Status = HttpStatusCode.NotFound;
                e.Response.Reason = "Requested resource is not found. Sorry ;(";
                exception         = new HttpException(HttpStatusCode.NotFound, "Failed to find uri " + e.Request.Uri);
            }
            DisplayErrorPage(e.Context, exception);
            e.IsHandled = true;
        }
Exemple #3
0
        private ProcessingResult HandleRequest(RequestEventArgs e)
        {
            var context = new RequestContext
            {
                HttpContext = e.Context,
                Request     = e.Request,
                Response    = e.Response
            };

            OnAuthentication(context);
            OnBeforeRequest(context);
            PrepareRequest(this, e);


            if (e.Request.ContentLength.Value > 0)
            {
                DecodeBody(e.Request);
            }

            // Process routers.
            ProcessingResult result = ProcessRouters(context);

            if (ProcessResult(result, e))
            {
                _logger.Debug("Routers processed the request.");
            }


            // process modules.
            result = ProcessModules(context);
            if (ProcessResult(result, e))
            {
                return(result);
            }

            RequestReceived(this, e);


            return(ProcessingResult.Continue);
        }
Exemple #4
0
        /// <exception cref="Exception">Throwing exception if in debug mode and not exception handler have been specified.</exception>
        private void OnRequest(object sender, RequestEventArgs e)
        {
            var context = (HttpContext)sender;

            HttpFactory.Current = Factory;
            HttpContext.Current = context;

            try
            {
                var args = new RequestEventArgs(context, e.Request, e.Response);
                RequestReceived(this, args);
                if (!args.IsHandled)
                {
                    // need to respond to the context.
                    var generator = new ResponseWriter();
                    generator.Send(context, args.Response);
                }

                // Disconnect when done.
                if (e.Response.HttpVersion == "HTTP/1.0" || e.Response.Connection.Type == ConnectionType.Close)
                {
                    context.Disconnect();
                }
            }
            catch (Exception err)
            {
                if (err is HttpException)
                {
                    var exception = (HttpException)err;
                    SendErrorPage(exception);
                }
                else
                {
                    _logger.Debug("Request failed.", err);
                    SendErrorPage(err);
                }
                e.IsHandled = true;
            }
        }