From 2dd0bc292f65a5dd48b352ecf2b3385d2216d8f9 Mon Sep 17 00:00:00 2001 From: Nils Kuhnhenn Date: Fri, 13 Mar 2015 13:13:00 +0100 Subject: [PATCH 1/6] Always call callbacks in lib/_http_outgoing.js --- lib/_http_outgoing.js | 58 +++++++++++++++---- .../test-http-destroyed-socket-write2.js | 6 +- .../test-http-many-ended-pipelines.js | 23 +++++++- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index f6144ba6a63910..b3675ddee8de34 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -70,6 +70,27 @@ function OutgoingMessage() { this._header = null; this._headers = null; this._headerNames = {}; + + var that = this; + + // Call remaining callbacks if stream closes prematurely + this.once('close', function(){ + if(that.output.length === 0) return; + + var err = new Error('stream closed prematurely'); + + var outputCallbacks = that.outputCallbacks; + + that.output = []; + that.outputEncodings = []; + that.outputCallbacks = []; + + for(var i=0; i 0){ + //If the user tried to write data, tell him it failed + var err = new Error('write after end'); + process.nextTick(function() { + self.emit('error', err); + if (callback) callback(err); + }); + } else { + //If he only wanted to end the stream anyways, lucky for him + if(callback) process.nextTick(callback); + } return false; } - var self = this; function finish() { self.emit('finish'); } diff --git a/test/parallel/test-http-destroyed-socket-write2.js b/test/parallel/test-http-destroyed-socket-write2.js index e7a47ca9cfe95f..a7dbd2b023d2b6 100644 --- a/test/parallel/test-http-destroyed-socket-write2.js +++ b/test/parallel/test-http-destroyed-socket-write2.js @@ -39,7 +39,11 @@ server.listen(common.PORT, function() { var sawEnd = false; req.on('error', function(er) { - assert(!gotError); + + // Each failed write will cause an error, but + // we are only interested in one + if(gotError) return; + gotError = true; switch (er.code) { // This is the expected case diff --git a/test/parallel/test-http-many-ended-pipelines.js b/test/parallel/test-http-many-ended-pipelines.js index 6824627151e741..285c48c32e26db 100644 --- a/test/parallel/test-http-many-ended-pipelines.js +++ b/test/parallel/test-http-many-ended-pipelines.js @@ -15,11 +15,28 @@ var numRequests = 20; var done = 0; var server = http.createServer(function(req, res) { - res.end('ok'); + + var callbackCalled = false; + + res.end('ok', function(){ + assert.ok(!callbackCalled); + callbackCalled = true; + }); + + // We might get a socket already closed error here + // Not really a surpise + res.on('error', function(){}); + + res.on('close', function(){ + process.nextTick(function(){ + assert.ok(callbackCalled, "end() callback should've been called"); + }); + }); // Oh no! The connection died! req.socket.destroy(); - if (++done == numRequests) + + if (++done === numRequests) server.close(); }); @@ -31,5 +48,7 @@ for (var i = 0; i < numRequests; i++) { 'Host: some.host.name\r\n'+ '\r\n\r\n'); } + client.end(); + client.pipe(process.stdout); From d7331960c56c9e2e0e2ed461ae6069bda585acfc Mon Sep 17 00:00:00 2001 From: Nils Kuhnhenn Date: Sat, 14 Mar 2015 13:30:49 +0100 Subject: [PATCH 2/6] Make code adhere to io.js style --- lib/_http_outgoing.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index b3675ddee8de34..e37b3f2def9074 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -71,19 +71,19 @@ function OutgoingMessage() { this._headers = null; this._headerNames = {}; - var that = this; + var self = this; // Call remaining callbacks if stream closes prematurely - this.once('close', function(){ - if(that.output.length === 0) return; + this.once('close', function() { + if(self.output.length === 0) return; var err = new Error('stream closed prematurely'); - var outputCallbacks = that.outputCallbacks; + var outputCallbacks = self.outputCallbacks; - that.output = []; - that.outputEncodings = []; - that.outputCallbacks = []; + self.output = []; + self.outputEncodings = []; + self.outputCallbacks = []; for(var i=0; i Date: Sat, 14 Mar 2015 13:37:48 +0100 Subject: [PATCH 3/6] Make jslint pass --- lib/_http_outgoing.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index e37b3f2def9074..e349138a9a4e78 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -75,7 +75,7 @@ function OutgoingMessage() { // Call remaining callbacks if stream closes prematurely this.once('close', function() { - if(self.output.length === 0) return; + if (self.output.length === 0) return; var err = new Error('stream closed prematurely'); @@ -85,10 +85,10 @@ function OutgoingMessage() { self.outputEncodings = []; self.outputCallbacks = []; - for(var i=0; i 0){ - //If the user tried to write data, tell him it failed - var err = new Error('write after end'); - process.nextTick(function() { - self.emit('error', err); - if (callback) callback(err); - }); + if (data && data.length > 0) { + //If the user tried to write data, tell him it failed + var err = new Error('write after end'); + process.nextTick(function() { + self.emit('error', err); + if (callback) callback(err); + }); } else { - //If he only wanted to end the stream anyways, lucky for him - if(callback) process.nextTick(callback); + //If he only wanted to end the stream anyways, lucky for him + if (callback) + process.nextTick(callback); } return false; } From 6d4bd060326745f200baf47bc02266595dbc3052 Mon Sep 17 00:00:00 2001 From: Nils Kuhnhenn Date: Sun, 15 Mar 2015 14:18:27 +0100 Subject: [PATCH 4/6] Change code style in test --- test/parallel/test-http-many-ended-pipelines.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-http-many-ended-pipelines.js b/test/parallel/test-http-many-ended-pipelines.js index 285c48c32e26db..14326e0886b033 100644 --- a/test/parallel/test-http-many-ended-pipelines.js +++ b/test/parallel/test-http-many-ended-pipelines.js @@ -18,17 +18,17 @@ var server = http.createServer(function(req, res) { var callbackCalled = false; - res.end('ok', function(){ + res.end('ok', function() { assert.ok(!callbackCalled); callbackCalled = true; }); // We might get a socket already closed error here // Not really a surpise - res.on('error', function(){}); + res.on('error', function() {}); - res.on('close', function(){ - process.nextTick(function(){ + res.on('close', function() { + process.nextTick(function() { assert.ok(callbackCalled, "end() callback should've been called"); }); }); @@ -36,7 +36,7 @@ var server = http.createServer(function(req, res) { // Oh no! The connection died! req.socket.destroy(); - if (++done === numRequests) + if (++done == numRequests) server.close(); }); From e712b26cfb0926e4e3548a12ca1808e9847a72bd Mon Sep 17 00:00:00 2001 From: Nils Kuhnhenn Date: Sun, 15 Mar 2015 14:19:32 +0100 Subject: [PATCH 5/6] Change code style in another test --- test/parallel/test-http-destroyed-socket-write2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-http-destroyed-socket-write2.js b/test/parallel/test-http-destroyed-socket-write2.js index a7dbd2b023d2b6..42c9a2e10ff539 100644 --- a/test/parallel/test-http-destroyed-socket-write2.js +++ b/test/parallel/test-http-destroyed-socket-write2.js @@ -42,7 +42,7 @@ server.listen(common.PORT, function() { // Each failed write will cause an error, but // we are only interested in one - if(gotError) return; + if (gotError) return; gotError = true; switch (er.code) { From 10d5da8158be9ef562755d2f48bdb262a5c58cf2 Mon Sep 17 00:00:00 2001 From: Nils Kuhnhenn Date: Tue, 17 Mar 2015 23:47:51 +0100 Subject: [PATCH 6/6] Revert to only a debug message for writes to HEAD responses --- lib/_http_outgoing.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index e349138a9a4e78..c8eedd78972145 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -441,11 +441,8 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) { } if (!this._hasBody) { - err = new Error('this type of response must not have a body'); - process.nextTick(function() { - self.emit('error', err); - if (callback) callback(err); - }); + debug('This type of response MUST NOT have a body. ' + + 'Ignoring write() calls.'); return true; }