This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

tls: fix double 'error' events on HTTPS Requests

Fixes #2549.
  • Loading branch information...
koichik committed Jan 17, 2012
1 parent 549443a commit 534df2f8d207877f9d09d172d4ba92406e351aa0
Showing with 56 additions and 10 deletions.
  1. +11 −10 lib/tls.js
  2. +45 −0 test/simple/test-tls-client-abort2.js
View
@@ -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();
+ }
}
};
@@ -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;
@@ -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;
@@ -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 534df2f

Please sign in to comment.