Skip to content

Commit

Permalink
fix bug preventing proxying https urls from working
Browse files Browse the repository at this point in the history
  • Loading branch information
mistermoe committed Sep 25, 2017
1 parent 07a7b7c commit 142bde0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface AxiosBasicCredentials {
export interface AxiosProxyConfig {
host: string;
port: number;
protocol?: string;
}

export interface AxiosRequestConfig {
Expand Down
26 changes: 17 additions & 9 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});

Expand All @@ -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));
});

Expand All @@ -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;
});
}

Expand Down

0 comments on commit 142bde0

Please sign in to comment.