Revert "Disable directory listings in http.FileServer."
continuous-integration/drone/push Build is passing Details

The middleware approach breaks automatic handling of index files, which
in turn breaks assets serving. A custom file system will be required
instead.

This reverts commit 2377477188.
This commit is contained in:
Rob Watson 2022-01-10 08:39:03 +01:00
parent 2377477188
commit 8a26b75127
3 changed files with 0 additions and 87 deletions

View File

@ -1,18 +0,0 @@
package server
import (
"net/http"
"strings"
)
// DisableDirectoryListings intercepts and responds with 404 to HTTP requests
// which would otherwise be served with a directory listing by http.FileServer.
func DisableDirectoryListings(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(w, r)
return
}
next.ServeHTTP(w, r)
})
}

View File

@ -1,66 +0,0 @@
package server_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"git.netflux.io/rob/clipper/server"
"github.com/stretchr/testify/assert"
)
func TestDisableDirectoryListings(t *testing.T) {
testCases := []struct {
path string
wantStatus int
}{
{
path: "/",
wantStatus: http.StatusNotFound,
},
{
path: "/index.html",
wantStatus: http.StatusOK,
},
{
path: "/foo",
wantStatus: http.StatusOK,
},
{
path: "/foo/",
wantStatus: http.StatusNotFound,
},
{
path: "/foo/bar",
wantStatus: http.StatusOK,
},
{
path: "/foo/bar/",
wantStatus: http.StatusNotFound,
},
{
path: "/foo/bar/baz",
wantStatus: http.StatusOK,
},
{
path: "/foo/bar/baz/index.html",
wantStatus: http.StatusOK,
},
}
for _, tc := range testCases {
t.Run("path="+tc.path, func(t *testing.T) {
req := httptest.NewRequest("GET", tc.path, nil)
rec := httptest.NewRecorder()
handler := server.DisableDirectoryListings(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, "Hello world")
}))
handler.ServeHTTP(rec, req)
resp := rec.Result()
assert.Equal(t, tc.wantStatus, resp.StatusCode)
})
}
}

View File

@ -307,9 +307,6 @@ func Start(options Options) error {
fileHandler = http.FileServer(http.Dir(options.Config.FileStoreHTTPRoot)) fileHandler = http.FileServer(http.Dir(options.Config.FileStoreHTTPRoot))
} }
// Ensure http.FileServer does not serve directory listings.
fileHandler = DisableDirectoryListings(fileHandler)
httpServer := http.Server{ httpServer := http.Server{
Addr: options.Config.BindAddr, Addr: options.Config.BindAddr,
ReadTimeout: options.Timeout, ReadTimeout: options.Timeout,