| 1 | using System.IO;
|
|---|
| 2 | using System.Net;
|
|---|
| 3 | using AllocsFixes.FileCache;
|
|---|
| 4 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
|
|---|
| 5 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
|---|
| 6 |
|
|---|
| 7 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
|
|---|
| 8 | public class StaticHandler : AbsHandler {
|
|---|
| 9 | private readonly AbstractCache cache;
|
|---|
| 10 | private readonly string datapath;
|
|---|
| 11 | private readonly bool logMissingFiles;
|
|---|
| 12 |
|
|---|
| 13 | public StaticHandler (string _filePath, AbstractCache _cache, bool _logMissingFiles,
|
|---|
| 14 | string _moduleName = null) : base (_moduleName) {
|
|---|
| 15 | datapath = _filePath + (_filePath [_filePath.Length - 1] == '/' ? "" : "/");
|
|---|
| 16 | cache = _cache;
|
|---|
| 17 | logMissingFiles = _logMissingFiles;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con,
|
|---|
| 21 | int _permissionLevel) {
|
|---|
| 22 | string fn = _requestPath.Remove (0, urlBasePath.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: \"" + _requestPath + "\" @ \"" + datapath + fn + "\"");
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|