Skip to content

Commit

Permalink
forward to dicer for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Aug 24, 2023
1 parent a0e89eb commit 75f0e52
Showing 1 changed file with 5 additions and 40 deletions.
45 changes: 5 additions & 40 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit 75f0e52

Please sign in to comment.