Skip to content

Commit d415399

Browse files
committed
🩹 Fix: static server in sub app with mount (#3104)
1 parent 6e74114 commit d415399

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

mount.go

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type mountFields struct {
2323
subAppsProcessed sync.Once
2424
// Prefix of app if it was mounted
2525
mountPath string
26+
// Parent app of the current app
27+
parentApp *App
2628
}
2729

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

5153
subApp.mountFields.mountPath = path
5254
app.mountFields.appList[path] = subApp
55+
subApp.mountFields.parentApp = app
5356
}
5457

5558
// register mounted group
@@ -99,6 +102,14 @@ func (app *App) MountPath() string {
99102
return app.mountFields.mountPath
100103
}
101104

105+
// FullMountPath returns the full mount path of the app, including the parent app's mount path.
106+
func (app *App) FullMountPath() string {
107+
if app.mountFields.parentApp == nil {
108+
return app.mountFields.mountPath
109+
}
110+
return getGroupPath(app.mountFields.parentApp.FullMountPath(), app.mountFields.mountPath)
111+
}
112+
102113
// hasMountedApps Checks if there are any mounted apps in the current application.
103114
func (app *App) hasMountedApps() bool {
104115
return len(app.mountFields.appList) > 1

router.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fiber
66

77
import (
8+
"bytes"
89
"fmt"
910
"html"
1011
"sort"
@@ -357,6 +358,10 @@ func (app *App) registerStatic(prefix, root string, config ...Static) {
357358
IndexNames: []string{"index.html"},
358359
PathRewrite: func(fctx *fasthttp.RequestCtx) []byte {
359360
path := fctx.Path()
361+
mountPath := app.FullMountPath()
362+
if n := len(mountPath); n > 0 && bytes.Equal(path[:n], utils.UnsafeBytes(mountPath)) {
363+
path = path[n:]
364+
}
360365
if len(path) >= prefixLen {
361366
if isStar && app.getString(path[0:prefixLen]) == prefix {
362367
path = append(path[0:0], '/')

router_test.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// 📃 Github Repository: https://github.com/gofiber/fiber
33
// 📌 API Documentation: https://docs.gofiber.io
44

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

88
import (
@@ -471,6 +471,53 @@ func Test_Route_Static_HasPrefix(t *testing.T) {
471471
body, err = io.ReadAll(resp.Body)
472472
utils.AssertEqual(t, nil, err, "app.Test(req)")
473473
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
474+
475+
app = New()
476+
app.Static("/css", dir)
477+
478+
resp, err = app.Test(httptest.NewRequest(MethodGet, "/css/style.css", nil))
479+
utils.AssertEqual(t, nil, err, "app.Test(req)")
480+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
481+
482+
body, err = io.ReadAll(resp.Body)
483+
utils.AssertEqual(t, nil, err, "app.Test(req)")
484+
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
485+
}
486+
487+
func Test_Route_Static_SubApp(t *testing.T) {
488+
t.Parallel()
489+
490+
dir := "./.github/testdata/fs/css"
491+
app := New()
492+
493+
// subapp
494+
subApp := New()
495+
subApp.Static("/css", dir)
496+
app.Mount("/sub", subApp)
497+
498+
// nested subapp
499+
nestApp := New()
500+
nestApp.Static("/css", dir)
501+
subApp.Mount("/nest", nestApp)
502+
503+
// test subapp
504+
resp, err := app.Test(httptest.NewRequest(MethodGet, "/sub/css/style.css", nil))
505+
utils.AssertEqual(t, nil, err, "app.Test(req)")
506+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
507+
508+
body, err := io.ReadAll(resp.Body)
509+
utils.AssertEqual(t, nil, err, "app.Test(req)")
510+
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
511+
512+
// test nested subapp
513+
514+
resp, err = app.Test(httptest.NewRequest(MethodGet, "/sub/nest/css/style.css", nil))
515+
utils.AssertEqual(t, nil, err, "app.Test(req)")
516+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
517+
518+
body, err = io.ReadAll(resp.Body)
519+
utils.AssertEqual(t, nil, err, "app.Test(req)")
520+
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
474521
}
475522

476523
func Test_Router_NotFound(t *testing.T) {

0 commit comments

Comments
 (0)