From d28d959bc8398df12aad68a27d6ae046d9662c4c Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Sat, 11 Feb 2017 16:02:53 +0000 Subject: [PATCH] Fix malformed content-type header causing exception on charset get (#897) --- lib/request.js | 8 +++++++- test/request/charset.js | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/request.js b/lib/request.js index d1a73ff..33c4b34 100644 --- a/lib/request.js +++ b/lib/request.js @@ -319,7 +319,13 @@ module.exports = { var type = this.get('Content-Type'); if (!type) return ''; - return contentType.parse(type).parameters.charset || ''; + try { + type = contentType.parse(type); + } catch (e) { + return ''; + } + + return type.parameters.charset || ''; }, /** diff --git a/test/request/charset.js b/test/request/charset.js index 12eb09b..2d7a582 100644 --- a/test/request/charset.js +++ b/test/request/charset.js @@ -26,5 +26,11 @@ describe('req.charset', function(){ req.header['content-type'] = 'text/plain; charset=utf-8'; req.charset.should.equal('utf-8'); }) + + it('should return "" if content-type is invalid', function(){ + var req = request(); + req.header['content-type'] = 'application/json; application/text; charset=utf-8'; + req.charset.should.equal(''); + }) }) })