Skip to content

Commit

Permalink
http: replace url.parse() with WHATWG URL parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackzzila committed Apr 25, 2018
1 parent 317c2e1 commit 4d78329
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 28 deletions.
5 changes: 0 additions & 5 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1112,11 +1112,6 @@ Invalid characters were detected in headers.
A cursor on a given stream cannot be moved to a specified row without a
specified column.

<a id="ERR_INVALID_DOMAIN_NAME"></a>
### ERR_INVALID_DOMAIN_NAME

`hostname` can not be parsed from a provided URL.

<a id="ERR_INVALID_FD"></a>
### ERR_INVALID_FD

Expand Down
3 changes: 2 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,7 @@ Node.js maintains several connections per server to make HTTP requests.
This function allows one to transparently issue requests.

`options` can be an object, a string, or a [`URL`][] object. If `options` is a
string, it is automatically parsed with [`url.parse()`][]. If it is a [`URL`][]
string, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
object, it will be automatically converted to an ordinary `options` object.

The optional `callback` parameter will be added as a one-time listener for
Expand Down Expand Up @@ -2027,6 +2027,7 @@ not abort the request or do anything besides add a `'timeout'` event.
[`EventEmitter`]: events.html#events_class_eventemitter
[`TypeError`]: errors.html#errors_class_typeerror
[`URL`]: url.html#url_the_whatwg_url_api
[`new URL()`]: url.html#url_constructor_new_url_input_base
[`agent.createConnection()`]: #http_agent_createconnection_options_callback
[`agent.getName()`]: #http_agent_getname_options
[`destroy()`]: #http_agent_destroy
Expand Down
6 changes: 3 additions & 3 deletions doc/api/https.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ changes:
Like [`http.get()`][] but for HTTPS.

`options` can be an object, a string, or a [`URL`][] object. If `options` is a
string, it is automatically parsed with [`url.parse()`][]. If it is a [`URL`][]
string, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
object, it will be automatically converted to an ordinary `options` object.

Example:
Expand Down Expand Up @@ -174,7 +174,7 @@ The following additional `options` from [`tls.connect()`][] are also accepted:
`secureOptions`, `secureProtocol`, `servername`, `sessionIdContext`

`options` can be an object, a string, or a [`URL`][] object. If `options` is a
string, it is automatically parsed with [`url.parse()`][]. If it is a [`URL`][]
string, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
object, it will be automatically converted to an ordinary `options` object.

Example:
Expand Down Expand Up @@ -346,6 +346,7 @@ headers: max-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; p

[`Agent`]: #https_class_https_agent
[`URL`]: url.html#url_the_whatwg_url_api
[`new URL()`]: url.html#url_constructor_new_url_input_base
[`http.Agent`]: http.html#http_class_http_agent
[`http.Server#keepAliveTimeout`]: http.html#http_server_keepalivetimeout
[`http.Server#setTimeout()`]: http.html#http_server_settimeout_msecs_callback
Expand All @@ -362,4 +363,3 @@ headers: max-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; p
[`tls.connect()`]: tls.html#tls_tls_connect_options_callback
[`tls.createSecureContext()`]: tls.html#tls_tls_createsecurecontext_options
[`tls.createServer()`]: tls.html#tls_tls_createserver_options_secureconnectionlistener
[`url.parse()`]: url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
8 changes: 2 additions & 6 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

const util = require('util');
const net = require('net');
const url = require('url');
const { URL } = require('url');
const { HTTPParser } = process.binding('http_parser');
const assert = require('assert').ok;
const {
Expand All @@ -42,7 +42,6 @@ const { outHeadersKey, ondrain } = require('internal/http');
const {
ERR_HTTP_HEADERS_SENT,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_DOMAIN_NAME,
ERR_INVALID_HTTP_TOKEN,
ERR_INVALID_PROTOCOL,
ERR_UNESCAPED_CHARACTERS
Expand All @@ -64,10 +63,7 @@ function ClientRequest(options, cb) {
OutgoingMessage.call(this);

if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
throw new ERR_INVALID_DOMAIN_NAME();
}
options = urlToOptions(new URL(options));
} else if (options && options[searchParamsSymbol] &&
options[searchParamsSymbol][searchParamsSymbol]) {
// url.URL instance
Expand Down
8 changes: 2 additions & 6 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
require('internal/util').assertCrypto();

const tls = require('tls');
const url = require('url');
const { URL } = require('url');
const util = require('util');
const { Agent: HttpAgent } = require('_http_agent');
const {
Expand All @@ -35,7 +35,6 @@ const { ClientRequest } = require('_http_client');
const { inherits } = util;
const debug = util.debuglog('https');
const { urlToOptions, searchParamsSymbol } = require('internal/url');
const { ERR_INVALID_DOMAIN_NAME } = require('internal/errors').codes;
const { IncomingMessage, ServerResponse } = require('http');
const { kIncomingMessage } = require('_http_common');
const { kServerResponse } = require('_http_server');
Expand Down Expand Up @@ -256,10 +255,7 @@ const globalAgent = new Agent();

function request(options, cb) {
if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
throw new ERR_INVALID_DOMAIN_NAME();
}
options = urlToOptions(new URL(options));
} else if (options && options[searchParamsSymbol] &&
options[searchParamsSymbol][searchParamsSymbol]) {
// url.URL instance
Expand Down
1 change: 0 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,6 @@ E('ERR_INVALID_CALLBACK', 'Callback must be a function', TypeError);
E('ERR_INVALID_CHAR', invalidChar, TypeError);
E('ERR_INVALID_CURSOR_POS',
'Cannot set cursor row without setting its column', TypeError);
E('ERR_INVALID_DOMAIN_NAME', 'Unable to determine the domain name', TypeError);
E('ERR_INVALID_FD',
'"fd" must be a positive integer: %s', RangeError);
E('ERR_INVALID_FD_TYPE', 'Unsupported fd type: %s', TypeError);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-invalid-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function test(host) {
const throws = () => { modules[module][fn](host, doNotCall); };
common.expectsError(throws, {
type: TypeError,
code: 'ERR_INVALID_DOMAIN_NAME'
code: 'ERR_INVALID_URL'
});
});
});
Expand Down
5 changes: 0 additions & 5 deletions test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ assert.strictEqual(
'Cannot render headers after they are sent to the client'
);

assert.strictEqual(
errors.message('ERR_INVALID_DOMAIN_NAME'),
'Unable to determine the domain name'
);

assert.strictEqual(
errors.message('ERR_INVALID_HTTP_TOKEN', ['Method', 'foo']),
'Method must be a valid HTTP token ["foo"]'
Expand Down

0 comments on commit 4d78329

Please sign in to comment.