requestService.ts 2.3 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';
14
import { ILogService } from 'vs/platform/log/common/log';
J
Joao Moreno 已提交
15 16 17 18 19

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

	_serviceBrand: any;

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

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

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

J
Johannes Rieken 已提交
43
	async request(options: IRequestOptions, requestFn: IRequestFunction = request): TPromise<IRequestContext> {
J
Joao Moreno 已提交
44 45
		this.logService.trace('RequestService#request', options.url);

J
Joao Moreno 已提交
46 47
		const { proxyUrl, strictSSL } = this;

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

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

55
		return requestFn(options);
J
Joao Moreno 已提交
56 57
	}
}