From 1421b2afcb233dfbe81ddc27380d6638b3be88b3 Mon Sep 17 00:00:00 2001 From: Johan Vromans Date: Thu, 15 Aug 2024 15:44:08 +0200 Subject: [PATCH 1/2] Append index.html to directory urls. --- lib/get-file-path-from-url.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/get-file-path-from-url.js b/lib/get-file-path-from-url.js index 22dc157..e7b25b2 100644 --- a/lib/get-file-path-from-url.js +++ b/lib/get-file-path-from-url.js @@ -33,7 +33,10 @@ function getFilePathFromUrl(url, basePath, { pathLib = path, baseHref = '' } = { } } - const absolutePath = pathLib.join(basePath, relativePath); + let absolutePath = pathLib.join(basePath, relativePath); + if ( absolutePath.endsWith(pathLib.sep) ) { + absolutePath += "index.html"; + } if ( !absolutePath.startsWith(basePath) || // if the path has broken out of the basePath, it should be rejected absolutePath.endsWith(pathLib.sep) // only files (not folders) can be served From 1963f85144e1b3e8c1c0247933b91efca35727c0 Mon Sep 17 00:00:00 2001 From: Johan Vromans Date: Fri, 16 Aug 2024 09:40:49 +0200 Subject: [PATCH 2/2] Serve any of the conventional default documents in path, if it exists. --- lib/get-file-path-from-url.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/get-file-path-from-url.js b/lib/get-file-path-from-url.js index e7b25b2..8d9f28d 100644 --- a/lib/get-file-path-from-url.js +++ b/lib/get-file-path-from-url.js @@ -1,4 +1,5 @@ const path = require('path'); +const fs = require('fs'); /** * Safely get the path for a file in the project directory, or reject by returning "dummy" @@ -35,8 +36,19 @@ function getFilePathFromUrl(url, basePath, { pathLib = path, baseHref = '' } = { let absolutePath = pathLib.join(basePath, relativePath); if ( absolutePath.endsWith(pathLib.sep) ) { - absolutePath += "index.html"; + // By convention, accessing a path routes to a default document in + // that directory if it exists. + // Commonly used default documents. + const defaultDocs = [ "index.html", "index.htm", "index.php" ]; + // See if one of these exists. + for ( let d of defaultDocs ) { + if ( fs.existsSync( absolutePath + d ) ) { + absolutePath += d; + break; + } + } } + if ( !absolutePath.startsWith(basePath) || // if the path has broken out of the basePath, it should be rejected absolutePath.endsWith(pathLib.sep) // only files (not folders) can be served