Example #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);
        }
Example #2
0
        public Request(HttpListenerContext ctx)
        {
            context = ctx;
            var          request = ctx.Request;
            StreamReader stream  = new StreamReader(request.InputStream);
            var          input   = stream.ReadToEnd();

            try
            {
                data = JsonConvert.DeserializeObject(input);
            }
            catch (Exception)
            {
                if (request.ContentType == "multipart/form-data" || request.ContentType == "application/x-www-form-urlencoded")
                {
                    data = ServerHelpers.ParseFormData(input);
                }
                else
                {
                    data = input;
                }
            }

            Dictionary <string, object> querys = new Dictionary <string, object>();

            foreach (var qu in request.QueryString.AllKeys)
            {
                querys.Add(qu, request.QueryString.Get(qu));
            }
            this.query = Utils.ObjectUtils.DictionaryToObject(querys);
        }
Example #3
0
        public static string OopsTemplate(Exception ex)
        {
            string error = null;

            if (JintAddons.debug)
            {
                error = ex.Message + " --> " + ex.StackTrace;
            }
            return(ServerHelpers.GetAssemblyTemplate("OopsTemplate.mustache", new { error }));
        }
Example #4
0
 public static string DDTemplate(object json)
 {
     if (!JintAddons.debug)
     {
         json = new
         {
             error   = "Wrong mode",
             message = "Debug mode must be on"
         };
     }
     return(ServerHelpers.GetAssemblyTemplate("DD.mustache", JsonConvert.SerializeObject(json)));
 }
Example #5
0
 public async void render(string viewPath, object data)
 {
     try
     {
         var html = ServerHelpers.GetFileTemplate(viewPath, data);
         send(html, 200, "text/html");
     }
     catch (Exception ex)
     {
         if (JintAddons.debug)
         {
             Log.Error(ex.Message + " " + ex.StackTrace);
         }
         send(StaticTemplates.OopsTemplate(ex), 500, "text/html");
     }
 }
Example #6
0
 public static string NoFoundTemplate(string route)
 {
     return(ServerHelpers.GetAssemblyTemplate("404.mustache", new { route }));
 }
Example #7
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);
            }
        }
Example #8
0
 public Server delete(string route, Action <Request, Response> callback)
 {
     DELETES.Add(ServerHelpers.NormalizeRoute(route), callback);
     return(this);
 }
Example #9
0
 public Server patch(string route, Action <Request, Response> callback)
 {
     PATCHS.Add(ServerHelpers.NormalizeRoute(route), callback);
     return(this);
 }
Example #10
0
        private bool IsFromPublicFolder(HttpListenerContext ctx)
        {
            string filename = ServerHelpers.GetFileFromFolders(PublicFolders, ServerHelpers.NormalizeRoute(ctx.Request.RawUrl));

            return(File.Exists(filename));
        }