Added the ability to route to root on StaticServer instead of 404

This commit is contained in:
Mike Nolan 2023-02-11 23:55:59 -06:00
parent 565dd7cce3
commit fe1911c3cc
1 changed files with 29 additions and 2 deletions

View File

@ -972,7 +972,7 @@ namespace Tesses.WebServer
/// </summary> /// </summary>
/// <param name="path">directory for server</param> /// <param name="path">directory for server</param>
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false) /// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
public bool RedirectToRootInsteadOfNotFound {get;set;}=false;
public StaticServer(string path,bool allowListing) public StaticServer(string path,bool allowListing)
{ {
_server = new NotFoundServer(); _server = new NotFoundServer();
@ -1070,7 +1070,34 @@ namespace Tesses.WebServer
} }
break; break;
case WebServerPathType.NotFound: case WebServerPathType.NotFound:
await _server.GetAsync(ctx); if(RedirectToRootInsteadOfNotFound)
{
var fileEntry2 = fileHandler.GetPath("/");
switch(fileEntry.Type)
{
case WebServerPathType.File:
using(var strm = fileHandler.Open(fileEntry2))
await ctx.SendStreamAsync(strm,HeyRed.Mime.MimeTypesMap.GetMimeType(fileEntry.FileName));
break;
case WebServerPathType.Directory:
if(AllowListingDirectories)
{
DirectoryLister lister=new DirectoryLister();
lister.FromDirectory(fileHandler,"/",ctx.OriginalUrlPath);
await lister.GetAsync(ctx);
}
else
{
await _forbidden.GetAsync(ctx);
}
break;
case WebServerPathType.NotFound:
await _server.GetAsync(ctx);
break;
}
}else{
await _server.GetAsync(ctx);
}
break; break;
} }
}catch(UnauthorizedAccessException ex) }catch(UnauthorizedAccessException ex)