1
0
Fork 0
forked from svrjs/svrjs

Replaced path.extname function with regex-based custom one. Also remove "pth" variable from static file serving function

This commit is contained in:
Dorian Niemiec 2024-05-01 12:08:49 +00:00
parent 34fdd89d37
commit 255963c954

66
svr.js
View file

@ -3241,12 +3241,13 @@ if (!cluster.isPrimary) {
} }
try { try {
head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header //head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header
foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer //foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer
} catch (err) { } catch (err) {
callServerError(500, err); callServerError(500, err);
} }
// Function to perform HTTP redirection to a specified destination URL // Function to perform HTTP redirection to a specified destination URL
function redirect(destination, isTemporary, keepMethod, customHeaders) { function redirect(destination, isTemporary, keepMethod, customHeaders) {
// If keepMethod is a object, then save it to customHeaders // If keepMethod is a object, then save it to customHeaders
@ -3382,8 +3383,9 @@ if (!cluster.isPrimary) {
var uobject = parseURL(req.url); var uobject = parseURL(req.url);
var search = uobject.search; var search = uobject.search;
var href = uobject.pathname; var href = uobject.pathname;
var ext = path.extname(href).toLowerCase(); var ext = href.match(/[^\/]\.([^.]+)$/);
ext = ext.substring(1, ext.length + 1); if(!ext) ext = "";
else ext = ext[1].toLowerCase();
var decodedHref = ""; var decodedHref = "";
try { try {
decodedHref = decodeURIComponent(href); decodedHref = decodeURIComponent(href);
@ -3394,6 +3396,7 @@ if (!cluster.isPrimary) {
return; return;
} }
if (req.headers["expect"] && req.headers["expect"] != "100-continue") { if (req.headers["expect"] && req.headers["expect"] != "100-continue") {
// Expectations not met. // Expectations not met.
callServerError(417); callServerError(417);
@ -3515,15 +3518,15 @@ if (!cluster.isPrimary) {
return; return;
} }
var pth = decodeURIComponent(href).replace(/\/+/g, "/").substring(1); var dHref = decodeURIComponent(href);
var readFrom = "./" + pth; var readFrom = "." + dHref;
var dirImagesMissing = false; var dirImagesMissing = false;
fs.stat(readFrom, function (err, stats) { fs.stat(readFrom, function (err, stats) {
if (err) { if (err) {
if (err.code == "ENOENT") { if (err.code == "ENOENT") {
if (__dirname != process.cwd() && pth.match(/^\.dirimages\/(?:(?!\.png$).)+\.png$/)) { if (__dirname != process.cwd() && dHref.match(/^\/\.dirimages\/(?:(?!\.png$).)+\.png$/)) {
dirImagesMissing = true; dirImagesMissing = true;
readFrom = __dirname + "/" + pth; readFrom = __dirname + dHref;
} else { } else {
callServerError(404); callServerError(404);
serverconsole.errmessage("Resource not found."); serverconsole.errmessage("Resource not found.");
@ -3564,25 +3567,22 @@ if (!cluster.isPrimary) {
properDirectoryListingAndStaticFileServe(); properDirectoryListingAndStaticFileServe();
} else { } else {
stats = s; stats = s;
pth = (pth + "/index.xhtml").replace(/\/+/g, "/");
ext = "xhtml"; ext = "xhtml";
readFrom = "./" + pth; readFrom = (readFrom + "/index.xhtml").replace(/\/+/g, "/");
properDirectoryListingAndStaticFileServe(); properDirectoryListingAndStaticFileServe();
} }
}); });
} else { } else {
stats = s; stats = s;
pth = (pth + "/index.htm").replace(/\/+/g, "/");
ext = "htm"; ext = "htm";
readFrom = "./" + pth; readFrom = (readFrom + "/index.htm").replace(/\/+/g, "/");
properDirectoryListingAndStaticFileServe(); properDirectoryListingAndStaticFileServe();
} }
}); });
} else { } else {
stats = s; stats = s;
pth = (pth + "/index.html").replace(/\/+/g, "/");
ext = "html"; ext = "html";
readFrom = "./" + pth; readFrom = (readFrom + "/index.html").replace(/\/+/g, "/");
properDirectoryListingAndStaticFileServe(); properDirectoryListingAndStaticFileServe();
} }
}); });
@ -3854,11 +3854,11 @@ if (!cluster.isPrimary) {
var customDirListingFooter = ""; var customDirListingFooter = "";
function getCustomDirListingHeader(callback) { function getCustomDirListingHeader(callback) {
fs.readFile(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/"), function (err, data) { fs.readFile(("." + dHref + "/.dirhead").replace(/\/+/g, "/"), function (err, data) {
if (err) { if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") { if (err.code == "ENOENT" || err.code == "EISDIR") {
if (os.platform != "win32" || href != "/") { if (os.platform != "win32" || href != "/") {
fs.readFile(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/"), function (err, data) { fs.readFile(("." + dHref + "/HEAD.html").replace(/\/+/g, "/"), function (err, data) {
if (err) { if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") { if (err.code == "ENOENT" || err.code == "EISDIR") {
callback(); callback();
@ -3884,11 +3884,11 @@ if (!cluster.isPrimary) {
} }
function getCustomDirListingFooter(callback) { function getCustomDirListingFooter(callback) {
fs.readFile(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/"), function (err, data) { fs.readFile(("." + dHref + "/.dirfoot").replace(/\/+/g, "/"), function (err, data) {
if (err) { if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") { if (err.code == "ENOENT" || err.code == "EISDIR") {
if (os.platform != "win32" || href != "/") { if (os.platform != "win32" || href != "/") {
fs.readFile(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/"), function (err, data) { fs.readFile(("." + dHref + "/FOOT.html").replace(/\/+/g, "/"), function (err, data) {
if (err) { if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") { if (err.code == "ENOENT" || err.code == "EISDIR") {
callback(); callback();
@ -3934,7 +3934,7 @@ if (!cluster.isPrimary) {
htmlFoot = "</table><hr/>" + fs.readFileSync("." + decodeURIComponent(href) + "/.maindesc".replace(/\/+/g, "/")) + htmlFoot; htmlFoot = "</table><hr/>" + fs.readFileSync("." + decodeURIComponent(href) + "/.maindesc".replace(/\/+/g, "/")) + htmlFoot;
} }
fs.readdir("." + decodeURIComponent(href), function (err, list) { fs.readdir(readFrom, function (err, list) {
try { try {
if (err) throw err; if (err) throw err;
list = list.sort(); list = list.sort();
@ -3982,7 +3982,7 @@ if (!cluster.isPrimary) {
} }
// Get stats for all files in the directory and generate the listing // Get stats for all files in the directory and generate the listing
getStatsForAllFiles(list, "." + decodeURIComponent(href), function (filelist) { getStatsForAllFiles(list, readFrom, function (filelist) {
var directoryListingRows = []; var directoryListingRows = [];
for (var i = 0; i < filelist.length; i++) { for (var i = 0; i < filelist.length; i++) {
if (filelist[i].name[0] !== ".") { if (filelist[i].name[0] !== ".") {
@ -4177,8 +4177,9 @@ if (!cluster.isPrimary) {
uobject = parseURL(req.url); uobject = parseURL(req.url);
search = uobject.search; search = uobject.search;
href = uobject.pathname; href = uobject.pathname;
ext = path.extname(href).toLowerCase(); ext = href.match(/[^\/]\.([^.]+)$/);
ext = ext.substring(1, ext.length + 1); if(!ext) ext = "";
else ext = ext[1].toLowerCase();
try { try {
decodedHref = decodeURIComponent(href); decodedHref = decodeURIComponent(href);
} catch (err) { } catch (err) {
@ -4355,8 +4356,9 @@ if (!cluster.isPrimary) {
uobject = parseURL(req.url); uobject = parseURL(req.url);
search = uobject.search; search = uobject.search;
href = uobject.pathname; href = uobject.pathname;
ext = path.extname(href).toLowerCase(); ext = href.match(/[^\/]\.([^.]+)$/);
ext = ext.substring(1, ext.length + 1); if(!ext) ext = "";
else ext = ext[1].toLowerCase();
try { try {
decodedHref = decodeURIComponent(href); decodedHref = decodeURIComponent(href);
@ -4390,8 +4392,9 @@ if (!cluster.isPrimary) {
uobject = parseURL(req.url); uobject = parseURL(req.url);
search = uobject.search; search = uobject.search;
href = uobject.pathname; href = uobject.pathname;
ext = path.extname(href).toLowerCase(); ext = href.match(/[^\/]\.([^.]+)$/);
ext = ext.substring(1, ext.length + 1); if(!ext) ext = "";
else ext = ext[1].toLowerCase();
try { try {
decodedHref = decodeURIComponent(href); decodedHref = decodeURIComponent(href);
} catch (err) { } catch (err) {
@ -4416,9 +4419,9 @@ if (!cluster.isPrimary) {
uobject = parseURL(req.url); uobject = parseURL(req.url);
search = uobject.search; search = uobject.search;
href = uobject.pathname; href = uobject.pathname;
ext = path.extname(href).toLowerCase(); ext = href.match(/[^\/]\.([^.]+)$/);
ext = ext.substring(1, ext.length + 1); if(!ext) ext = "";
else ext = ext[1].toLowerCase();
try { try {
decodedHref = decodeURIComponent(href); decodedHref = decodeURIComponent(href);
} catch (err) { } catch (err) {
@ -4451,8 +4454,9 @@ if (!cluster.isPrimary) {
uobject = parseURL(req.url); uobject = parseURL(req.url);
search = uobject.search; search = uobject.search;
href = uobject.pathname; href = uobject.pathname;
ext = path.extname(href).toLowerCase(); ext = href.match(/[^\/]\.([^.]+)$/);
ext = ext.substring(1, ext.length + 1); if(!ext) ext = "";
else ext = ext[1].toLowerCase();
try { try {
decodedHref = decodeURIComponent(href); decodedHref = decodeURIComponent(href);
} catch (err) { } catch (err) {