summaryrefslogtreecommitdiff
path: root/server/http.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/http.js')
-rw-r--r--server/http.js256
1 files changed, 34 insertions, 222 deletions
diff --git a/server/http.js b/server/http.js
index 0e3baa4..c679d95 100644
--- a/server/http.js
+++ b/server/http.js
@@ -14,7 +14,7 @@ module.exports.start = function () {
http.createServer(function (req, res) {
global.res = res;
req.url_orig = req.url;
-
+
if (req.headers["x-forwarded-for"]) {
global.address = req.headers["x-forwarded-for"];
} else {
@@ -23,199 +23,48 @@ module.exports.start = function () {
log.verbose("request: " + address + "; " + req.url)
- if (req.url.startsWith("/@info")) {
- log.verbose("return info");
- file = wwwdata + req.url.slice(6);
-
- json = new Object
- json.file = req.url.slice(6)
- if (fs.existsSync(file)) {
- if (fs.lstatSync(file).isDirectory()) {
- json.type = "inode/directory"
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- } else {
- json.type = mime.lookUpType(file.split(".").pop());
- // json.type = file.split(".").pop();
- json.size = fs.lstatSync(file).size
- json.inode = fs.lstatSync(file).ino
- json.device = fs.lstatSync(file).dev
- json.mode = fs.lstatSync(file).mode
- }
- } else {
- if (json.file.startsWith("/debug")) {
- json.type = "mpws-special/debuginfo";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- } else if (json.file.startsWith("/@info")) {
- json.type = "mpws-special/fileinfo";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- } else if (json.file.startsWith("/@json")) {
- json.type = "mpws-special/fileapi";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- } else {
- json.type = "error/notfound";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- }
- }
-
- res.writeHead(200, {'Content-Type': 'application/json'});
- res.write(JSON.stringify(json));
- res.end()
- return;
- }
-
- if (req.url.startsWith("/@json")) {
- log.verbose("return json");
- file = wwwdata + req.url.slice(6);
-
- json = new Object
- json.file = req.url.slice(6)
- if (fs.existsSync(file)) {
- if (fs.lstatSync(file).isDirectory()) {
- json.type = "inode/directory"
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- json.lines = null
- } else {
- json.type = mime.lookUpType(file.split(".").pop());
- json.size = fs.lstatSync(file).size
- json.inode = fs.lstatSync(file).ino
- json.device = fs.lstatSync(file).dev
- json.mode = fs.lstatSync(file).mode
- json.lines = fs.readFileSync(file).toString().split("\n");
- }
- } else {
- if (json.file.startsWith("/debug")) {
- json.type = "mpws-special/debuginfo";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- json.lines = null
- } else if (json.file.startsWith("/@info")) {
- json.type = "mpws-special/fileinfo";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- json.lines = null
- } else if (json.file.startsWith("/@json")) {
- json.type = "mpws-special/fileapi";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- json.lines = null
- } else {
- json.type = "error/notfound";
- json.size = null
- json.inode = null
- json.device = null
- json.mode = null
- json.lines = null
- }
- }
-
- res.writeHead(200, {'Content-Type': 'application/json'});
- res.write(JSON.stringify(json));
- res.end()
- return;
- }
+ // TODO: Detect user language
+ let dlang = "en";
- if (req.url == "/debug") {
- log.verbose("return debug")
- res.writeHead(200, {'Content-Type': 'application/json'});
- var date = new Date();
- var hour = date.getHours();
- // hour = (hour < 10 ? "0" : "") + hour;
- var min = date.getMinutes();
- // min = (min < 10 ? "0" : "") + min;
- var sec = date.getSeconds();
- // sec = (sec < 10 ? "0" : "") + sec;
- var year = date.getFullYear();
- var month = date.getMonth() + 1;
- // month = (month < 10 ? "0" : "") + month;
- var day = date.getDate();
- // day = (day < 10 ? "0" : "") + day;
- debug = {
- "version": {
- "server": mpws.version,
- "kernel": process.version.replace("v", "")
- },
- "date": {
- "day": day,
- "month": month,
- "year": year,
- "hour": hour,
- "min": min,
- "sec": sec
- },
- "server": {
- "process": {
- "pid": process.pid,
- "name": process.title,
- "priority": os.getPriority(process.pid)
- },
- "software": {
- "hostname": os.hostname(),
- "arch": os.arch(),
- "platform": os.platform(),
- "release": os.release(),
- "type": os.type(),
- "uptime": os.uptime(),
- "tempfiles": os.tmpdir()
- },
- "hardware": {
- "cpus": os.cpus(),
- "network": os.networkInterfaces(),
- "memory": {
- "total": os.totalmem(),
- "free": os.freemem(),
- "used": os.totalmem() - os.freemem(),
- "process": process.memoryUsage()
- }
- }
- }
- }
- res.write(JSON.stringify(debug));
- res.end();
- return;
- }
- // res.writeHead(200, {'Content-Type': 'text/html'});
- // res.write('Node.js says hello!');
- // res.end();
frhtml = false;
ejs = false;
if (req.url.includes(".")) {
} else {
if (fs.existsSync(wwwdata + req.url + "/index.html")) {
req.url = req.url + "/index.html"
- } else if (fs.existsSync(wwwdata + req.url + "/index.fr.html")) {
- req.url = req.url + "/index.fr.html"
- frhtml = true;
} else {
req.url = req.url + "/index.ejs"
ejs = true;
}
}
- if (req.url.endsWith('.fr.html')) {
- frhtml = true;
+ if ((fs.existsSync(wwwdata + req.url) || fs.existsSync(wwwdata + req.url_orig + "/$command.json")) && !req.url.startsWith("/assets/")) {
+ if (req.url_orig.replace(/\/+/g, '/').trim() !== "/") {
+ res.writeHead(301, {"Location": "/" + dlang + req.url_orig.replace(/\/+/g, '/')});
+ } else {
+ res.writeHead(301, {"Location": "/" + dlang});
+ }
+ res.end();
+ } else if (!req.url.startsWith("/assets/")) {
+ slang = req.url.split("/")[1];
+ if (!slang.includes(".") && !slang.includes("/") && fs.existsSync(serverRoot + "/data/lang/" + slang + ".json")) {
+ global.lang = JSON.parse(fs.readFileSync(serverRoot + "/data/lang/" + slang + ".json").toString());
+ parts = req.url.split("/");
+ parts.shift();
+ parts.shift();
+ req.url = "/" + parts.join("/");
+ } else {
+ parts = req.url_orig.split("/");
+ parts.shift();
+ parts.shift();
+
+ newUrl = "/" + parts.join("/");
+ if (newUrl.replace(/\/+/g, '/').trim() !== "/") {
+ res.writeHead(301, {"Location": "/en" + newUrl.replace(/\/+/g, '/')});
+ } else {
+ res.writeHead(301, {"Location": "/en"});
+ }
+ res.end();
+ }
}
if (req.url.includes("..")) {
if (config.errors_show_trace) {
@@ -270,45 +119,8 @@ module.exports.start = function () {
}
}
}
- if (fs.existsSync(wwwdata + req.url) || fs.existsSync(wwwdata + req.url_orig + "/$command.json")) {
- if (fs.existsSync(wwwdata + req.url_orig + "/$command.json")) {
- res.writeHead(200, {
- 'Content-Type': 'application/json',
- 'Set-Cookie': '__mpws_request=' + year + month + day + hour + min + sec
- });
- filebuffer = fs.readFileSync(wwwdata + req.url_orig + "/$command.json");
- filestring = filebuffer.toString()
- filejson = JSON.parse(filestring);
- require('./commands.js')(filejson, (callback) => {
- output = JSON.stringify(callback);
- res.write(output);
- res.end();
- });
- return;
- }
- if (frhtml) {
- log.verbose("return " + mime.lookUpType((wwwdata + req.url).split(".").pop()))
- log.verbose("parser: frhtml");
- var date = new Date();
- var hour = date.getHours();
- hour = (hour < 10 ? "0" : "") + hour;
- var min = date.getMinutes();
- min = (min < 10 ? "0" : "") + min;
- var sec = date.getSeconds();
- sec = (sec < 10 ? "0" : "") + sec;
- var year = date.getFullYear();
- var month = date.getMonth() + 1;
- month = (month < 10 ? "0" : "") + month;
- var day = date.getDate();
- day = (day < 10 ? "0" : "") + day;
- res.writeHead(200, {
- 'Content-Type': mime.lookUpType((wwwdata + req.url).split(".").pop()),
- 'Set-Cookie': '__mpws_request=' + year + month + day + hour + min + sec
- });
- res.write(require('./frhtml.js').ConvertirVersHTML(fs.readFileSync(wwwdata + req.url).toString()));
- res.end();
- return;
- } else if (ejs) {
+ if (fs.existsSync(wwwdata + req.url)) {
+ if (ejs) {
log.verbose("return " + mime.lookUpType((wwwdata + req.url).split(".").pop()))
log.verbose("parser: ejs");
var date = new Date();
@@ -328,7 +140,7 @@ module.exports.start = function () {
'Set-Cookie': '__mpws_request=' + year + month + day + hour + min + sec
});
try {
- require('ejs').renderFile(wwwdata + req.url, {wwwdata, private, req, res}, (err, str) => {
+ require('ejs').renderFile(wwwdata + req.url, {wwwdata, private, req, res, slang, lang}, (err, str) => {
if (err) throw err;
res.write(str);
res.end();