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