requestService.ts 2.4 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { IDisposable } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
7
import { assign } from 'vs/base/common/objects';
8
import { IRequestOptions, IRequestContext, IRequestFunction, request } from 'vs/base/node/request';
J
Joao Moreno 已提交
9
import { getProxyAgent } from 'vs/base/node/proxy';
J
Joao Moreno 已提交
10
import { IRequestService, IHTTPConfiguration } from 'vs/platform/request/node/request';
11
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
12
import { ILogService } from 'vs/platform/log/common/log';
13
import { CancellationToken } from 'vs/base/common/cancellation';
J
Joao Moreno 已提交
14 15 16 17 18

/**
 * This service exposes the `request` API, while using the global
 * or configured proxy settings.
 */
J
Joao Moreno 已提交
19
export class RequestService implements IRequestService {
J
Joao Moreno 已提交
20 21 22

	_serviceBrand: any;

M
Matt Bierner 已提交
23
	private proxyUrl?: string;
J
Joao Moreno 已提交
24
	private strictSSL: boolean;
M
Matt Bierner 已提交
25
	private authorization?: string;
J
Joao Moreno 已提交
26 27 28
	private disposables: IDisposable[] = [];

	constructor(
J
Joao Moreno 已提交
29 30
		@IConfigurationService configurationService: IConfigurationService,
		@ILogService private logService: ILogService
J
Joao Moreno 已提交
31
	) {
32 33
		this.configure(configurationService.getValue<IHTTPConfiguration>());
		configurationService.onDidChangeConfiguration(() => this.configure(configurationService.getValue()), this, this.disposables);
J
Joao Moreno 已提交
34 35
	}

36
	private configure(config: IHTTPConfiguration) {
J
Joao Moreno 已提交
37
		this.proxyUrl = config.http && config.http.proxy;
M
Matt Bierner 已提交
38
		this.strictSSL = !!(config.http && config.http.proxyStrictSSL);
J
Joao Moreno 已提交
39
		this.authorization = config.http && config.http.proxyAuthorization;
J
Joao Moreno 已提交
40 41
	}

S
Sandeep Somavarapu 已提交
42
	request(options: IRequestOptions, token: CancellationToken, requestFn: IRequestFunction = request): Promise<IRequestContext> {
J
Joao Moreno 已提交
43 44
		this.logService.trace('RequestService#request', options.url);

J
Joao Moreno 已提交
45
		const { proxyUrl, strictSSL } = this;
M
Matt Bierner 已提交
46
		const agentPromise = options.agent ? Promise.resolve(options.agent) : Promise.resolve(getProxyAgent(options.url || '', { proxyUrl, strictSSL }));
J
Joao Moreno 已提交
47

J
Joao Moreno 已提交
48 49 50
		return agentPromise.then(agent => {
			options.agent = agent;
			options.strictSSL = strictSSL;
J
Joao Moreno 已提交
51

J
Joao Moreno 已提交
52 53 54
			if (this.authorization) {
				options.headers = assign(options.headers || {}, { 'Proxy-Authorization': this.authorization });
			}
J
Joao Moreno 已提交
55

56
			return requestFn(options, token);
J
Joao Moreno 已提交
57
		});
J
Joao Moreno 已提交
58 59
	}
}