From 5e557faaafe7b6b2a31c06f36fe412b253208288 Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Thu, 8 Jun 2023 14:04:59 -0500 Subject: [PATCH] Fix SPA Server and 404 on IVirtualFileSystem RouteServer now returns NotFoundServer or another server MountableServer now can implicitly support NotFoundServer --- Tesses.WebServer.FileServer/Program.cs | 1 + .../files/apple/demi-lovato/file.txt | 1 + Tesses.WebServer.FileServer/files/index.html | 12 +++++++ Tesses.WebServer.FileServer/files/table | 0 .../SimpleHttpCode.cs | 35 +++++++++++++++---- .../Tesses.WebServer.NetStandard.csproj | 6 ++-- Tesses.WebServer.NetStandard/TessesServer.cs | 11 ++++-- 7 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 Tesses.WebServer.FileServer/files/apple/demi-lovato/file.txt create mode 100644 Tesses.WebServer.FileServer/files/index.html create mode 100644 Tesses.WebServer.FileServer/files/table diff --git a/Tesses.WebServer.FileServer/Program.cs b/Tesses.WebServer.FileServer/Program.cs index a91b268..32f7e71 100644 --- a/Tesses.WebServer.FileServer/Program.cs +++ b/Tesses.WebServer.FileServer/Program.cs @@ -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); diff --git a/Tesses.WebServer.FileServer/files/apple/demi-lovato/file.txt b/Tesses.WebServer.FileServer/files/apple/demi-lovato/file.txt new file mode 100644 index 0000000..b14df64 --- /dev/null +++ b/Tesses.WebServer.FileServer/files/apple/demi-lovato/file.txt @@ -0,0 +1 @@ +Hi diff --git a/Tesses.WebServer.FileServer/files/index.html b/Tesses.WebServer.FileServer/files/index.html new file mode 100644 index 0000000..ef7d251 --- /dev/null +++ b/Tesses.WebServer.FileServer/files/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +

Hello

+ + \ No newline at end of file diff --git a/Tesses.WebServer.FileServer/files/table b/Tesses.WebServer.FileServer/files/table new file mode 100644 index 0000000..e69de29 diff --git a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs index c3ca0a0..da0d5c2 100644 --- a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs +++ b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs @@ -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 Process(ServerContext ctx) { foreach(var (shouldProcessFunc,action) in Methods) { @@ -514,8 +536,9 @@ namespace Tesses.WebServer } await action(ctx); - return; + return true; } + return false; } /// /// Adds the specified action to the route collection. diff --git a/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj b/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj index 44fb39d..4642ce6 100644 --- a/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj +++ b/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj @@ -5,9 +5,9 @@ Tesses.WebServer Mike Nolan Tesses - 1.0.4.0 - 1.0.4.0 - 1.0.4.0 + 1.0.4.1 + 1.0.4.1 + 1.0.4.1 A TCP Listener HTTP(s) Server GPL-3.0-only true diff --git a/Tesses.WebServer.NetStandard/TessesServer.cs b/Tesses.WebServer.NetStandard/TessesServer.cs index 5ca5946..7120e23 100644 --- a/Tesses.WebServer.NetStandard/TessesServer.cs +++ b/Tesses.WebServer.NetStandard/TessesServer.cs @@ -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)