| [230] | 1 | using System;
 | 
|---|
 | 2 | using System.Collections.Generic;
 | 
|---|
 | 3 | using System.IO;
 | 
|---|
 | 4 | using System.Net;
 | 
|---|
 | 5 | using System.Threading;
 | 
|---|
 | 6 | 
 | 
|---|
| [244] | 7 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers
 | 
|---|
| [230] | 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 | 
 | 
|---|
| [244] | 16 |                 public StaticHandler (string staticPart, string filePath, AllocsFixes.FileCache.AbstractCache cache, bool logMissingFiles, string moduleName = null) : base(moduleName)
 | 
|---|
| [230] | 17 |                 {
 | 
|---|
 | 18 |                         this.staticPart = staticPart;
 | 
|---|
 | 19 |                         this.datapath = filePath;
 | 
|---|
 | 20 |                         this.cache = cache;
 | 
|---|
 | 21 |                         this.logMissingFiles = logMissingFiles;
 | 
|---|
 | 22 |                 }
 | 
|---|
 | 23 | 
 | 
|---|
| [244] | 24 |                 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel)
 | 
|---|
| [230] | 25 |                 {
 | 
|---|
 | 26 |                         string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
 | 
|---|
 | 27 | 
 | 
|---|
| [251] | 28 |                         byte[] content = cache.GetFileContent (datapath + "/" + fn);
 | 
|---|
 | 29 | 
 | 
|---|
| [230] | 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 |                 }
 | 
|---|
 | 41 |         }
 | 
|---|
 | 42 | }
 | 
|---|
 | 43 | 
 | 
|---|