diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 0985d7c87c..b864afeb18 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -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'; @@ -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) { @@ -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) { @@ -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:') { @@ -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; @@ -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, diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 59c5a11f72..516ee078b1 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -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);