From 3182234d0e91dca37f3b71d20696408c174c9137 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sat, 5 Sep 2015 15:39:55 +0300 Subject: [PATCH] allow requests via unix domain socket Closes #96 --- index.js | 17 +++++++++++++++ package.json | 1 + readme.md | 22 ++++++++++++++++++++ test/test-unix-socket.js | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 test/test-unix-socket.js diff --git a/index.js b/index.js index 28cae16..76be965 100644 --- a/index.js +++ b/index.js @@ -230,6 +230,23 @@ function normalizeArguments(url, opts) { opts.method = opts.method || 'GET'; + // check for unix domain socket + if (opts.hostname === 'unix') { + // extract socket path and request path + var matches = /(.+)\:(.+)/.exec(opts.path); + + if (matches) { + var socketPath = matches[1]; + var path = matches[2]; + + // make http.request use unix domain socket + // instead of host:port combination + opts.socketPath = socketPath; + opts.path = path; + opts.host = null; + } + } + return opts; } diff --git a/package.json b/package.json index df462a9..87ad2b6 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "istanbul": "^0.3.13", "pem": "^1.4.4", "tap": "^1.0.0", + "tempfile": "^1.1.1", "xo": "*" } } diff --git a/readme.md b/readme.md index c2345bc..5a80361 100644 --- a/readme.md +++ b/readme.md @@ -201,6 +201,28 @@ got('todomvc.com', { }, function () {}); ``` +### Unix Domain Sockets + +Requests can also be sent via [unix domain sockets](http://serverfault.com/questions/124517/whats-the-difference-between-unix-socket-and-tcp-ip-socket). Use the following URL scheme: `PROTOCOL://unix:SOCKET:PATH`. + +- `PROTOCOL` - `http` or `https` *(optional)* +- `SOCKET` - absolute path to a unix domain socket, e.g. `/var/run/docker.sock` +- `PATH` - request path, e.g. `/v2/keys` + +Example: + +```js +got('http://unix:/var/run/docker.sock:/containers/json'); + +// or without protocol (http by default) +got('unix:/var/run/docker.sock:/containers/json'); +``` + +Use-cases: + +- [Docker API](https://docs.docker.com/articles/basics/#bind-docker-to-another-host-port-or-a-unix-socket) (/var/run/docker.sock) +- [fleet API](https://coreos.com/fleet/docs/latest/deployment-and-configuration.html#api) (/var/run/fleet.sock) + ## Tip diff --git a/test/test-unix-socket.js b/test/test-unix-socket.js new file mode 100644 index 0000000..5919b79 --- /dev/null +++ b/test/test-unix-socket.js @@ -0,0 +1,45 @@ +'use strict'; +var tempfile = require('tempfile'); +var format = require('util').format; +var test = require('tap').test; +var got = require('../'); +var server = require('./server.js'); +var s = server.createServer(); + +var socketPath = tempfile('.socket'); + +s.on('/', function (req, res) { + res.end('ok'); +}); + +test('setup', function (t) { + s.listen(socketPath, function () { + t.end(); + }); +}); + +test('request via unix socket', function (t) { + // borrow unix domain socket url format from request module + var url = format('http://unix:%s:%s', socketPath, '/'); + + got(url, function (err, data) { + t.error(err); + t.equal(data, 'ok'); + t.end(); + }); +}); + +test('protocol-less request', function (t) { + var url = format('unix:%s:%s', socketPath, '/'); + + got(url, function (err, data) { + t.error(err); + t.equal(data, 'ok'); + t.end(); + }); +}); + +test('cleanup', function (t) { + s.close(); + t.end(); +});