Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 18 additions & 35 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down