Skip to content

Commit

Permalink
Use WHATWG URL API instead of url.parse() (#4852)
Browse files Browse the repository at this point in the history
* replace url.parse with new whatwg url api

* remove comments

* use const instead of var

Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
felipedamin and jasonsaayman committed Sep 14, 2022
1 parent dd5ba02 commit 738fa63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions lib/adapters/http.js
Expand Up @@ -8,7 +8,6 @@ import {getProxyForUrl} from 'proxy-from-env';
import http from 'http';
import https from 'https';
import followRedirects from 'follow-redirects';
import url from 'url';
import zlib from 'zlib';
import {VERSION} from '../env/data.js';
import transitionalDefaults from '../defaults/transitional.js';
Expand Down Expand Up @@ -62,13 +61,15 @@ function setProxy(options, configProxy, location) {
if (!proxy && proxy !== false) {
const proxyUrl = getProxyForUrl(location);
if (proxyUrl) {
proxy = url.parse(proxyUrl);
// replace 'host' since the proxy object is not a URL object
proxy.host = proxy.hostname;
proxy = new URL(proxyUrl);
}
}
if (proxy) {
// Basic proxy authorization
if (proxy.username) {
proxy.auth = (proxy.username || '') + ':' + (proxy.password || '');
}

if (proxy.auth) {
// Support proxy auth object form
if (proxy.auth.username || proxy.auth.password) {
Expand All @@ -81,8 +82,9 @@ function setProxy(options, configProxy, location) {
}

options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
options.hostname = proxy.host;
options.host = proxy.host;
options.hostname = proxy.hostname;
// Replace 'host' since options is not a URL object
options.host = proxy.hostname;
options.port = proxy.port;
options.path = location;
if (proxy.protocol) {
Expand Down Expand Up @@ -163,7 +165,7 @@ export default function httpAdapter(config) {

// Parse url
const fullPath = buildFullPath(config.baseURL, config.url);
const parsed = url.parse(fullPath);
const parsed = new URL(fullPath);
const protocol = parsed.protocol || supportedProtocols[0];

if (protocol === 'data:') {
Expand Down Expand Up @@ -291,17 +293,17 @@ export default function httpAdapter(config) {
auth = username + ':' + password;
}

if (!auth && parsed.auth) {
const urlAuth = parsed.auth.split(':');
const urlUsername = urlAuth[0] || '';
const urlPassword = urlAuth[1] || '';
if (!auth && parsed.username) {
const urlUsername = parsed.username;
const urlPassword = parsed.password;
auth = urlUsername + ':' + urlPassword;
}

auth && headers.delete('authorization');

const path = parsed.pathname.concat(parsed.searchParams);
try {
buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, '');
buildURL(path, config.params, config.paramsSerializer).replace(/^\?/, '');
} catch (err) {
const customErr = new Error(err.message);
customErr.config = config;
Expand All @@ -313,8 +315,8 @@ export default function httpAdapter(config) {
headers.set('Accept-Encoding', 'gzip, deflate, br', false);

const options = {
path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
method,
path: buildURL(path, config.params, config.paramsSerializer).replace(/^\?/, ''),
method: method,
headers: headers.toJSON(),
agents: { http: config.httpAgent, https: config.httpsAgent },
auth,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/adapters/http.js
Expand Up @@ -706,7 +706,7 @@ describe('supports http with nodejs', function () {
}).listen(socketName, function () {
axios({
socketPath: socketName,
url: '/'
url: 'http://localhost:4444/socket'
})
.then(function (resp) {
assert.equal(resp.status, 200);
Expand Down

0 comments on commit 738fa63

Please sign in to comment.