Fix malformed content-type header causing exception on charset get (#897)
This commit is contained in:
parent
948d3efaf6
commit
d28d959bc8
2 changed files with 13 additions and 1 deletions
|
@ -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 || '';
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -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('');
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue