[230] | 1 | using System.IO;
|
---|
| 2 | using System.Net;
|
---|
[325] | 3 | using AllocsFixes.FileCache;
|
---|
[230] | 4 |
|
---|
[325] | 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;
|
---|
[230] | 11 |
|
---|
[351] | 12 | public StaticHandler (string _staticPart, string _filePath, AbstractCache _cache, bool _logMissingFiles,
|
---|
| 13 | string _moduleName = null) : base (_moduleName) {
|
---|
| 14 | staticPart = _staticPart;
|
---|
| 15 | datapath = _filePath + (_filePath [_filePath.Length - 1] == '/' ? "" : "/");
|
---|
| 16 | cache = _cache;
|
---|
| 17 | logMissingFiles = _logMissingFiles;
|
---|
[230] | 18 | }
|
---|
| 19 |
|
---|
[351] | 20 | public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
|
---|
| 21 | int _permissionLevel) {
|
---|
| 22 | string fn = _req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
[230] | 23 |
|
---|
[332] | 24 | byte[] content = cache.GetFileContent (datapath + fn);
|
---|
[251] | 25 |
|
---|
[230] | 26 | if (content != null) {
|
---|
[351] | 27 | _resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));
|
---|
| 28 | _resp.ContentLength64 = content.Length;
|
---|
| 29 | _resp.OutputStream.Write (content, 0, content.Length);
|
---|
[230] | 30 | } else {
|
---|
[351] | 31 | _resp.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
[325] | 32 | if (logMissingFiles) {
|
---|
[351] | 33 | Log.Out ("Web:Static:FileNotFound: \"" + _req.Url.AbsolutePath + "\" @ \"" + datapath + fn + "\"");
|
---|
[325] | 34 | }
|
---|
[230] | 35 | }
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
[325] | 38 | }
|
---|