Rev | Line | |
---|
[133] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Net;
|
---|
[134] | 5 | using System.Threading;
|
---|
[133] | 6 |
|
---|
| 7 | namespace 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 | {
|
---|
| 26 | try {
|
---|
| 27 | string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
| 28 |
|
---|
[199] | 29 | byte[] content = cache.GetFileContent (datapath + "/" + fn);
|
---|
| 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);
|
---|
[133] | 34 | } else {
|
---|
[199] | 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;
|
---|
[133] | 39 | }
|
---|
| 40 | } catch (Exception e) {
|
---|
| 41 | Log.Out ("Error in StaticHandler.HandleRequest: " + e);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.