Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
tls: fix double 'error' events on HTTPS Requests
Browse files Browse the repository at this point in the history
Fixes #2549.
  • Loading branch information
koichik committed Jan 17, 2012
1 parent 2541009 commit fcdf2d0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/tls.js
Expand Up @@ -271,7 +271,11 @@ CryptoStream.prototype._done = function() {
this.pair.encrypted._doneFlag &&
!this.pair._doneFlag) {
// If both streams are done:
this.pair.destroy();
if (!this.pair._secureEstablished) {
this.pair.error();
} else {
this.pair.destroy();
}
}
};

Expand Down Expand Up @@ -720,7 +724,6 @@ SecurePair.prototype.maybeInitFinished = function() {

SecurePair.prototype.destroy = function() {
var self = this;
var error = this.ssl.error;

if (!this._doneFlag) {
this._doneFlag = true;
Expand All @@ -736,21 +739,19 @@ SecurePair.prototype.destroy = function() {
self.encrypted.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() {
if (!this._secureEstablished) {
var error = this.ssl.error;
if (!error) {
error = new Error('socket hang up');
error.code = 'ECONNRESET';
}
this.destroy();
this.emit('error', error);
} else {
var err = this.ssl.error;
this.ssl.error = null;
Expand Down
45 changes: 45 additions & 0 deletions 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);
});

0 comments on commit fcdf2d0

Please sign in to comment.