Skip to content

Commit

Permalink
Fix https request on http proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Sitronik committed Dec 28, 2017
1 parent d7f021b commit 36c1f46
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ typings/
coverage/
test/typescript/axios.js*
sauce_connect.log
package-lock.json
31 changes: 20 additions & 11 deletions lib/adapters/http.js
Expand Up @@ -12,6 +12,7 @@ var zlib = require('zlib');
var pkg = require('./../../package.json');
var createError = require('../core/createError');
var enhanceError = require('../core/enhanceError');
var HttpsProxyAgent = require('https-proxy-agent');

/*eslint consistent-return:0*/
module.exports = function httpAdapter(config) {
Expand Down Expand Up @@ -71,6 +72,14 @@ module.exports = function httpAdapter(config) {
var isHttps = protocol === 'https:';
var agent = isHttps ? config.httpsAgent : config.httpAgent;

if(!agent && config.proxy && isHttps) {
if (config.proxy.auth) {
agent = new HttpsProxyAgent('http://'+config.proxy.auth.username+':'+config.proxy.auth.password+'@'+config.proxy.host+':'+config.proxy.port);
} else {
agent = new HttpsProxyAgent('http://'+config.proxy.host+':'+config.proxy.port);
}
}

var options = {
hostname: parsed.hostname,
port: parsed.port,
Expand Down Expand Up @@ -102,7 +111,7 @@ module.exports = function httpAdapter(config) {
}
}

if (proxy) {
if (proxy && (agent instanceof HttpsProxyAgent) === false) {
options.hostname = proxy.host;
options.host = proxy.host;
options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
Expand Down Expand Up @@ -139,16 +148,16 @@ module.exports = function httpAdapter(config) {
// uncompress the response body transparently if required
var stream = res;
switch (res.headers['content-encoding']) {
/*eslint default-case:0*/
case 'gzip':
case 'compress':
case 'deflate':
// add the unzipper to the body stream processing pipeline
stream = stream.pipe(zlib.createUnzip());

// remove the content-encoding in order to not confuse downstream operations
delete res.headers['content-encoding'];
break;
/*eslint default-case:0*/
case 'gzip':
case 'compress':
case 'deflate':
// add the unzipper to the body stream processing pipeline
stream = stream.pipe(zlib.createUnzip());

// remove the content-encoding in order to not confuse downstream operations
delete res.headers['content-encoding'];
break;
}

// return the last request in case of redirects
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -74,7 +74,8 @@
"typings": "./index.d.ts",
"dependencies": {
"follow-redirects": "^1.2.5",
"is-buffer": "^1.1.5"
"is-buffer": "^1.1.5",
"https-proxy-agent": "^2.1.1"
},
"bundlesize": [
{
Expand Down

0 comments on commit 36c1f46

Please sign in to comment.