summaryrefslogtreecommitdiff
path: root/std/http/file_server.ts
diff options
context:
space:
mode:
authorKhải <hvksmr1996@gmail.com>2020-04-01 23:51:01 +0700
committerGitHub <noreply@github.com>2020-04-01 12:51:01 -0400
commitfa7929ad2c05c5d04106800adbe944c54bd443d7 (patch)
tree5cd0b829f64e55d45787075fb19b7610394e2739 /std/http/file_server.ts
parent5ac2c4aa2e3018b9b997af432e6fb4803383b4d8 (diff)
fix(file_server): use media_types for Content-Type header (#4555)
Diffstat (limited to 'std/http/file_server.ts')
-rwxr-xr-xstd/http/file_server.ts82
1 files changed, 45 insertions, 37 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts
index 5582afd07..4ddec7000 100755
--- a/std/http/file_server.ts
+++ b/std/http/file_server.ts
@@ -7,7 +7,8 @@
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js
const { args, stat, readdir, open, exit } = Deno;
-import { posix } from "../path/mod.ts";
+import { contentType } from "../media_types/mod.ts";
+import { posix, extname } from "../path/mod.ts";
import { listenAndServe, ServerRequest, Response } from "./server.ts";
import { parse } from "../flags/mod.ts";
import { assert } from "../testing/asserts.ts";
@@ -96,21 +97,22 @@ function fileLenToString(len: number): string {
return `${(len / base).toFixed(2)}${suffix[suffixIndex]}`;
}
-async function serveFile(
+export async function serveFile(
req: ServerRequest,
filePath: string
): Promise<Response> {
const [file, fileInfo] = await Promise.all([open(filePath), stat(filePath)]);
const headers = new Headers();
headers.set("content-length", fileInfo.size.toString());
- headers.set("content-type", "text/plain; charset=utf-8");
-
- const res = {
+ const contentTypeValue = contentType(extname(filePath));
+ if (contentTypeValue) {
+ headers.set("content-type", contentTypeValue);
+ }
+ return {
status: 200,
body: file,
headers,
};
- return res;
}
// TODO: simplify this after deno.stat and deno.readdir are fixed
@@ -296,39 +298,45 @@ function html(strings: TemplateStringsArray, ...values: unknown[]): string {
return html;
}
-listenAndServe(
- addr,
- async (req): Promise<void> => {
- let normalizedUrl = posix.normalize(req.url);
- try {
- normalizedUrl = decodeURIComponent(normalizedUrl);
- } catch (e) {
- if (!(e instanceof URIError)) {
- throw e;
+function main(): void {
+ listenAndServe(
+ addr,
+ async (req): Promise<void> => {
+ let normalizedUrl = posix.normalize(req.url);
+ try {
+ normalizedUrl = decodeURIComponent(normalizedUrl);
+ } catch (e) {
+ if (!(e instanceof URIError)) {
+ throw e;
+ }
}
- }
- const fsPath = posix.join(target, normalizedUrl);
+ const fsPath = posix.join(target, normalizedUrl);
- let response: Response | undefined;
- try {
- const info = await stat(fsPath);
- if (info.isDirectory()) {
- response = await serveDir(req, fsPath);
- } else {
- response = await serveFile(req, fsPath);
+ let response: Response | undefined;
+ try {
+ const info = await stat(fsPath);
+ if (info.isDirectory()) {
+ response = await serveDir(req, fsPath);
+ } else {
+ response = await serveFile(req, fsPath);
+ }
+ } catch (e) {
+ console.error(e.message);
+ response = await serveFallback(req, e);
+ } finally {
+ if (CORSEnabled) {
+ assert(response);
+ setCORS(response);
+ }
+ serverLog(req, response!);
+ req.respond(response!);
}
- } catch (e) {
- console.error(e.message);
- response = await serveFallback(req, e);
- } finally {
- if (CORSEnabled) {
- assert(response);
- setCORS(response);
- }
- serverLog(req, response!);
- req.respond(response!);
}
- }
-);
+ );
-console.log(`HTTP server listening on http://${addr}/`);
+ console.log(`HTTP server listening on http://${addr}/`);
+}
+
+if (import.meta.main) {
+ main();
+}