X-Git-Url: http://git.squeep.com/?p=squeep-api-dingus;a=blobdiff_plain;f=lib%2Fdingus.js;h=11bdf998373789b9a9a1584ffb71f5b30661be76;hp=84c1d14b6a38e4d9050e76eaf7095588f501196d;hb=21c492b52444c5c95db2913d7429e281384a469f;hpb=e0dd51f3000ff49be0a0ebc326666dce3f7a3e6b diff --git a/lib/dingus.js b/lib/dingus.js index 84c1d14..11bdf99 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -337,12 +337,21 @@ class Dingus { /** * Return all body data from a request. * @param {http.ClientRequest} req + * @param {Number=} maximumBodySize */ - async bodyData(req) { + async bodyData(req, maximumBodySize) { const _scope = _fileScope('bodyData'); return new Promise((resolve, reject) => { const body = []; - req.on('data', (chunk) => body.push(chunk)); + let length = 0; + req.on('data', (chunk) => { + body.push(chunk); + length += Buffer.byteLength(chunk); + if (maximumBodySize && length > maximumBodySize) { + this.logger.debug(_scope, 'body data exceeded limit', { length, maximumBodySize }); + reject(new ResponseError(Enum.ErrorResponse.RequestEntityTooLarge)); + } + }); req.on('end', () => resolve(Buffer.concat(body).toString())); req.on('error', (e) => { this.logger.error(_scope, 'failed', { error: e });