From 97386397c22af1f9d853147ea06e4e409eaa871c Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Wed, 22 Apr 2015 02:36:57 +0300 Subject: [PATCH] add a code to bind on unix sockets use "listen: 'unix:sinopia.sock'" for this --- conf/full.yaml | 1 + lib/cli.js | 26 ++++++++++++++++---------- lib/utils.js | 29 ++++++++++++++++++++--------- test/unit/listen_addr.js | 12 +++++++++++- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/conf/full.yaml b/conf/full.yaml index 10cce90d5..b8c344181 100644 --- a/conf/full.yaml +++ b/conf/full.yaml @@ -91,6 +91,7 @@ packages: # - 0.0.0.0:4873 # listen on all addresses (INADDR_ANY) # - https://example.org:4873 # if you want to use https # - [::1]:4873 # ipv6 +# - unix:/tmp/sinopia.sock # unix socket # Configure HTTPS, it is required if you use "https" protocol above. #https: diff --git a/lib/cli.js b/lib/cli.js index d9799e107..e78926de8 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -77,16 +77,16 @@ function get_listen_addresses() { } addresses = addresses.map(function(addr) { - addr = Utils.parse_address(addr) + var parsed_addr = Utils.parse_address(addr) - if (!addr) { + if (!parsed_addr) { logger.logger.warn({ addr: addr }, 'invalid address - @{addr}, we expect a port (e.g. "4873"),' + ' host:port (e.g. "localhost:4873") or full url' + ' (e.g. "http://localhost:4873/")') } - return addr + return parsed_addr }).filter(Boolean) @@ -142,19 +142,25 @@ function afterConfigLoad() { } webServer - .listen(addr.port, addr.host) + .listen(addr.port || addr.path, addr.host) .on('error', function(err) { logger.logger.fatal({ err: err }, 'cannot create server: @{err.message}') process.exit(2) }) logger.logger.warn({ - addr: URL.format({ - protocol: addr.proto, - hostname: addr.host, - port: addr.port, - pathname: '/', - }), + addr: ( addr.path + ? URL.format({ + protocol: 'unix', + pathname: addr.path, + }) + : URL.format({ + protocol: addr.proto, + hostname: addr.host, + port: addr.port, + pathname: '/', + }) + ), version: 'Sinopia/'+pkg.version, }, 'http address - @{addr}') }) diff --git a/lib/utils.js b/lib/utils.js index 0eae16727..bf558115f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -126,23 +126,34 @@ module.exports.parse_address = function parse_address(addr) { // // Allow: // - // - https:localhost:1234 - protocol + host + port - // - localhost:1234 - host + port - // - 1234 - port - // - http::1234 - protocol + port - // - https://localhost:443/ - full url + https - // - http://[::1]:443/ - ipv6 + // - https:localhost:1234 - protocol + host + port + // - localhost:1234 - host + port + // - 1234 - port + // - http::1234 - protocol + port + // - https://localhost:443/ - full url + https + // - http://[::1]:443/ - ipv6 + // - unix:/tmp/http.sock - unix sockets + // - https://unix:/tmp/http.sock - unix sockets (https) + // + // TODO: refactor it to something more reasonable? // // protocol : // ( host )|( ipv6 ): port / var m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr) - if (!m) return null - - return { + if (m) return { proto: m[2] || 'http', host: m[6] || m[7] || 'localhost', port: m[8] || '4873', } + + var m = /^((https?):(\/\/)?)?unix:(.*)$/.exec(addr) + + if (m) return { + proto: m[2] || 'http', + path: m[4], + } + + return null } // function filters out bad semver versions and sorts the array diff --git a/test/unit/listen_addr.js b/test/unit/listen_addr.js index 5b1ae5f6a..dc2fd2ed8 100644 --- a/test/unit/listen_addr.js +++ b/test/unit/listen_addr.js @@ -6,12 +6,17 @@ describe('Parse address', function() { it(what, function() { if (proto === null) { assert.strictEqual(parse(what), null) - } else { + } else if (port) { assert.deepEqual(parse(what), { proto: proto, host: host, port: port, }) + } else { + assert.deepEqual(parse(what), { + proto: proto, + path: host, + }) } }) } @@ -26,7 +31,12 @@ describe('Parse address', function() { addTest('[::1]:4873', 'http', '::1', '4873') addTest('https:[::1]:4873', 'https', '::1', '4873') + addTest('unix:/tmp/foo.sock', 'http', '/tmp/foo.sock') + addTest('http:unix:foo.sock', 'http', 'foo.sock') + addTest('https://unix:foo.sock', 'https', 'foo.sock') + addTest('blah', null) addTest('blah://4873', null) addTest('https://blah:4873///', null) + addTest('unix:1234', 'http', 'unix', '1234') // not unix socket })