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

Last change on this file since 215 was 202, checked in by alloc, 10 years ago

Server fixes

File size: 1.3 KB
RevLine 
[133]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
[134]5using System.Threading;
[133]6
7namespace AllocsFixes.NetConnections.Servers.Web
8{
9 public class StaticHandler : PathHandler
10 {
11 private string datapath;
12 private string staticPart;
[199]13 private AllocsFixes.FileCache.AbstractCache cache;
[134]14 private bool logMissingFiles;
[133]15
[199]16 public StaticHandler (string staticPart, string filePath, AllocsFixes.FileCache.AbstractCache cache, bool logMissingFiles)
[133]17 {
18 this.staticPart = staticPart;
19 this.datapath = filePath;
20 this.cache = cache;
[134]21 this.logMissingFiles = logMissingFiles;
[133]22 }
23
[134]24 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user)
[133]25 {
[202]26 string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
[133]27
[202]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;
[133]38 }
39 }
40 }
41}
42
Note: See TracBrowser for help on using the repository browser.