From 75f0e52e34c2620d486ccb696862b23e473d9ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 24 Aug 2023 14:54:37 +0300 Subject: [PATCH] forward to dicer for validation --- lib/types/multipart.js | 45 +++++------------------------------------- 1 file changed, 5 insertions(+), 40 deletions(-) diff --git a/lib/types/multipart.js b/lib/types/multipart.js index e5de06d..0f3e4e6 100644 --- a/lib/types/multipart.js +++ b/lib/types/multipart.js @@ -22,7 +22,6 @@ const RE_FIELD = /^form-data$/i const RE_CHARSET = /^charset$/i const RE_FILENAME = /^filename$/i const RE_NAME = /^name$/i -const DASH = 0x2d /* - */ Multipart.detect = /^multipart\/form-data/i function Multipart (boy, cfg) { @@ -48,10 +47,7 @@ function Multipart (boy, cfg) { function checkFinished () { if (nends === 0 && finished && !boy._done) { finished = false - process.nextTick(function () { - boy._done = true - boy.emit('finish') - }) + self.end() } } @@ -72,8 +68,6 @@ function Multipart (boy, cfg) { let curField let finished = false - this._boundary = boundary - this._boundaryNotFound = true this._needDrain = false this._pause = false this._cb = undefined @@ -265,18 +259,12 @@ function Multipart (boy, cfg) { }).on('error', function (err) { boy.emit('error', err) }).on('finish', function () { - if (self._nparts === 0 && self._boundaryNotFound) { - boy.emit('error', new Error('Unexpected end of multipart data')) - } finished = true checkFinished() }) } Multipart.prototype.write = function (chunk, cb) { - if (this._boundaryNotFound && (this._nparts !== 0 || this._checkBoundary(chunk))) { - this._boundaryNotFound = false - } const r = this.parser.write(chunk) if (r && !this._pause) { cb() @@ -288,38 +276,15 @@ Multipart.prototype.write = function (chunk, cb) { Multipart.prototype.end = function () { const self = this - if (self._nparts === 0 && self._boundaryNotFound) { - self._boy.emit('error', new Error('Unexpected end of multipart data')) - } - if (self._nparts === 0 && !self._boy._done) { + + if (self.parser.writable) { + self.parser.end() + } else if (!self._boy._done) { process.nextTick(function () { self._boy._done = true self._boy.emit('finish') }) - } else if (self.parser.writable) { self.parser.end() } -} - -Multipart.prototype._checkBoundary = function (chunk) { - const boundaryLen = this._boundary.length - - if (chunk.length < (boundaryLen + 4)) { return false } - - if ( - chunk[0] !== DASH /* - */ || - chunk[1] !== DASH /* - */ || - chunk[2 + boundaryLen] !== DASH /* - */ || - chunk[2 + boundaryLen + 1] !== DASH /* - */ - ) { - return false - } - - const val = Buffer.from(this._boundary) - - for (let i = 0; i < boundaryLen; ++i) { - if (chunk[2 + i] !== val[i]) { return false } } - - return true } function skipPart (part) {