@@ -4,6 +4,7 @@ var utils = require('./../utils');
4
4
var settle = require ( './../core/settle' ) ;
5
5
var buildFullPath = require ( '../core/buildFullPath' ) ;
6
6
var buildURL = require ( './../helpers/buildURL' ) ;
7
+ var getProxyForUrl = require ( 'proxy-from-env' ) . getProxyForUrl ;
7
8
var http = require ( 'http' ) ;
8
9
var https = require ( 'https' ) ;
9
10
var httpFollow = require ( 'follow-redirects' ) . http ;
@@ -22,25 +23,46 @@ var supportedProtocols = [ 'http:', 'https:', 'file:' ];
22
23
/**
23
24
*
24
25
* @param {http.ClientRequestArgs } options
25
- * @param {AxiosProxyConfig } proxy
26
+ * @param {AxiosProxyConfig } configProxy
26
27
* @param {string } location
27
28
*/
28
- function setProxy ( options , proxy , location ) {
29
- options . hostname = proxy . host ;
30
- options . host = proxy . host ;
31
- options . port = proxy . port ;
32
- options . path = location ;
33
-
34
- // Basic proxy authorization
35
- if ( proxy . auth ) {
36
- var base64 = Buffer . from ( proxy . auth . username + ':' + proxy . auth . password , 'utf8' ) . toString ( 'base64' ) ;
37
- options . headers [ 'Proxy-Authorization' ] = 'Basic ' + base64 ;
29
+ function setProxy ( options , configProxy , location ) {
30
+ var proxy = configProxy ;
31
+ if ( ! proxy && proxy !== false ) {
32
+ var proxyUrl = getProxyForUrl ( location ) ;
33
+ if ( proxyUrl ) {
34
+ proxy = url . parse ( proxyUrl ) ;
35
+ // replace 'host' since the proxy object is not a URL object
36
+ proxy . host = proxy . hostname ;
37
+ }
38
+ }
39
+ if ( proxy ) {
40
+ // Basic proxy authorization
41
+ if ( proxy . auth ) {
42
+ // Support proxy auth object form
43
+ if ( proxy . auth . username || proxy . auth . password ) {
44
+ proxy . auth = ( proxy . auth . username || '' ) + ':' + ( proxy . auth . password || '' ) ;
45
+ }
46
+ var base64 = Buffer
47
+ . from ( proxy . auth , 'utf8' )
48
+ . toString ( 'base64' ) ;
49
+ options . headers [ 'Proxy-Authorization' ] = 'Basic ' + base64 ;
50
+ }
51
+
52
+ options . headers . host = options . hostname + ( options . port ? ':' + options . port : '' ) ;
53
+ options . hostname = proxy . host ;
54
+ options . host = proxy . host ;
55
+ options . port = proxy . port ;
56
+ options . path = location ;
57
+ if ( proxy . protocol ) {
58
+ options . protocol = proxy . protocol ;
59
+ }
38
60
}
39
61
40
- // If a proxy is used, any redirects must also pass through the proxy
41
- options . beforeRedirect = function beforeRedirect ( redirection ) {
42
- redirection . headers . host = redirection . host ;
43
- setProxy ( redirection , proxy , redirection . href ) ;
62
+ options . beforeRedirect = function beforeRedirect ( redirectOptions ) {
63
+ // Configure proxy for redirected request, passing the original config proxy to apply
64
+ // the exact same logic as if the redirected request was performed by axios directly.
65
+ setProxy ( redirectOptions , configProxy , redirectOptions . href ) ;
44
66
} ;
45
67
}
46
68
@@ -152,9 +174,6 @@ module.exports = function httpAdapter(config) {
152
174
delete headers [ headerNames . authorization ] ;
153
175
}
154
176
155
- var isHttpsRequest = isHttps . test ( protocol ) ;
156
- var agent = isHttpsRequest ? config . httpsAgent : config . httpAgent ;
157
-
158
177
try {
159
178
buildURL ( parsed . path , config . params , config . paramsSerializer ) . replace ( / ^ \? / , '' ) ;
160
179
} catch ( err ) {
@@ -169,85 +188,34 @@ module.exports = function httpAdapter(config) {
169
188
path : buildURL ( parsed . path , config . params , config . paramsSerializer ) . replace ( / ^ \? / , '' ) ,
170
189
method : config . method . toUpperCase ( ) ,
171
190
headers : headers ,
172
- agent : agent ,
173
191
agents : { http : config . httpAgent , https : config . httpsAgent } ,
174
- auth : auth
192
+ auth : auth ,
193
+ protocol : protocol
175
194
} ;
176
195
177
196
if ( config . socketPath ) {
178
197
options . socketPath = config . socketPath ;
179
198
} else {
180
199
options . hostname = parsed . hostname ;
181
200
options . port = parsed . port ;
182
- }
183
-
184
- var proxy = config . proxy ;
185
- if ( ! proxy && proxy !== false ) {
186
- var proxyEnv = protocol . slice ( 0 , - 1 ) + '_proxy' ;
187
- var proxyUrl = process . env [ proxyEnv ] || process . env [ proxyEnv . toUpperCase ( ) ] ;
188
- if ( proxyUrl ) {
189
- var parsedProxyUrl = url . parse ( proxyUrl ) ;
190
- var noProxyEnv = process . env . no_proxy || process . env . NO_PROXY ;
191
- var shouldProxy = true ;
192
-
193
- if ( noProxyEnv ) {
194
- var noProxy = noProxyEnv . split ( ',' ) . map ( function trim ( s ) {
195
- return s . trim ( ) ;
196
- } ) ;
197
-
198
- shouldProxy = ! noProxy . some ( function proxyMatch ( proxyElement ) {
199
- if ( ! proxyElement ) {
200
- return false ;
201
- }
202
- if ( proxyElement === '*' ) {
203
- return true ;
204
- }
205
- if ( proxyElement [ 0 ] === '.' &&
206
- parsed . hostname . slice ( parsed . hostname . length - proxyElement . length ) === proxyElement ) {
207
- return true ;
208
- }
209
-
210
- return parsed . hostname === proxyElement ;
211
- } ) ;
212
- }
213
-
214
- if ( shouldProxy ) {
215
- proxy = {
216
- host : parsedProxyUrl . hostname ,
217
- port : parsedProxyUrl . port ,
218
- protocol : parsedProxyUrl . protocol
219
- } ;
220
-
221
- if ( parsedProxyUrl . auth ) {
222
- var proxyUrlAuth = parsedProxyUrl . auth . split ( ':' ) ;
223
- proxy . auth = {
224
- username : proxyUrlAuth [ 0 ] ,
225
- password : proxyUrlAuth [ 1 ]
226
- } ;
227
- }
228
- }
229
- }
230
- }
231
-
232
- if ( proxy ) {
233
- options . headers . host = parsed . hostname + ( parsed . port ? ':' + parsed . port : '' ) ;
234
- setProxy ( options , proxy , protocol + '//' + parsed . hostname + ( parsed . port ? ':' + parsed . port : '' ) + options . path ) ;
201
+ setProxy ( options , config . proxy , protocol + '//' + parsed . hostname + ( parsed . port ? ':' + parsed . port : '' ) + options . path ) ;
235
202
}
236
203
237
204
var transport ;
238
- var isHttpsProxy = isHttpsRequest && ( proxy ? isHttps . test ( proxy . protocol ) : true ) ;
205
+ var isHttpsRequest = isHttps . test ( options . protocol ) ;
206
+ options . agent = isHttpsRequest ? config . httpsAgent : config . httpAgent ;
239
207
if ( config . transport ) {
240
208
transport = config . transport ;
241
209
} else if ( config . maxRedirects === 0 ) {
242
- transport = isHttpsProxy ? https : http ;
210
+ transport = isHttpsRequest ? https : http ;
243
211
} else {
244
212
if ( config . maxRedirects ) {
245
213
options . maxRedirects = config . maxRedirects ;
246
214
}
247
215
if ( config . beforeRedirect ) {
248
216
options . beforeRedirect = config . beforeRedirect ;
249
217
}
250
- transport = isHttpsProxy ? httpsFollow : httpFollow ;
218
+ transport = isHttpsRequest ? httpsFollow : httpFollow ;
251
219
}
252
220
253
221
if ( config . maxBodyLength > - 1 ) {
0 commit comments