diff --git a/lib/Dicer.js b/lib/Dicer.js index 9d580cb..3c613a4 100644 --- a/lib/Dicer.js +++ b/lib/Dicer.js @@ -31,7 +31,6 @@ function Dicer(cfg) { this._dashes = 0; this._parts = 0; this._finished = false; - this._realFinish = false; this._isPreamble = true; this._justMatched = false; this._firstWrite = true; @@ -54,7 +53,7 @@ function Dicer(cfg) { inherits(Dicer, WritableStream); Dicer.prototype.emit = function(ev) { - if (ev === 'finish' && !this._realFinish) { + if (ev === 'finish') { if (!this._finished) { var self = this; process.nextTick(function() { @@ -64,15 +63,11 @@ Dicer.prototype.emit = function(ev) { self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data')); self._part.push(null); process.nextTick(function() { - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); }); return; } - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); }); } } else @@ -160,9 +155,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) { this._finished = true; // no more parts will be added if (self._parts === 0) { - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); } } if (this._dashes) @@ -190,7 +183,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) { shouldWriteMore = this._part.push(data.slice(start, end)); if (!shouldWriteMore) this._pause = true; - } else if (!this._isPreamble && this._inHeader) { + } else { if (buf) this._hparser.push(buf); r = this._hparser.push(data.slice(start, end)); @@ -207,9 +200,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) { this._part.on('end', function() { if (--self._parts === 0) { if (self._finished) { - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); } else { self._unpause(); } diff --git a/lib/HeaderParser.js b/lib/HeaderParser.js index 8ccb6e5..851f01b 100644 --- a/lib/HeaderParser.js +++ b/lib/HeaderParser.js @@ -26,7 +26,7 @@ function HeaderParser(cfg) { this.ss.on('info', function(isMatch, data, start, end) { if (data && !self.maxed) { if (self.nread + (end - start) > MAX_HEADER_SIZE) { - end = (MAX_HEADER_SIZE - self.nread); + end = (MAX_HEADER_SIZE - self.nread) + start; self.nread = MAX_HEADER_SIZE; } else self.nread += (end - start); @@ -72,8 +72,7 @@ HeaderParser.prototype._parseHeader = function() { if (this.npairs === this.maxHeaderPairs) return; - var lines = this.buffer.split(RE_CRLF), len = lines.length, m, h, - modded = false; + var lines = this.buffer.split(RE_CRLF), len = lines.length, m, h; for (var i = 0; i < len; ++i) { if (lines[i].length === 0) @@ -82,29 +81,26 @@ HeaderParser.prototype._parseHeader = function() { // folded header content // RFC2822 says to just remove the CRLF and not the whitespace following // it, so we follow the RFC and include the leading whitespace ... - this.header[h][this.header[h].length - 1] += lines[i]; - } else { - m = RE_HDR.exec(lines[i]); - if (m) { - h = m[1].toLowerCase(); - if (m[2]) { - if (this.header[h] === undefined) - this.header[h] = [m[2]]; - else - this.header[h].push(m[2]); - } else - this.header[h] = ['']; - if (++this.npairs === this.maxHeaderPairs) - break; - } else { - this.buffer = lines[i]; - modded = true; - break; + if (h) { + this.header[h][this.header[h].length - 1] += lines[i]; + continue; } } + m = RE_HDR.exec(lines[i]); + if (m) { + h = m[1].toLowerCase(); + if (m[2]) { + if (this.header[h] === undefined) + this.header[h] = [m[2]]; + else + this.header[h].push(m[2]); + } else + this.header[h] = ['']; + if (++this.npairs === this.maxHeaderPairs) + break; + } else + return; } - if (!modded) - this.buffer = ''; }; module.exports = HeaderParser;