added FileSystemRouter
parent
085ee59d45
commit
787e0c860c
|
@ -26,8 +26,8 @@ namespace ln.http.tests
|
||||||
HttpRouter testRouter = new HttpRouter(server);
|
HttpRouter testRouter = new HttpRouter(server);
|
||||||
testRouter.Map(HttpMethod.ANY, "/controller/*", HttpRoutePriority.NORMAL, new TestApiController().Route);
|
testRouter.Map(HttpMethod.ANY, "/controller/*", HttpRoutePriority.NORMAL, new TestApiController().Route);
|
||||||
|
|
||||||
StaticRouter staticRouter = new StaticRouter(AppContext.BaseDirectory);
|
FileSystemRouter fileSystemRouter = new FileSystemRouter(AppContext.BaseDirectory);
|
||||||
testRouter.Map(HttpMethod.ANY, "/static/*", staticRouter.Route);
|
testRouter.Map(HttpMethod.ANY, "/static/*", fileSystemRouter.Route);
|
||||||
|
|
||||||
HttpListener.DefaultPort = 0;
|
HttpListener.DefaultPort = 0;
|
||||||
HttpListener httpListener = new HttpListener(server);
|
HttpListener httpListener = new HttpListener(server);
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
// /**
|
||||||
|
// * 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;
|
||||||
|
using ln.http.mime;
|
||||||
|
|
||||||
|
namespace ln.http
|
||||||
|
{
|
||||||
|
public class FileSystemRouter : IDisposable
|
||||||
|
{
|
||||||
|
private string _rootPath;
|
||||||
|
public String RootPath
|
||||||
|
{
|
||||||
|
get => _rootPath;
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
_rootPath = Path.GetFullPath(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<string> indexNames = new List<string>();
|
||||||
|
public String[] IndexNames => indexNames.ToArray();
|
||||||
|
|
||||||
|
private HttpServer _httpServer;
|
||||||
|
|
||||||
|
public FileSystemRouter(HttpServer httpServer, string path)
|
||||||
|
{
|
||||||
|
_httpServer = httpServer;
|
||||||
|
httpServer?.AddRouter(this.Route);
|
||||||
|
|
||||||
|
if (!Directory.Exists(path))
|
||||||
|
throw new FileNotFoundException();
|
||||||
|
|
||||||
|
RootPath = path;
|
||||||
|
Console.Error.WriteLine("FileSystemRouter created ({0})", RootPath);
|
||||||
|
|
||||||
|
AddIndex("index.html");
|
||||||
|
AddIndex("index.htm");
|
||||||
|
}
|
||||||
|
public FileSystemRouter(string path)
|
||||||
|
:this(null, path)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddIndex(string indexName) => indexNames.Add(indexName);
|
||||||
|
public void RemoveIndex(string indexName) => indexNames.Remove(indexName);
|
||||||
|
|
||||||
|
public bool Route(HttpContext httpContext)
|
||||||
|
{
|
||||||
|
string finalPath = httpContext.RoutableUri.Length > 0 ? Path.Combine(RootPath, httpContext.RoutableUri.Substring(1)) : ".";
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_httpServer?.RemoveRouter(this.Route);
|
||||||
|
_httpServer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -10,7 +10,7 @@
|
||||||
<Copyright>(c) 2020 Harald Wolff-Thobaben</Copyright>
|
<Copyright>(c) 2020 Harald Wolff-Thobaben</Copyright>
|
||||||
<PackageTags>http server</PackageTags>
|
<PackageTags>http server</PackageTags>
|
||||||
<LangVersion>9</LangVersion>
|
<LangVersion>9</LangVersion>
|
||||||
<PackageVersion>0.6.2</PackageVersion>
|
<PackageVersion>0.6.4</PackageVersion>
|
||||||
<AssemblyVersion>0.6.2.0</AssemblyVersion>
|
<AssemblyVersion>0.6.2.0</AssemblyVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue