Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle invalid status codes #3137

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
31 changes: 17 additions & 14 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ var charsetRegExp = /;\s*charset\s*=/;
*/

res.status = function status(code) {
this.statusCode = code;
var statusCode = parseInt(code, 10);
if (statusCode < 100 || statusCode > 999) {
statusCode = 500;
}
this.statusCode = statusCode;
return this;
};

Expand Down Expand Up @@ -112,10 +116,10 @@ res.send = function send(body) {
// res.send(body, status) backwards compat
if (typeof arguments[0] !== 'number' && typeof arguments[1] === 'number') {
deprecate('res.send(body, status): Use res.status(status).send(body) instead');
this.statusCode = arguments[1];
this.status(arguments[1]);
} else {
deprecate('res.send(status, body): Use res.status(status).send(body) instead');
this.statusCode = arguments[0];
this.status(arguments[0]);
chunk = arguments[1];
}
}
Expand All @@ -128,8 +132,8 @@ res.send = function send(body) {
}

deprecate('res.send(status): Use res.sendStatus(status) instead');
this.statusCode = chunk;
chunk = statusCodes[chunk];
this.status(chunk);
chunk = statusCodes[this.statusCode] || String(this.statusCode);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly different behavior here: "code" vs undefined for invalid codes. Happy to change it back.

}

switch (typeof chunk) {
Expand Down Expand Up @@ -228,10 +232,10 @@ res.json = function json(obj) {
// res.json(body, status) backwards compat
if (typeof arguments[1] === 'number') {
deprecate('res.json(obj, status): Use res.status(status).json(obj) instead');
this.statusCode = arguments[1];
this.status(arguments[1]);
} else {
deprecate('res.json(status, obj): Use res.status(status).json(obj) instead');
this.statusCode = arguments[0];
this.status(arguments[0]);
val = arguments[1];
}
}
Expand Down Expand Up @@ -270,10 +274,10 @@ res.jsonp = function jsonp(obj) {
// res.json(body, status) backwards compat
if (typeof arguments[1] === 'number') {
deprecate('res.jsonp(obj, status): Use res.status(status).json(obj) instead');
this.statusCode = arguments[1];
this.status(arguments[1]);
} else {
deprecate('res.jsonp(status, obj): Use res.status(status).jsonp(obj) instead');
this.statusCode = arguments[0];
this.status(arguments[0]);
val = arguments[1];
}
}
Expand Down Expand Up @@ -334,11 +338,10 @@ res.jsonp = function jsonp(obj) {
*/

res.sendStatus = function sendStatus(statusCode) {
var body = statusCodes[statusCode] || String(statusCode);

this.statusCode = statusCode;
this.status(statusCode);
this.type('txt');


var body = statusCodes[this.statusCode] || String(this.statusCode);
return this.send(body);
};

Expand Down Expand Up @@ -890,7 +893,7 @@ res.redirect = function redirect(url) {
});

// Respond
this.statusCode = status;
this.status(status);
this.set('Content-Length', Buffer.byteLength(body));

if (this.req.method === 'HEAD') {
Expand Down
36 changes: 36 additions & 0 deletions test/res.status.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,41 @@ describe('res', function(){
.expect('Created')
.expect(201, done);
})

it('should set the status to 500 if invalid', function(done){
var app = express();

app.use(function(req, res){
res.status(10000).end();
});

request(app)
.get('/')
.expect(500, done);
})

it('should handle numeric strings', function(done) {
var app = express();

app.use(function(req, res){
res.status('400').end();
});

request(app)
.get('/')
.expect(400, done);
})

it('should handle floats as integers', function(done) {
var app = express();

app.use(function(req, res){
res.status(404.04).end();
});

request(app)
.get('/')
.expect(404, done);
})
})
})