2020-11-17 23:46:07 +01:00
|
|
|
|
// /**
|
|
|
|
|
// * File: FileSystemRouter.cs
|
|
|
|
|
// * Author: haraldwolff
|
|
|
|
|
// *
|
|
|
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
|
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
|
|
|
// *
|
|
|
|
|
// *
|
|
|
|
|
// **/
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
2021-12-15 10:56:33 +01:00
|
|
|
|
using ln.http.mime;
|
|
|
|
|
|
2020-11-17 23:46:07 +01:00
|
|
|
|
namespace ln.http.router
|
|
|
|
|
{
|
2022-05-27 07:25:41 +02:00
|
|
|
|
public class StaticRouter : IDisposable
|
2020-11-17 23:46:07 +01:00
|
|
|
|
{
|
2022-05-27 07:25:41 +02:00
|
|
|
|
private string _rootPath;
|
|
|
|
|
public String RootPath
|
|
|
|
|
{
|
|
|
|
|
get => _rootPath;
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
_rootPath = Path.GetFullPath(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-17 23:46:07 +01:00
|
|
|
|
|
|
|
|
|
List<string> indexNames = new List<string>();
|
|
|
|
|
public String[] IndexNames => indexNames.ToArray();
|
|
|
|
|
|
2022-05-27 07:25:41 +02:00
|
|
|
|
private HTTPServer _httpServer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public StaticRouter(HTTPServer httpServer)
|
|
|
|
|
{
|
|
|
|
|
_httpServer = httpServer;
|
|
|
|
|
httpServer.AddRouter(this.Route);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StaticRouter(HTTPServer httpServer, string path)
|
|
|
|
|
:this(path)
|
|
|
|
|
{
|
|
|
|
|
_httpServer = httpServer;
|
|
|
|
|
httpServer.AddRouter(this.Route);
|
|
|
|
|
}
|
2020-11-17 23:46:07 +01:00
|
|
|
|
public StaticRouter(string path)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
|
|
2022-05-27 07:25:41 +02:00
|
|
|
|
RootPath = path;
|
2020-11-17 23:46:07 +01:00
|
|
|
|
|
|
|
|
|
AddIndex("index.html");
|
|
|
|
|
AddIndex("index.htm");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddIndex(string indexName) => indexNames.Add(indexName);
|
|
|
|
|
public void RemoveIndex(string indexName) => indexNames.Remove(indexName);
|
|
|
|
|
|
2022-02-07 09:29:30 +01:00
|
|
|
|
public bool Route(HttpContext httpContext)
|
2020-11-17 23:46:07 +01:00
|
|
|
|
{
|
2022-02-07 09:29:30 +01:00
|
|
|
|
string finalPath = httpContext.RoutableUri.Length > 0 ? Path.Combine(RootPath, httpContext.RoutableUri.Substring(1)) : ".";
|
2020-11-17 23:46:07 +01:00
|
|
|
|
|
|
|
|
|
if (Directory.Exists(finalPath))
|
|
|
|
|
{
|
|
|
|
|
foreach (string indexName in indexNames)
|
|
|
|
|
{
|
|
|
|
|
string indexFileName = Path.Combine(finalPath, indexName);
|
|
|
|
|
if (File.Exists(indexFileName))
|
|
|
|
|
{
|
|
|
|
|
finalPath = indexFileName;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (File.Exists(finalPath))
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
2022-02-07 09:29:30 +01:00
|
|
|
|
httpContext.Response = new HttpResponse(httpContext.Request, new FileStream(finalPath, FileMode.Open, FileAccess.Read));
|
|
|
|
|
httpContext.Response.SetHeader("content-type", MimeTypeMap.GetMimeType(Path.GetExtension(finalPath)));
|
|
|
|
|
return true;
|
2020-11-17 23:46:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-07 09:29:30 +01:00
|
|
|
|
return false;
|
2020-11-17 23:46:07 +01:00
|
|
|
|
}
|
2022-05-27 07:25:41 +02:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_httpServer?.RemoveRouter(this.Route);
|
|
|
|
|
_httpServer = null;
|
|
|
|
|
}
|
2020-11-17 23:46:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|