0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

feat(std/http): add --no-dir-listing flag to file_server (#6808)

This commit is contained in:
木杉 2020-08-12 23:38:25 +08:00 committed by GitHub
parent c4edd09816
commit 988790834e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 20 deletions

View file

@ -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);
}

View file

@ -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();
}
});