forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http_client: ensure empty socket on error
Read all pending data out of the socket on `error` event and ensure that no `data`/`end` handlers will be invoked on `socket.destroy()`. Otherwise following assertion happens: AssertionError: null == true at TLSSocket.socketOnData (_http_client.js:308:3) at TLSSocket.emit (events.js:107:17) at TLSSocket.Readable.read (_stream_readable.js:373:10) at TLSSocket.socketCloseListener (_http_client.js:229:10) at TLSSocket.emit (events.js:129:20) at TCP.close (net.js:476:12) Fix: nodejs/node-v0.x-archive#9348
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
var net = require('net'); | ||
var http = require('http'); | ||
var util = require('util'); | ||
|
||
function Agent() { | ||
http.Agent.call(this); | ||
} | ||
util.inherits(Agent, http.Agent); | ||
|
||
Agent.prototype.createConnection = function() { | ||
var self = this; | ||
var socket = new net.Socket(); | ||
|
||
socket.on('error', function() { | ||
socket.push('HTTP/1.1 200\r\n\r\n'); | ||
}); | ||
|
||
socket.on('newListener', function onNewListener(name) { | ||
if (name !== 'error') | ||
return; | ||
socket.removeListener('newListener', onNewListener); | ||
|
||
// Let other listeners to be set up too | ||
process.nextTick(function() { | ||
self.breakSocket(socket); | ||
}); | ||
}); | ||
|
||
return socket; | ||
}; | ||
|
||
Agent.prototype.breakSocket = function breakSocket(socket) { | ||
socket.emit('error', new Error('Intentional error')); | ||
}; | ||
|
||
var agent = new Agent(); | ||
|
||
http.request({ | ||
agent: agent | ||
}).once('error', function() { | ||
console.log('ignore'); | ||
}); |