1111 * @private
1212 */
1313
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 ( 'node:zlib' )
14+ const createError = require ( 'http-errors' )
15+ const getBody = require ( 'raw-body' )
16+ const iconv = require ( 'iconv-lite' )
17+ const onFinished = require ( 'on-finished' )
18+ const zlib = require ( 'node:zlib' )
1919
2020/**
2121 * Module exports.
@@ -36,49 +36,55 @@ module.exports = read
3636 */
3737
3838function read ( req , res , next , parse , debug , options ) {
39- var length
40- var opts = options
41- var stream
42-
4339 // 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+ } ) )
5651 }
5752
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+ }
6366
6467 // 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 ( ) ,
6871 type : 'charset.unsupported'
6972 } ) )
7073 }
7174
75+ // set raw-body encoding
76+ options . encoding = options . verify ? null : charset
77+
7278 // read body
7379 debug ( 'read body' )
74- getBody ( stream , opts , function ( error , body ) {
80+ getBody ( stream , options , function ( error , body ) {
7581 if ( error ) {
76- var _error
82+ let _error
7783
7884 if ( error . type === 'encoding.unsupported' ) {
7985 // 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 ( ) ,
8288 type : 'charset.unsupported'
8389 } )
8490 } else {
@@ -100,10 +106,10 @@ function read (req, res, next, parse, debug, options) {
100106 }
101107
102108 // verify
103- if ( verify ) {
109+ if ( options . verify ) {
104110 try {
105111 debug ( 'verify body' )
106- verify ( req , res , body , encoding )
112+ options . verify ( req , res , body , charset )
107113 } catch ( err ) {
108114 next ( createError ( 403 , err , {
109115 body : body ,
@@ -114,13 +120,13 @@ function read (req, res, next, parse, debug, options) {
114120 }
115121
116122 // parse
117- var str = body
123+ let str = body
118124 try {
119125 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 )
122128 : body
123- req . body = parse ( str , encoding )
129+ req . body = parse ( str , charset )
124130 } catch ( err ) {
125131 next ( createError ( 400 , err , {
126132 body : str ,
@@ -133,39 +139,6 @@ function read (req, res, next, parse, debug, options) {
133139 } )
134140}
135141
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-
169142/**
170143 * Create a decompression stream for the given encoding.
171144 * @param {string } encoding
0 commit comments