Prevent data loss at end of the response

This commit is contained in:
Dorian Niemiec 2024-02-16 22:02:21 +01:00
parent be5e86a59d
commit 067e54e8a2
2 changed files with 21 additions and 7 deletions

Binary file not shown.

View file

@ -166,15 +166,13 @@ function createFastCGIHandler(options) {
var processedPacket = parseFastCGIPacket(packet);
if (processedPacket.requestID != requestID) return; //Drop the packet
if (processedPacket.type == STDOUT) {
emulatedStdout.push(processedPacket.content);
if(processedPacket.content.length > 0) emulatedStdout.push(processedPacket.content);
} else if (processedPacket.type == STDERR) {
emulatedStderr.push(processedPacket.content);
if(processedPacket.content.length > 0) emulatedStderr.push(processedPacket.content);
} else if (processedPacket.type == END_REQUEST && processedPacket.content.length > 5) {
if (typeof socket !== "undefined") socket.removeListener("data", fastCGISocketHandler);
var appStatus = processedPacket.content.readUInt32BE(0);
var protocolStatus = processedPacket.content.readUInt8(4);
emulatedStdout.push(null);
emulatedStderr.push(null);
if (protocolStatus != REQUEST_COMPLETE) {
var err = new Error("Unknown error");
if (protocolStatus == OVERLOADED) {
@ -186,9 +184,25 @@ function createFastCGIHandler(options) {
} else if (protocolStatus == CANT_MPX_CONN) {
err = new Error("Multiplexed connections not supported by the FastCGI application");
}
eventEmitter.emit("error", err);
emulatedStdout.push(null);
emulatedStderr.push(null);
if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null) {
emulatedStdout.on("end", function() {
eventEmitter.emit("error", err);
});
} else {
eventEmitter.emit("error", err);
}
} else {
eventEmitter.emit("exit", appStatus, null);
emulatedStdout.push(null);
emulatedStderr.push(null);
if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null) {
emulatedStdout.on("end", function() {
eventEmitter.emit("exit", appStatus, null);
});
} else {
eventEmitter.emit("exit", appStatus, null);
}
}
}
}
@ -441,7 +455,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
stderr += data.toString();
});
req.pipe(handler.stdin);
handler.on("exit", (code, signal) => {
handler.on("exit", function (code, signal) {
if (!cned && (signal || code !== 0)) {
var ex = new Error("Process execution failed!" + (stderr ? " Reason: " + stderr.trim() : ""));
if (!callServerError) {