Skip to content

Commit

Permalink
🩹 Fix: static server in sub app with mount (gofiber#3104)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinheli committed Sep 15, 2024
1 parent 6e74114 commit d415399
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
11 changes: 11 additions & 0 deletions mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type mountFields struct {
subAppsProcessed sync.Once
// Prefix of app if it was mounted
mountPath string
// Parent app of the current app
parentApp *App
}

// Create empty mountFields instance
Expand Down Expand Up @@ -50,6 +52,7 @@ func (app *App) Mount(prefix string, subApp *App) Router {

subApp.mountFields.mountPath = path
app.mountFields.appList[path] = subApp
subApp.mountFields.parentApp = app
}

// register mounted group
Expand Down Expand Up @@ -99,6 +102,14 @@ func (app *App) MountPath() string {
return app.mountFields.mountPath
}

// FullMountPath returns the full mount path of the app, including the parent app's mount path.
func (app *App) FullMountPath() string {
if app.mountFields.parentApp == nil {
return app.mountFields.mountPath
}
return getGroupPath(app.mountFields.parentApp.FullMountPath(), app.mountFields.mountPath)
}

// hasMountedApps Checks if there are any mounted apps in the current application.
func (app *App) hasMountedApps() bool {
return len(app.mountFields.appList) > 1
Expand Down
5 changes: 5 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package fiber

import (
"bytes"
"fmt"
"html"
"sort"
Expand Down Expand Up @@ -357,6 +358,10 @@ func (app *App) registerStatic(prefix, root string, config ...Static) {
IndexNames: []string{"index.html"},
PathRewrite: func(fctx *fasthttp.RequestCtx) []byte {
path := fctx.Path()
mountPath := app.FullMountPath()
if n := len(mountPath); n > 0 && bytes.Equal(path[:n], utils.UnsafeBytes(mountPath)) {
path = path[n:]
}
if len(path) >= prefixLen {
if isStar && app.getString(path[0:prefixLen]) == prefix {
path = append(path[0:0], '/')
Expand Down
49 changes: 48 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 📃 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io

//nolint:bodyclose // Much easier to just ignore memory leaks in tests
//nolint:bodyclose,goconst // Much easier to just ignore memory leaks in tests and constant variables in tests
package fiber

import (
Expand Down Expand Up @@ -471,6 +471,53 @@ func Test_Route_Static_HasPrefix(t *testing.T) {
body, err = io.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))

app = New()
app.Static("/css", dir)

resp, err = app.Test(httptest.NewRequest(MethodGet, "/css/style.css", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

body, err = io.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
}

func Test_Route_Static_SubApp(t *testing.T) {
t.Parallel()

dir := "./.github/testdata/fs/css"
app := New()

// subapp
subApp := New()
subApp.Static("/css", dir)
app.Mount("/sub", subApp)

// nested subapp
nestApp := New()
nestApp.Static("/css", dir)
subApp.Mount("/nest", nestApp)

// test subapp
resp, err := app.Test(httptest.NewRequest(MethodGet, "/sub/css/style.css", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

body, err := io.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))

// test nested subapp

resp, err = app.Test(httptest.NewRequest(MethodGet, "/sub/nest/css/style.css", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

body, err = io.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
}

func Test_Router_NotFound(t *testing.T) {
Expand Down

0 comments on commit d415399

Please sign in to comment.