Rev | Line | |
---|
[230] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Net;
|
---|
| 5 | using System.Threading;
|
---|
| 6 |
|
---|
| 7 | namespace AllocsFixes.NetConnections.Servers.Web
|
---|
| 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 |
|
---|
| 16 | public StaticHandler (string staticPart, string filePath, AllocsFixes.FileCache.AbstractCache cache, bool logMissingFiles)
|
---|
| 17 | {
|
---|
| 18 | this.staticPart = staticPart;
|
---|
| 19 | this.datapath = filePath;
|
---|
| 20 | this.cache = cache;
|
---|
| 21 | this.logMissingFiles = logMissingFiles;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user)
|
---|
| 25 | {
|
---|
| 26 | string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
| 27 |
|
---|
| 28 | byte[] content = cache.GetFileContent (datapath + "/" + fn);
|
---|
| 29 | if (content != null) {
|
---|
| 30 | resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));
|
---|
| 31 | resp.ContentLength64 = content.Length;
|
---|
| 32 | resp.OutputStream.Write (content, 0, content.Length);
|
---|
| 33 | } else {
|
---|
| 34 | resp.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
| 35 | if (logMissingFiles)
|
---|
| 36 | Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" + req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\"");
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.