提交 d34c4fcc 编写于 作者: M Matt Bierner

Remove duplicate implementation of loading resources

For #100442

Also make sure we use the correct scheme for the making requests
上级 80d8aff4
......@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { VSBuffer, VSBufferReadableStream } from 'vs/base/common/buffer';
import { VSBufferReadableStream } from 'vs/base/common/buffer';
import { CancellationToken } from 'vs/base/common/cancellation';
import { isUNC } from 'vs/base/common/extpath';
import { Schemas } from 'vs/base/common/network';
......@@ -18,7 +18,6 @@ import { getWebviewContentMimeType } from 'vs/platform/webview/common/mimeTypes'
export const webviewPartitionId = 'webview';
export namespace WebviewResourceResponse {
export enum Type { Success, Failed, AccessDenied }
......@@ -31,44 +30,13 @@ export namespace WebviewResourceResponse {
) { }
}
export class BufferSuccess {
readonly type = Type.Success;
constructor(
public readonly buffer: VSBuffer,
public readonly mimeType: string
) { }
}
export const Failed = { type: Type.Failed } as const;
export const AccessDenied = { type: Type.AccessDenied } as const;
export type BufferResponse = BufferSuccess | typeof Failed | typeof AccessDenied;
export type StreamResponse = StreamSuccess | typeof Failed | typeof AccessDenied;
}
export async function loadLocalResource(
requestUri: URI,
fileService: IFileService,
extensionLocation: URI | undefined,
roots: ReadonlyArray<URI>
): Promise<WebviewResourceResponse.BufferResponse> {
const resourceToLoad = getResourceToLoad(requestUri, extensionLocation, roots);
if (!resourceToLoad) {
return WebviewResourceResponse.AccessDenied;
}
try {
const data = await fileService.readFile(resourceToLoad);
const mime = getWebviewContentMimeType(requestUri); // Use the original path for the mime
return new WebviewResourceResponse.BufferSuccess(data.value, mime);
} catch (err) {
console.log(err);
return WebviewResourceResponse.Failed;
}
}
export async function loadLocalResourceStream(
requestUri: URI,
options: {
extensionLocation: URI | undefined;
......@@ -78,7 +46,7 @@ export async function loadLocalResourceStream(
fileService: IFileService,
requestService: IRequestService,
): Promise<WebviewResourceResponse.StreamResponse> {
const resourceToLoad = getResourceToLoad(requestUri, options.extensionLocation, options.roots);
const resourceToLoad = getResourceToLoad(requestUri, options.roots);
if (!resourceToLoad) {
return WebviewResourceResponse.AccessDenied;
}
......@@ -87,7 +55,8 @@ export async function loadLocalResourceStream(
if (options.remoteConnectionData) {
// Remote uris must go to the resolved server.
if (resourceToLoad.scheme === Schemas.vscodeRemote || (options.extensionLocation?.scheme === REMOTE_HOST_SCHEME)) {
const uri = URI.parse(`http://${options.remoteConnectionData.host}:${options.remoteConnectionData.port}`).with({
const scheme = options.remoteConnectionData.host === 'localhost' || options.remoteConnectionData.host === '127.0.0.1' ? 'http' : 'https';
const uri = URI.parse(`${scheme}://${options.remoteConnectionData.host}:${options.remoteConnectionData.port}`).with({
path: '/vscode-remote-resource',
query: `tkn=${options.remoteConnectionData.connectionToken}&path=${encodeURIComponent(resourceToLoad.path)}`,
});
......@@ -111,7 +80,6 @@ export async function loadLocalResourceStream(
function getResourceToLoad(
requestUri: URI,
extensionLocation: URI | undefined,
roots: ReadonlyArray<URI>
): URI | undefined {
const normalizedPath = normalizeRequestPath(requestUri);
......
......@@ -12,7 +12,7 @@ import { URI } from 'vs/base/common/uri';
import { IFileService } from 'vs/platform/files/common/files';
import { IRemoteConnectionData } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { IRequestService } from 'vs/platform/request/common/request';
import { loadLocalResourceStream, webviewPartitionId, WebviewResourceResponse } from 'vs/platform/webview/common/resourceLoader';
import { loadLocalResource, webviewPartitionId, WebviewResourceResponse } from 'vs/platform/webview/common/resourceLoader';
interface WebviewMetadata {
readonly extensionLocation: URI | undefined;
......@@ -39,7 +39,7 @@ export class WebviewProtocolProvider extends Disposable {
const id = uri.authority;
const metadata = this.webviewMetadata.get(id);
if (metadata) {
const result = await loadLocalResourceStream(uri, {
const result = await loadLocalResource(uri, {
extensionLocation: metadata.extensionLocation,
roots: metadata.localResourceRoots,
remoteConnectionData: metadata.remoteConnectionData,
......
......@@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { addDisposableListener } from 'vs/base/browser/dom';
import { streamToBuffer } from 'vs/base/common/buffer';
import { IDisposable } from 'vs/base/common/lifecycle';
import { isWeb } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
......@@ -13,6 +14,7 @@ import { IFileService } from 'vs/platform/files/common/files';
import { ILogService } from 'vs/platform/log/common/log';
import { IRemoteAuthorityResolverService } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { ITunnelService } from 'vs/platform/remote/common/tunnel';
import { IRequestService } from 'vs/platform/request/common/request';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { loadLocalResource, WebviewResourceResponse } from 'vs/platform/webview/common/resourceLoader';
import { WebviewPortMappingManager } from 'vs/platform/webview/common/webviewPortMapping';
......@@ -32,6 +34,7 @@ export class IFrameWebview extends BaseWebview<HTMLIFrameElement> implements Web
webviewThemeDataProvider: WebviewThemeDataProvider,
@ITunnelService tunnelService: ITunnelService,
@IFileService private readonly fileService: IFileService,
@IRequestService private readonly requestService: IRequestService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@ITelemetryService telemetryService: ITelemetryService,
@IEnvironmentService environmentService: IEnvironmentService,
......@@ -141,15 +144,22 @@ export class IFrameWebview extends BaseWebview<HTMLIFrameElement> implements Web
private async loadResource(requestPath: string, uri: URI) {
try {
const result = await loadLocalResource(uri, this.fileService, this.extension ? this.extension.location : undefined,
this.content.options.localResourceRoots || []);
const remoteAuthority = this._workbenchEnvironmentService.configuration.remoteAuthority;
const remoteConnectionData = remoteAuthority ? this._remoteAuthorityResolverService.getConnectionData(remoteAuthority) : null;
const result = await loadLocalResource(uri, {
extensionLocation: this.extension?.location,
roots: this.content.options.localResourceRoots || [],
remoteConnectionData
}, this.fileService, this.requestService);
if (result.type === WebviewResourceResponse.Type.Success) {
const { buffer } = await streamToBuffer(result.stream);
return this._send('did-load-resource', {
status: 200,
path: requestPath,
mime: result.mimeType,
data: result.buffer.buffer,
data: buffer,
});
}
} catch {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册