RequestService2
This commit is contained in:
parent
ca41e24feb
commit
b9d112985f
5 changed files with 99 additions and 30 deletions
|
@ -7,6 +7,7 @@
|
|||
|
||||
import { Url, parse as parseUrl } from 'url';
|
||||
import { isBoolean } from 'vs/base/common/types';
|
||||
import { Agent } from './request';
|
||||
import HttpProxyAgent = require('http-proxy-agent');
|
||||
import HttpsProxyAgent = require('https-proxy-agent');
|
||||
|
||||
|
@ -25,7 +26,7 @@ export interface IOptions {
|
|||
strictSSL?: boolean;
|
||||
}
|
||||
|
||||
export function getProxyAgent(rawRequestURL: string, options: IOptions = {}): any {
|
||||
export function getProxyAgent(rawRequestURL: string, options: IOptions = {}): Agent {
|
||||
const requestURL = parseUrl(rawRequestURL);
|
||||
const proxyURL = options.proxyUrl || getSystemProxyURI(requestURL);
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ import { createWriteStream } from 'fs';
|
|||
import { assign } from 'vs/base/common/objects';
|
||||
import { createGunzip } from 'zlib';
|
||||
|
||||
export type Agent = any;
|
||||
|
||||
export interface IRequestOptions {
|
||||
type?: string;
|
||||
url?: string;
|
||||
|
@ -23,21 +25,21 @@ export interface IRequestOptions {
|
|||
headers?: any;
|
||||
timeout?: number;
|
||||
data?: any;
|
||||
agent?: any;
|
||||
agent?: Agent;
|
||||
followRedirects?: number;
|
||||
strictSSL?: boolean;
|
||||
}
|
||||
|
||||
export interface IRequestResult {
|
||||
export interface IRequestContext {
|
||||
req: http.ClientRequest;
|
||||
res: http.ClientResponse;
|
||||
stream: Stream;
|
||||
}
|
||||
|
||||
export function request(options: IRequestOptions): TPromise<IRequestResult> {
|
||||
export function request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
let req: http.ClientRequest;
|
||||
|
||||
return new TPromise<IRequestResult>((c, e) => {
|
||||
return new TPromise<IRequestContext>((c, e) => {
|
||||
let endpoint = parseUrl(options.url);
|
||||
|
||||
let opts: https.RequestOptions = {
|
||||
|
|
|
@ -4,9 +4,13 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { localize } from 'vs/nls';
|
||||
import http = require('vs/base/common/http');
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
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/platform';
|
||||
|
||||
export const IRequestService = createDecorator<IRequestService>('requestService');
|
||||
|
||||
|
@ -19,3 +23,38 @@ export interface IRequestService {
|
|||
*/
|
||||
makeRequest(options: http.IXHROptions): TPromise<http.IXHRResponse>;
|
||||
}
|
||||
|
||||
export const IRequestService2 = createDecorator<IRequestService>('requestService2');
|
||||
|
||||
export interface IRequestService2 {
|
||||
_serviceBrand: any;
|
||||
|
||||
request(options: IRequestOptions): TPromise<IRequestContext>;
|
||||
}
|
||||
|
||||
export interface IHTTPConfiguration {
|
||||
http?: {
|
||||
proxy?: string;
|
||||
proxyStrictSSL?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
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', "Whether the proxy server certificate should be verified against the list of supplied CAs.")
|
||||
}
|
||||
}
|
||||
});
|
50
src/vs/platform/request/node/requestService2.ts
Normal file
50
src/vs/platform/request/node/requestService2.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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';
|
||||
import { IRequestOptions, IRequestContext, request } from 'vs/base/node/request';
|
||||
import { getProxyAgent } from 'vs/base/node/proxy';
|
||||
import { IRequestService2, IHTTPConfiguration } from 'vs/platform/request/common/request';
|
||||
import { IConfigurationService, IConfigurationServiceEvent } from 'vs/platform/configuration/common/configuration';
|
||||
|
||||
/**
|
||||
* This service exposes the `request` API, while using the global
|
||||
* or configured proxy settings.
|
||||
*/
|
||||
export class RequestService2 implements IRequestService2 {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private proxyUrl: string;
|
||||
private strictSSL: boolean;
|
||||
private disposables: IDisposable[] = [];
|
||||
|
||||
constructor(
|
||||
@IConfigurationService configurationService: IConfigurationService
|
||||
) {
|
||||
this.configure(configurationService.getConfiguration<IHTTPConfiguration>());
|
||||
configurationService.onDidUpdateConfiguration(this.onDidUpdateConfiguration, this, this.disposables);
|
||||
}
|
||||
|
||||
private onDidUpdateConfiguration(e: IConfigurationServiceEvent) {
|
||||
this.configure(e.config);
|
||||
}
|
||||
|
||||
private configure(config: IHTTPConfiguration) {
|
||||
this.proxyUrl = config.http && config.http.proxy;
|
||||
this.strictSSL = config.http && config.http.proxyStrictSSL;
|
||||
}
|
||||
|
||||
request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
if (!options.agent) {
|
||||
const { proxyUrl, strictSSL } = this;
|
||||
options.agent = getProxyAgent(options.url, { proxyUrl, strictSSL });
|
||||
}
|
||||
|
||||
return request(options);
|
||||
}
|
||||
}
|
|
@ -7,11 +7,9 @@
|
|||
|
||||
import { TPromise, Promise } from 'vs/base/common/winjs.base';
|
||||
import { xhr } from 'vs/base/common/network';
|
||||
import { IConfigurationRegistry, Extensions } from 'vs/platform/configuration/common/configurationRegistry';
|
||||
import strings = require('vs/base/common/strings');
|
||||
import nls = require('vs/nls');
|
||||
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
|
||||
import platform = require('vs/platform/platform');
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
import { BaseRequestService } from 'vs/platform/request/common/baseRequestService';
|
||||
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
|
||||
|
@ -133,24 +131,3 @@ export class RequestService extends BaseRequestService {
|
|||
this.disposables = dispose(this.disposables);
|
||||
}
|
||||
}
|
||||
|
||||
// Configuration
|
||||
let confRegistry = <IConfigurationRegistry>platform.Registry.as(Extensions.Configuration);
|
||||
confRegistry.registerConfiguration({
|
||||
id: 'http',
|
||||
order: 15,
|
||||
title: nls.localize('httpConfigurationTitle', "HTTP"),
|
||||
type: 'object',
|
||||
properties: {
|
||||
'http.proxy': {
|
||||
type: 'string',
|
||||
pattern: '^https?://([^:]*(:[^@]*)?@)?([^:]+)(:\\d+)?/?$|^$',
|
||||
description: nls.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: nls.localize('strictSSL', "Whether the proxy server certificate should be verified against the list of supplied CAs.")
|
||||
}
|
||||
}
|
||||
});
|
Loading…
Reference in a new issue