Beispiel #1
0
        private void SetupResponse()
        {
            State = HttpStates.WRITEBEGIN;
            try {
                if (!owner.AuthorizeClient(this))
                {
                    throw new HttpStatusException(HttpCode.Denied);
                }
                if (string.IsNullOrEmpty(path))
                {
                    _logger.Error("Empty path");
                    throw new HttpStatusException(HttpCode.NotFound);
                }
                var handler = owner.FindHandler(path);
                if (handler == null)
                {
                    throw new HttpStatusException(HttpCode.NotFound);
                }
                response = handler.HandleRequest(this);
                if (response == null)
                {
                    throw new ArgumentException("Handler did not return a response");
                }
            }
            catch (HttpStatusException ex) {
#if DEBUG
                _logger.Warn(String.Format("{0} - Got a {2}: {1}", this, path, ex.Code), ex);
#else
                _logger.InfoFormat("{0} - Got a {2}: {1}", this, path, ex.Code);
#endif
                switch (ex.Code)
                {
                case HttpCode.NotFound:
                    response = Error404.HandleRequest(this);
                    break;

                case HttpCode.Denied:
                    response = Error403.HandleRequest(this);
                    break;

                case HttpCode.InternalError:
                    response = Error500.HandleRequest(this);
                    break;

                default:
                    response = new StaticHandler(new StringResponse(
                                                     ex.Code,
                                                     "text/plain",
                                                     ex.Message
                                                     )).HandleRequest(this);
                    break;
                }
            }
            catch (Exception ex) {
                _logger.Warn(String.Format("{0} - Failed to process response", this), ex);
                response = Error500.HandleRequest(this);
            }
            SendResponse();
        }