From d4a9c30bbdcdec3bbf408eecaf9030c428e81f33 Mon Sep 17 00:00:00 2001 From: mscdex Date: Sun, 30 Jun 2013 22:56:04 -0400 Subject: [PATCH] Parser: don't set _read() for body until it's needed (push() returns false) This fixes a problem where _read() was firing before the parsing function exited, causing the wrong data to be pulled in from the socket stream. --- lib/Parser.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index 3815b1b..a5213bf 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -35,14 +35,14 @@ function Parser(stream, debug) { this.setStream(stream); var self = this; - function cb() { - if (self._literallen > 0) + this._cbReadable = function() { + if (self._literallen > 0 && !self._body) self._tryread(self._literallen); else self._tryread(); } - this._stream.on('readable', cb); - process.nextTick(cb); + this._stream.on('readable', this._cbReadable); + process.nextTick(this._cbReadable); } inherits(Parser, EventEmitter); @@ -75,8 +75,10 @@ Parser.prototype._parse = function(data) { } else { this._literallen -= datalen; var r = body.push(data); - if (!r && this._literallen > 0) + if (!r && this._literallen > 0) { + body._read = this._cbReadable; return; + } i = datalen; } if (this._literallen === 0) { @@ -166,9 +168,7 @@ Parser.prototype._resUntagged = function() { var which = m[1], size = parseInt(m[2], 10); this._literallen = size; this._body = new ReadableStream(); - this._body._read = function bodyread(n) { - self._tryread(); - }; + this._body._read = EMPTY_READCB; m = RE_SEQNO.exec(this._buffer); this._buffer = this._buffer.replace(RE_BODYLITERAL, ''); this.emit('body', this._body, {