mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 18:46:40 -05:00
SQL Operations Studio Public Preview 1 (0.23) release source code
This commit is contained in:
99
src/vs/platform/request/electron-browser/requestService.ts
Normal file
99
src/vs/platform/request/electron-browser/requestService.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { IRequestOptions, IRequestContext, IRequestFunction } from 'vs/base/node/request';
|
||||
import { Readable } from 'stream';
|
||||
import { RequestService as NodeRequestService } from 'vs/platform/request/node/requestService';
|
||||
|
||||
/**
|
||||
* This service exposes the `request` API, while using the global
|
||||
* or configured proxy settings.
|
||||
*/
|
||||
export class RequestService extends NodeRequestService {
|
||||
request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
return super.request(options, xhrRequest);
|
||||
}
|
||||
}
|
||||
|
||||
class ArrayBufferStream extends Readable {
|
||||
|
||||
private _buffer: Buffer;
|
||||
private _offset: number;
|
||||
private _length: number;
|
||||
|
||||
constructor(arraybuffer: ArrayBuffer) {
|
||||
super();
|
||||
this._buffer = new Buffer(new Uint8Array(arraybuffer));
|
||||
this._offset = 0;
|
||||
this._length = this._buffer.length;
|
||||
}
|
||||
|
||||
_read(size: number) {
|
||||
if (this._offset < this._length) {
|
||||
this.push(this._buffer.slice(this._offset, (this._offset + size)));
|
||||
this._offset += size;
|
||||
} else {
|
||||
this.push(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const xhrRequest: IRequestFunction = (options: IRequestOptions): TPromise<IRequestContext> => {
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
return new TPromise<IRequestContext>((resolve, reject) => {
|
||||
|
||||
xhr.open(options.type || 'GET', options.url, true, options.user, options.password);
|
||||
setRequestHeaders(xhr, options);
|
||||
|
||||
xhr.responseType = 'arraybuffer';
|
||||
xhr.onerror = e => reject(new Error('XHR failed: ' + xhr.statusText));
|
||||
xhr.onload = (e) => {
|
||||
resolve({
|
||||
res: {
|
||||
statusCode: xhr.status,
|
||||
headers: getResponseHeaders(xhr)
|
||||
},
|
||||
stream: new ArrayBufferStream(xhr.response)
|
||||
});
|
||||
};
|
||||
|
||||
xhr.send(options.data);
|
||||
return null;
|
||||
|
||||
}, () => {
|
||||
// cancel
|
||||
xhr.abort();
|
||||
});
|
||||
};
|
||||
|
||||
function setRequestHeaders(xhr: XMLHttpRequest, options: IRequestOptions): void {
|
||||
if (options.headers) {
|
||||
outer: for (let k in options.headers) {
|
||||
switch (k) {
|
||||
case 'User-Agent':
|
||||
case 'Accept-Encoding':
|
||||
case 'Content-Length':
|
||||
// unsafe headers
|
||||
continue outer;
|
||||
}
|
||||
xhr.setRequestHeader(k, options.headers[k]);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getResponseHeaders(xhr: XMLHttpRequest): { [name: string]: string } {
|
||||
const headers: { [name: string]: string } = Object.create(null);
|
||||
for (const line of xhr.getAllResponseHeaders().split(/\r\n|\n|\r/g)) {
|
||||
if (line) {
|
||||
const idx = line.indexOf(':');
|
||||
headers[line.substr(0, idx).trim().toLowerCase()] = line.substr(idx + 1).trim();
|
||||
}
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
22
src/vs/platform/request/electron-main/requestService.ts
Normal file
22
src/vs/platform/request/electron-main/requestService.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { IRequestOptions, IRequestContext, request, IRawRequestFunction } from 'vs/base/node/request';
|
||||
import { RequestService as NodeRequestService } from 'vs/platform/request/node/requestService';
|
||||
import { assign } from 'vs/base/common/objects';
|
||||
import { net } from 'electron';
|
||||
|
||||
function getRawRequest(options: IRequestOptions): IRawRequestFunction {
|
||||
return net.request as any as IRawRequestFunction;
|
||||
}
|
||||
|
||||
export class RequestService extends NodeRequestService {
|
||||
|
||||
request(options: IRequestOptions): TPromise<IRequestContext> {
|
||||
return super.request(options, options => request(assign({}, options || {}, { getRawRequest })));
|
||||
}
|
||||
}
|
||||
53
src/vs/platform/request/node/request.ts
Normal file
53
src/vs/platform/request/node/request.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { localize } from 'vs/nls';
|
||||
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/registry/common/platform';
|
||||
|
||||
export const IRequestService = createDecorator<IRequestService>('requestService2');
|
||||
|
||||
export interface IRequestService {
|
||||
_serviceBrand: any;
|
||||
|
||||
request(options: IRequestOptions): TPromise<IRequestContext>;
|
||||
}
|
||||
|
||||
export interface IHTTPConfiguration {
|
||||
http?: {
|
||||
proxy?: string;
|
||||
proxyStrictSSL?: boolean;
|
||||
proxyAuthorization?: string;
|
||||
};
|
||||
}
|
||||
|
||||
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.")
|
||||
},
|
||||
'http.proxyAuthorization': {
|
||||
type: ['null', 'string'],
|
||||
default: null,
|
||||
description: localize('proxyAuthorization', "The value to send as the 'Proxy-Authorization' header for every network request.")
|
||||
}
|
||||
}
|
||||
});
|
||||
53
src/vs/platform/request/node/requestService.ts
Normal file
53
src/vs/platform/request/node/requestService.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. 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 { assign } from 'vs/base/common/objects';
|
||||
import { IRequestOptions, IRequestContext, IRequestFunction, request } from 'vs/base/node/request';
|
||||
import { getProxyAgent } from 'vs/base/node/proxy';
|
||||
import { IRequestService, IHTTPConfiguration } from 'vs/platform/request/node/request';
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
|
||||
/**
|
||||
* This service exposes the `request` API, while using the global
|
||||
* or configured proxy settings.
|
||||
*/
|
||||
export class RequestService implements IRequestService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private proxyUrl: string;
|
||||
private strictSSL: boolean;
|
||||
private authorization: string;
|
||||
private disposables: IDisposable[] = [];
|
||||
|
||||
constructor(
|
||||
@IConfigurationService configurationService: IConfigurationService
|
||||
) {
|
||||
this.configure(configurationService.getConfiguration<IHTTPConfiguration>());
|
||||
configurationService.onDidUpdateConfiguration(() => this.configure(configurationService.getConfiguration()), this, this.disposables);
|
||||
}
|
||||
|
||||
private configure(config: IHTTPConfiguration) {
|
||||
this.proxyUrl = config.http && config.http.proxy;
|
||||
this.strictSSL = config.http && config.http.proxyStrictSSL;
|
||||
this.authorization = config.http && config.http.proxyAuthorization;
|
||||
}
|
||||
|
||||
async request(options: IRequestOptions, requestFn: IRequestFunction = request): TPromise<IRequestContext> {
|
||||
const { proxyUrl, strictSSL } = this;
|
||||
|
||||
options.agent = options.agent || await getProxyAgent(options.url, { proxyUrl, strictSSL });
|
||||
options.strictSSL = strictSSL;
|
||||
|
||||
if (this.authorization) {
|
||||
options.headers = assign(options.headers || {}, { 'Proxy-Authorization': this.authorization });
|
||||
}
|
||||
|
||||
return requestFn(options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user