From f347077e786b1ca78d44e2c77b1bc1366735af76 Mon Sep 17 00:00:00 2001 From: Shigeki Ohtsu Date: Fri, 31 Aug 2012 00:11:05 +0900 Subject: [PATCH] tls: support unix domain socket/named pipe in tls.connect --- lib/net.js | 1 + lib/tls.js | 45 ++++++++++--------------- test/simple/test-tls-connect-pipe.js | 49 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 27 deletions(-) create mode 100644 test/simple/test-tls-connect-pipe.js diff --git a/lib/net.js b/lib/net.js index d0a4c641bf..efed0cacde 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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 */ diff --git a/lib/tls.js b/lib/tls.js index 350b5776d0..ee3695567c 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -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(); @@ -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() { diff --git a/test/simple/test-tls-connect-pipe.js b/test/simple/test-tls-connect-pipe.js new file mode 100644 index 0000000000..f58aaabcfe --- /dev/null +++ b/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); +});