From 72b22eaded0d3215527d3817a4a48b12389709bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20=C5=9Awi=C4=99cki?= Date: Tue, 5 Sep 2023 09:58:22 +0200 Subject: [PATCH] Handle index.html redirection in HTTP layer --- pkg/cmd/cinode_web_proxy/root.go | 4 ++-- pkg/cmd/static_datastore/static_datastore_test.go | 4 ++-- pkg/datastore/webinterface_test.go | 2 +- pkg/structure/cinodefs.go | 5 ----- pkg/structure/http.go | 9 +++++++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/cinode_web_proxy/root.go b/pkg/cmd/cinode_web_proxy/root.go index fbe902e..19e1d84 100644 --- a/pkg/cmd/cinode_web_proxy/root.go +++ b/pkg/cmd/cinode_web_proxy/root.go @@ -101,11 +101,11 @@ func setupCinodeProxy( ), RootEntrypoint: entrypoint, MaxLinkRedirects: 10, - IndexFile: "index.html", } return &structure.HTTPHandler{ - FS: &fs, + FS: &fs, + IndexFile: "index.html", } } diff --git a/pkg/cmd/static_datastore/static_datastore_test.go b/pkg/cmd/static_datastore/static_datastore_test.go index 986d6db..2e8521c 100644 --- a/pkg/cmd/static_datastore/static_datastore_test.go +++ b/pkg/cmd/static_datastore/static_datastore_test.go @@ -137,11 +137,11 @@ func (s *CompileAndReadTestSuite) validateDataset( BE: blenc.FromDatastore(ds), RootEntrypoint: ep, MaxLinkRedirects: 10, - IndexFile: "index.html", } testServer := httptest.NewServer(&structure.HTTPHandler{ - FS: &fs, + FS: &fs, + IndexFile: "index.html", }) defer testServer.Close() diff --git a/pkg/datastore/webinterface_test.go b/pkg/datastore/webinterface_test.go index 49afb3c..e733108 100644 --- a/pkg/datastore/webinterface_test.go +++ b/pkg/datastore/webinterface_test.go @@ -32,7 +32,7 @@ import ( ) func testServer(t *testing.T) string { - log := slog.New(slog.NewTextHandler(io.Discard)) + log := slog.New(slog.NewTextHandler(io.Discard, nil)) // Test web interface and web connector server := httptest.NewServer(WebInterface( diff --git a/pkg/structure/cinodefs.go b/pkg/structure/cinodefs.go index 972d737..614aabb 100644 --- a/pkg/structure/cinodefs.go +++ b/pkg/structure/cinodefs.go @@ -33,7 +33,6 @@ type CinodeFS struct { BE blenc.BE RootEntrypoint *protobuf.Entrypoint MaxLinkRedirects int - IndexFile string CurrentTimeF func() time.Time } @@ -67,10 +66,6 @@ func (d *CinodeFS) findEntrypointInDir( return nil, ErrNotADirectory } - if remainingPath == "" { - remainingPath = d.IndexFile - } - rc, err := d.OpenContent(ctx, ep) if err != nil { return nil, err diff --git a/pkg/structure/http.go b/pkg/structure/http.go index d572016..8c41f3d 100644 --- a/pkg/structure/http.go +++ b/pkg/structure/http.go @@ -25,7 +25,8 @@ import ( ) type HTTPHandler struct { - FS *CinodeFS + FS *CinodeFS + IndexFile string } func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -34,7 +35,11 @@ func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - path := strings.TrimPrefix(r.URL.Path, "/") + path := r.URL.Path + if strings.HasSuffix(path, "/") { + path += h.IndexFile + } + path = strings.TrimPrefix(path, "/") fileEP, err := h.FS.FindEntrypoint(r.Context(), path) switch {