void ClientHandler(IAsyncResult result) { try { HttpListenerContext context = listener.EndGetContext(result); listener.BeginGetContext(ClientHandler, null); var request = new HttpRequest(context.Request); var response = new HttpResponse(context.Response); try { if (LogAllRequests) { Log(request.RequestId, $"{request.Method} request for '{request.Url}' from '{request.ClientIP}'."); } if (HttpRoute.InvokeMatched(Routes, request, response)) { DefaultRoute.Invoke(request, response); } } catch (HttpListenerException) { throw; } catch (Exception e) { Log(request.RequestId, $"{e.GetType()} : {e.Message}"); if (ResetResponseOnError) { if (response.IsDataSent) { response.Close(); } else { response.Reset(); } } if (HttpErrorRoute.InvokeAll(ErrorRoutes, e, request, response)) { DefaultErrorRoute.Invoke(e, request, response); } } if (response.IsOpen) { response.Close(); } } catch (HttpListenerException) { } }
public void PrependErrorRoute(HttpErrorRoute errorRoute) => ErrorRoutes.Insert(0, errorRoute);
public void SetDefaultErrorRoute(HttpErrorRoute errorRoute) => DefaultErrorRoute = errorRoute;
public void AddErrorRoute(HttpErrorRoute errorRoute) => ErrorRoutes.Add(errorRoute);