summaryrefslogtreecommitdiff
path: root/std/http/file_server.ts
diff options
context:
space:
mode:
author木杉 <zhmushan@qq.com>2020-08-12 23:38:25 +0800
committerGitHub <noreply@github.com>2020-08-12 11:38:25 -0400
commit988790834e9611b45663e48a7bea49219da7511f (patch)
treecc55ea6e670060472928eab978434fab0cb5d540 /std/http/file_server.ts
parentc4edd09816b3cc6ab992f85602388f2f344fcf0e (diff)
feat(std/http): add --no-dir-listing flag to file_server (#6808)
Diffstat (limited to 'std/http/file_server.ts')
-rw-r--r--std/http/file_server.ts35
1 files changed, 21 insertions, 14 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);
}