1 /*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
6 Object.defineProperty(exports, "__esModule", { value: true });
7 var url_1 = require("url");
8 var https = require("https");
9 var http = require("http");
10 var HttpProxyAgent = require("http-proxy-agent");
11 var HttpsProxyAgent = require("https-proxy-agent");
12 var zlib = require("zlib");
13 var nls = require("vscode-nls");
14 if (process.env.VSCODE_NLS_CONFIG) {
15 var VSCODE_NLS_CONFIG = process.env.VSCODE_NLS_CONFIG;
16 nls.config(JSON.parse(VSCODE_NLS_CONFIG));
18 var localize = nls.loadMessageBundle();
21 function configure(_proxyUrl, _strictSSL) {
23 strictSSL = _strictSSL;
25 exports.configure = configure;
26 function xhr(options) {
27 var agent = getProxyAgent(options.url, { proxyUrl: proxyUrl, strictSSL: strictSSL });
28 options = assign({}, options);
29 options = assign(options, { agent: agent, strictSSL: strictSSL });
30 if (typeof options.followRedirects !== 'number') {
31 options.followRedirects = 5;
33 return request(options).then(function (result) { return new Promise(function (c, e) {
36 var encoding = res.headers && res.headers['content-encoding'];
37 var isCompleted = false;
38 if (encoding === 'gzip') {
39 var gunzip = zlib.createGunzip();
43 else if (encoding === 'deflate') {
44 var inflate = zlib.createInflate();
49 readable.on('data', function (c) { return data.push(c); });
50 readable.on('end', function () {
55 if (options.followRedirects > 0 && (res.statusCode >= 300 && res.statusCode <= 303 || res.statusCode === 307)) {
56 var location = res.headers['location'];
59 type: options.type, url: location, user: options.user, password: options.password, responseType: options.responseType, headers: options.headers,
60 timeout: options.timeout, followRedirects: options.followRedirects - 1, data: options.data
62 xhr(newOptions).then(c, e);
67 responseText: data.join(''),
68 status: res.statusCode,
69 headers: res.headers || {}
71 if ((res.statusCode >= 200 && res.statusCode < 300) || res.statusCode === 1223) {
78 readable.on('error', function (err) {
80 responseText: localize('error', 'Unable to access {0}. Error: {1}', options.url, err.message),
87 }); }, function (err) {
90 message = localize('error.cannot.connect.proxy', 'Unable to connect to {0} through a proxy . Error: {1}', options.url, err.message);
93 message = localize('error.cannot.connect', 'Unable to connect to {0}. Error: {1}', options.url, err.message);
95 return Promise.reject({
96 responseText: message,
102 function assign(destination) {
104 for (var _i = 1; _i < arguments.length; _i++) {
105 sources[_i - 1] = arguments[_i];
107 sources.forEach(function (source) { return Object.keys(source).forEach(function (key) { return destination[key] = source[key]; }); });
110 function request(options) {
112 return new Promise(function (c, e) {
113 var endpoint = url_1.parse(options.url);
115 hostname: endpoint.hostname,
116 port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
118 method: options.type || 'GET',
119 headers: options.headers,
120 agent: options.agent,
121 rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
123 if (options.user && options.password) {
124 opts.auth = options.user + ':' + options.password;
126 var handler = function (res) {
127 if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
128 c(request(assign({}, options, {
129 url: res.headers['location'],
130 followRedirects: options.followRedirects - 1
134 c({ req: req, res: res });
137 if (endpoint.protocol === 'https:') {
138 req = https.request(opts, handler);
141 req = http.request(opts, handler);
144 if (options.timeout) {
145 req.setTimeout(options.timeout);
148 req.write(options.data);
153 function getErrorStatusDescription(status) {
158 case 400: return localize('status.400', 'Bad request. The request cannot be fulfilled due to bad syntax.');
159 case 401: return localize('status.401', 'Unauthorized. The server is refusing to respond.');
160 case 403: return localize('status.403', 'Forbidden. The server is refusing to respond.');
161 case 404: return localize('status.404', 'Not Found. The requested location could not be found.');
162 case 405: return localize('status.405', 'Method not allowed. A request was made using a request method not supported by that location.');
163 case 406: return localize('status.406', 'Not Acceptable. The server can only generate a response that is not accepted by the client.');
164 case 407: return localize('status.407', 'Proxy Authentication Required. The client must first authenticate itself with the proxy.');
165 case 408: return localize('status.408', 'Request Timeout. The server timed out waiting for the request.');
166 case 409: return localize('status.409', 'Conflict. The request could not be completed because of a conflict in the request.');
167 case 410: return localize('status.410', 'Gone. The requested page is no longer available.');
168 case 411: return localize('status.411', 'Length Required. The "Content-Length" is not defined.');
169 case 412: return localize('status.412', 'Precondition Failed. The precondition given in the request evaluated to false by the server.');
170 case 413: return localize('status.413', 'Request Entity Too Large. The server will not accept the request, because the request entity is too large.');
171 case 414: return localize('status.414', 'Request-URI Too Long. The server will not accept the request, because the URL is too long.');
172 case 415: return localize('status.415', 'Unsupported Media Type. The server will not accept the request, because the media type is not supported.');
173 case 500: return localize('status.500', 'Internal Server Error.');
174 case 501: return localize('status.501', 'Not Implemented. The server either does not recognize the request method, or it lacks the ability to fulfill the request.');
175 case 503: return localize('status.503', 'Service Unavailable. The server is currently unavailable (overloaded or down).');
176 default: return localize('status.416', 'HTTP status code {0}', status);
179 exports.getErrorStatusDescription = getErrorStatusDescription;
181 function getSystemProxyURI(requestURL) {
182 if (requestURL.protocol === 'http:') {
183 return process.env.HTTP_PROXY || process.env.http_proxy || null;
185 else if (requestURL.protocol === 'https:') {
186 return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || null;
190 function getProxyAgent(rawRequestURL, options) {
191 if (options === void 0) { options = {}; }
192 var requestURL = url_1.parse(rawRequestURL);
193 var proxyURL = options.proxyUrl || getSystemProxyURI(requestURL);
197 var proxyEndpoint = url_1.parse(proxyURL);
198 if (!/^https?:$/.test(proxyEndpoint.protocol)) {
202 host: proxyEndpoint.hostname,
203 port: Number(proxyEndpoint.port),
204 auth: proxyEndpoint.auth,
205 rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
207 return requestURL.protocol === 'http:' ? new HttpProxyAgent(opts) : new HttpsProxyAgent(opts);