Repository navigation
Expand file tree
/
Copy pathphp-fpm.ts
More file actions
244 lines (218 loc) · 7.93 KB
/
Copy pathphp-fpm.ts
File metadata and controls
244 lines (218 loc) · 7.93 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import * as net from "net";
import * as path from "path";
import { PHPEngine } from "../PHPEngine.js";
const FCGI_VERSION_1 = 1;
const FCGI_BEGIN_REQUEST = 1;
const FCGI_ABORT_REQUEST = 2;
const FCGI_END_REQUEST = 3;
const FCGI_PARAMS = 4;
const FCGI_STDIN = 5;
const FCGI_STDOUT = 6;
const FCGI_STDERR = 7;
function makeHeader(type: number, requestId: number, contentLength: number, paddingLength: number): Buffer {
const buf = Buffer.alloc(8);
buf.writeUInt8(FCGI_VERSION_1, 0);
buf.writeUInt8(type, 1);
buf.writeUInt16BE(requestId, 2);
buf.writeUInt16BE(contentLength, 4);
buf.writeUInt8(paddingLength, 6);
buf.writeUInt8(0, 7);
return buf;
}
function writeRecord(socket: net.Socket, type: number, requestId: number, content: Buffer): void {
let offset = 0;
while (offset < content.length || content.length === 0) {
const chunkLen = Math.min(content.length - offset, 65535);
const chunk = content.subarray(offset, offset + chunkLen);
const paddingLen = (8 - (chunkLen % 8)) % 8;
const header = makeHeader(type, requestId, chunkLen, paddingLen);
socket.write(header);
if (chunkLen > 0) socket.write(chunk);
if (paddingLen > 0) socket.write(Buffer.alloc(paddingLen));
offset += chunkLen;
if (content.length === 0) break;
}
}
function parseNameValuePairs(buf: Buffer): Record<string, string> {
const params: Record<string, string> = {};
let offset = 0;
while (offset < buf.length) {
if (offset >= buf.length) break;
let nameLen = buf[offset++];
if (nameLen >> 7) {
nameLen = ((nameLen & 0x7f) << 24) | (buf[offset++] << 16) | (buf[offset++] << 8) | buf[offset++];
}
if (offset >= buf.length) break;
let valLen = buf[offset++];
if (valLen >> 7) {
valLen = ((valLen & 0x7f) << 24) | (buf[offset++] << 16) | (buf[offset++] << 8) | buf[offset++];
}
const name = buf.toString("utf8", offset, offset + nameLen);
offset += nameLen;
const value = buf.toString("utf8", offset, offset + valLen);
offset += valLen;
params[name] = value;
}
return params;
}
interface FCGIRequestState {
requestId: number;
role: number;
keepConn: boolean;
paramsBuffers: Buffer[];
params: Record<string, string>;
stdinBuffers: Buffer[];
}
export async function runFPM(port: number = 9000, host: string = "127.0.0.1"): Promise<net.Server> {
const engine = new PHPEngine();
const server = net.createServer((socket) => {
let rxBuffer = Buffer.alloc(0);
const requests: Record<number, FCGIRequestState> = Object.create(null);
socket.on("data", (chunk) => {
rxBuffer = Buffer.concat([rxBuffer, chunk]);
while (rxBuffer.length >= 8) {
const version = rxBuffer.readUInt8(0);
const type = rxBuffer.readUInt8(1);
const requestId = rxBuffer.readUInt16BE(2);
const contentLength = rxBuffer.readUInt16BE(4);
const paddingLength = rxBuffer.readUInt8(6);
const totalRecordLen = 8 + contentLength + paddingLength;
if (rxBuffer.length < totalRecordLen) {
break; // Wait for complete record
}
const content = rxBuffer.subarray(8, 8 + contentLength);
rxBuffer = rxBuffer.subarray(totalRecordLen);
if (type === FCGI_BEGIN_REQUEST) {
const role = content.readUInt16BE(0);
const flags = content.readUInt8(2);
const keepConn = Boolean(flags & 1);
requests[requestId] = {
requestId,
role,
keepConn,
paramsBuffers: [],
params: {},
stdinBuffers: [],
};
} else if (type === FCGI_PARAMS) {
const req = requests[requestId];
if (req) {
if (content.length > 0) {
req.paramsBuffers.push(content);
} else {
const fullParamsBuf = Buffer.concat(req.paramsBuffers);
req.params = parseNameValuePairs(fullParamsBuf);
}
}
} else if (type === FCGI_STDIN) {
const req = requests[requestId];
if (req) {
if (content.length > 0) {
req.stdinBuffers.push(content);
} else {
// STDIN EOF -> Execute request
executeFCGIRequest(engine, socket, req).then(() => {
delete requests[requestId];
if (!req.keepConn) {
socket.end();
}
}).catch((err) => {
const errBuf = Buffer.from(`Status: 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\nPHP-FPM Error: ${err.message || err}`);
writeRecord(socket, FCGI_STDOUT, requestId, errBuf);
writeRecord(socket, FCGI_STDOUT, requestId, Buffer.alloc(0));
const endBuf = Buffer.alloc(8);
endBuf.writeUInt32BE(1, 0); // AppStatus 1
endBuf.writeUInt8(0, 4); // ProtocolStatus complete
writeRecord(socket, FCGI_END_REQUEST, requestId, endBuf);
delete requests[requestId];
if (!req.keepConn) {
socket.end();
}
});
}
}
}
}
});
});
return new Promise((resolve) => {
server.listen(port, host, () => {
console.log(`[jsphp-fpm] FastCGI server listening on ${host}:${port}`);
resolve(server);
});
});
}
async function executeFCGIRequest(
engine: PHPEngine,
socket: net.Socket,
req: FCGIRequestState
): Promise<void> {
const p = req.params;
const scriptPath = p["SCRIPT_FILENAME"] || p["PATH_TRANSLATED"] || "";
const docRoot = p["DOCUMENT_ROOT"] || path.dirname(scriptPath);
// Parse GET parameters
const queryString = p["QUERY_STRING"] || "";
const getParams: Record<string, string> = {};
if (queryString) {
const sp = new URLSearchParams(queryString);
for (const [k, v] of sp.entries()) {
getParams[k] = v;
}
}
// Parse POST parameters
const postData = Buffer.concat(req.stdinBuffers).toString("utf8");
const postParams: Record<string, string> = {};
if (postData && p["CONTENT_TYPE"]?.includes("application/x-www-form-urlencoded")) {
const sp = new URLSearchParams(postData);
for (const [k, v] of sp.entries()) {
postParams[k] = v;
}
}
let outputText = "";
const ctx = engine.createContext({
cwd: docRoot,
stdout: (data) => { outputText += data; },
stderr: (data) => {
writeRecord(socket, FCGI_STDERR, req.requestId, Buffer.from(data));
},
superglobals: {
server: p,
get: getParams,
post: postParams,
cookie: parseCookieHeader(p["HTTP_COOKIE"] || ""),
env: process.env as Record<string, string>,
},
});
ctx.setInternalVar("hasServerResponseHandler", true);
await ctx.require(scriptPath);
const headersList = ctx.response.getHeadersList();
if (!headersList.some((h) => h.toLowerCase().startsWith("status:"))) {
headersList.unshift(`Status: ${ctx.response.statusCode}`);
}
if (!headersList.some((h) => h.toLowerCase().startsWith("content-type:"))) {
headersList.push("Content-Type: text/html; charset=utf-8");
}
const headerBlock = headersList.join("\r\n") + "\r\n\r\n";
const finalBuf = Buffer.from(headerBlock + outputText, "utf8");
writeRecord(socket, FCGI_STDOUT, req.requestId, finalBuf);
writeRecord(socket, FCGI_STDOUT, req.requestId, Buffer.alloc(0)); // EOF
const endBuf = Buffer.alloc(8);
endBuf.writeUInt32BE(0, 0); // AppStatus 0
endBuf.writeUInt8(0, 4); // ProtocolStatus COMPLETE
writeRecord(socket, FCGI_END_REQUEST, req.requestId, endBuf);
ctx.close();
}
function parseCookieHeader(cookieHeader: string): Record<string, string> {
const cookies: Record<string, string> = {};
if (!cookieHeader) return cookies;
const pairs = cookieHeader.split(";");
for (const pair of pairs) {
const idx = pair.indexOf("=");
if (idx > 0) {
const name = pair.slice(0, idx).trim();
const val = pair.slice(idx + 1).trim();
cookies[name] = decodeURIComponent(val);
}
}
return cookies;
}