Browse Source

allow requests via unix domain socket

Closes #96
http2
vdemedes 10 years ago
committed by Vsevolod Strukchinsky
parent
commit
3182234d0e
  1. 17
      index.js
  2. 1
      package.json
  3. 22
      readme.md
  4. 45
      test/test-unix-socket.js

17
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;
}

1
package.json

@ -61,6 +61,7 @@
"istanbul": "^0.3.13",
"pem": "^1.4.4",
"tap": "^1.0.0",
"tempfile": "^1.1.1",
"xo": "*"
}
}

22
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

45
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();
});
Loading…
Cancel
Save