private void SetupResponse() { State = HttpStates.WriteBegin; try { if (!owner.AuthorizeClient(this)) { throw new HttpStatusException(HttpCode.Denied); } if (string.IsNullOrEmpty(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 Warn(String.Format("{0} - Got a {2}: {1}", this, path, ex.Code), ex); #else DebugFormat("{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) { Warn($"{this} - Failed to process response", ex); response = error500.HandleRequest(this); } SendResponse(); }