GeezerVison is showing github.com — http: allow url and options to be passed to http*.request and http*.get · nodejs/node@37369eb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/http.md +16-3Lines changed: 16 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1794,15 +1794,20 @@ The `requestListener` is a function which is automatically | 1794 | 1794 | added to the [`'request'`][] event. | 1795 | 1795 |
| 1796 | 1796 | ## http.get(options[, callback]) | | 1797 | +## http.get(url[, options][, callback]) | 1797 | 1798 | <!-- YAML | 1798 | 1799 | added: v0.3.6 | 1799 | 1800 | changes: | | 1801 | + - version: REPLACEME | | 1802 | + pr-url: https://github.com/nodejs/node/pull/21616 | | 1803 | + description: allow both url and options to be passed to `http.get()` | 1800 | 1804 | - version: v7.5.0 | 1801 | 1805 | pr-url: https://github.com/nodejs/node/pull/10638 | 1802 | 1806 | description: The `options` parameter can be a WHATWG `URL` object. | 1803 | 1807 | --> | 1804 | 1808 |
| 1805 | | -* `options` {Object | string | URL} Accepts the same `options` as | | 1809 | +* `url` {string | URL} | | 1810 | +* `options` {Object} Accepts the same `options` as | 1806 | 1811 | [`http.request()`][], with the `method` always set to `GET`. | 1807 | 1812 | Properties that are inherited from the prototype are ignored. | 1808 | 1813 | * `callback` {Function} | @@ -1866,15 +1871,20 @@ Global instance of `Agent` which is used as the default for all HTTP client | 1866 | 1871 | requests. | 1867 | 1872 |
| 1868 | 1873 | ## http.request(options[, callback]) | | 1874 | +## http.request(url[, options][, callback]) | 1869 | 1875 | <!-- YAML | 1870 | 1876 | added: v0.3.6 | 1871 | 1877 | changes: | | 1878 | + - version: REPLACEME | | 1879 | + pr-url: https://github.com/nodejs/node/pull/21616 | | 1880 | + description: allow both url and options to be passed to `http.request()` | 1872 | 1881 | - version: v7.5.0 | 1873 | 1882 | pr-url: https://github.com/nodejs/node/pull/10638 | 1874 | 1883 | description: The `options` parameter can be a WHATWG `URL` object. | 1875 | 1884 | --> | 1876 | 1885 |
| 1877 | | -* `options` {Object | string | URL} | | 1886 | +* `url` {string | URL} | | 1887 | +* `options` {Object} | 1878 | 1888 | * `protocol` {string} Protocol to use. **Default:** `'http:'`. | 1879 | 1889 | * `host` {string} A domain name or IP address of the server to issue the | 1880 | 1890 | request to. **Default:** `'localhost'`. | @@ -1916,10 +1926,13 @@ changes: | 1916 | 1926 | Node.js maintains several connections per server to make HTTP requests. | 1917 | 1927 | This function allows one to transparently issue requests. | 1918 | 1928 |
| 1919 | | -`options` can be an object, a string, or a [`URL`][] object. If `options` is a | | 1929 | +`url` can be a string or a [`URL`][] object. If `url` is a | 1920 | 1930 | string, it is automatically parsed with [`url.parse()`][]. If it is a [`URL`][] | 1921 | 1931 | object, it will be automatically converted to an ordinary `options` object. | 1922 | 1932 |
| | 1933 | +If both `url` and `options` are specified, the objects are merged, with the | | 1934 | +`options` properties taking precedence. | | 1935 | + | 1923 | 1936 | The optional `callback` parameter will be added as a one-time listener for | 1924 | 1937 | the [`'response'`][] event. | 1925 | 1938 |
| |
Collapse file Copy file name to clipboardExpand all lines: doc/api/https.md +12-2Lines changed: 12 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -112,14 +112,19 @@ https.createServer(options, (req, res) => { | 112 | 112 | ``` | 113 | 113 |
| 114 | 114 | ## https.get(options[, callback]) | | 115 | +## https.get(url[, options][, callback]) | 115 | 116 | <!-- YAML | 116 | 117 | added: v0.3.6 | 117 | 118 | changes: | | 119 | + - version: REPLACEME | | 120 | + pr-url: https://github.com/nodejs/node/pull/21616 | | 121 | + description: allow both url and options to be passed to `https.get()` | 118 | 122 | - version: v7.5.0 | 119 | 123 | pr-url: https://github.com/nodejs/node/pull/10638 | 120 | 124 | description: The `options` parameter can be a WHATWG `URL` object. | 121 | 125 | --> | 122 | | -- `options` {Object | string | URL} Accepts the same `options` as | | 126 | +- `url` {string | URL} | | 127 | +- `options` {Object} Accepts the same `options` as | 123 | 128 | [`https.request()`][], with the `method` always set to `GET`. | 124 | 129 | - `callback` {Function} | 125 | 130 |
| @@ -155,17 +160,22 @@ added: v0.5.9 | 155 | 160 | Global instance of [`https.Agent`][] for all HTTPS client requests. | 156 | 161 |
| 157 | 162 | ## https.request(options[, callback]) | | 163 | +## https.request(url[, options][, callback]) | 158 | 164 | <!-- YAML | 159 | 165 | added: v0.3.6 | 160 | 166 | changes: | | 167 | + - version: REPLACEME | | 168 | + pr-url: https://github.com/nodejs/node/pull/21616 | | 169 | + description: allow both url and options to be passed to `https.request()` | 161 | 170 | - version: v9.3.0 | 162 | 171 | pr-url: https://github.com/nodejs/node/pull/14903 | 163 | 172 | description: The `options` parameter can now include `clientCertEngine`. | 164 | 173 | - version: v7.5.0 | 165 | 174 | pr-url: https://github.com/nodejs/node/pull/10638 | 166 | 175 | description: The `options` parameter can be a WHATWG `URL` object. | 167 | 176 | --> | 168 | | -- `options` {Object | string | URL} Accepts all `options` from | | 177 | +- `url` {string | URL} | | 178 | +- `options` {Object} Accepts all `options` from | 169 | 179 | [`http.request()`][], with some differences in default values: | 170 | 180 | - `protocol` **Default:** `'https:'` | 171 | 181 | - `port` **Default:** `443` | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +17-8Lines changed: 17 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,22 +60,31 @@ function validateHost(host, name) { | 60 | 60 | return host; | 61 | 61 | } | 62 | 62 |
| 63 | | -function ClientRequest(options, cb) { | | 63 | +function ClientRequest(input, options, cb) { | 64 | 64 | OutgoingMessage.call(this); | 65 | 65 |
| 66 | | - if (typeof options === 'string') { | 67 | | - options = url.parse(options); | 68 | | - if (!options.hostname) { | | 66 | + if (typeof input === 'string') { | | 67 | + input = url.parse(input); | | 68 | + if (!input.hostname) { | 69 | 69 | throw new ERR_INVALID_DOMAIN_NAME(); | 70 | 70 | } | 71 | | - } else if (options && options[searchParamsSymbol] && | 72 | | - options[searchParamsSymbol][searchParamsSymbol]) { | | 71 | + } else if (input && input[searchParamsSymbol] && | | 72 | + input[searchParamsSymbol][searchParamsSymbol]) { | 73 | 73 | // url.URL instance | 74 | | - options = urlToOptions(options); | | 74 | + input = urlToOptions(input); | 75 | 75 | } else { | 76 | | - options = util._extend({}, options); | | 76 | + cb = options; | | 77 | + options = input; | | 78 | + input = null; | 77 | 79 | } | 78 | 80 |
| | 81 | + if (typeof options === 'function') { | | 82 | + cb = options; | | 83 | + options = null; | | 84 | + } | | 85 | + | | 86 | + options = util._extend(input || {}, options || {}); | | 87 | + | 79 | 88 | var agent = options.agent; | 80 | 89 | var defaultAgent = options._defaultAgent || Agent.globalAgent; | 81 | 90 | if (agent === false) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/http.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,12 +37,12 @@ function createServer(opts, requestListener) { | 37 | 37 | return new Server(opts, requestListener); | 38 | 38 | } | 39 | 39 |
| 40 | | -function request(options, cb) { | 41 | | - return new ClientRequest(options, cb); | | 40 | +function request(url, options, cb) { | | 41 | + return new ClientRequest(url, options, cb); | 42 | 42 | } | 43 | 43 |
| 44 | | -function get(options, cb) { | 45 | | - var req = request(options, cb); | | 44 | +function get(url, options, cb) { | | 45 | + var req = request(url, options, cb); | 46 | 46 | req.end(); | 47 | 47 | return req; | 48 | 48 | } | |
Collapse file Copy file name to clipboard +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +const assert = require('assert'); | | 4 | +const http = require('http'); | | 5 | + | | 6 | +// Test providing both a url and options, with the options partially | | 7 | +// replacing address and port portions of the URL provided. | | 8 | +{ | | 9 | + const server = http.createServer( | | 10 | + common.mustCall((req, res) => { | | 11 | + assert.strictEqual(req.url, '/testpath'); | | 12 | + res.end(); | | 13 | + server.close(); | | 14 | + }) | | 15 | + ); | | 16 | + server.listen( | | 17 | + 0, | | 18 | + common.mustCall(() => { | | 19 | + http.get( | | 20 | + 'http://example.com/testpath', | | 21 | + { hostname: 'localhost', port: server.address().port }, | | 22 | + common.mustCall((res) => { | | 23 | + res.resume(); | | 24 | + }) | | 25 | + ); | | 26 | + }) | | 27 | + ); | | 28 | +} |
|