From 04dfc38fc89fe54165cc4d3d09ef377ffe87c139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Tue, 17 May 2022 15:45:29 +0200 Subject: [PATCH] serve index file instead of directory listing if available --- .../http/routing/handler/ResourceHandler.d | 31 ++++++++++++++----- source/hunt/http/server/HttpServer.d | 8 ++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/source/hunt/http/routing/handler/ResourceHandler.d b/source/hunt/http/routing/handler/ResourceHandler.d index 25b8405..7647190 100644 --- a/source/hunt/http/routing/handler/ResourceHandler.d +++ b/source/hunt/http/routing/handler/ResourceHandler.d @@ -37,6 +37,7 @@ abstract class AbstractResourceHandler : RouteHandler { * If directory listing is enabled. */ private bool _isListingEnabled = false; + private string _indexFile; private string _virtualPath; private string _basePath; // private string requestPath; @@ -99,7 +100,16 @@ abstract class AbstractResourceHandler : RouteHandler { AbstractResourceHandler isListingEnabled(bool flag) { _isListingEnabled = flag; return this; - } + } + + string indexFile() { + return _indexFile; + } + + AbstractResourceHandler indexFile(string indexFile) { + _indexFile = indexFile; + return this; + } void handle(RoutingContext context) { // string requestPath = context.getURI().getPath(); @@ -186,13 +196,20 @@ class DefaultResourceHandler : AbstractResourceHandler { } else { string requestFile = context.getAttribute(CurrentRequestFile).get!string(); if(requestFile.isDir()) { - version(HUNT_HTTP_DEBUG) { - tracef("Try to list a directory: %s", requestFile); - } - if(isListingEnabled()) { - content = renderFileList(basePath(), requestFile, format(`Index of %s`, requestPath)); + if (_indexFile && exists(buildPath(requestFile, _indexFile))) { + version(HUNT_HTTP_DEBUG) { + tracef("Serving %s for directory %s", _indexFile, requestFile); + } + handleRequestFile(context, buildPath(requestFile, _indexFile)); } else { - content = format(`Index of %s`, requestPath); + version(HUNT_HTTP_DEBUG) { + tracef("Try to list a directory: %s", requestFile); + } + if(isListingEnabled()) { + content = renderFileList(basePath(), requestFile, format(`Index of %s`, requestPath)); + } else { + content = format(`Index of %s`, requestPath); + } } } else { diff --git a/source/hunt/http/server/HttpServer.d b/source/hunt/http/server/HttpServer.d index b57e350..302d78a 100644 --- a/source/hunt/http/server/HttpServer.d +++ b/source/hunt/http/server/HttpServer.d @@ -548,14 +548,20 @@ class HttpServer : AbstractLifecycle { alias addNotFoundRoute = setDefaultRequest; Builder resource(string path, string localPath, bool canList = true, - string groupName=null, RouteGroupType groupType = RouteGroupType.Host) { + string groupName=null, RouteGroupType groupType = RouteGroupType.Host, + string indexFile = "index.html") { DefaultResourceHandler handler = new DefaultResourceHandler(amendingPath(path), localPath); handler.isListingEnabled = canList; handler.cacheTime = _resourceCacheTime; + handler.indexFile = indexFile; return resource(path, handler, groupName, groupType); } + Builder resource(string path, string localPath, string indexFile) { + return resource(path, localPath, true, null, RouteGroupType.Host, indexFile); + } + private static string amendingPath(string path) { if(path.empty) return "";