I have the following Javascript code which shows a different behaviour depending on whether you run it against a HTTP or HTTPS endpoint.
var url=require('url').parse('https://anyserver'); // Change here to http to reproduce the difference
var secure=url.protocol.indexOf('https')===0;
var socket=new require('net').Socket();
socket.setTimeout(2000, function() {
this.destroy();
console.log('TCP connection timed out');
});
socket.connect({ host: url.hostname, port: url.port || (secure?443:80) }, function() {
if (secure)
{
require('tls').connect({ socket: this, rejectUnauthorized: false, servername: url.hostname }, function() {
this.destroy();
console.log('SSL successfully connected');
}).on('error', function(error){
this.destroy();
console.log('Error during SSL handshake');
});
}
else
{
this.destroy();
console.log('Non-SSL successfully connected');
}
}).on('error', function(error) {
this.destroy();
console.log('Error during TCP connection '+error.code);
});
// Just a dummy listener to keep node from exiting
require('http').createServer().listen(2000);Everything is fine in the case of a regular HTTP connection, however switching to HTTPS the timeout handler will be called regardless.
From what I debugged it might happen because I am not closing the socket itself but the TLS wrapper socket object. Could this be the reason or could it be a bug in the library (it started happening only recently after the upgrade from 0.10 to 0.12).