wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
 
'use strict';
 
var assert = require('assert-plus');
var errors = require('restify-errors');
 
var bodyReader = require('./bodyReader');
var regex = require('./utils/regex');
 
///--- API
 
/**
 * Parses json body from the request.
 *
 * @public
 * @function jsonBodyParser
 * @param    {Object}               options - an options object
 * @throws   {InvalidContentError}            on bad input
 * @returns  {Function} Handler
 */
function jsonBodyParser(options) {
    assert.optionalObject(options, 'options');
    var opts = options || {};
 
    var override = opts.overrideParams;
 
    function parseJson(req, res, next) {
        // save original body on req.rawBody and req._body
        req.rawBody = req._body = req.body;
 
        var contentType = req.getContentType();
 
        // check for empty body first, don't pay regex tax unless necessary.
        // for content type, check for exact match and any of the *+json types
        if (
            !req.body ||
            (contentType !== 'application/json' &&
                !regex.jsonContentType.test(contentType))
        ) {
            return next();
        }
 
        var params;
 
        try {
            params = JSON.parse(req.body, opts.reviver);
        } catch (e) {
            return next(
                new errors.InvalidContentError(
                    '%s',
                    'Invalid JSON: ' + e.message
                )
            );
        }
 
        if (opts.mapParams === true) {
            if (Array.isArray(params)) {
                // if req.params exists, we have url params. we can't map an
                // array safely onto req.params, throw an error.
                if (
                    req.params &&
                    Object.keys(req.params).length > 0 &&
                    !(req.params instanceof Array)
                ) {
                    return next(
                        new errors.InternalServerError(
                            'Cannot map POST body of [Array array] onto ' +
                                'req.params'
                        )
                    );
                }
                req.params = params;
            } else if (typeof params === 'object' && params !== null) {
                // else, try to merge the objects
                Object.keys(params).forEach(function forEach(k) {
                    var p = req.params[k];
 
                    if (p && !override) {
                        return;
                    }
                    req.params[k] = params[k];
                });
            } else {
                // otherwise, do a wholesale stomp, no need to merge one by one.
                req.params = params || req.params;
            }
        }
 
        req.body = params;
 
        return next();
    }
 
    var chain = [];
 
    if (!opts.bodyReader) {
        chain.push(bodyReader(opts));
    }
    chain.push(parseJson);
    return chain;
}
 
module.exports = jsonBodyParser;