Skip to content

Commit

Permalink
return error if interpreter does not exit with 0
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnass committed Aug 27, 2012
1 parent 1c9181c commit 8fd2f7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 9 additions & 2 deletions gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ module.exports = function cgi(docroot, options) {
, path = url.pathname
, handler = options[extname(path)]

if (!next) next = function() { res.end() }
if (!next) next = function(err) {
if (err) res.writeHead(500, err)
res.end()
}

if (!handler) return next()

var script = normalize(join(docroot, path))
Expand Down Expand Up @@ -109,7 +113,10 @@ module.exports = function cgi(docroot, options) {
}
}
})
child.on('close', res.end)
child.on('exit', function(code) {
if (code === 0) return res.end()
next(handler + ' exited with code ' + code)
})
}

}
11 changes: 10 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var gateway = require('../gateway')
, http = require('http')
, request = require('supertest')
, app = http.createServer(gateway(__dirname))
, app = http.createServer(gateway(__dirname, {
'.php': 'php-cgi',
'.foo': __dirname + '/non-existing-interpreter'
}))

describe('gateway()', function() {
it('should pass on the query string', function(done) {
Expand Down Expand Up @@ -32,4 +35,10 @@ describe('gateway()', function() {
.expect('Plain Text Response\n')
.end(done)
})
it('should return 500 if interpreter is not found', function(done) {
request(app)
.get('/test.foo')
.expect(500)
.end(done)
})
})
1 change: 1 addition & 0 deletions test/test.foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test for invalid interpreters

0 comments on commit 8fd2f7c

Please sign in to comment.