Add content types
This commit is contained in:
parent
68fe085aa3
commit
98f001395c
20
server.ts
20
server.ts
@ -6,6 +6,8 @@ import * as util from "util";
|
|||||||
import * as url from "url";
|
import * as url from "url";
|
||||||
|
|
||||||
import { Emitter } from "vs/base/common/event";
|
import { Emitter } from "vs/base/common/event";
|
||||||
|
import { getMediaMime } from "vs/base/common/mime";
|
||||||
|
import { extname } from "vs/base/common/path";
|
||||||
import { IPCServer, ClientConnectionEvent } from "vs/base/parts/ipc/common/ipc";
|
import { IPCServer, ClientConnectionEvent } from "vs/base/parts/ipc/common/ipc";
|
||||||
import { validatePaths } from "vs/code/node/paths";
|
import { validatePaths } from "vs/code/node/paths";
|
||||||
import { parseMainProcessArgv } from "vs/platform/environment/node/argvHelper";
|
import { parseMainProcessArgv } from "vs/platform/environment/node/argvHelper";
|
||||||
@ -59,10 +61,11 @@ export class Server implements IServer {
|
|||||||
public constructor() {
|
public constructor() {
|
||||||
this.server = http.createServer(async (request, response): Promise<void> => {
|
this.server = http.createServer(async (request, response): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const content = await this.handleRequest(request);
|
const [content, headers] = await this.handleRequest(request);
|
||||||
response.writeHead(HttpCode.Ok, {
|
response.writeHead(HttpCode.Ok, {
|
||||||
"Cache-Control": "max-age=86400",
|
"Cache-Control": "max-age=86400",
|
||||||
// TODO: ETag?
|
// TODO: ETag?
|
||||||
|
...headers,
|
||||||
});
|
});
|
||||||
response.end(content);
|
response.end(content);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -113,7 +116,7 @@ export class Server implements IServer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleRequest(request: http.IncomingMessage): Promise<string | Buffer> {
|
private async handleRequest(request: http.IncomingMessage): Promise<[string | Buffer, http.OutgoingHttpHeaders]> {
|
||||||
if (request.method !== "GET") {
|
if (request.method !== "GET") {
|
||||||
throw new HttpError(
|
throw new HttpError(
|
||||||
`Unsupported method ${request.method}`,
|
`Unsupported method ${request.method}`,
|
||||||
@ -150,14 +153,23 @@ export class Server implements IServer {
|
|||||||
|
|
||||||
html = html.replace('{{WEBVIEW_ENDPOINT}}', JSON.stringify(options.WEBVIEW_ENDPOINT));
|
html = html.replace('{{WEBVIEW_ENDPOINT}}', JSON.stringify(options.WEBVIEW_ENDPOINT));
|
||||||
|
|
||||||
return html;
|
return [html, {
|
||||||
|
"Content-Type": "text/html",
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const content = await util.promisify(fs.readFile)(
|
const content = await util.promisify(fs.readFile)(
|
||||||
path.join(this.rootPath, requestPath),
|
path.join(this.rootPath, requestPath),
|
||||||
);
|
);
|
||||||
return content;
|
return [content, {
|
||||||
|
"Content-Type": getMediaMime(requestPath) || {
|
||||||
|
".css": "text/css",
|
||||||
|
".html": "text/html",
|
||||||
|
".js": "text/javascript",
|
||||||
|
".json": "application/json",
|
||||||
|
}[extname(requestPath)] || "text/plain",
|
||||||
|
}];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === "ENOENT" || error.code === "EISDIR") {
|
if (error.code === "ENOENT" || error.code === "EISDIR") {
|
||||||
throw new HttpError("Not found", HttpCode.NotFound);
|
throw new HttpError("Not found", HttpCode.NotFound);
|
||||||
|
Reference in New Issue
Block a user