diff --git a/index.js b/index.js index 74693ed..90d9e59 100644 --- a/index.js +++ b/index.js @@ -166,7 +166,11 @@ function createFastCGIHandler(options) { var processedPacket = parseFastCGIPacket(packet); if (processedPacket.requestID != requestID) return; //Drop the packet if (processedPacket.type == STDOUT) { - if(processedPacket.content.length > 0) emulatedStdout.push(processedPacket.content); + try { + if(processedPacket.content.length > 0) emulatedStdout.push(processedPacket.content); + } catch (err) { + //STDOUT will be lost instead of crashing the server + } } else if (processedPacket.type == STDERR) { try { if(processedPacket.content.length > 0) emulatedStderr.push(processedPacket.content); @@ -174,7 +178,10 @@ function createFastCGIHandler(options) { //STDERR will be lost anyway... } } else if (processedPacket.type == END_REQUEST && processedPacket.content.length > 5) { - if (typeof socket !== "undefined") socket.removeListener("data", fastCGISocketHandler); + if (typeof socket !== "undefined") { + socket.removeListener("data", fastCGISocketHandler); + processFastCGIPacket = function() {}; + } var appStatus = processedPacket.content.readUInt32BE(0); var protocolStatus = processedPacket.content.readUInt8(4); if (protocolStatus != REQUEST_COMPLETE) { @@ -189,7 +196,7 @@ function createFastCGIHandler(options) { err = new Error("Multiplexed connections not supported by the FastCGI application"); } emulatedStdout.push(null); - if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null) { + if (emulatedStdout._readableState && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { emulatedStdout.on("end", function() { emulatedStderr.push(null); eventEmitter.emit("error", err); @@ -200,7 +207,7 @@ function createFastCGIHandler(options) { } } else { emulatedStdout.push(null); - if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null) { + if (emulatedStdout._readableState && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { emulatedStdout.on("end", function() { emulatedStderr.push(null); eventEmitter.emit("exit", appStatus, null); @@ -279,6 +286,8 @@ function createFastCGIHandler(options) { eventEmitter.emit("error", err); }).on("data", fastCGISocketHandler); + eventEmitter.socket = socket; + return eventEmitter; } @@ -461,7 +470,11 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e } }); - handler.on("connect", function () { + res.on("close", function() { + if(handler.stdout) handler.stdout.unpipe(res); //Prevent server crashes with write after the end + }); + + function handlerConnection() { handler.init(); handler.stdout.on("data", dataHandler); handler.stderr.on("data", function (data) { @@ -484,10 +497,12 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e serverconsole.errmessage(preparedStderr); } handler.stdout.unpipe(res); //Prevent server crashes with write after the end - res.end(); + if(!res.finished) res.end(); } }); - }); + } + if(typeof handler.socket.connecting == "undefined" || handler.socket.connecting) handler.on("connect", handlerConnection); + else if(!handler.socket.destroyed) handlerConnection(); } function executeFastCGIWithEnv(a, b, req, res, pubip, port, software, dh, user, cPath) { diff --git a/mod.info b/mod.info index be13921..3600377 100755 --- a/mod.info +++ b/mod.info @@ -1,4 +1,4 @@ { "name": "GreenRhombus FastCGI client for SVR.JS", - "version": "1.0.2" + "version": "1.0.3" }