source: binary-improvements2/MapRendering/Web/Handlers/StaticHandler.cs@ 389

Last change on this file since 389 was 387, checked in by alloc, 2 years ago

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

File size: 1.2 KB
RevLine 
[230]1using System.IO;
2using System.Net;
[325]3using AllocsFixes.FileCache;
[230]4
[325]5namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
[382]6 public class StaticHandler : AbsHandler {
[325]7 private readonly AbstractCache cache;
8 private readonly string datapath;
9 private readonly bool logMissingFiles;
[230]10
[367]11 public StaticHandler (string _filePath, AbstractCache _cache, bool _logMissingFiles,
[351]12 string _moduleName = null) : base (_moduleName) {
13 datapath = _filePath + (_filePath [_filePath.Length - 1] == '/' ? "" : "/");
14 cache = _cache;
15 logMissingFiles = _logMissingFiles;
[230]16 }
17
[387]18 public override void HandleRequest (RequestContext _context) {
19 string fn = _context.RequestPath.Remove (0, urlBasePath.Length);
[230]20
[332]21 byte[] content = cache.GetFileContent (datapath + fn);
[251]22
[230]23 if (content != null) {
[387]24 _context.Response.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));
25 _context.Response.ContentLength64 = content.Length;
26 _context.Response.OutputStream.Write (content, 0, content.Length);
[230]27 } else {
[387]28 _context.Response.StatusCode = (int) HttpStatusCode.NotFound;
[325]29 if (logMissingFiles) {
[387]30 Log.Out ("Web:Static:FileNotFound: \"" + _context.RequestPath + "\" @ \"" + datapath + fn + "\"");
[325]31 }
[230]32 }
33 }
34 }
[325]35}
Note: See TracBrowser for help on using the repository browser.