Skip to content

Serve Index File Instead of Directory Listing if Available #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions source/hunt/http/routing/handler/ResourceHandler.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion source/hunt/http/server/HttpServer.d
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";

Expand Down