Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973 (#6381)

* Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973

* disable strict null check
This commit is contained in:
Anthony Dresser
2019-07-15 22:35:46 -07:00
committed by GitHub
parent f720ec642f
commit 0b7e7ddbf9
2406 changed files with 59140 additions and 35464 deletions

View File

@@ -0,0 +1,52 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Url, parse as parseUrl } from 'url';
import { isBoolean } from 'vs/base/common/types';
export type Agent = any;
function getSystemProxyURI(requestURL: Url): string | null {
if (requestURL.protocol === 'http:') {
return process.env.HTTP_PROXY || process.env.http_proxy || null;
} else if (requestURL.protocol === 'https:') {
return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || null;
}
return null;
}
export interface IOptions {
proxyUrl?: string;
strictSSL?: boolean;
}
export async function getProxyAgent(rawRequestURL: string, options: IOptions = {}): Promise<Agent> {
const requestURL = parseUrl(rawRequestURL);
const proxyURL = options.proxyUrl || getSystemProxyURI(requestURL);
if (!proxyURL) {
return null;
}
const proxyEndpoint = parseUrl(proxyURL);
if (!/^https?:$/.test(proxyEndpoint.protocol || '')) {
return null;
}
const opts = {
host: proxyEndpoint.hostname || '',
port: Number(proxyEndpoint.port),
auth: proxyEndpoint.auth,
rejectUnauthorized: isBoolean(options.strictSSL) ? options.strictSSL : true
};
const Ctor = requestURL.protocol === 'http:'
? await import('http-proxy-agent')
: await import('https-proxy-agent');
return new Ctor(opts);
}

View File

@@ -1,68 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IRequestOptions, IRequestContext } from 'vs/base/node/request';
import { IConfigurationRegistry, Extensions } from 'vs/platform/configuration/common/configurationRegistry';
import { Registry } from 'vs/platform/registry/common/platform';
import { CancellationToken } from 'vs/base/common/cancellation';
export const IRequestService = createDecorator<IRequestService>('requestService');
export interface IRequestService {
_serviceBrand: any;
request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext>;
}
export interface IHTTPConfiguration {
http?: {
proxy?: string;
proxyStrictSSL?: boolean;
proxyAuthorization?: string;
};
}
Registry.as<IConfigurationRegistry>(Extensions.Configuration)
.registerConfiguration({
id: 'http',
order: 15,
title: localize('httpConfigurationTitle', "HTTP"),
type: 'object',
properties: {
'http.proxy': {
type: 'string',
pattern: '^https?://([^:]*(:[^@]*)?@)?([^:]+)(:\\d+)?/?$|^$',
description: localize('proxy', "The proxy setting to use. If not set will be taken from the http_proxy and https_proxy environment variables.")
},
'http.proxyStrictSSL': {
type: 'boolean',
default: true,
description: localize('strictSSL', "Controls whether the proxy server certificate should be verified against the list of supplied CAs.")
},
'http.proxyAuthorization': {
type: ['null', 'string'],
default: null,
description: localize('proxyAuthorization', "The value to send as the 'Proxy-Authorization' header for every network request.")
},
'http.proxySupport': {
type: 'string',
enum: ['off', 'on', 'override'],
enumDescriptions: [
localize('proxySupportOff', "Disable proxy support for extensions."),
localize('proxySupportOn', "Enable proxy support for extensions."),
localize('proxySupportOverride', "Enable proxy support for extensions, override request options."),
],
default: 'override',
description: localize('proxySupport', "Use the proxy support for extensions.")
},
'http.systemCertificates': {
type: 'boolean',
default: true,
description: localize('systemCertificates', "Controls whether CA certificates should be loaded from the OS. (On Windows and macOS a reload of the window is required after turning this off.)")
}
}
});

View File

@@ -3,34 +3,51 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IDisposable } from 'vs/base/common/lifecycle';
import * as https from 'https';
import * as http from 'http';
import { Stream } from 'stream';
import { createGunzip } from 'zlib';
import { parse as parseUrl } from 'url';
import { Disposable } from 'vs/base/common/lifecycle';
import { assign } from 'vs/base/common/objects';
import { IRequestOptions, IRequestContext, IRequestFunction, request } from 'vs/base/node/request';
import { getProxyAgent } from 'vs/base/node/proxy';
import { IRequestService, IHTTPConfiguration } from 'vs/platform/request/node/request';
import { isBoolean, isNumber } from 'vs/base/common/types';
import { canceled } from 'vs/base/common/errors';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IRequestOptions, IRequestContext, IRequestService, IHTTPConfiguration } from 'vs/platform/request/common/request';
import { getProxyAgent, Agent } from 'vs/platform/request/node/proxy';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ILogService } from 'vs/platform/log/common/log';
import { CancellationToken } from 'vs/base/common/cancellation';
import { toVSBufferReadableStream } from 'vs/base/common/buffer';
export interface IRawRequestFunction {
(options: http.RequestOptions, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
}
export interface NodeRequestOptions extends IRequestOptions {
agent?: Agent;
strictSSL?: boolean;
getRawRequest?(options: IRequestOptions): IRawRequestFunction;
}
/**
* This service exposes the `request` API, while using the global
* or configured proxy settings.
*/
export class RequestService implements IRequestService {
export class RequestService extends Disposable implements IRequestService {
_serviceBrand: any;
private proxyUrl?: string;
private strictSSL: boolean;
private authorization?: string;
private disposables: IDisposable[] = [];
constructor(
@IConfigurationService configurationService: IConfigurationService,
@ILogService private readonly logService: ILogService
) {
super();
this.configure(configurationService.getValue<IHTTPConfiguration>());
configurationService.onDidChangeConfiguration(() => this.configure(configurationService.getValue()), this, this.disposables);
this._register(configurationService.onDidChangeConfiguration(() => this.configure(configurationService.getValue()), this));
}
private configure(config: IHTTPConfiguration) {
@@ -39,21 +56,91 @@ export class RequestService implements IRequestService {
this.authorization = config.http && config.http.proxyAuthorization;
}
request(options: IRequestOptions, token: CancellationToken, requestFn: IRequestFunction = request): Promise<IRequestContext> {
async request(options: NodeRequestOptions, token: CancellationToken): Promise<IRequestContext> {
this.logService.trace('RequestService#request', options.url);
const { proxyUrl, strictSSL } = this;
const agentPromise = options.agent ? Promise.resolve(options.agent) : Promise.resolve(getProxyAgent(options.url || '', { proxyUrl, strictSSL }));
const agent = options.agent ? options.agent : await getProxyAgent(options.url || '', { proxyUrl, strictSSL });
return agentPromise.then(agent => {
options.agent = agent;
options.strictSSL = strictSSL;
options.agent = agent;
options.strictSSL = strictSSL;
if (this.authorization) {
options.headers = assign(options.headers || {}, { 'Proxy-Authorization': this.authorization });
if (this.authorization) {
options.headers = assign(options.headers || {}, { 'Proxy-Authorization': this.authorization });
}
return this._request(options, token);
}
private async getNodeRequest(options: IRequestOptions): Promise<IRawRequestFunction> {
const endpoint = parseUrl(options.url!);
const module = endpoint.protocol === 'https:' ? await import('https') : await import('http');
return module.request;
}
private _request(options: NodeRequestOptions, token: CancellationToken): Promise<IRequestContext> {
return new Promise<IRequestContext>(async (c, e) => {
let req: http.ClientRequest;
const endpoint = parseUrl(options.url!);
const rawRequest = options.getRawRequest
? options.getRawRequest(options)
: await this.getNodeRequest(options);
const opts: https.RequestOptions = {
hostname: endpoint.hostname,
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
protocol: endpoint.protocol,
path: endpoint.path,
method: options.type || 'GET',
headers: options.headers,
agent: options.agent,
rejectUnauthorized: isBoolean(options.strictSSL) ? options.strictSSL : true
};
if (options.user && options.password) {
opts.auth = options.user + ':' + options.password;
}
return requestFn(options, token);
req = rawRequest(opts, (res: http.IncomingMessage) => {
const followRedirects: number = isNumber(options.followRedirects) ? options.followRedirects : 3;
if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && followRedirects > 0 && res.headers['location']) {
this._request(assign({}, options, {
url: res.headers['location'],
followRedirects: followRedirects - 1
}), token).then(c, e);
} else {
let stream: Stream = res;
if (res.headers['content-encoding'] === 'gzip') {
stream = stream.pipe(createGunzip());
}
c({ res, stream: toVSBufferReadableStream(stream) } as IRequestContext);
}
});
req.on('error', e);
if (options.timeout) {
req.setTimeout(options.timeout);
}
if (options.data) {
if (typeof options.data === 'string') {
req.write(options.data);
}
}
req.end();
token.onCancellationRequested(() => {
req.abort();
e(canceled());
});
});
}
}
}