-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (94 loc) · 3.2 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
const { post_bbsync, get_bbsync } = require("./bbsync");
const fs = require("fs");
const { parse } = require("querystring");
function requestHandler(req, res) {
let authorized = true;
if(req.method === "POST") {
//{{{ forms and authentication
let body = "";
let data = {};
//{{{ multipart setup
let multipartState = 0;
let multipartDelimiter;
let multipartContentLast = "";
if(req.headers["content-type"] !== undefined && req.headers["content-type"].startsWith("multipart/form-data")) {
multipartDelimiter = req.headers["content-type"].match(/(?<=boundary=).+?(?=;|\r|$)/)[0];
if(multipartDelimiter !== undefined) multipartDelimiter = "--" + multipartDelimiter;
else {
res.writeHead(500);
res.end("Internal server error, failed to get multipart form data delimiter.");
return;
}
}
let post_bbsync_instance = post_bbsync(req, res, authorized);
//}}}
req.on("data", function(chunk) {
//body += chunk.toString(req.headers["charset"]);
body += chunk.toString("latin1");
if(req.headers["content-type"] !== undefined && req.headers["content-type"].startsWith("multipart/form-data")) {
//{{{ multipart form parsing
let index = body.indexOf("\n");
//{{{ prevent content from accumulating if no newline chars
// 5 is because ending delimiter has trailing --\r before \n
if(index === -1 && multipartState === 3 && body.length > multipartDelimiter.length + 3) index = body.length;
//}}}
while(index != -1) {
if(multipartState === 0) {
if(body.startsWith(multipartDelimiter)) multipartState++;
}
else if(multipartState === 1) {
//{{{ headers
if(body.startsWith(multipartDelimiter)) {
res.writeHead(500);
res.end("Internal server error, failed to parse multipart form data at state 1.");
return;
}
if(/^\s*$/.test(body.slice(0, index))) multipartState++;
else {
post_bbsync_instance.header(body.slice(0, index));
}
//}}}
}
else if(multipartState === 2) {
//{{{ content
if(body.startsWith(multipartDelimiter)) {
multipartContentLast = multipartContentLast.slice(0, (multipartContentLast.slice(-2) === "\r\n") ? -2 : -1);
multipartState = 1;
}
if(multipartContentLast !== "") {
post_bbsync_instance.content(multipartContentLast);
}
if(body.startsWith(multipartDelimiter)) {
post_bbsync_instance.end_section();
multipartContentLast = "";
}
else {
multipartContentLast = body.slice(0, index+1);
}
//}}}
}
body = body.slice(index+1);
index = body.indexOf("\n");
}
//}}}
}
});
req.on("end", function() {
if(req.headers["content-type"] !== undefined && req.headers["content-type"].startsWith("multipart/form-data")) data = {};
else data = parse(body);
post_bbsync_instance.end();
});
//}}}
}
else {
if(get_bbsync(req, res, authorized)) return;
}
}
const http_server = require("http").createServer(requestHandler);
http_server.listen(8851, function() {});
//const options = {
// key: fs.readFileSync("key.pem"),
// cert: fs.readFileSync("cert.pem"),
//};
//const https_server = require("https").createServer(options, requestHandler);
//https_server.listen(8852, function() {});