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.
fork
mscdex 11 years ago
parent 75e7b921e0
commit d4a9c30bbd

@ -35,14 +35,14 @@ function Parser(stream, debug) {
this.setStream(stream); this.setStream(stream);
var self = this; var self = this;
function cb() { this._cbReadable = function() {
if (self._literallen > 0) if (self._literallen > 0 && !self._body)
self._tryread(self._literallen); self._tryread(self._literallen);
else else
self._tryread(); self._tryread();
} }
this._stream.on('readable', cb); this._stream.on('readable', this._cbReadable);
process.nextTick(cb); process.nextTick(this._cbReadable);
} }
inherits(Parser, EventEmitter); inherits(Parser, EventEmitter);
@ -75,8 +75,10 @@ Parser.prototype._parse = function(data) {
} else { } else {
this._literallen -= datalen; this._literallen -= datalen;
var r = body.push(data); var r = body.push(data);
if (!r && this._literallen > 0) if (!r && this._literallen > 0) {
body._read = this._cbReadable;
return; return;
}
i = datalen; i = datalen;
} }
if (this._literallen === 0) { if (this._literallen === 0) {
@ -166,9 +168,7 @@ Parser.prototype._resUntagged = function() {
var which = m[1], size = parseInt(m[2], 10); var which = m[1], size = parseInt(m[2], 10);
this._literallen = size; this._literallen = size;
this._body = new ReadableStream(); this._body = new ReadableStream();
this._body._read = function bodyread(n) { this._body._read = EMPTY_READCB;
self._tryread();
};
m = RE_SEQNO.exec(this._buffer); m = RE_SEQNO.exec(this._buffer);
this._buffer = this._buffer.replace(RE_BODYLITERAL, ''); this._buffer = this._buffer.replace(RE_BODYLITERAL, '');
this.emit('body', this._body, { this.emit('body', this._body, {

Loading…
Cancel
Save