Skip to content

Commit 733357b

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

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
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

+7
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,12 @@ 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 {
363+
if bytes.Equal(path[:n], utils.UnsafeBytes(mountPath)) {
364+
path = path[n:]
365+
}
366+
}
360367
if len(path) >= prefixLen {
361368
if isStar && app.getString(path[0:prefixLen]) == prefix {
362369
path = append(path[0:0], '/')

router_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// 📌 API Documentation: https://docs.gofiber.io
44

55
//nolint:bodyclose // Much easier to just ignore memory leaks in tests
6+
//nolint:goconst // Ignore constant variable
67
package fiber
78

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

476524
func Test_Router_NotFound(t *testing.T) {

0 commit comments

Comments
 (0)