source: binary-improvements/MapRendering/Web/StaticHandler.cs@ 236

Last change on this file since 236 was 230, checked in by alloc, 10 years ago

Binary improvements

File size: 1.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using System.Threading;
6
7namespace AllocsFixes.NetConnections.Servers.Web
8{
9 public class StaticHandler : PathHandler
10 {
11 private string datapath;
12 private string staticPart;
13 private AllocsFixes.FileCache.AbstractCache cache;
14 private bool logMissingFiles;
15
16 public StaticHandler (string staticPart, string filePath, AllocsFixes.FileCache.AbstractCache cache, bool logMissingFiles)
17 {
18 this.staticPart = staticPart;
19 this.datapath = filePath;
20 this.cache = cache;
21 this.logMissingFiles = logMissingFiles;
22 }
23
24 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user)
25 {
26 string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
27
28 byte[] content = cache.GetFileContent (datapath + "/" + fn);
29 if (content != null) {
30 resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));
31 resp.ContentLength64 = content.Length;
32 resp.OutputStream.Write (content, 0, content.Length);
33 } else {
34 resp.StatusCode = (int)HttpStatusCode.NotFound;
35 if (logMissingFiles)
36 Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" + req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\"");
37 return;
38 }
39 }
40 }
41}
42
Note: See TracBrowser for help on using the repository browser.