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

master
Rui Marinho 2017-02-13 03:05:35 +00:00 committed by Yiyu He
parent 2db3b1b49a
commit 7ae9c3e109
2 changed files with 14 additions and 2 deletions

View File

@ -315,10 +315,16 @@ module.exports = {
*/
get charset() {
const type = this.get('Content-Type');
let 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', () => {
req.header['content-type'] = 'text/plain; charset=utf-8';
req.charset.should.equal('utf-8');
});
it('should return "" if content-type is invalid', () => {
const req = request();
req.header['content-type'] = 'application/json; application/text; charset=utf-8';
req.charset.should.equal('');
});
});
});