private void handlePublicResponse(HttpListenerContext ctx) { string filename = ServerHelpers.GetFileFromFolders(PublicFolders, ServerHelpers.NormalizeRoute(ctx.Request.RawUrl)); filename = WebUtility.UrlDecode(filename); new Response(ctx).file(filename); }
private async void HandleIncomingRequest(IAsyncResult res) { HttpListenerContext ctx = null; try { ctx = listener.EndGetContext(res); } catch (Exception) { return; } Dictionary <string, Action <Request, Response> > routesDictionary = null; foreach (var resHeader in ResponseHeaders) { ctx.Response.Headers.Add(resHeader.Key, resHeader.Value); } switch (ctx.Request.HttpMethod) { case "GET": routesDictionary = GETS; break; case "POST": routesDictionary = POSTS; break; case "PUT": routesDictionary = PUTS; break; case "PATCH": routesDictionary = PATCHS; break; case "DELETE": routesDictionary = DELETES; break; } try { handleIndexRedirection(ctx); if (!IsFromPublicFolder(ctx)) { bool found = false; foreach (var route in routesDictionary.Keys) { if (ServerHelpers.NormalizeRoute(ctx.Request.RawUrl) == route) { found = true; var response = new Response(ctx); var request = new Request(ctx); routesDictionary[route].Invoke(request, response); } } if (!found) { if (!UseClientSideRouting) { handle404(ctx); } else { new Response(ctx).file(ServerHelpers.GetFileFromFolders(PublicFolders, "index.html")); } } } else { handlePublicResponse(ctx); } } catch (Exception ex) { if (JintAddons.debug) { Log.Error(ex.Message + " " + ex.StackTrace); } } if (enabled) { handle = listener.BeginGetContext(new AsyncCallback(HandleIncomingRequest), listener); } }
public Server delete(string route, Action <Request, Response> callback) { DELETES.Add(ServerHelpers.NormalizeRoute(route), callback); return(this); }
public Server patch(string route, Action <Request, Response> callback) { PATCHS.Add(ServerHelpers.NormalizeRoute(route), callback); return(this); }
private bool IsFromPublicFolder(HttpListenerContext ctx) { string filename = ServerHelpers.GetFileFromFolders(PublicFolders, ServerHelpers.NormalizeRoute(ctx.Request.RawUrl)); return(File.Exists(filename)); }