From dcc5ba0c7e291a4a70d52713cd9fa2b8d47eb582 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 29 Jun 2018 02:18:44 -0600 Subject: [PATCH] add unix domain socket support and update README --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 18 ++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae3a5fd..16a0673 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,57 @@ request('http://www.google.com', function (error, response, body) { }); ``` +## Table of contents + +- [Custom HTTP Headers](#custom-http-headers) +- [Unix Domain Sockets](#unix-domain-sockets) +- [**All Available Options**](#requestoptions-callback) + +## Custom HTTP Headers + +HTTP Headers, such as `User-Agent`, can be set in the `options` object. +In the example below, we call the github API to find out the number +of stars and forks for the request repository. This requires a +custom `User-Agent` header as well as https. + +```js +var request = require('request'); + +var options = { + url: 'https://api.github.com/repos/request/request', + headers: { + 'User-Agent': 'request' + } +}; + +function callback(error, response, body) { + if (!error && response.statusCode == 200) { + var info = JSON.parse(body); + console.log(info.stargazers_count + " Stars"); + console.log(info.forks_count + " Forks"); + } +} + +request(options, callback); +``` + +[back to top](#table-of-contents) + +--- + +## UNIX Domain Sockets + +`request` supports making requests to [UNIX Domain Sockets](https://en.wikipedia.org/wiki/Unix_domain_socket). To make one, use the following URL scheme: + +```js +/* Pattern */ 'http://unix:SOCKET:PATH' +/* Example */ request.get('http://unix:/absolute/path/to/unix.socket:/request/path') +``` + +Note: The `SOCKET` path is assumed to be absolute to the root of the host file system. + +[back to top](#table-of-contents) + --- ## request(options, callback) @@ -133,3 +184,5 @@ There are at least two ways to debug the operation of `request`: [back to top](#table-of-contents) --> + +[back to top](#table-of-contents) diff --git a/index.js b/index.js index c6ed343..f6fd1a2 100644 --- a/index.js +++ b/index.js @@ -102,6 +102,7 @@ function setDefaults(defs) { if (opts.removeRefererHeader && opts.headers) { delete opts.headers.referer; } + // TODO needs baseUrl, maybe test for host / socketPath? opts.url = resp.headers.location; opts.uri = url.parse(opts.url); return urequestHelper(opts, cb); @@ -206,6 +207,19 @@ function setDefaults(defs) { } } + function parseUrl(str) { + var obj = url.parse(str); + var paths; + if ('unix' !== (obj.hostname||obj.host||'').toLowerCase()) { + return obj; + } + obj.hostname = null; + paths = (obj.pathname||obj.path||'').split(':'); + obj.socketPath = paths.shift(); + obj.pathname = obj.path = paths.join(':'); + obj.href = null; + } + function urequest(opts, cb) { debug("\n[urequest] received options:"); debug(opts); @@ -231,10 +245,10 @@ function setDefaults(defs) { if ('string' === typeof opts.url || 'string' === typeof opts.uri) { if ('string' === typeof opts.url) { reqOpts.url = opts.url; - reqOpts.uri = url.parse(opts.url); + reqOpts.uri = parseUrl(opts.url); } else if ('string' === typeof opts.uri) { reqOpts.url = opts.uri; - reqOpts.uri = url.parse(opts.uri); + reqOpts.uri = parseUrl(opts.uri); } } else { if ('object' === typeof opts.uri) {