private void DeliverLocal(HttpListenerContext ctx) { Logger.Debug("HTTP: local resource"); var URL = ctx.Request.Url.AbsolutePath.ToLower().Substring(2); var Entry = LocalContent.FirstOrDefault(m => m.URL == URL); if (Entry != null && Entry.URL == URL) { ctx.Response.AddHeader("ETag", Entry.Hash); var Hash = ctx.Request.Headers["If-None-Match"]; if (Hash != null && Hash == Entry.Hash) { ctx.Response.ContentType = MimeTypeLookup.GetMimeType(Entry.URL) + (Entry.IsUtf8 ? ";charset=utf-8" : ""); ctx.Response.StatusCode = 304; ctx.Response.Close(); } else { SendBinary(ctx, Entry.Data, Entry.URL, Entry.IsUtf8); } } else if (URL == "/") { Redirect(ctx, "/~/index.html"); } else { HTTP404(ctx); } }
public static void SendFile(HttpListenerContext ctx, string FileName, bool Cache = true) { Logger.Debug("HTTP: Sending {0}", FileName); var Hdr = Tools.StrOrDefault(ctx.Request.Headers["If-None-Match"]); var DT = File.GetLastWriteTimeUtc(FileName).Ticks; if (Cache) { ctx.Response.AddHeader("ETag", DT.ToString()); } if (Cache && Hdr == DT.ToString()) { ctx.Response.StatusCode = 304; ctx.Response.Close(); } else { ctx.Response.ContentType = MimeTypeLookup.GetMimeType(FileName); ctx.Response.ContentLength64 = (new FileInfo(FileName)).Length; using (var FS = File.OpenRead(FileName)) { FS.CopyTo(ctx.Response.OutputStream); } ctx.Response.Close(); } }
public static void SendBinary(HttpListenerContext ctx, byte[] Content, string FakeName, bool IsUtf8 = false) { Logger.Debug("HTTP: Sending {0} ({1} bytes)", FakeName, Content == null ? 0 : Content.Length); ctx.Response.ContentType = MimeTypeLookup.GetMimeType(FakeName); ctx.Response.ContentEncoding = IsUtf8 ? Encoding.UTF8 : Encoding.Default; ctx.Response.Close(Content, false); }
private async Task <CloudBlockBlob> GetBlobReferenceAsync(string path, CancellationToken cancellationToken) { path = path.Replace("\\", "/"); var pathSegments = PathUtilities.GetSegments(path); var containerName = pathSegments.First(); var blobName = string.Join(PathUtilities.Delimiter, pathSegments.Skip(1)); var container = await GetCloudBlobContainerAsync(containerName, cancellationToken).ConfigureAwait(false); var result = container.GetBlockBlobReference(blobName); // set proper content type based on the file name extension result.Properties.ContentType = MimeTypeLookup.GetMimeType(Path.GetExtension(blobName)); return(result); }
public override ResponseMessage Process(RequestMessage request) { ResponseMessage response = new ResponseMessage { Body = new MemoryStream() }; var fs = new FileStream(FilePath, FileMode.Open); fs.CopyTo(response.Body); fs.Flush(); fs.Close(); response.StatusCode = HttpStatusCode.OK; string mimeType = MimeTypeLookup.GetMimeType(FilePath); response.Headers.Add("Content-Type", mimeType); return(response); }
public override ResponseMessage Process(RequestMessage request) { ResponseMessage response = new ResponseMessage(); var path = FolderLocation + GetFileRequestName(request); response.Body = new MemoryStream(); var fs = new FileStream(path, FileMode.Open); fs.CopyTo(response.Body); fs.Flush(); fs.Close(); response.Body.Position = 0; string mimeType = MimeTypeLookup.GetMimeType(request.Path); response.Headers.Add("Content-Type", mimeType); response.StatusCode = HttpStatusCode.OK; return(response); }
/// <summary> /// Gets Content Type based on the file extension. /// </summary> /// <param name="fileName"></param> /// <returns>NULL or the registered contenttype</returns> public static string GetContentTypeForFileName(string fileName) { return(MimeTypeLookup.GetMimeType(fileName.ToLower())); }
private void SendFile(HttpListenerContext ctx, string file) { Logger.Debug("HTTP: Sending {0}", file); ctx.Response.ContentType = MimeTypeLookup.GetMimeType(file); ctx.Response.Close(File.ReadAllBytes(file), false); }
private void SendBinary(HttpListenerContext ctx, byte[] Content, string FakeName, bool IsUtf8 = false) { Logger.Debug("HTTP: Sending {0} ({1} bytes)", FakeName, Content == null ? 0 : Content.Length); ctx.Response.ContentType = MimeTypeLookup.GetMimeType(FakeName) + (IsUtf8 ? ";charset=utf-8" : ""); ctx.Response.Close(Content, false); }