Rev | Line | |
---|
[391] | 1 | using System.IO;
|
---|
| 2 | using System.Net;
|
---|
| 3 | using AllocsFixes.FileCache;
|
---|
| 4 |
|
---|
| 5 | namespace Webserver.UrlHandlers {
|
---|
| 6 | public class StaticHandler : AbsHandler {
|
---|
| 7 | private readonly AbstractCache cache;
|
---|
| 8 | private readonly string datapath;
|
---|
| 9 | private readonly bool logMissingFiles;
|
---|
| 10 |
|
---|
| 11 | public StaticHandler (string _filePath, AbstractCache _cache, bool _logMissingFiles,
|
---|
| 12 | string _moduleName = null) : base (_moduleName) {
|
---|
| 13 | datapath = _filePath + (_filePath [_filePath.Length - 1] == '/' ? "" : "/");
|
---|
| 14 | cache = _cache;
|
---|
| 15 | logMissingFiles = _logMissingFiles;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | public override void HandleRequest (RequestContext _context) {
|
---|
| 19 | string fn = _context.RequestPath.Remove (0, urlBasePath.Length);
|
---|
| 20 |
|
---|
| 21 | byte[] content = cache.GetFileContent (datapath + fn);
|
---|
| 22 |
|
---|
| 23 | if (content != null) {
|
---|
| 24 | _context.Response.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));
|
---|
| 25 | _context.Response.ContentLength64 = content.Length;
|
---|
| 26 | _context.Response.OutputStream.Write (content, 0, content.Length);
|
---|
| 27 | } else {
|
---|
| 28 | _context.Response.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
| 29 | if (logMissingFiles) {
|
---|
[399] | 30 | Log.Warning ("[Web] Static: FileNotFound: \"" + _context.RequestPath + "\" @ \"" + datapath + fn + "\"");
|
---|
[391] | 31 | }
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.