예제 #1
0
        private void handlePublicResponse(HttpListenerContext ctx)
        {
            string filename = ServerHelpers.GetFileFromFolders(PublicFolders, ServerHelpers.NormalizeRoute(ctx.Request.RawUrl));

            filename = WebUtility.UrlDecode(filename);
            new Response(ctx).file(filename);
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        private bool IsFromPublicFolder(HttpListenerContext ctx)
        {
            string filename = ServerHelpers.GetFileFromFolders(PublicFolders, ServerHelpers.NormalizeRoute(ctx.Request.RawUrl));

            return(File.Exists(filename));
        }