Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http): file server with showDirListing + urlRoot #3691

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,12 @@ async function serveDirIndex(
options: {
showDotfiles: boolean;
target: string;
urlRoot: string | undefined;
quiet: boolean | undefined;
},
): Promise<Response> {
const { showDotfiles } = options;
const urlRoot = options.urlRoot ? "/" + options.urlRoot : "";
const dirUrl = `/${
relative(options.target, dirPath).replaceAll(
new RegExp(SEP_PATTERN, "g"),
Expand All @@ -310,7 +312,7 @@ async function serveDirIndex(
mode: modeToString(true, fileInfo.mode),
size: "",
name: "../",
url: posixJoin(dirUrl, ".."),
url: `${urlRoot}${posixJoin(dirUrl, "..")}`,
}));
listEntryPromise.push(entryInfo);
}
Expand All @@ -331,7 +333,7 @@ async function serveDirIndex(
mode: modeToString(entry.isDirectory, fileInfo.mode),
size: entry.isFile ? formatBytes(fileInfo.size ?? 0) : "",
name: `${entry.name}${entry.isDirectory ? "/" : ""}`,
url: `${fileUrl}${entry.isDirectory ? "/" : ""}`,
url: `${urlRoot}${fileUrl}${entry.isDirectory ? "/" : ""}`,
};
} catch (error) {
// Note: Deno.stat for windows system files may be rejected with os error 32.
Expand All @@ -340,7 +342,7 @@ async function serveDirIndex(
mode: "(unknown mode)",
size: "",
name: `${entry.name}${entry.isDirectory ? "/" : ""}`,
url: `${fileUrl}${entry.isDirectory ? "/" : ""}`,
url: `${urlRoot}${fileUrl}${entry.isDirectory ? "/" : ""}`,
};
}
})());
Expand Down Expand Up @@ -709,7 +711,7 @@ async function createServeDirResponse(
}

if (showDirListing) { // serve directory list
return serveDirIndex(fsPath, { showDotfiles, target, quiet });
return serveDirIndex(fsPath, { urlRoot, showDotfiles, target, quiet });
}

return createCommonResponse(Status.NotFound);
Expand Down