Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
#95 started
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Benjamin Ries committed Apr 10, 2021
1 parent 1e5007f commit 2aa42cd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
1 change: 1 addition & 0 deletions EcosApp/report.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
93 changes: 59 additions & 34 deletions railessentials/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class WebServer
{
public string RootDir { get; set; }
public string DefaultIndex { get; set; } = "index.html";
public string DefaultReport { get; set; } = "report.html";
private readonly HttpListener _listener = new();
private Configuration _cfg;
private readonly Sniffer _sniffer;
Expand Down Expand Up @@ -114,9 +115,9 @@ private void FillWithSubstitutions(string pathToFile)
subst.Add("{{COMMENT_END_DEBUG_BUILD}}", string.Empty);
#endif

if(_cfg?.Theme != null)
if (_cfg?.Theme != null)
{
if(string.IsNullOrEmpty(_cfg.Theme.PlanBackground))
if (string.IsNullOrEmpty(_cfg.Theme.PlanBackground))
subst.Add("{{BACKGROUND_COLOR}}", string.Empty);
else
subst.Add("{{BACKGROUND_COLOR}}", $"background-color: {_cfg.Theme.PlanBackground};");
Expand Down Expand Up @@ -201,6 +202,17 @@ private void HandleRequest(object state)
return;
}

//
// generate report page and send to client
//
if (fileName.EndsWith(DefaultReport, StringComparison.OrdinalIgnoreCase))
{
var path = GenerateReport();
SendDataToClient(path, response);
response.OutputStream.Close();
return;
}

var fullFilePath = Path.Combine(RootDir, fileName).Replace("/", "\\");
var decodedFullFilePath = Uri.UnescapeDataString(fullFilePath);
var lastIdx = decodedFullFilePath.LastIndexOf("?r=", StringComparison.OrdinalIgnoreCase);
Expand Down Expand Up @@ -229,38 +241,7 @@ private void HandleRequest(object state)
}
}

using (var fileStream = File.OpenRead(decodedFullFilePath))
{
response.ContentType = MimeTypesUtilities.GetMimeType(decodedFullFilePath);
response.ContentLength64 = new FileInfo(decodedFullFilePath).Length;
//response.AddHeader(
// "Content-Disposition",
// "Attachment; filename=\"" + Path.GetFileName(decodedFullFilePath) + "\"");
response.AddHeader("Date", DateTime.Now.ToString("r"));
response.AddHeader("Last-Modified", File.GetLastWriteTime(decodedFullFilePath).ToString("r"));

/*
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-store" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
*/
response.AddHeader("cache-control", "max-age=0");
response.AddHeader("cache-control", "no-store");
response.AddHeader("expires", "-1");
response.AddHeader("expires", "Tue, 01 Jan 1980 1:00:00 GMT");
response.AddHeader("pragma", "no-cache");

try
{
fileStream.CopyTo(response.OutputStream);
}
catch
{
// ignore
}
}
SendDataToClient(decodedFullFilePath, response);

response.OutputStream.Close();
}
Expand All @@ -270,6 +251,50 @@ private void HandleRequest(object state)
}
}

private string GenerateReport()
{
var fullFilePath = Path.Combine(RootDir, DefaultReport).Replace("/", "\\");
File.WriteAllText(fullFilePath, "Hello World!", Encoding.UTF8);
return fullFilePath;
}

private static void SendDataToClient(string filePath, HttpListenerResponse response)
{
if (string.IsNullOrEmpty(filePath)) return;
if (!File.Exists(filePath)) return;

using var fileStream = File.OpenRead(filePath);
response.ContentType = MimeTypesUtilities.GetMimeType(filePath);
response.ContentLength64 = new FileInfo(filePath).Length;
//response.AddHeader(
// "Content-Disposition",
// "Attachment; filename=\"" + Path.GetFileName(decodedFullFilePath) + "\"");
response.AddHeader("Date", DateTime.Now.ToString("r"));
response.AddHeader("Last-Modified", File.GetLastWriteTime(filePath).ToString("r"));

/*
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-store" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
*/
response.AddHeader("cache-control", "max-age=0");
response.AddHeader("cache-control", "no-store");
response.AddHeader("expires", "-1");
response.AddHeader("expires", "Tue, 01 Jan 1980 1:00:00 GMT");
response.AddHeader("pragma", "no-cache");

try
{
fileStream.CopyTo(response.OutputStream);
}
catch
{
// ignore
}
}

private void SendErrorResponse(HttpListenerResponse response, int statusCode, string statusResponse)
{
response.ContentLength64 = 0;
Expand Down

0 comments on commit 2aa42cd

Please sign in to comment.