From e3157972e1f58a986442696f5bf32870210d40fb Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 15 Aug 2013 08:54:49 -0700 Subject: [PATCH] http: make OutgoingMessage._flush inline-able --- lib/_http_outgoing.js | 73 ++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index c2b13b6..f1493d2 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -525,46 +525,41 @@ OutgoingMessage.prototype._finish = function() { }; +// This logic is probably a bit confusing. Let me explain a bit: +// +// In both HTTP servers and clients it is possible to queue up several +// outgoing messages. This is easiest to imagine in the case of a client. +// Take the following situation: +// +// req1 = client.request('GET', '/'); +// req2 = client.request('POST', '/'); +// +// When the user does +// +// req2.write('hello world\n'); +// +// it's possible that the first request has not been completely flushed to +// the socket yet. Thus the outgoing messages need to be prepared to queue +// up data internally before sending it on further to the socket's queue. +// +// This function, outgoingFlush(), is called by both the Server and Client +// to attempt to flush any pending messages out to the socket. OutgoingMessage.prototype._flush = function() { - // This logic is probably a bit confusing. Let me explain a bit: - // - // In both HTTP servers and clients it is possible to queue up several - // outgoing messages. This is easiest to imagine in the case of a client. - // Take the following situation: - // - // req1 = client.request('GET', '/'); - // req2 = client.request('POST', '/'); - // - // When the user does - // - // req2.write('hello world\n'); - // - // it's possible that the first request has not been completely flushed to - // the socket yet. Thus the outgoing messages need to be prepared to queue - // up data internally before sending it on further to the socket's queue. - // - // This function, outgoingFlush(), is called by both the Server and Client - // to attempt to flush any pending messages out to the socket. - - if (!this.socket) return; - - var ret; - while (this.output.length) { - - if (!this.socket.writable) return; // XXX Necessary? - - var data = this.output.shift(); - var encoding = this.outputEncodings.shift(); - var cb = this.outputCallbacks.shift(); - - ret = this.socket.write(data, encoding, cb); - } + if (this.socket && this.socket.writable) { + var ret; + while (this.output.length) { + var data = this.output.shift(); + var encoding = this.outputEncodings.shift(); + var cb = this.outputCallbacks.shift(); + ret = this.socket.write(data, encoding, cb); + } - if (this.finished) { - // This is a queue to the server or client to bring in the next this. - this._finish(); - } else if (ret) { - // This is necessary to prevent https from breaking - this.emit('drain'); + if (this.finished) { + // This is a queue to the server or client to bring in the next this. + this._finish(); + } else if (ret) { + // This is necessary to prevent https from breaking + this.emit('drain'); + } } }; -- 2.7.4