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 PrependRoute(HttpRoute route) => Routes.Insert(0, route);
public void SetDefaultRoute(HttpRoute route) => DefaultRoute = route;
public void AddRoute(HttpRoute route) => Routes.Add(route);