2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
13 private string rootFolder;
14 ConcurrentDictionary<string, Tuple<byte[], DateTime>> fileCache =
new ConcurrentDictionary<string, Tuple<byte[], DateTime>>();
15 List<string> toRemoveCache =
new List<string>();
18 this.rootFolder = Path.GetFullPath(rootFolder);
20 #region Interface Implementation 25 if(request.RequestURI ==
@"/")
27 request.RequestURI =
@"/index.html";
29 string requestFile = this.rootFolder + request.RequestURI;
30 if(requestFile.IsSubPathOf(
this.rootFolder) && File.Exists(requestFile))
36 if (fileCache.ContainsKey(requestFile))
38 bodyData = fileCache[requestFile].Item1;
42 bodyData = File.ReadAllBytes(requestFile);
47 Tuple<byte[], DateTime> tuple =
new Tuple<byte[], DateTime>(bodyData, DateTime.Now);
48 fileCache.AddOrUpdate(requestFile, tuple, (key, oldValue) => oldValue = tuple);
52 response.Body = bodyData;
54 response[
HttpMessage.EntityHeadersEnum.ContentType] =
"text/html";
55 if (!fileCache.ContainsKey(requestFile))
57 server.LogDebug(
"Adding " + requestFile +
" to server cache.");
63 server.LogDebug(
"File not found: " + requestFile);
64 response.StatusCode =
HttpMessage.StatusCodes.NotFound;
72 foreach(KeyValuePair<
string, Tuple<
byte[], DateTime>> keyValuePair
in fileCache)
74 if((DateTime.Now - keyValuePair.Value.Item2).TotalSeconds > 60)
76 toRemoveCache.Add(keyValuePair.Key);
79 foreach(
string fileName
in toRemoveCache)
81 Tuple<byte[], DateTime> tp;
82 while(!fileCache.TryRemove(fileName, out tp)) { };
83 server.LogDebug(
"Removed " + fileName +
" from cache.");
85 toRemoveCache.Clear();
void Tick(Server.Server server, HttpHandler handler)
Perform periodic operations.
Structure containing client information.
Handler for handling server http requests.
Server()
Creates an instance of server with default values.
Basic server class to handle TCP connections.
bool HandleRequest(Server.Server server, HttpHandler handler, ClientData client, HttpMessage request, ref HttpMessage response)
Handle the received request.
Interface for Http modules used by the server http handler.