Skip to content

Commit

Permalink
Handle index.html redirection in HTTP layer
Browse files Browse the repository at this point in the history
  • Loading branch information
byo committed Sep 5, 2023
1 parent 90fc2f5 commit 72b22ea
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/cinode_web_proxy/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func setupCinodeProxy(
),
RootEntrypoint: entrypoint,
MaxLinkRedirects: 10,
IndexFile: "index.html",
}

return &structure.HTTPHandler{
FS: &fs,
FS: &fs,
IndexFile: "index.html",
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/static_datastore/static_datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/datastore/webinterface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 0 additions & 5 deletions pkg/structure/cinodefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type CinodeFS struct {
BE blenc.BE
RootEntrypoint *protobuf.Entrypoint
MaxLinkRedirects int
IndexFile string
CurrentTimeF func() time.Time
}

Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pkg/structure/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

type HTTPHandler struct {
FS *CinodeFS
FS *CinodeFS
IndexFile string
}

func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -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 {
Expand Down

0 comments on commit 72b22ea

Please sign in to comment.