|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
| 4 | +/// <reference path="../node/tunnel.d.ts"/> |
| 5 | + |
4 | 6 | import url = require("url"); |
5 | 7 |
|
6 | 8 | import http = require("http"); |
7 | 9 | import https = require("https"); |
| 10 | +import tunnel = require("tunnel"); |
8 | 11 | import ifm = require('./interfaces/common/VsoBaseInterfaces'); |
9 | 12 |
|
10 | 13 | http.globalAgent.maxSockets = 100; |
@@ -95,43 +98,46 @@ export class HttpClient implements ifm.IHttpClient { |
95 | 98 | this.isSsl = usingSsl; |
96 | 99 |
|
97 | 100 | var proxyUrl: url.Url; |
98 | | - if (process.env.HTTP_PROXY) { |
| 101 | + if (process.env.HTTPS_PROXY && usingSsl) { |
| 102 | + proxyUrl = url.parse(process.env.HTTPS_PROXY); |
| 103 | + } else if (process.env.HTTP_PROXY) { |
99 | 104 | proxyUrl = url.parse(process.env.HTTP_PROXY); |
100 | | - prot = proxyUrl.protocol === 'https:' ? https: http; |
101 | 105 | } |
102 | 106 |
|
103 | | - var options: any = { headers: {}}; |
| 107 | + var options: any = { |
| 108 | + host: parsedUrl.hostname, |
| 109 | + port: parsedUrl.port || defaultPort, |
| 110 | + path: (parsedUrl.pathname || '') + (parsedUrl.search || ''), |
| 111 | + method: method, |
| 112 | + headers: headers || {} |
| 113 | + }; |
| 114 | + |
| 115 | + //options.headers["Accept"] = contentType; |
| 116 | + options.headers["User-Agent"] = this.userAgent; |
104 | 117 |
|
105 | 118 | var useProxy = proxyUrl && proxyUrl.hostname; |
106 | 119 | if (useProxy) { |
107 | | - // TODO: support proxy-authorization |
108 | | - options = { |
109 | | - host: proxyUrl.hostname, |
110 | | - port: proxyUrl.port || 8888, |
111 | | - path: requestUrl, |
112 | | - method: method, |
113 | | - headers: {} |
114 | | - } |
115 | | - } |
116 | | - else { |
117 | | - options = { |
118 | | - host: parsedUrl.hostname, |
119 | | - port: parsedUrl.port || defaultPort, |
120 | | - path: (parsedUrl.pathname || '') + (parsedUrl.search || ''), |
121 | | - method: method, |
122 | | - headers: {} |
| 120 | + var agentOptions: tunnel.TunnelOptions = { |
| 121 | + maxSockets: http.globalAgent.maxSockets, |
| 122 | + proxy: { |
| 123 | + // TODO: support proxy-authorization |
| 124 | + //proxyAuth: "user:password", |
| 125 | + host: proxyUrl.hostname, |
| 126 | + port: proxyUrl.port |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + var tunnelAgent: Function; |
| 131 | + var overHttps = proxyUrl.protocol === 'https:'; |
| 132 | + if (usingSsl) { |
| 133 | + tunnelAgent = overHttps ? tunnel.httpsOverHttps : tunnel.httpsOverHttp; |
| 134 | + } else { |
| 135 | + tunnelAgent = overHttps ? tunnel.httpOverHttps : tunnel.httpOverHttp; |
123 | 136 | } |
| 137 | + |
| 138 | + options.agent = tunnelAgent(agentOptions); |
124 | 139 | } |
125 | 140 |
|
126 | | - options.headers = headers; |
127 | | - |
128 | | - if (useProxy) { |
129 | | - options.headers['Host'] = parsedUrl.hostname; |
130 | | - } |
131 | | - |
132 | | - //options.headers["Accept"] = contentType; |
133 | | - options.headers["User-Agent"] = this.userAgent; |
134 | | - |
135 | 141 | if (this.handlers) { |
136 | 142 | this.handlers.forEach((handler) => { |
137 | 143 | handler.prepareRequest(options); |
|
0 commit comments