Line | |
---|
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 | try {
|
---|
27 | string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
28 |
|
---|
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);
|
---|
34 | } else {
|
---|
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;
|
---|
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.