Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use WHATWG URL API instead of url.parse() #4852

Merged
merged 4 commits into from Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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