Exemplo n.º 1
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");
     }
 }
Exemplo n.º 2
0
 public async void redirect(string route)
 {
     try
     {
         context.Response.RedirectLocation = route;
         context.Response.Redirect(route);
     }
     catch (Exception ex)
     {
         if (JintAddons.debug)
         {
             Log.Error(ex.Message + " " + ex.StackTrace);
         }
         send(StaticTemplates.OopsTemplate(ex), 500, "text/html");
     }
 }
Exemplo n.º 3
0
        public async void send(string data, int statusCode, string contentType)
        {
            try
            {
                var    response = context.Response;
                byte[] bytes    = Encoding.UTF8.GetBytes(data);
                response.ContentType     = contentType;
                response.ContentEncoding = Encoding.UTF8;
                response.ContentLength64 = bytes.LongLength;
                response.StatusCode      = statusCode;
                await response.OutputStream.WriteAsync(bytes, 0, bytes.Length);

                response.Close();
            }
            catch (Exception ex)
            {
                if (JintAddons.debug)
                {
                    Log.Error(ex.Message + " " + ex.StackTrace);
                }
                send(StaticTemplates.OopsTemplate(ex), 500, "text/html");
            }
        }
Exemplo n.º 4
0
        public async void file(string filePath)
        {
            if (!File.Exists(filePath))
            {
                send(StaticTemplates.NoFoundTemplate(filePath), 404, "text/html");
                return;
            }
            try
            {
                HttpListenerResponse response = context.Response;
                response.KeepAlive          = true;
                response.SendChunked        = true;
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                string mime;
                var    stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                response.ContentType     = ServerHelpers.mimeTypesMap.TryGetValue(Path.GetExtension(filePath), out mime) ? mime : "undefined";
                response.ContentLength64 = stream.Length;
                response.AddHeader("Accept-Ranges", "bytes");
                response.AddHeader("Date", DateTime.Now.ToString("r"));
                response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filePath).ToString("r"));
                response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", 0, Convert.ToInt32(stream.Length) - 1, Convert.ToInt32(stream.Length)));
                response.ContentLength64 = stream.Length;

                await stream.CopyToAsync(context.Response.OutputStream);

                await stream.FlushAsync();

                response.Close();
            }
            catch (Exception ex)
            {
                if (JintAddons.debug)
                {
                    Log.Error(ex.Message + " " + ex.StackTrace);
                }
            }
        }
Exemplo n.º 5
0
 private async void handle404(HttpListenerContext ctx)
 {
     new Response(ctx).send(StaticTemplates.NoFoundTemplate(ctx.Request.RawUrl), 404, "text/html");
 }
Exemplo n.º 6
0
 public async void dd(object data)
 {
     send(StaticTemplates.DDTemplate(data), 200, "text/html");
 }