source: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/StaticHandler.cs@ 200

Last change on this file since 200 was 199, checked in by alloc, 10 years ago

fixes

File size: 1.4 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 try {
27 string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
28
29 byte[] content = cache.GetFileContent (datapath + "/" + fn);
30 if (content != null) {
31 resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));
32 resp.ContentLength64 = content.Length;
33 resp.OutputStream.Write (content, 0, content.Length);
34 } else {
35 resp.StatusCode = (int)HttpStatusCode.NotFound;
36 if (logMissingFiles)
37 Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" + req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\"");
38 return;
39 }
40 } catch (Exception e) {
41 Log.Out ("Error in StaticHandler.HandleRequest: " + e);
42 }
43 }
44 }
45}
46
Note: See TracBrowser for help on using the repository browser.