@@ -36,49 +36,55 @@ module.exports = read
36
36
*/
37
37
38
38
function read ( req , res , next , parse , debug , options ) {
39
- var length
40
- var opts = options
41
- var stream
42
-
43
39
// read options
44
- var encoding = opts . encoding !== null
45
- ? opts . encoding
46
- : null
47
- var verify = opts . verify
48
-
49
- try {
50
- // get the content stream
51
- stream = contentstream ( req , debug , opts . inflate )
52
- length = stream . length
53
- stream . length = undefined
54
- } catch ( err ) {
55
- return next ( err )
40
+ const charset = options . charset
41
+
42
+ // get the content stream
43
+ const contentEncoding = ( req . headers [ 'content-encoding' ] || 'identity' ) . toLowerCase ( )
44
+ debug ( 'content-encoding "%s"' , contentEncoding )
45
+
46
+ if ( options . inflate === false && contentEncoding !== 'identity' ) {
47
+ return next ( createError ( 415 , 'content encoding unsupported' , {
48
+ encoding : contentEncoding ,
49
+ type : 'encoding.unsupported'
50
+ } ) )
56
51
}
57
52
58
- // set raw-body options
59
- opts . length = length
60
- opts . encoding = verify
61
- ? null
62
- : encoding
53
+ let stream
54
+ if ( contentEncoding === 'identity' ) {
55
+ // set raw-body expected length
56
+ stream = req
57
+ options . length = req . headers [ 'content-length' ]
58
+ } else {
59
+ try {
60
+ stream = createDecompressionStream ( contentEncoding , debug )
61
+ req . pipe ( stream )
62
+ } catch ( err ) {
63
+ return next ( err )
64
+ }
65
+ }
63
66
64
67
// assert charset is supported
65
- if ( opts . encoding === null && encoding !== null && ! iconv . encodingExists ( encoding ) ) {
66
- return next ( createError ( 415 , 'unsupported charset "' + encoding . toUpperCase ( ) + '"' , {
67
- charset : encoding . toLowerCase ( ) ,
68
+ if ( options . verify && charset !== null && ! iconv . encodingExists ( charset ) ) {
69
+ return next ( createError ( 415 , 'unsupported charset "' + charset . toUpperCase ( ) + '"' , {
70
+ charset : charset . toLowerCase ( ) ,
68
71
type : 'charset.unsupported'
69
72
} ) )
70
73
}
71
74
75
+ // set raw-body encoding
76
+ options . encoding = options . verify ? null : charset
77
+
72
78
// read body
73
79
debug ( 'read body' )
74
- getBody ( stream , opts , function ( error , body ) {
80
+ getBody ( stream , options , function ( error , body ) {
75
81
if ( error ) {
76
82
var _error
77
83
78
84
if ( error . type === 'encoding.unsupported' ) {
79
85
// echo back charset
80
- _error = createError ( 415 , 'unsupported charset "' + encoding . toUpperCase ( ) + '"' , {
81
- charset : encoding . toLowerCase ( ) ,
86
+ _error = createError ( 415 , 'unsupported charset "' + charset . toUpperCase ( ) + '"' , {
87
+ charset : charset . toLowerCase ( ) ,
82
88
type : 'charset.unsupported'
83
89
} )
84
90
} else {
@@ -100,10 +106,10 @@ function read (req, res, next, parse, debug, options) {
100
106
}
101
107
102
108
// verify
103
- if ( verify ) {
109
+ if ( options . verify ) {
104
110
try {
105
111
debug ( 'verify body' )
106
- verify ( req , res , body , encoding )
112
+ options . verify ( req , res , body , charset )
107
113
} catch ( err ) {
108
114
next ( createError ( 403 , err , {
109
115
body : body ,
@@ -117,10 +123,10 @@ function read (req, res, next, parse, debug, options) {
117
123
var str = body
118
124
try {
119
125
debug ( 'parse body' )
120
- str = typeof body !== 'string' && encoding !== null
121
- ? iconv . decode ( body , encoding )
126
+ str = typeof body !== 'string' && charset !== null
127
+ ? iconv . decode ( body , charset )
122
128
: body
123
- req . body = parse ( str , encoding )
129
+ req . body = parse ( str , charset )
124
130
} catch ( err ) {
125
131
next ( createError ( 400 , err , {
126
132
body : str ,
@@ -133,39 +139,6 @@ function read (req, res, next, parse, debug, options) {
133
139
} )
134
140
}
135
141
136
- /**
137
- * Get the content stream of the request.
138
- *
139
- * @param {object } req
140
- * @param {function } debug
141
- * @param {boolean } [inflate=true]
142
- * @return {object }
143
- * @api private
144
- */
145
-
146
- function contentstream ( req , debug , inflate ) {
147
- var encoding = ( req . headers [ 'content-encoding' ] || 'identity' ) . toLowerCase ( )
148
- var length = req . headers [ 'content-length' ]
149
-
150
- debug ( 'content-encoding "%s"' , encoding )
151
-
152
- if ( inflate === false && encoding !== 'identity' ) {
153
- throw createError ( 415 , 'content encoding unsupported' , {
154
- encoding : encoding ,
155
- type : 'encoding.unsupported'
156
- } )
157
- }
158
-
159
- if ( encoding === 'identity' ) {
160
- req . length = length
161
- return req
162
- }
163
-
164
- var stream = createDecompressionStream ( encoding , debug )
165
- req . pipe ( stream )
166
- return stream
167
- }
168
-
169
142
/**
170
143
* Create a decompression stream for the given encoding.
171
144
* @param {string } encoding
0 commit comments