Skip to content

Commit 2acf75b

Browse files
authored
feat: add unix domain socket file support (#352)
1 parent f2a42d1 commit 2acf75b

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ httpclient.request('http://nodejs.org', function (err, body) {
165165
- ***lookup*** Function - Custom DNS lookup function, default is `dns.lookup`. Require node >= 4.0.0(for http protocol) and node >=8(for https protocol)
166166
- ***checkAddress*** Function: optional, check request address to protect from SSRF and similar attacks. It receive tow arguments(`ip` and `family`) and should return true or false to identified the address is legal or not. It rely on `lookup` and have the same version requirement.
167167
- ***trace*** Boolean - Enable capture stack include call site of library entrance, default is `false`.
168+
- ***socketPath*** String - optional Unix Domain Socket. (Refer to [Node.js Document](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_http_request_options_callback))
168169
- ***callback(err, data, res)*** Function - Optional callback.
169170
- **err** Error - Would be `null` if no error accured.
170171
- **data** Buffer | Object - The data responsed. Would be a Buffer if `dataType` is set to `text` or an JSON parsed into Object if it's set to `json`.

lib/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export interface RequestOptions {
130130
* It rely on lookup and have the same version requirement.
131131
*/
132132
checkAddress?: (ip: string, family: number | string) => boolean;
133+
/**
134+
* UNIX domain socket path. (Windows is not supported)
135+
*/
136+
socketPath?: string;
133137
}
134138

135139
export interface HttpClientResponse<T> {

lib/urllib.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ var SOCKET_RESPONSE_COUNT = '_URLLIB_SOCKET_RESPONSE_COUNT';
138138
* Require node >= 4.0.0 and only work on `http` protocol.
139139
* - {Boolean} [enableProxy]: optional, enable proxy request. Default is `false`.
140140
* - {String|Object} [proxy]: optional proxy agent uri or options. Default is `null`.
141+
* - {String} [socketPath]: optional, unix domain socket file path.
141142
* - {Function} checkAddress: optional, check request address to protect from SSRF and similar attacks.
142143
* @param {Function} [callback]: callback(error, data, res). If missing callback, will return a promise object.
143144
* @return {HttpRequest} req object.
@@ -315,6 +316,9 @@ function requestWithCallback(url, args, callback) {
315316
options.headers[key] = args.headers[name];
316317
}
317318
}
319+
if (args.socketPath) {
320+
options.socketPath = args.socketPath;
321+
}
318322

319323
var sslNames = [
320324
'pfx',

test/urllib.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,36 @@ describe('test/urllib.test.js', function () {
17401740
});
17411741
});
17421742

1743+
if (process.platform !== 'win32') {
1744+
describe('options.socketPath', function() {
1745+
let srv;
1746+
let socketPath;
1747+
beforeEach(function (done) {
1748+
socketPath = path.join(os.tmpdir(), `urllib-${Date.now()}.sock`);
1749+
srv = http.createServer(function (req, resp) {
1750+
resp.end(req.url);
1751+
});
1752+
srv.listen(socketPath, function() {
1753+
done();
1754+
});
1755+
});
1756+
1757+
this.afterEach(function() {
1758+
srv.close();
1759+
});
1760+
1761+
it('should request socket path', function (done) {
1762+
urllib.request('/ping?hello=world', {
1763+
socketPath,
1764+
}, function (err, data) {
1765+
assert(!err);
1766+
assert.strictEqual(data.toString(), '/?hello=world');
1767+
done();
1768+
});
1769+
});
1770+
});
1771+
}
1772+
17431773
describe('options.fixJSONCtlChars = true | false', function () {
17441774

17451775
it('should auto fix json control characters', function (done) {

0 commit comments

Comments
 (0)