* 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)
| 38 | */ |
| 39 | |
| 40 | function read (req, res, next, parse, debug, options) { |
| 41 | var length |
| 42 | var opts = options |
| 43 | var stream |
| 44 | |
| 45 | // flag as parsed |
| 46 | req._body = true |
| 47 | |
| 48 | // read options |
| 49 | var encoding = opts.encoding !== null |
| 50 | ? opts.encoding |
| 51 | : null |
| 52 | var verify = opts.verify |
| 53 | |
| 54 | try { |
| 55 | // get the content stream |
| 56 | stream = contentstream(req, debug, opts.inflate) |
| 57 | length = stream.length |
| 58 | stream.length = undefined |
| 59 | } catch (err) { |
| 60 | return next(err) |
| 61 | } |
| 62 | |
| 63 | // set raw-body options |
| 64 | opts.length = length |
| 65 | opts.encoding = verify |
| 66 | ? null |
| 67 | : encoding |
| 68 | |
| 69 | // assert charset is supported |
| 70 | if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) { |
| 71 | return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', { |
| 72 | charset: encoding.toLowerCase(), |
| 73 | type: 'charset.unsupported' |
| 74 | })) |
| 75 | } |
| 76 | |
| 77 | // read body |
| 78 | debug('read body') |
| 79 | getBody(stream, opts, function (error, body) { |
| 80 | if (error) { |
| 81 | var _error |
| 82 | |
| 83 | if (error.type === 'encoding.unsupported') { |
| 84 | // echo back charset |
| 85 | _error = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', { |
| 86 | charset: encoding.toLowerCase(), |
| 87 | type: 'charset.unsupported' |
| 88 | }) |
| 89 | } else { |
| 90 | // set status code on error |
| 91 | _error = createError(400, error) |
| 92 | } |
| 93 | |
| 94 | // unpipe from stream and destroy |
| 95 | if (stream !== req) { |
| 96 | unpipe(req) |
| 97 | destroy(stream, true) |
no test coverage detected