Skip to content

Commit

Permalink
feat: add unix domain socket file support (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX authored Apr 2, 2021
1 parent f2a42d1 commit 2acf75b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ httpclient.request('http://nodejs.org', function (err, body) {
- ***lookup*** Function - Custom DNS lookup function, default is `dns.lookup`. Require node >= 4.0.0(for http protocol) and node >=8(for https protocol)
- ***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.
- ***trace*** Boolean - Enable capture stack include call site of library entrance, default is `false`.
- ***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))
- ***callback(err, data, res)*** Function - Optional callback.
- **err** Error - Would be `null` if no error accured.
- **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`.
Expand Down
4 changes: 4 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export interface RequestOptions {
* It rely on lookup and have the same version requirement.
*/
checkAddress?: (ip: string, family: number | string) => boolean;
/**
* UNIX domain socket path. (Windows is not supported)
*/
socketPath?: string;
}

export interface HttpClientResponse<T> {
Expand Down
4 changes: 4 additions & 0 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ var SOCKET_RESPONSE_COUNT = '_URLLIB_SOCKET_RESPONSE_COUNT';
* Require node >= 4.0.0 and only work on `http` protocol.
* - {Boolean} [enableProxy]: optional, enable proxy request. Default is `false`.
* - {String|Object} [proxy]: optional proxy agent uri or options. Default is `null`.
* - {String} [socketPath]: optional, unix domain socket file path.
* - {Function} checkAddress: optional, check request address to protect from SSRF and similar attacks.
* @param {Function} [callback]: callback(error, data, res). If missing callback, will return a promise object.
* @return {HttpRequest} req object.
Expand Down Expand Up @@ -315,6 +316,9 @@ function requestWithCallback(url, args, callback) {
options.headers[key] = args.headers[name];
}
}
if (args.socketPath) {
options.socketPath = args.socketPath;
}

var sslNames = [
'pfx',
Expand Down
30 changes: 30 additions & 0 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,36 @@ describe('test/urllib.test.js', function () {
});
});

if (process.platform !== 'win32') {
describe('options.socketPath', function() {
let srv;
let socketPath;
beforeEach(function (done) {
socketPath = path.join(os.tmpdir(), `urllib-${Date.now()}.sock`);
srv = http.createServer(function (req, resp) {
resp.end(req.url);
});
srv.listen(socketPath, function() {
done();
});
});

this.afterEach(function() {
srv.close();
});

it('should request socket path', function (done) {
urllib.request('/ping?hello=world', {
socketPath,
}, function (err, data) {
assert(!err);
assert.strictEqual(data.toString(), '/?hello=world');
done();
});
});
});
}

describe('options.fixJSONCtlChars = true | false', function () {

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

0 comments on commit 2acf75b

Please sign in to comment.