diff --git a/lib/read.js b/lib/read.js index 7683ae4c..2b6e94fe 100644 --- a/lib/read.js +++ b/lib/read.js @@ -84,10 +84,25 @@ function read (req, res, next, parse, debug, options) { const verify = options.verify try { + const contentEncoding = (req.headers['content-encoding'] || 'identity').toLowerCase() + + debug('content-encoding "%s"', contentEncoding) + + if (options.inflate === false && contentEncoding !== 'identity') { + throw createError(415, 'content encoding unsupported', { + encoding: contentEncoding, + type: 'encoding.unsupported' + }) + } + // get the content stream - stream = contentstream(req, debug, options.inflate) - length = stream.length - stream.length = undefined + if (contentEncoding === 'identity') { + length = req.headers['content-length'] + stream = req + } else { + stream = createDecompressionStream(contentEncoding, debug) + req.pipe(stream) + } } catch (err) { return next(err) } @@ -171,38 +186,6 @@ function read (req, res, next, parse, debug, options) { }) } -/** - * Get the content stream of the request. - * - * @param {Object} req - * @param {Function} debug - * @param {boolean} inflate - * @returns {Object} - * @private - */ -function contentstream (req, debug, inflate) { - const encoding = (req.headers['content-encoding'] || 'identity').toLowerCase() - const length = req.headers['content-length'] - - debug('content-encoding "%s"', encoding) - - if (inflate === false && encoding !== 'identity') { - throw createError(415, 'content encoding unsupported', { - encoding: encoding, - type: 'encoding.unsupported' - }) - } - - if (encoding === 'identity') { - req.length = length - return req - } - - const stream = createDecompressionStream(encoding, debug) - req.pipe(stream) - return stream -} - /** * Create a decompression stream for the given encoding. * @param {string} encoding