Fix malformed content-type header causing exception on charset get (#897)

This commit is contained in:
Rui Marinho 2017-02-11 16:02:53 +00:00 committed by Yiyu He
parent 948d3efaf6
commit d28d959bc8
2 changed files with 13 additions and 1 deletions

View file

@ -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 || '';
},
/**

View file

@ -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('');
})
})
})