From 45557cdd61850abc30959c39054d29b45fefb8c2 Mon Sep 17 00:00:00 2001 From: Minteck Date: Thu, 16 Dec 2021 23:06:28 +0100 Subject: It's finally done! --- server/http.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'server/http.js') diff --git a/server/http.js b/server/http.js index fed72a7..ba2633a 100644 --- a/server/http.js +++ b/server/http.js @@ -11,9 +11,11 @@ module.exports.start = function () { global.http = require('http'); - http.createServer(function (req, res) { + http.createServer(async function (req, res) { global.res = res; + req.url_early = req.url; + if (req.url.length > 1 && req.url.endsWith("/")) { req.url = req.url.substr(0, req.url.length - 1); } @@ -30,8 +32,14 @@ module.exports.start = function () { log.verbose("request: " + address + "; " + req.url) - // TODO: Detect user language let dlang = "en"; + let ulang = req.headers["accept-language"].substr(0, 2); + + if (/^[a-z]{2}$/gm.test(ulang)) { + if (fs.existsSync(wwwdata + "/../../data/lang/" + ulang + ".json")) { + dlang = ulang; + } + } frhtml = false; ejs = false; @@ -46,9 +54,9 @@ module.exports.start = function () { } 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, '/')}); + res.writeHead(301, {"Location": "/" + dlang + req.url_orig.replace(/\/+/g, '/') + "/"}); } else { - res.writeHead(301, {"Location": "/" + dlang}); + res.writeHead(301, {"Location": "/" + dlang + "/"}); } res.end(); } else if (!req.url.startsWith("/assets/")) { @@ -66,13 +74,17 @@ module.exports.start = function () { newUrl = "/" + parts.join("/"); if (newUrl.replace(/\/+/g, '/').trim() !== "/") { - res.writeHead(301, {"Location": "/en" + newUrl.replace(/\/+/g, '/')}); + res.writeHead(301, {"Location": "/en" + newUrl.replace(/\/+/g, '/') + "/"}); } else { - res.writeHead(301, {"Location": "/en"}); + res.writeHead(301, {"Location": "/en/"}); } res.end(); } } + if (req.url_early.split("/").length === 2 && req.url_early !== "/") { + res.writeHead(301, {"Location": req.url_early + "/"}); + res.end(); + } if (req.url.includes("..")) { if (config.errors_show_trace) { trace = "
Satellite Server - System Backtrace

Server Backtrace:
401F0000 PERMISSION_DENIED
00000001 SERVER_RUNTIME
0000001A HTTP_WEBSERVER

Kernel Backtrace:
" @@ -152,11 +164,9 @@ module.exports.start = function () { } else { get = {}; } - require('ejs').renderFile(wwwdata + req.url, {wwwdata, private, req, res, slang, lang, get, fs: require('fs'), child_process: require('child_process')}, (err, str) => { - if (err) throw err; - res.write(str); - res.end(); - }) + str = await require('ejs').renderFile(wwwdata + req.url, {axios: require('axios'), address, crypto: require('crypto'), wwwdata, private, req, res, slang, lang, get, fs: require('fs'), child_process: require('child_process')}, {async: true}); + res.write(str); + res.end(); } catch (e) { res.writeHead(200, { 'Content-Type': "text/plain", @@ -208,4 +218,4 @@ module.exports.start = function () { } }).listen(config.port); log.info('Started Satellite at port ' + config.port) -} \ No newline at end of file +} -- cgit