Skip to content

Commit

Permalink
don't send error if headers are already written
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnass committed Aug 30, 2012
1 parent 4dd414a commit 8e69dd8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions gateway.js
Expand Up @@ -90,17 +90,18 @@ module.exports = function gateway(docroot, options) {
env[name] = req.headers[header]
}

var child = spawn(handler, [], {
'env': env
})

req.pipe(child.stdin)
if (options.stderr) child.stderr.pipe(options.stderr)

var body
, line = []
, statusCode
, reason
, exitCode

var child = spawn(handler, [], {
'env': env
})
.on('exit', function(code) {
exitCode = code
})

child.stdout.on('data', function(buf) {
if (body) return res.write(buf)
Expand Down Expand Up @@ -133,10 +134,14 @@ module.exports = function gateway(docroot, options) {
}
}
})
child.on('exit', function(code) {
if (code === 0) return res.end()
error(500, handler + ' exited with code ' + code)
.on('end', function() {
if (exitCode && !body) error(500, handler + ' exited with code ' + exitCode)
else res.end()
})

req.pipe(child.stdin)
if (options.stderr) child.stderr.pipe(options.stderr)

})
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "gateway",
"description": "Middleware to execute CGI scripts",
"homepage": "http://github.com/fgnass/gateway",
"version": "0.0.1",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "git://github.com/fgnass/gateway.git"
Expand Down

0 comments on commit 8e69dd8

Please sign in to comment.