示例#1
0
        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) { }
        }
示例#2
0
 public void PrependErrorRoute(HttpErrorRoute errorRoute) => ErrorRoutes.Insert(0, errorRoute);
示例#3
0
 public void SetDefaultErrorRoute(HttpErrorRoute errorRoute) => DefaultErrorRoute = errorRoute;
示例#4
0
 public void AddErrorRoute(HttpErrorRoute errorRoute) => ErrorRoutes.Add(errorRoute);