|
| 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 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +import { TPromise } from 'vs/base/common/winjs.base'; |
| 8 | +import { IDisposable } from 'vs/base/common/lifecycle'; |
| 9 | +import { IRequestOptions, IRequestContext, request } from 'vs/base/node/request'; |
| 10 | +import { getProxyAgent } from 'vs/base/node/proxy'; |
| 11 | +import { IRequestService2, IHTTPConfiguration } from 'vs/platform/request/common/request'; |
| 12 | +import { IConfigurationService, IConfigurationServiceEvent } from 'vs/platform/configuration/common/configuration'; |
| 13 | + |
| 14 | +/** |
| 15 | + * This service exposes the `request` API, while using the global |
| 16 | + * or configured proxy settings. |
| 17 | + */ |
| 18 | +export class RequestService2 implements IRequestService2 { |
| 19 | + |
| 20 | + _serviceBrand: any; |
| 21 | + |
| 22 | + private proxyUrl: string; |
| 23 | + private strictSSL: boolean; |
| 24 | + private disposables: IDisposable[] = []; |
| 25 | + |
| 26 | + constructor( |
| 27 | + @IConfigurationService configurationService: IConfigurationService |
| 28 | + ) { |
| 29 | + this.configure(configurationService.getConfiguration<IHTTPConfiguration>()); |
| 30 | + configurationService.onDidUpdateConfiguration(this.onDidUpdateConfiguration, this, this.disposables); |
| 31 | + } |
| 32 | + |
| 33 | + private onDidUpdateConfiguration(e: IConfigurationServiceEvent) { |
| 34 | + this.configure(e.config); |
| 35 | + } |
| 36 | + |
| 37 | + private configure(config: IHTTPConfiguration) { |
| 38 | + this.proxyUrl = config.http && config.http.proxy; |
| 39 | + this.strictSSL = config.http && config.http.proxyStrictSSL; |
| 40 | + } |
| 41 | + |
| 42 | + request(options: IRequestOptions): TPromise<IRequestContext> { |
| 43 | + if (!options.agent) { |
| 44 | + const { proxyUrl, strictSSL } = this; |
| 45 | + options.agent = getProxyAgent(options.url, { proxyUrl, strictSSL }); |
| 46 | + } |
| 47 | + |
| 48 | + return request(options); |
| 49 | + } |
| 50 | +} |
0 commit comments