1
0
Fork 0
mirror of https://gitlab.com/sbrl/GalleryShare.git synced 2018-06-12 22:45:16 +00:00

(untested) Extensively refactor request handler.

This commit is contained in:
Starbeamrainbowlabs 2016-07-06 20:03:03 +01:00
parent 40be688f26
commit 008cb25636

View file

@ -11,6 +11,14 @@ using System.Reflection;
namespace GalleryShare
{
enum OutputFunction
{
None,
SpecialFile,
DirectoryListing,
SendFile
}
class GalleryServer
{
int port;
@ -60,47 +68,81 @@ namespace GalleryShare
/// <param name="cycle">The Http request to handle.</param>
private async Task Handle(HttpListenerContext cycle)
{
if(cycle.Request.RawUrl == @"/!Transform-DirListing.xslt")
OutputFunction outFunction = OutputFunction.None;
if (cycle.Request.RawUrl.StartsWith("/!"))
outFunction = OutputFunction.SpecialFile;
string requestedPath = GetFullReqestedPath(cycle.Request.RawUrl);
if (Directory.Exists(requestedPath))
outFunction = OutputFunction.DirectoryListing;
if (File.Exists(requestedPath))
outFunction = OutputFunction.SendFile;
switch(outFunction)
{
/*string[] resNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach (string resName in resNames)
Console.WriteLine(resName);*/
cycle.Response.ContentType = "text/xsl";
byte[] xsltData = await Utilities.GetEmbeddedResourceContent(@"GalleryShare.XSLT.DirectoryListing.xslt");
await cycle.Response.OutputStream.WriteAsync(xsltData, 0, xsltData.Length);
case OutputFunction.SpecialFile:
await sendSpecialFile(cycle);
break;
case OutputFunction.DirectoryListing:
cycle.Response.ContentType = "text/xml";
await sendDirectoryListing(cycle.Response.OutputStream, cycle.Request.RawUrl, requestedPath);
break;
case OutputFunction.SendFile:
break;
default:
await sendMessage(cycle, 404, "Error: File or directory '{0}' not found.", requestedPath);
break;
}
logCycle(cycle);
cycle.Response.Close();
return;
}
string requestedPath = Path.GetFullPath(Path.Combine(servingDirectory, "." + cycle.Request.RawUrl));
if (!File.Exists(requestedPath) && !Directory.Exists(requestedPath))
private string GetFullReqestedPath(string rawUrl)
{
await sendMessage(cycle, 404, "Error: File or directory '{0}' not found.", requestedPath);
logCycle(cycle);
return;
return Path.GetFullPath(Path.Combine(servingDirectory, "." + rawUrl));
}
FileAttributes reqPathAttrs = File.GetAttributes(requestedPath);
private async Task sendMessage(HttpListenerContext cycle, int statusCode, string message, params object[] paramObjects)
{
StreamWriter responseData = new StreamWriter(cycle.Response.OutputStream);
if(reqPathAttrs.HasFlag(FileAttributes.Directory))
cycle.Response.StatusCode = statusCode;
await responseData.WriteLineAsync(string.Format(message, paramObjects));
/*responseData.Close();
cycle.Response.Close();*/
}
private void logCycle(HttpListenerContext cycle)
{
Console.WriteLine("[{0}] [{1}] [{2}] {3} {4}",
DateTime.Now.ToString("hh:mm tt"),
cycle.Request.RemoteEndPoint,
cycle.Response.StatusCode,
cycle.Request.HttpMethod,
cycle.Request.RawUrl
);
}
private async Task sendDirectoryListing(Stream outgoingData, string rawUrl, string requestedPath)
{
List<string> dirFiles = new List<string>(Directory.GetFiles(requestedPath));
List<string> dirDirectories = new List<string>(Directory.GetDirectories(requestedPath));
cycle.Response.ContentType = "text/xml";
await responseData.FlushAsync();
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Async = true;
XmlWriter xmlData = XmlWriter.Create(cycle.Response.OutputStream, writerSettings);
XmlWriter xmlData = XmlWriter.Create(outgoingData, writerSettings);
await xmlData.WriteStartDocumentAsync();
await xmlData.WriteProcessingInstructionAsync("xsl-stylesheet", "type=\"text/xsl\" href=\"/!Transform-DirListing.xslt\"");
await xmlData.WriteStartElementAsync(null, "DirectoryListing", null);
await xmlData.WriteElementStringAsync(null, "CurrentDirectory", null, rawUrl);
await xmlData.WriteStartElementAsync(null, "Contents", null);
foreach (string directoryname in dirDirectories)
{
@ -130,31 +172,30 @@ namespace GalleryShare
await xmlData.FlushAsync();
}
logCycle(cycle);
responseData.Close();
cycle.Response.Close();
}
private async Task sendMessage(HttpListenerContext cycle, int statusCode, string message, params object[] paramObjects)
private async Task sendSpecialFile(HttpListenerContext cycle)
{
StreamWriter responseData = new StreamWriter(cycle.Response.OutputStream);
string specialFileName = cycle.Request.RawUrl.Substring(2);
string outputFileName = string.Empty;
cycle.Response.StatusCode = statusCode;
await responseData.WriteLineAsync(string.Format(message, paramObjects));
responseData.Close();
cycle.Response.Close();
}
private void logCycle(HttpListenerContext cycle)
switch(specialFileName)
{
Console.WriteLine("[{0}] [{1}] [{2}] {3} {4}",
DateTime.Now.ToString("hh:mm tt"),
cycle.Request.RemoteEndPoint,
cycle.Response.StatusCode,
cycle.Request.HttpMethod,
cycle.Request.RawUrl
);
case "Transform-DirListing.xslt":
outputFileName = @"GalleryShare.XSLT.DirectoryListing.xslt";
break;
}
if (outputFileName == string.Empty)
{
await sendMessage(cycle, 404, "Error: Unknown special file '{0}' requested.", specialFileName);
return;
}
/*string[] resNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach (string resName in resNames)
Console.WriteLine(resName);*/
cycle.Response.ContentType = "text/xsl";
byte[] xsltData = await Utilities.GetEmbeddedResourceContent(outputFileName);
await cycle.Response.OutputStream.WriteAsync(xsltData, 0, xsltData.Length);
}
}
}