Esempio n. 1
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);
                }
            }
        }
Esempio n. 2
0
 private async void handle404(HttpListenerContext ctx)
 {
     new Response(ctx).send(StaticTemplates.NoFoundTemplate(ctx.Request.RawUrl), 404, "text/html");
 }