Skip to content

Commit

Permalink
Revert "add support for next(status[, msg])"
Browse files Browse the repository at this point in the history
see senchalabs#723

This reverts commit 87ffafd.
  • Loading branch information
tj committed Jan 9, 2013
1 parent 62ebde1 commit 6db901f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/middleware/basicAuth.js
Expand Up @@ -69,13 +69,13 @@ module.exports = function basicAuth(callback, realm) {

var parts = authorization.split(' ');

if (parts.length !== 2) return next(400);
if (parts.length !== 2) return next(utils.error(400));

var scheme = parts[0]
, credentials = new Buffer(parts[1], 'base64').toString()
, index = credentials.indexOf(':');

if ('Basic' != scheme || index < 0) return next(400);
if ('Basic' != scheme || index < 0) return next(utils.error(400));

var user = credentials.slice(0, index)
, pass = credentials.slice(index + 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/csrf.js
Expand Up @@ -51,7 +51,7 @@ module.exports = function csrf(options) {
var val = value(req);

// check
if (val != token) return next(403);
if (val != token) return next(utils.error(403));

next();
}
Expand Down
7 changes: 4 additions & 3 deletions lib/middleware/directory.js
Expand Up @@ -16,6 +16,7 @@

var fs = require('fs')
, parse = require('url').parse
, utils = require('../utils')
, path = require('path')
, normalize = path.normalize
, extname = path.extname
Expand Down Expand Up @@ -66,10 +67,10 @@ exports = module.exports = function directory(root, options){
, showUp = path != root && path != root + '/';

// null byte(s), bad request
if (~path.indexOf('\0')) return next(400);
if (~path.indexOf('\0')) return next(utils.error(400));

// malicious path, forbidden
if (0 != path.indexOf(root)) return next(403);
if (0 != path.indexOf(root)) return next(utils.error(403));

// check if we have a directory
fs.stat(path, function(err, stat){
Expand All @@ -95,7 +96,7 @@ exports = module.exports = function directory(root, options){
}

// not acceptable
next(406);
next(utils.error(406));
});
});
};
Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/json.js
Expand Up @@ -68,10 +68,10 @@ exports = module.exports = function(options){
var first = buf.trim()[0];

if (0 == buf.length) {
return next(400, 'invalid json, empty body');
return next(utils.error(400, 'invalid json, empty body'));
}

if (strict && '{' != first && '[' != first) return next(400, 'invalid json');
if (strict && '{' != first && '[' != first) return next(utils.error(400, 'invalid json'));
try {
req.body = JSON.parse(buf, options.reviver);
next();
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/limit.js
Expand Up @@ -42,7 +42,7 @@ module.exports = function limit(bytes){
req._limit = true;

// limit by content-length
if (len && len > bytes) return next(413);
if (len && len > bytes) return next(utils.error(413));

// limit
req.on('data', function(chunk){
Expand Down
11 changes: 1 addition & 10 deletions lib/proto.js
Expand Up @@ -106,7 +106,7 @@ app.handle = function(req, res, out) {
, slashAdded = false
, index = 0;

function next(err, msg) {
function next(err) {
var layer, path, status, c;

if (slashAdded) {
Expand All @@ -118,15 +118,6 @@ app.handle = function(req, res, out) {
req.originalUrl = req.originalUrl || req.url;
removed = '';

// next(status, msg) support
if (typeof err === 'number') {
var status = err;
var name = http.STATUS_CODES[status];
err = new Error(msg || name);
err.name = name;
err.status = status;
}

// next callback
layer = stack[index++];

Expand Down
16 changes: 16 additions & 0 deletions lib/utils.js
Expand Up @@ -41,6 +41,22 @@ exports.mime = function(req) {
return str.split(';')[0];
};

/**
* Generate an `Error` from the given status `code`
* and optional `msg`.
*
* @param {Number} code
* @param {String} msg
* @return {Error}
* @api private
*/

exports.error = function(code, msg){
var err = new Error(msg || http.STATUS_CODES[code]);
err.status = code;
return err;
};

/**
* Return md5 hash of the given string and optional encoding,
* defaulting to hex.
Expand Down

0 comments on commit 6db901f

Please sign in to comment.