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

import { TPromise } from 'vs/base/common/winjs.base';
import { IDisposable } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
9
import { assign } from 'vs/base/common/objects';
10
import { IRequestOptions, IRequestContext, IRequestFunction, request } from 'vs/base/node/request';
J
Joao Moreno 已提交
11
import { getProxyAgent } from 'vs/base/node/proxy';
J
Joao Moreno 已提交
12
import { IRequestService, IHTTPConfiguration } from 'vs/platform/request/node/request';
13
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
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 23 24

	_serviceBrand: any;

	private proxyUrl: string;
	private strictSSL: boolean;
J
Joao Moreno 已提交
25
	private authorization: string;
J
Joao Moreno 已提交
26 27 28 29 30 31
	private disposables: IDisposable[] = [];

	constructor(
		@IConfigurationService configurationService: IConfigurationService
	) {
		this.configure(configurationService.getConfiguration<IHTTPConfiguration>());
32
		configurationService.onDidChangeConfiguration(() => this.configure(configurationService.getConfiguration()), this, this.disposables);
J
Joao Moreno 已提交
33 34
	}

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

J
Johannes Rieken 已提交
41
	async request(options: IRequestOptions, requestFn: IRequestFunction = request): TPromise<IRequestContext> {
J
Joao Moreno 已提交
42 43
		const { proxyUrl, strictSSL } = this;

J
Johannes Rieken 已提交
44
		options.agent = options.agent || await getProxyAgent(options.url, { proxyUrl, strictSSL });
J
Joao Moreno 已提交
45
		options.strictSSL = strictSSL;
J
Joao Moreno 已提交
46

J
Joao Moreno 已提交
47 48 49 50
		if (this.authorization) {
			options.headers = assign(options.headers || {}, { 'Proxy-Authorization': this.authorization });
		}

51
		return requestFn(options);
J
Joao Moreno 已提交
52 53
	}
}