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

add an option for directory listing, default disabled #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var defaults = FileServerOptions{

// FileServerOptions specifies options for FileServer.
type FileServerOptions struct {
// DirListing controls whether a directory listing is shown for directories.
DirListing bool

// IndexHTML controls special handling of "index.html" file.
IndexHTML bool

Expand Down Expand Up @@ -143,6 +146,10 @@ func (fs *fileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if checkLastModified(w, req, fi.ModTime()) {
return
}
if !fs.opt.DirListing {
http.Error(w, "403 Forbidden", http.StatusForbidden)
return
}
err := dirList(w, f, path == "/")
if err != nil {
fs.opt.ServeError(w, req, err)
Expand Down
26 changes: 25 additions & 1 deletion fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ func TestServeContent_detectContentType(t *testing.T) {
}
}

// Test that no dir listing is shown if the DirListing option is false.
func TestFileServer_noDirListing(t *testing.T) {
fs := httpfs.New(mapfs.New(map[string]string{
"foo.txt": "Hello world",
}))
ts := httptest.NewServer(http.StripPrefix("/bar/", httpgzip.FileServer(fs, httpgzip.FileServerOptions{})))
defer ts.Close()
res, err := http.Get(ts.URL + "/bar/")
if err != nil {
t.Fatalf("Get: %v", err)
}
defer res.Body.Close()
if want := http.StatusForbidden; res.StatusCode != want {
t.Fatalf("got status %d, want %d", res.StatusCode, want)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("ReadAll: %v", err)
}
if want := "403 Forbidden\n"; string(b) != want {
t.Fatalf("got body %q, want %q", b, want)
}
}

// Test that there are no infinite redirects at root path even if the entire
// req.URL.Path is stripped, e.g., via an overly aggressive http.StripPrefix.
// See https://github.com/shurcooL/httpgzip/pull/3
Expand All @@ -59,7 +83,7 @@ func TestFileServer_implicitLeadingSlash(t *testing.T) {
fs := httpfs.New(mapfs.New(map[string]string{
"foo.txt": "Hello world",
}))
ts := httptest.NewServer(http.StripPrefix("/bar/", httpgzip.FileServer(fs, httpgzip.FileServerOptions{})))
ts := httptest.NewServer(http.StripPrefix("/bar/", httpgzip.FileServer(fs, httpgzip.FileServerOptions{DirListing: true})))
defer ts.Close()
get := func(suffix string) string {
res, err := http.Get(ts.URL + suffix)
Expand Down