forked from protamail/quickjs-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·125 lines (109 loc) · 3.75 KB
/
test.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
#!/bin/env -S qjs -m
import * as http from "./http-server.js";
import * as os from "os";
import * as std from "std";
import * as path from "path.so";
import Console from "console.js";
console.log("path", Object.keys(path));
const mainProcName = (scriptArgs[0].match(/.*\/(.*)/) ?? [])[1] || scriptArgs[0];
const inspectOptions = {
maxStringLength: 100,
compact: 1
};
globalThis.console = new Console(inspectOptions);
for(let [fd,st] of [0,1,2].map(fd => [fd,http.fstat(fd)])) {
console.log(`${fd}:`, st);
}
try {
http.setProcName(mainProcName);
http.start({
listen: "0.0.0.0",
port: 7000,
minWorkers: 1,
maxWorkers: 20,
workerTimeoutSec: 300,
requestHandler: handleRequest
});
} catch (e) {
console.log(e);
console.log(e?.stack || "");
http.shutdown(); //shutdown workers
}
function handleRequest(req) {
const { h, url, method, httpMajor, httpMinor, query, originalActionPath, actionPath, remoteAddr, p } = req;
let rsp = { status: 404, h: { "Content-Type": "text/html; charset=UTF-8" }, body: `<html><head></head><body>The URL ${req.path} was not found on this server</body>` };
// if (/\/favicon.ico$/.test(req.path)) return;
console.log("request", req);
let file = req.path.replace(/^\//, "");
console.log("file", file);
if (file === "") {
rsp = {
status: 302,
h: {
Host: "localhost",
"User-Agent": "quickjs-http",
Location: "/index.html"
}
};
} else {
file = path.collapse(file);
console.log("file", file);
const type =
({
js: "application/javascript",
mjs: "application/javascript",
c: "text/x-csrc",
h: "text/x-chdr",
diff: "text/x-diff",
html: "text/html",
txt: "text/plain"
}[file.replace(/.*\./g, "")] ?? "text/plain") + "; charset=utf-8";
let [obj, err] = os.stat(file);
console.log("stat:", obj);
if (!err) {
rsp = {
status: 200,
h: {
Host: "localhost",
"User-Agent": "quickjs-http",
"Content-Type": type,
"Cache-Control": "no-cache",
Date: new Date(obj.mtime).toUTCString()
},
body: std.loadFile(file, "utf-8")
};
}
}
console.log("rsp", rsp);
return rsp;
}
function simpleFetchUrl(host, port, req) {
var conn = http.connect(host, port);
http.sendHttpRequest(conn, req);
var resp = http.recvHttpResponse(conn, req.maxBodySize || -1);
http.close(conn);
return resp;
}
function simpleSendMail(host, port, from, to, subj, text) {
var conn = http.connect("10.8.1.1", 587);
assertResp("220 ");
assertResp("250 ", `ehlo localhost\n`);
assertResp("250 ", `mail from: ${from}\n`);
assertResp("250 ", `rcpt to: ${to}\n`);
assertResp("354 ", "data\n");
assertResp("250 ", `Subject: ${subj}\nFrom: ${from}\nTo: ${to}\nContent-Type: text/plain; charset=utf-8;\n\n${text}\req\n.\req\n`);
http.sendString(conn, "quit\n");
http.close(conn);
function assertResp(respStart, cmd) {
if (cmd) http.sendString(conn, cmd);
let resp = http.recvLine(conn);
if (resp.indexOf(respStart) != 0) {
while (1) {
//could receive multiple lines in rsp to ehlo
if (resp.match(/^\d\d\d /gm)) break;
resp += http.recvLine(conn);
}
if (!resp.match(new RegExp(`^${respStart}`, "mg"))) throw new Error(`Unexpected reply: ${resp} in rsp to: ${cmd}`);
}
}
}