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

Last change on this file since 383 was 382, checked in by alloc, 2 years ago

Switched to use SpaceWizards.HttpListener

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