diff --git a/History.md b/History.md index 1fd7337..7836590 100644 --- a/History.md +++ b/History.md @@ -4,6 +4,7 @@ unreleased * Fix content negotiation when no `Accept` header * Properly support all HTTP methods * Support vanilla node.js http servers + * Treat `ENAMETOOLONG` as code 414 1.0.3 / 2014-05-20 ================== diff --git a/index.js b/index.js index 3fa44cf..42f4d50 100644 --- a/index.js +++ b/index.js @@ -109,9 +109,16 @@ exports = module.exports = function serveIndex(root, options){ // check if we have a directory fs.stat(path, function(err, stat){ - if (err) return 'ENOENT' == err.code - ? next() - : next(err); + if (err && err.code === 'ENOENT') { + return next(); + } + + if (err) { + err.status = err.code === 'ENAMETOOLONG' + ? 414 + : 500; + return next(err); + } if (!stat.isDirectory()) return next(); diff --git a/test/test.js b/test/test.js index 74bffc6..1321652 100644 --- a/test/test.js +++ b/test/test.js @@ -75,6 +75,15 @@ describe('serveIndex(root)', function () { .expect(404, 'Not Found', done) }) + it('should treat an ENAMETOOLONG as a 414', function (done) { + var path = Array(11000).join('foobar') + var server = createServer() + + request(server) + .get('/' + path) + .expect(414, done) + }) + it('should skip non-directories', function (done) { var server = createServer() @@ -373,7 +382,7 @@ function createServer(dir, opts) { return http.createServer(function (req, res) { _serveIndex(req, res, function (err) { res.statusCode = err ? (err.status || 500) : 404 - res.end(err ? err.mesage : 'Not Found') + res.end(err ? err.message : 'Not Found') }) }) }