aboutsummaryrefslogtreecommitdiff
path: root/server/http.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2021-12-16 23:06:28 +0100
committerMinteck <contact@minteck.org>2021-12-16 23:06:28 +0100
commit45557cdd61850abc30959c39054d29b45fefb8c2 (patch)
tree60f0ec310aae36eeb020b7ba5796bdd4b84d4607 /server/http.js
parent7769ba124e9325a4848ca2925241de0526bd6a4e (diff)
downloadwolfeye-js-45557cdd61850abc30959c39054d29b45fefb8c2.tar.gz
wolfeye-js-45557cdd61850abc30959c39054d29b45fefb8c2.tar.bz2
wolfeye-js-45557cdd61850abc30959c39054d29b45fefb8c2.zip
It's finally done!
Diffstat (limited to 'server/http.js')
-rw-r--r--server/http.js34
1 files changed, 22 insertions, 12 deletions
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 = "<div><code>Satellite Server - System Backtrace<br><br>Server Backtrace:<br>401F0000 PERMISSION_DENIED<br>00000001 SERVER_RUNTIME<br>0000001A HTTP_WEBSERVER<br><br>Kernel Backtrace:<br></code></div>"
@@ -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
+}