Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / body-parser / lib / read.js
1 /*!
2  * body-parser
3  * Copyright(c) 2014-2015 Douglas Christopher Wilson
4  * MIT Licensed
5  */
6
7 'use strict'
8
9 /**
10  * Module dependencies.
11  * @private
12  */
13
14 var createError = require('http-errors')
15 var getBody = require('raw-body')
16 var iconv = require('iconv-lite')
17 var onFinished = require('on-finished')
18 var zlib = require('zlib')
19
20 /**
21  * Module exports.
22  */
23
24 module.exports = read
25
26 /**
27  * Read a request into a buffer and parse.
28  *
29  * @param {object} req
30  * @param {object} res
31  * @param {function} next
32  * @param {function} parse
33  * @param {function} debug
34  * @param {object} [options]
35  * @api private
36  */
37
38 function read(req, res, next, parse, debug, options) {
39   var length
40   var stream
41
42   // flag as parsed
43   req._body = true
44
45   var opts = options || {}
46
47   try {
48     stream = contentstream(req, debug, opts.inflate)
49     length = stream.length
50     stream.length = undefined
51   } catch (err) {
52     return next(err)
53   }
54
55   opts.length = length
56
57   var encoding = opts.encoding !== null
58     ? opts.encoding || 'utf-8'
59     : null
60   var verify = opts.verify
61
62   opts.encoding = verify
63     ? null
64     : encoding
65
66   // read body
67   debug('read body')
68   getBody(stream, opts, function (err, body) {
69     if (err) {
70       // default to 400
71       setErrorStatus(err, 400)
72
73       // echo back charset
74       if (err.type === 'encoding.unsupported') {
75         err = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
76           charset: encoding.toLowerCase()
77         })
78       }
79
80       // read off entire request
81       stream.resume()
82       onFinished(req, function onfinished() {
83         next(err)
84       })
85       return
86     }
87
88     // verify
89     if (verify) {
90       try {
91         debug('verify body')
92         verify(req, res, body, encoding)
93       } catch (err) {
94         // default to 403
95         setErrorStatus(err, 403)
96         next(err)
97         return
98       }
99     }
100
101     // parse
102     var str
103     try {
104       debug('parse body')
105       str = typeof body !== 'string' && encoding !== null
106         ? iconv.decode(body, encoding)
107         : body
108       req.body = parse(str)
109     } catch (err) {
110       err.body = str === undefined
111         ? body
112         : str
113
114       // default to 400
115       setErrorStatus(err, 400)
116
117       next(err)
118       return
119     }
120
121     next()
122   })
123 }
124
125 /**
126  * Get the content stream of the request.
127  *
128  * @param {object} req
129  * @param {function} debug
130  * @param {boolean} [inflate=true]
131  * @return {object}
132  * @api private
133  */
134
135 function contentstream(req, debug, inflate) {
136   var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
137   var length = req.headers['content-length']
138   var stream
139
140   debug('content-encoding "%s"', encoding)
141
142   if (inflate === false && encoding !== 'identity') {
143     throw createError(415, 'content encoding unsupported')
144   }
145
146   switch (encoding) {
147     case 'deflate':
148       stream = zlib.createInflate()
149       debug('inflate body')
150       req.pipe(stream)
151       break
152     case 'gzip':
153       stream = zlib.createGunzip()
154       debug('gunzip body')
155       req.pipe(stream)
156       break
157     case 'identity':
158       stream = req
159       stream.length = length
160       break
161     default:
162       throw createError(415, 'unsupported content encoding "' + encoding + '"', {
163         encoding: encoding
164       })
165   }
166
167   return stream
168 }
169
170 /**
171  * Set a status on an error object, if ones does not exist
172  * @private
173  */
174
175 function setErrorStatus(error, status) {
176   if (!error.status && !error.statusCode) {
177     error.status = status
178     error.statusCode = status
179   }
180 }