From 142bde0b7ebc51bbb6f5c4d04a37dbf7dcabf532 Mon Sep 17 00:00:00 2001 From: mistermoe Date: Mon, 25 Sep 2017 16:20:00 -0500 Subject: [PATCH] fix bug preventing proxying https urls from working --- index.d.ts | 1 + lib/adapters/http.js | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/index.d.ts b/index.d.ts index 188895d2cd..60e05c9747 100644 --- a/index.d.ts +++ b/index.d.ts @@ -14,6 +14,7 @@ export interface AxiosBasicCredentials { export interface AxiosProxyConfig { host: string; port: number; + protocol?: string; } export interface AxiosRequestConfig { diff --git a/lib/adapters/http.js b/lib/adapters/http.js index af2f59dcd1..239bb4c514 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -13,12 +13,15 @@ var pkg = require('./../../package.json'); var createError = require('../core/createError'); var enhanceError = require('../core/enhanceError'); +var isHttps = /https:?/; + /*eslint consistent-return:0*/ module.exports = function httpAdapter(config) { return new Promise(function dispatchHttpRequest(resolve, reject) { var data = config.data; var headers = config.headers; var timer; + var aborted = false; // Set User-Agent (required by some servers) // Only set header if it hasn't been set in config @@ -68,8 +71,8 @@ module.exports = function httpAdapter(config) { delete headers.Authorization; } - var isHttps = protocol === 'https:'; - var agent = isHttps ? config.httpsAgent : config.httpAgent; + var isHttpsRequest = isHttps.test(protocol); + var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent; var options = { hostname: parsed.hostname, @@ -82,7 +85,7 @@ module.exports = function httpAdapter(config) { }; var proxy = config.proxy; - if (!proxy && proxy !== false) { + if (!proxy) { var proxyEnv = protocol.slice(0, -1) + '_proxy'; var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()]; if (proxyUrl) { @@ -117,18 +120,19 @@ module.exports = function httpAdapter(config) { } var transport; + var isHttpsProxy = isHttpsRequest && (proxy ? isHttps.test(proxy.protocol) : true); if (config.maxRedirects === 0) { - transport = isHttps ? https : http; + transport = isHttpsProxy ? https : http; } else { if (config.maxRedirects) { options.maxRedirects = config.maxRedirects; } - transport = isHttps ? httpsFollow : httpFollow; + transport = isHttpsProxy ? httpsFollow : httpFollow; } // Create the request var req = transport.request(options, function handleResponse(res) { - if (req.aborted) return; + if (aborted) return; // Response has been received so kill timer that handles request timeout clearTimeout(timer); @@ -176,7 +180,7 @@ module.exports = function httpAdapter(config) { }); stream.on('error', function handleStreamError(err) { - if (req.aborted) return; + if (aborted) return; reject(enhanceError(err, config, null, lastRequest)); }); @@ -194,7 +198,7 @@ module.exports = function httpAdapter(config) { // Handle errors req.on('error', function handleRequestError(err) { - if (req.aborted) return; + if (aborted) return; reject(enhanceError(err, config, null, req)); }); @@ -203,16 +207,20 @@ module.exports = function httpAdapter(config) { timer = setTimeout(function handleRequestTimeout() { req.abort(); reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req)); + aborted = true; }, config.timeout); } if (config.cancelToken) { // Handle cancellation config.cancelToken.promise.then(function onCanceled(cancel) { - if (req.aborted) return; + if (aborted) { + return; + } req.abort(); reject(cancel); + aborted = true; }); }