summaryrefslogtreecommitdiff
path: root/std/http
diff options
context:
space:
mode:
Diffstat (limited to 'std/http')
-rw-r--r--std/http/file_server.ts35
-rw-r--r--std/http/file_server_test.ts23
2 files changed, 38 insertions, 20 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts
index 0ece60560..ff9a073e7 100644
--- a/std/http/file_server.ts
+++ b/std/http/file_server.ts
@@ -24,24 +24,26 @@ interface EntryInfo {
name: string;
}
-interface FileServerArgs {
+export interface FileServerArgs {
_: string[];
// -p --port
- p: number;
- port: number;
+ p?: number;
+ port?: number;
// --cors
- cors: boolean;
- // -h --help
- h: boolean;
- help: boolean;
+ cors?: boolean;
+ // --no-dir-listing
+ "dir-listing"?: boolean;
// --host
- host: string;
+ host?: string;
// -c --cert
- c: string;
- cert: string;
+ c?: string;
+ cert?: string;
// -k --key
- k: string;
- key: string;
+ k?: string;
+ key?: string;
+ // -h --help
+ h?: boolean;
+ help?: boolean;
}
const encoder = new TextEncoder();
@@ -327,6 +329,7 @@ function main(): void {
const tlsOpts = {} as HTTPSOptions;
tlsOpts.certFile = serverArgs.cert ?? serverArgs.c ?? "";
tlsOpts.keyFile = serverArgs.key ?? serverArgs.k ?? "";
+ const dirListingEnabled = serverArgs["dir-listing"] ?? true;
if (tlsOpts.keyFile || tlsOpts.certFile) {
if (tlsOpts.keyFile === "" || tlsOpts.certFile === "") {
@@ -352,9 +355,9 @@ function main(): void {
--host <HOST> Hostname (default is 0.0.0.0)
-c, --cert <FILE> TLS certificate file (enables TLS)
-k, --key <FILE> TLS key file (enables TLS)
+ --no-dir-listing Disable directory listing
All TLS options are required when one is provided.`);
-
Deno.exit();
}
@@ -373,7 +376,11 @@ function main(): void {
try {
const fileInfo = await Deno.stat(fsPath);
if (fileInfo.isDirectory) {
- response = await serveDir(req, fsPath);
+ if (dirListingEnabled) {
+ response = await serveDir(req, fsPath);
+ } else {
+ throw new Deno.errors.NotFound();
+ }
} else {
response = await serveFile(req, fsPath);
}
diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts
index a25c13a7d..818fd5ecb 100644
--- a/std/http/file_server_test.ts
+++ b/std/http/file_server_test.ts
@@ -3,17 +3,15 @@ import { assert, assertEquals } from "../testing/asserts.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { ServerRequest } from "./server.ts";
-import { serveFile } from "./file_server.ts";
+import { serveFile, FileServerArgs } from "./file_server.ts";
let fileServer: Deno.Process<Deno.RunOptions & { stdout: "piped" }>;
-type FileServerCfg = {
- target?: string;
- port?: number;
-};
+type FileServerCfg = Omit<FileServerArgs, "_"> & { target?: string };
async function startFileServer({
target = ".",
port = 4507,
+ "dir-listing": dirListing = true,
}: FileServerCfg = {}): Promise<void> {
fileServer = Deno.run({
cmd: [
@@ -26,6 +24,7 @@ async function startFileServer({
"--cors",
"-p",
`${port}`,
+ `${dirListing ? "" : "--no-dir-listing"}`,
],
stdout: "piped",
stderr: "null",
@@ -100,7 +99,6 @@ Deno.test(
const localFile = new TextDecoder().decode(
await Deno.readFile("./http/README.md"),
);
- console.log(downloadedFile, localFile);
assertEquals(downloadedFile, localFile);
} finally {
await killFileServer();
@@ -288,3 +286,16 @@ Deno.test("partial TLS arguments fail", async function (): Promise<void> {
await killFileServer();
}
});
+
+Deno.test("file_server disable dir listings", async function (): Promise<void> {
+ await startFileServer({ "dir-listing": false });
+ try {
+ const res = await fetch("http://localhost:4507/");
+ assert(res.headers.has("access-control-allow-origin"));
+ assert(res.headers.has("access-control-allow-headers"));
+ assertEquals(res.status, 404);
+ const _ = await res.text();
+ } finally {
+ await killFileServer();
+ }
+});