* Read a request into a buffer and parse. * * @param {Object} req * @param {Object} res * @param {Function} next * @param {Function} parse * @param {Function} debug * @param {Object} options * @private
(req, res, next, parse, debug, options)
| 37 | * @private |
| 38 | */ |
| 39 | function read (req, res, next, parse, debug, options) { |
| 40 | if (onFinished.isFinished(req)) { |
| 41 | debug('body already parsed') |
| 42 | next() |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | if (!('body' in req)) { |
| 47 | req.body = undefined |
| 48 | } |
| 49 | |
| 50 | // skip requests without bodies |
| 51 | if (!hasBody(req)) { |
| 52 | debug('skip empty body') |
| 53 | next() |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | debug('content-type %j', req.headers['content-type']) |
| 58 | |
| 59 | // determine if request should be parsed |
| 60 | if (!options.shouldParse(req)) { |
| 61 | debug('skip parsing') |
| 62 | next() |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | let encoding = null |
| 67 | if (options?.skipCharset !== true) { |
| 68 | encoding = getCharset(req) || options.defaultCharset |
| 69 | |
| 70 | // validate charset |
| 71 | if (!!options?.isValidCharset && !options.isValidCharset(encoding)) { |
| 72 | debug('invalid charset') |
| 73 | next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', { |
| 74 | charset: encoding, |
| 75 | type: 'charset.unsupported' |
| 76 | })) |
| 77 | return |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | let length |
| 82 | const opts = options |
| 83 | let stream |
| 84 | |
| 85 | // read options |
| 86 | const verify = opts.verify |
| 87 | |
| 88 | try { |
| 89 | // get the content stream |
| 90 | stream = contentstream(req, debug, opts.inflate) |
| 91 | length = stream.length |
| 92 | stream.length = undefined |
| 93 | } catch (err) { |
| 94 | return next(err) |
| 95 | } |
| 96 |
no test coverage detected
searching dependent graphs…