serveDirectory method
Serves a directory over HTTP.
The request parameter specifies the HTTP request.
The dirPath parameter specifies the directory to serve.
Implementation
Future<void> serveDirectory(
EngineContext ctx,
String dirPath, [
String parent = '',
]) async {
final pathContext = fileSystem.path;
final baseDir = pathContext.isAbsolute(dirPath)
? dirPath
: pathContext.join(rootPath, dirPath);
// First try to serve index.html if it exists
final indexPath = pathContext.join(baseDir, 'index.html');
try {
final indexFileStat = await fileSystem.stat(indexPath);
if (indexFileStat.type == FileSystemEntityType.file) {
await _serveFile(ctx, indexPath, indexFileStat);
return;
}
} catch (_) {
// No index.html, continue to directory listing check
}
// Check if directory listing is allowed
if (!allowDirectoryListing) {
ctx.abortWithStatus(
HttpStatus.notFound,
ctx.method == 'HEAD' ? '' : 'Not Found',
);
return;
}
await _listDirectory(ctx, dirPath, parent);
}