1
0
Fork 0
forked from svrjs/svrjs

Removed blocking calls from directory listing function

This commit is contained in:
Dorian Niemiec 2024-05-01 07:20:56 +02:00
parent b51ac3e171
commit 34fdd89d37

78
svr.js
View file

@ -3850,18 +3850,72 @@ if (!cluster.isPrimary) {
customHeaders["Content-Type"] = "text/html; charset=utf-8";
res.writeHead(200, http.STATUS_CODES[200], customHeaders);
// Read custom header and footer content (if available)
var customDirListingHeader = fs.existsSync(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/")) ?
fs.readFileSync(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/")).toString() :
(fs.existsSync(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/")) ?
fs.readFileSync(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/")).toString() :
"";
var customDirListingFooter = fs.existsSync(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/")) ?
fs.readFileSync(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/")).toString() :
(fs.existsSync(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/")) ?
fs.readFileSync(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/")).toString() :
"";
var customDirListingHeader = "";
var customDirListingFooter = "";
function getCustomDirListingHeader(callback) {
fs.readFile(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
if (os.platform != "win32" || href != "/") {
fs.readFile(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
callback();
} else {
callServerError(500, err);
}
} else {
customDirListingHeader = data.toString();
callback();
}
});
} else {
callback();
}
} else {
callServerError(500, err);
}
} else {
customDirListingHeader = data.toString();
callback();
}
});
}
function getCustomDirListingFooter(callback) {
fs.readFile(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
if (os.platform != "win32" || href != "/") {
fs.readFile(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
callback();
} else {
callServerError(500, err);
}
} else {
customDirListingFooter = data.toString();
callback();
}
});
} else {
callback();
}
} else {
callServerError(500, err);
}
} else {
customDirListingFooter = data.toString();
callback();
}
});
}
// Read custom header and footer content (if available)
getCustomDirListingHeader(function () {
getCustomDirListingFooter(function () {
// Check if custom header has HTML tag
var headerHasHTMLTag = customDirListingHeader.replace(/<!--(?:(?:(?!--\>)[\s\S])*|)(?:-->|$)/g, "").match(/<html(?![a-zA-Z0-9])(?:"(?:\\(?:[\s\S]|$)|[^\\"])*(?:"|$)|'(?:\\(?:[\s\S]|$)|[^\\'])*(?:'|$)|[^'">])*(?:>|$)/i);
@ -4045,6 +4099,8 @@ if (!cluster.isPrimary) {
}
}
});
});
});
} else {
// Directory listing is disabled, call 403 Forbidden error
callServerError(403);