Fix SPA Server and 404 on IVirtualFileSystem

RouteServer now returns NotFoundServer or another server
MountableServer now can implicitly support NotFoundServer
This commit is contained in:
Mike Nolan 2023-06-08 14:04:59 -05:00
parent e75b2048c9
commit 5e557faaaf
7 changed files with 54 additions and 12 deletions

View File

@ -15,6 +15,7 @@ namespace Tesses.WebServer.ConsoleApp
StaticServer static_server = new StaticServer(lfs,true);
static_server.AllowUpload=true;
static_server.RedirectToRootInsteadOfNotFound = true;
HttpServerListener s = new HttpServerListener(new System.Net.IPEndPoint(ip, 24240),static_server);

View File

@ -0,0 +1 @@
Hi

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>

View File

View File

@ -485,25 +485,47 @@ namespace Tesses.WebServer
public delegate void HttpAction(ServerContext ctx);
public class RouteServer : Server
{
public RouteServer() : this(new NotFoundServer())
{
}
public RouteServer(IServer otherServer)
{
this.otherServer=otherServer;
}
public List<(ShouldProcessFunc ShouldProcessFunc, HttpActionAsync Action)> Methods = new List<(ShouldProcessFunc ShouldProcessFunc, HttpActionAsync Action)>();
private IServer otherServer;
public override async Task GetAsync(ServerContext ctx)
{
await Process(ctx);
if(!await Process(ctx))
{
await Guaranteed(otherServer).GetAsync(ctx);
}
}
public override async Task PostAsync(ServerContext ctx)
{
await Process(ctx);
if(!await Process(ctx))
{
await Guaranteed(otherServer).PostAsync(ctx);
}
}
public override async Task OtherAsync(ServerContext ctx)
{
await Process(ctx);
if(!await Process(ctx))
{
await Guaranteed(otherServer).OtherAsync(ctx);
}
}
public override async Task OptionsAsync(ServerContext ctx)
{
await Process(ctx);
if(!await Process(ctx))
{
await Guaranteed(otherServer).OptionsAsync(ctx);
}
}
private async Task Process(ServerContext ctx)
private async Task<bool> Process(ServerContext ctx)
{
foreach(var (shouldProcessFunc,action) in Methods)
{
@ -514,8 +536,9 @@ namespace Tesses.WebServer
}
await action(ctx);
return;
return true;
}
return false;
}
/// <summary>
/// Adds the specified action to the route collection.

View File

@ -5,9 +5,9 @@
<PackageId>Tesses.WebServer</PackageId>
<Author>Mike Nolan</Author>
<Company>Tesses</Company>
<Version>1.0.4.0</Version>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<Version>1.0.4.1</Version>
<AssemblyVersion>1.0.4.1</AssemblyVersion>
<FileVersion>1.0.4.1</FileVersion>
<Description>A TCP Listener HTTP(s) Server</Description>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

View File

@ -604,7 +604,7 @@ namespace Tesses.WebServer
{
foreach(var def in _defaultFileNames)
{
var _name = Special.Root / path;
var _name = Special.Root / path / def; //woops we need the def here
name=_name.Path;
if(_fs.FileExists(_name))
{
@ -1072,12 +1072,13 @@ namespace Tesses.WebServer
case WebServerPathType.NotFound:
if(RedirectToRootInsteadOfNotFound)
{
var fileEntry2 = fileHandler.GetPath("/");
switch(fileEntry.Type)
switch(fileEntry2.Type)
{
case WebServerPathType.File:
using(var strm = fileHandler.Open(fileEntry2))
await ctx.SendStreamAsync(strm,HeyRed.Mime.MimeTypesMap.GetMimeType(fileEntry.FileName));
await ctx.SendStreamAsync(strm,HeyRed.Mime.MimeTypesMap.GetMimeType(fileEntry2.FileName));
break;
case WebServerPathType.Directory:
if(AllowListingDirectories)
@ -1336,6 +1337,10 @@ namespace Tesses.WebServer
public MountableServer(IServer root)
{
_root = root;
}
public MountableServer() : this(new NotFoundServer())
{
}
IServer _root;
private (string Key,IServer Value) GetFromPath(ServerContext ctx)