|
| 1 | +/*! |
| 2 | + * body-parser |
| 3 | + * Copyright(c) 2014 Jonathan Ong |
| 4 | + * Copyright(c) 2014-2015 Douglas Christopher Wilson |
| 5 | + * MIT Licensed |
| 6 | + */ |
| 7 | + |
| 8 | +'use strict' |
| 9 | + |
| 10 | +/** |
| 11 | + * Module dependencies. |
| 12 | + * @private |
| 13 | + */ |
| 14 | + |
| 15 | +var bytes = require('bytes') |
| 16 | +var contentType = require('content-type') |
| 17 | +var createError = require('http-errors') |
| 18 | +var debug = require('debug')('body-parser:generic') |
| 19 | +var isFinished = require('on-finished').isFinished |
| 20 | +var read = require('./read') |
| 21 | +var typeis = require('type-is') |
| 22 | + |
| 23 | +/** |
| 24 | + * Module exports. |
| 25 | + */ |
| 26 | + |
| 27 | +module.exports = generic |
| 28 | + |
| 29 | +/** |
| 30 | + * Use this to create a middleware that parses request bodies |
| 31 | + * |
| 32 | + * @param {object} [options] |
| 33 | + * @return {function} |
| 34 | + * @public |
| 35 | + */ |
| 36 | + |
| 37 | +function generic (parserOptions, parserOverrides) { |
| 38 | + // Squash the options and the overrides down into one object |
| 39 | + var opts = Object.create(parserOptions) |
| 40 | + Object.assign(opts, parserOverrides) |
| 41 | + |
| 42 | + var limit = typeof opts.limit !== 'number' |
| 43 | + ? bytes.parse(opts.limit || '100kb') |
| 44 | + : opts.limit |
| 45 | + var charset = opts.charset |
| 46 | + var inflate = opts.inflate !== false |
| 47 | + var verify = opts.verify || false |
| 48 | + var parse = opts.parse || defaultParse |
| 49 | + var defaultReqCharset = opts.defaultCharset || 'utf-8' |
| 50 | + var type = opts.type |
| 51 | + |
| 52 | + if (verify !== false && typeof verify !== 'function') { |
| 53 | + throw new TypeError('option verify must be function') |
| 54 | + } |
| 55 | + |
| 56 | + // create the appropriate type checking function |
| 57 | + var shouldParse = typeof type !== 'function' |
| 58 | + ? typeChecker(type) |
| 59 | + : type |
| 60 | + |
| 61 | + // create the appropriate charset validating function |
| 62 | + var validCharset = typeof charset !== 'function' |
| 63 | + ? charsetValidator(charset) |
| 64 | + : charset |
| 65 | + |
| 66 | + return function genericParser (req, res, next) { |
| 67 | + if (isFinished(req)) { |
| 68 | + debug('body already parsed') |
| 69 | + next() |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + if (!('body' in req)) { |
| 74 | + req.body = undefined |
| 75 | + } |
| 76 | + |
| 77 | + // skip requests without bodies |
| 78 | + if (!typeis.hasBody(req)) { |
| 79 | + debug('skip empty body') |
| 80 | + next() |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + debug('content-type %j', req.headers['content-type']) |
| 85 | + |
| 86 | + // determine if request should be parsed |
| 87 | + if (!shouldParse(req)) { |
| 88 | + debug('skip parsing') |
| 89 | + next() |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // assert charset per RFC 7159 sec 8.1 |
| 94 | + var reqCharset = null |
| 95 | + if (charset !== undefined) { |
| 96 | + reqCharset = getCharset(req) || defaultReqCharset |
| 97 | + if (!validCharset(reqCharset)) { |
| 98 | + debug('invalid charset') |
| 99 | + next(createError(415, 'unsupported charset "' + reqCharset.toUpperCase() + '"', { |
| 100 | + charset: reqCharset, |
| 101 | + type: 'charset.unsupported' |
| 102 | + })) |
| 103 | + return |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // read |
| 108 | + read(req, res, next, parse, debug, { |
| 109 | + encoding: reqCharset, |
| 110 | + inflate: inflate, |
| 111 | + limit: limit, |
| 112 | + verify: verify |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +function defaultParse (buf) { |
| 118 | + return buf |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Get the charset of a request. |
| 123 | + * |
| 124 | + * @param {object} req |
| 125 | + * @api private |
| 126 | + */ |
| 127 | + |
| 128 | +function getCharset (req) { |
| 129 | + try { |
| 130 | + return (contentType.parse(req).parameters.charset || '').toLowerCase() |
| 131 | + } catch (e) { |
| 132 | + return undefined |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Get the simple type checker. |
| 138 | + * |
| 139 | + * @param {string} type |
| 140 | + * @return {function} |
| 141 | + */ |
| 142 | + |
| 143 | +function typeChecker (type) { |
| 144 | + return function checkType (req) { |
| 145 | + return Boolean(typeis(req, type)) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Get the simple charset validator. |
| 151 | + * |
| 152 | + * @param {string} type |
| 153 | + * @return {function} |
| 154 | + */ |
| 155 | + |
| 156 | +function charsetValidator (charset) { |
| 157 | + return function validateCharset (reqCharset) { |
| 158 | + return charset === reqCharset |
| 159 | + } |
| 160 | +} |
0 commit comments