2024-04-01 08:02:21 +02:00
|
|
|
//SVR.JS SimpleServe
|
2023-07-29 20:44:13 +02:00
|
|
|
|
2023-07-29 20:32:17 +02:00
|
|
|
var http = require("http");
|
2024-04-01 08:02:21 +02:00
|
|
|
var fs = require("fs");
|
2023-07-29 20:32:17 +02:00
|
|
|
var mime = require("mime-types");
|
2024-04-01 08:02:21 +02:00
|
|
|
var path = require("path");
|
|
|
|
var config = {};
|
|
|
|
if(fs.existsSync(__dirname + "/config.json")) {
|
|
|
|
config = JSON.parse(fs.readFileSync(__dirname + "/config.json"));
|
|
|
|
}
|
|
|
|
var port = config.port === undefined ? 80 : config.port;
|
|
|
|
var exposeServerVersion = config.exposeServerVersion === undefined ? true : config.exposeServerVersion;
|
|
|
|
if(config.wwwroot) {
|
|
|
|
process.chdir(config.wwwroot);
|
|
|
|
}
|
|
|
|
var version = "Nightly-20240401";
|
|
|
|
var server = http.createServer(function (req, res) {
|
|
|
|
var urlObject = new URL(req.url, "http://localhost");
|
|
|
|
var filename = "";
|
|
|
|
try {
|
|
|
|
filename = "." + decodeURIComponent(urlObject.pathname);
|
|
|
|
} catch(ex) {
|
|
|
|
//Malformed URI means bad request.
|
|
|
|
res.writeHead(400, "Bad Request", {
|
|
|
|
"Content-Type": "text/plain",
|
|
|
|
"Server": "SVR.JS-SimpleServe" + (exposeServerVersion ? (" " + version) : "")
|
|
|
|
});
|
|
|
|
res.end("400 Bad Request");
|
|
|
|
return;
|
2023-07-29 20:32:17 +02:00
|
|
|
}
|
2024-04-01 08:02:21 +02:00
|
|
|
filename = filename.replace(/\\/g,"/").replace(/\/\.\.?(?=\/|$)/g,"/").replace(/\/+/g,"/"); //Poor mans URL sanitizer
|
|
|
|
if(filename == "./") filename = "./index.html";
|
|
|
|
var ext = path.extname(filename).substr(1); //path.extname gives "." character, so we're using substr(1) method.
|
|
|
|
if(!config.wwwroot && (filename.toLowerCase() == "./config.json" || filename.toLowerCase() == "./" + path.basename(__filename).toLowerCase() || filename.toLowerCase().match(/^\.\/node_modules(?:$|\/)/))) {
|
|
|
|
//Prevent leakage of some files
|
|
|
|
res.writeHead(403, "Forbidden", {
|
|
|
|
"Content-Type": "text/plain",
|
|
|
|
"Server": "SVR.JS-SimpleServe" + (exposeServerVersion ? (" " + version) : "")
|
2023-08-19 22:38:58 +02:00
|
|
|
});
|
2024-04-01 08:02:21 +02:00
|
|
|
res.end("403 Forbidden");
|
2023-07-29 20:32:17 +02:00
|
|
|
}
|
2024-04-01 08:02:21 +02:00
|
|
|
fs.readFile(filename, function(err, data) {
|
|
|
|
if(err) {
|
|
|
|
if(err.code == "ENOENT") {
|
|
|
|
//ENOENT means "File doesn't exist"
|
|
|
|
res.writeHead(404, "Not Found", {
|
|
|
|
"Content-Type": "text/plain",
|
|
|
|
"Server": "SVR.JS-SimpleServe" + (exposeServerVersion ? (" " + version) : "")
|
|
|
|
});
|
|
|
|
res.end("404 Not Found");
|
2023-07-29 20:32:17 +02:00
|
|
|
} else {
|
2024-04-01 08:02:21 +02:00
|
|
|
res.writeHead(500, "Internal Server Error", {
|
|
|
|
"Content-Type": "text/plain",
|
|
|
|
"Server": "SVR.JS-SimpleServe" + (exposeServerVersion ? (" " + version) : "")
|
|
|
|
});
|
|
|
|
res.end("500 Internal Server Error! Reason: " + err.message);
|
2023-07-29 20:32:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-04-01 08:02:21 +02:00
|
|
|
res.writeHead(200, "OK", {
|
|
|
|
"Content-Type": mime.lookup(ext) || undefined,
|
|
|
|
"Server": "SVR.JS-SimpleServe" + (exposeServerVersion ? (" " + version) : "")
|
2023-07-29 20:32:17 +02:00
|
|
|
});
|
2024-04-01 08:02:21 +02:00
|
|
|
res.end(data);
|
2023-07-29 20:32:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-04-01 08:02:21 +02:00
|
|
|
server.listen(port, function() {
|
|
|
|
console.log("Started SVR.JS SimpleServe " + version + " server at port " + port + ".");
|
2023-08-19 22:38:58 +02:00
|
|
|
});
|