diff --git a/test/webserver.mjs b/test/webserver.mjs index 57c913d66a9772..f9094f0fb5c161 100644 --- a/test/webserver.mjs +++ b/test/webserver.mjs @@ -162,7 +162,7 @@ class WebServer { return; } if (isDir) { - serveDirectoryIndex(filePath); + self.#serveDirectoryIndex(res, pathPart, queryPart, filePath); return; } @@ -196,97 +196,95 @@ class WebServer { } self.#serveFile(res, filePath, fileSize); } + } - function escapeHTML(untrusted) { + #serveDirectoryIndex(response, pathPart, queryPart, directory) { + const escapeHTML = untrusted => // Escape untrusted input so that it can safely be used in a HTML response // in HTML and in HTML attributes. - return untrusted + untrusted .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); - } - function serveDirectoryIndex(dir) { - res.setHeader("Content-Type", "text/html"); - res.writeHead(200); + response.setHeader("Content-Type", "text/html"); + response.writeHead(200); - if (queryPart === "frame") { - res.end( - "
', - "utf8" - ); + if (queryPart === "frame") { + response.end( + ` + + `, + "utf8" + ); + return; + } + + const all = queryPart === "all"; + fs.readdir(directory, function (error, files) { + if (error) { + response.end(); return; } - var all = queryPart === "all"; - fs.readdir(dir, function (err, files) { - if (err) { - res.end(); - return; - } - res.write( - '' + - "no files found
\n"); } - if (!all && queryPart !== "side") { - res.write( - "(only PDF files are shown, " + - 'show all)
\n' + if (label) { + response.write( + `${escapeHTML(label)}No files found
"); + } + if (!all && queryPart !== "side") { + response.write( + '(only PDF files are shown, show all)
' + ); + } + response.end(""); + }); } #serveFile(response, filePath, fileSize) {