Browse Source

tls: fix double 'error' events on HTTPS Requests

Fixes #2549.
v0.7.4-release
koichik 13 years ago
parent
commit
534df2f8d2
  1. 19
      lib/tls.js
  2. 45
      test/simple/test-tls-client-abort2.js

19
lib/tls.js

@ -271,8 +271,12 @@ CryptoStream.prototype._done = function() {
this.pair.encrypted._doneFlag && this.pair.encrypted._doneFlag &&
!this.pair._doneFlag) { !this.pair._doneFlag) {
// If both streams are done: // If both streams are done:
if (!this.pair._secureEstablished) {
this.pair.error();
} else {
this.pair.destroy(); this.pair.destroy();
} }
}
}; };
@ -720,7 +724,6 @@ SecurePair.prototype.maybeInitFinished = function() {
SecurePair.prototype.destroy = function() { SecurePair.prototype.destroy = function() {
var self = this; var self = this;
var error = this.ssl.error;
if (!this._doneFlag) { if (!this._doneFlag) {
this._doneFlag = true; this._doneFlag = true;
@ -736,21 +739,19 @@ SecurePair.prototype.destroy = function() {
self.encrypted.emit('close'); self.encrypted.emit('close');
self.cleartext.emit('close'); self.cleartext.emit('close');
}); });
if (!this._secureEstablished) {
if (!error) {
error = new Error('socket hang up');
error.code = 'ECONNRESET';
}
this.emit('error', error);
}
} }
}; };
SecurePair.prototype.error = function() { SecurePair.prototype.error = function() {
if (!this._secureEstablished) { if (!this._secureEstablished) {
var error = this.ssl.error;
if (!error) {
error = new Error('socket hang up');
error.code = 'ECONNRESET';
}
this.destroy(); this.destroy();
this.emit('error', error);
} else { } else {
var err = this.ssl.error; var err = this.ssl.error;
this.ssl.error = null; this.ssl.error = null;

45
test/simple/test-tls-client-abort2.js

@ -0,0 +1,45 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
if (!process.versions.openssl) {
console.error('Skipping because node compiled without OpenSSL.');
process.exit(0);
}
var common = require('../common');
var assert = require('assert');
var tls = require('tls');
var errors = 0;
var conn = tls.connect(common.PORT, function() {
assert(false); // callback should never be executed
});
conn.on('error', function() {
++errors;
assert.doesNotThrow(function() {
conn.destroy();
});
});
process.on('exit', function() {
assert.equal(errors, 1);
});
Loading…
Cancel
Save