-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
139 lines (122 loc) · 4.36 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const http = require('http'), url = require('url'), fs = require('fs'), port = 9000,
types = {
'ico': 'image/x-icon', 'html': 'text/html', 'js': 'text/javascript',
'json': 'application/json', 'css': 'text/css', 'png': 'image/png',
'jpg': 'image/jpeg', 'wav': 'audio/wav', 'mp3': 'audio/mpeg',
'pdf': 'application/pdf'
}, requestListener = (req, res) => {
switch (req.method) {
case 'GET':
cookieParser(req, res);
if (!req.cookies['seen-tutorial']) {
res.setHeader('Set-Cookie', genCookie('seen-tutorial', 'true',
{sameSite: 'strict', httpOnly: true, expires: 'Tue, 19 Jan 2038 03:14:07 UTC'})); // fuck 32 bit users
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream('tutorial.html').pipe(res);
return;
}
let parsURL = url.parse(req.url, true).pathname.substring(1);
if (parsURL.startsWith('map--')) {
let p = `palyak/${parsURL.substring(5)}.json`;
if (!fs.existsSync(p)) {
res.writeHead(404, {'Content-Type': 'text/html'});
fs.createReadStream('404.html').pipe(res);
return;
}
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream(p).pipe(res);
return;
}
switch (parsURL) {
case '':
parsURL = 'index.html';
break;
case 'loc':
case 'editor':
case 'lore':
parsURL += '.html';
break;
default:
break;
}
if (!fs.existsSync(parsURL)) {
res.writeHead(404, {'Content-Type': 'text/html'});
fs.createReadStream('404.html').pipe(res);
return;
}
res.writeHead(200, {'content-type': types[parsURL.substring(parsURL.lastIndexOf('.') + 1)], 'content-length' : fs.statSync(parsURL).size});
fs.createReadStream(parsURL).pipe(res);
break;
default:
break;
}
}, server = http.createServer(requestListener), cookieParser = (req, res) => { // Credit: Szűcs Geri
let reportInvalidSyntax = () => {
res.writeHead(400, {'Content-Type': 'text/html'});
res.end('400 Rossz kérés: Hibás a kérés sütiket tároló fejlécének a szintaxisa');
}
const cookies = {};
if (!req.headers.cookie) {
req.cookies = {};
return;
}
const cookieString = req.headers.cookie;
let keyBuffer = '', valueBuffer = '', readingKey = true;
for (let i = 0; i < cookieString.length; i++) {
if (cookieString[i] === '=') {
if (keyBuffer === '')
return reportInvalidSyntax();
readingKey = false;
continue;
}
if (cookieString[i] === ';') {
if (keyBuffer === '')
return reportInvalidSyntax();
const num = Number(valueBuffer);
cookies[keyBuffer] = valueBuffer === 'true' ? true :
valueBuffer === 'false' ? false :
!Number.isNaN(num) ? num :
valueBuffer;
keyBuffer = '';
valueBuffer = '';
readingKey = true;
i++;
continue;
}
if (readingKey)
keyBuffer += cookieString[i];
else
valueBuffer += cookieString[i];
}
if (cookieString[cookieString.length - 1] !== ';') {
if (valueBuffer === '' || keyBuffer === '')
return reportInvalidSyntax();
const num = Number(valueBuffer);
cookies[keyBuffer] = valueBuffer === 'true' ? true :
valueBuffer === 'false' ? false :
!Number.isNaN(num) ? num :
valueBuffer;
}
req.cookies = cookies;
}, genCookie = (key, value, options) => { // Credit: Szűcs Geri
let c = `${key}=${value};`;
if (options) {
if (options.domain !== undefined)
c += ` Domain=${options.domain};`;
if (options.path !== undefined)
c += ` Path=${options.path};`;
if (options.expires !== undefined)
c += ` Expires=${options.expires};`;
if (options.maxAge !== undefined)
c += ` Max-Age=${options.maxAge};`;
if (options.sameSite !== undefined)
c += ` SameSite=${options.sameSite};`;
if (options.httpOnly)
c += ` HttpOnly;`;
if (options.secure)
c += ` Secure;`;
}
return c.substr(0, c.length - 1);
};
server.listen(port);
console.log(`Server listening on port ${port}`);