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

Commit

Permalink
tls: support unix domain socket/named pipe in tls.connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigeki Ohtsu authored and bnoordhuis committed Aug 30, 2012
1 parent 9603f08 commit f347077
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/net.js
Expand Up @@ -106,6 +106,7 @@ function normalizeConnectArgs(args) {
var cb = args[args.length - 1];
return (typeof cb === 'function') ? [options, cb] : [options];
}
exports._normalizeConnectArgs = normalizeConnectArgs;


/* called when creating new Socket, or when re-using a closed Socket */
Expand Down
45 changes: 18 additions & 27 deletions lib/tls.js
Expand Up @@ -1183,34 +1183,24 @@ Server.prototype.SNICallback = function(servername) {
// });
//
//
exports.connect = function(/* [port, host], options, cb */) {
var options, port, host, cb;

if (typeof arguments[0] === 'object') {
options = arguments[0];
} else if (typeof arguments[1] === 'object') {
options = arguments[1];
port = arguments[0];
} else if (typeof arguments[2] === 'object') {
options = arguments[2];
port = arguments[0];
host = arguments[1];
} else {
// This is what happens when user passes no `options` argument, we can't
// throw `TypeError` here because it would be incompatible with old API
if (typeof arguments[0] === 'number') {
port = arguments[0];
}
if (typeof arguments[1] === 'string') {
host = arguments[1];
}
function normalizeConnectArgs(listArgs) {
var args = net._normalizeConnectArgs(listArgs);
var options = args[0];
var cb = args[1];

if (typeof listArgs[1] === 'object') {
options = util._extend(options, listArgs[1]);
} else if (typeof listArgs[2] === 'object') {
options = util._extend(options, listArgs[2]);
}

options = util._extend({ port: port, host: host }, options || {});
return (cb) ? [options, cb] : [options];
}

if (typeof arguments[arguments.length - 1] === 'function') {
cb = arguments[arguments.length - 1];
}
exports.connect = function(/* [port, host], options, cb */) {
var args = normalizeConnectArgs(arguments);
var options = args[0];
var cb = args[1];

var socket = options.socket ? options.socket : new net.Stream();

Expand All @@ -1235,11 +1225,12 @@ exports.connect = function(/* [port, host], options, cb */) {
}

if (!options.socket) {
socket.connect({
var connect_opt = (options.path && !options.port) ? {path: options.path} : {
port: options.port,
host: options.host,
localAddress: options.localAddress
});
};
socket.connect(connect_opt);
}

pair.on('secure', function() {
Expand Down
49 changes: 49 additions & 0 deletions test/simple/test-tls-connect-pipe.js
@@ -0,0 +1,49 @@
// 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.

var common = require('../common');
var assert = require('assert');
var tls = require('tls');
var fs = require('fs');

var clientConnected = 0;
var serverConnected = 0;

var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};

var server = tls.Server(options, function(socket) {
++serverConnected;
server.close();
});
server.listen(common.PIPE, function() {
var client = tls.connect(common.PIPE, function() {
++clientConnected;
client.end();
});
});

process.on('exit', function() {
assert.equal(clientConnected, 1);
assert.equal(serverConnected, 1);
});

0 comments on commit f347077

Please sign in to comment.