-
-
Notifications
You must be signed in to change notification settings - Fork 746
refactor: optimize internal read function #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -64,61 +64,68 @@ function read (req, res, next, parse, debug, options) { | |||||
| return | ||||||
| } | ||||||
|
|
||||||
| var encoding = null | ||||||
| var charset = null | ||||||
| if (options?.skipCharset !== true) { | ||||||
| encoding = getCharset(req) || options.defaultCharset | ||||||
| charset = getCharset(req) || options.defaultCharset | ||||||
|
|
||||||
| // validate charset | ||||||
| if (!!options?.isValidCharset && !options.isValidCharset(encoding)) { | ||||||
| if (!!options?.isValidCharset && !options.isValidCharset(charset)) { | ||||||
| debug('invalid charset') | ||||||
| next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', { | ||||||
| charset: encoding, | ||||||
| next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', { | ||||||
| charset, | ||||||
| type: 'charset.unsupported' | ||||||
| })) | ||||||
| return | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| var length | ||||||
| var opts = options | ||||||
| var stream | ||||||
| // get the content stream | ||||||
| const contentEncoding = (req.headers['content-encoding'] || 'identity').toLowerCase() | ||||||
| debug('content-encoding "%s"', contentEncoding) | ||||||
|
|
||||||
| // read options | ||||||
| var verify = opts.verify | ||||||
|
|
||||||
| try { | ||||||
| // get the content stream | ||||||
| stream = contentstream(req, debug, opts.inflate) | ||||||
| length = stream.length | ||||||
| stream.length = undefined | ||||||
| } catch (err) { | ||||||
| return next(err) | ||||||
| if (options.inflate === false && contentEncoding !== 'identity') { | ||||||
| return next(createError(415, 'content encoding unsupported', { | ||||||
| encoding: contentEncoding, | ||||||
| type: 'encoding.unsupported' | ||||||
| })) | ||||||
| } | ||||||
|
|
||||||
| // set raw-body options | ||||||
| opts.length = length | ||||||
| opts.encoding = verify | ||||||
| ? null | ||||||
| : encoding | ||||||
| let stream | ||||||
| if (contentEncoding === 'identity') { | ||||||
| // set raw-body expected length | ||||||
| stream = req | ||||||
| options.length = req.headers['content-length'] | ||||||
| } else { | ||||||
| try { | ||||||
| stream = createDecompressionStream(contentEncoding, debug) | ||||||
| req.pipe(stream) | ||||||
| options.length = undefined | ||||||
| } catch (err) { | ||||||
| return next(err) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // assert charset is supported | ||||||
| if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) { | ||||||
| return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', { | ||||||
| charset: encoding.toLowerCase(), | ||||||
| if (options.verify && charset !== null && !iconv.encodingExists(charset)) { | ||||||
|
||||||
| if (options.verify && charset !== null && !iconv.encodingExists(charset)) { | |
| if (charset !== null && !iconv.encodingExists(charset)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be seen as follow-up to #564. I included the contentstream function in the main read function for multiple reasons:
- to prevent the
reqobject from being altered by settinglengthand resetting it inread. - This also improves code by only setting the
raw-bodylengthoption and thus also only callingreq.headers['content-length']when the requests needs no inflation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improves the code by only accessing
req.headers['content-length']and setting thelengthoption forraw-bodywhen the stream needs no decompression