Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cgi.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ function cgi(cgiBin, options) {
var cgiSpawn = spawn(cgiBin, opts.args, opts);
debug('cgi spawn (pid: %o)', cgiSpawn.pid);

var exited = false;

if (options.timeout) {
setTimeout(function() {
if (!exited) cgiSpawn.kill();
}, options.timeout * 1000);
}

// The request body is piped to 'stdin' of the CGI spawn
req.pipe(cgiSpawn.stdin);

Expand Down Expand Up @@ -146,6 +154,7 @@ function cgi(cgiBin, options) {

cgiSpawn.on('exit', function(code, signal) {
debug('cgi spawn %o "exit" event (code %o) (signal %o)', cgiSpawn.pid, code, signal);
exited = true;
// TODO: react on a failure status code (dump stderr to the response?)
});

Expand All @@ -154,6 +163,11 @@ function cgi(cgiBin, options) {
debug('cgi spawn %o stdout "end" event', cgiSpawn.pid);
if (cgiResult) {
cgiResult.cleanup();
if (!res.headersSent) {
res.writeHead(500, { 'Content-type': 'text/plain' });
res.write('Internal Server Error');
res.end();
}
}
//if (options.stderr) {
// cgiSpawn.stderr.unpipe(options.stderr);
Expand Down