Exemplo n.º 1
0
        private void RouteRequest(HttpListenerContext context)
        {
            Console.WriteLine($"Request received from '{context.Request.RemoteEndPoint}'.");

            var path = context.Request.Url.AbsolutePath;

            if (path.StartsWith(RootPath))
            {
                path = path.Substring(RootPath.Length);
            }

            try {
                HttpHandlerResult result;
                if (Routes.TryFind(path, out RouteEvent routeAction))
                {
                    try {
                        result = routeAction.Invoke(context);
                    }
                    catch (Exception error) {
                        result = HttpHandlerResult.Exception(error);
                    }
                }
                else
                {
                    result = HttpHandlerResult.NotFound()
                             .SetText($"No handler found matching path '{path}'!");
                }

                result.Apply(context);
            }
            finally {
                try {
                    context.Response.Close();
                }
                catch {}
            }
        }
Exemplo n.º 2
0
 public HttpHandlerResult NotFound()
 {
     return(HttpHandlerResult.NotFound());
 }