1 | using System.IO;
|
---|
2 | using System.Net;
|
---|
3 | using AllocsFixes.FileCache;
|
---|
4 |
|
---|
5 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
|
---|
6 | public class StaticHandler : PathHandler {
|
---|
7 | private readonly AbstractCache cache;
|
---|
8 | private readonly string datapath;
|
---|
9 | private readonly bool logMissingFiles;
|
---|
10 | private readonly string staticPart;
|
---|
11 |
|
---|
12 | public StaticHandler (string staticPart, string filePath, AbstractCache cache, bool logMissingFiles,
|
---|
13 | string moduleName = null) : base (moduleName) {
|
---|
14 | this.staticPart = staticPart;
|
---|
15 | datapath = filePath;
|
---|
16 | this.cache = cache;
|
---|
17 | this.logMissingFiles = logMissingFiles;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
|
---|
21 | int permissionLevel) {
|
---|
22 | string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
23 |
|
---|
24 | byte[] content = cache.GetFileContent (datapath + "/" + fn);
|
---|
25 |
|
---|
26 | if (content != null) {
|
---|
27 | resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));
|
---|
28 | resp.ContentLength64 = content.Length;
|
---|
29 | resp.OutputStream.Write (content, 0, content.Length);
|
---|
30 | } else {
|
---|
31 | resp.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
32 | if (logMissingFiles) {
|
---|
33 | Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" +
|
---|
34 | req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\"");
|
---|
35 | }
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|