Compare commits
2
Commits
ext-v1.3.1
...
ext-v1.3.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6374d1593 | ||
|
|
9ede4bc63d |
No files matched your search
@@ -8,6 +8,21 @@
|
||||
|
||||
### Changed
|
||||
|
||||
## Ext pack v1.3.2 — 2026-10-13
|
||||
|
||||
### Highlights
|
||||
|
||||
**Trailing slash 路由修复(CLI 仍为 v1.3.0)**:Go `ServeMux` 精确匹配导致 `/post/foo/`(带尾部斜杠)落入 404。新增 `withStripTrailingSlash` 中间件作为最外层 handler wrapper,所有下游中间件(RBAC、Tenant、路由)均看到规范化路径。覆盖静态路由、动态路由、根路径、查询参数保留。
|
||||
|
||||
```bash
|
||||
marqdo ext add web # CDN latest → 1.3.2
|
||||
# 或:MARQDO_EXT_VERSION=1.3.2 marqdo ext add web
|
||||
```
|
||||
|
||||
### Fixed
|
||||
|
||||
- **web**: `/post/foo/` trailing slash now routes the same as `/post/foo` (dynamic and static routes). Root `/` unchanged. Query strings preserved after normalization.
|
||||
|
||||
## Ext pack v1.3.1 — 2026-10-08
|
||||
|
||||
### Highlights
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
1.3.1
|
||||
1.3.2
|
||||
@@ -247,6 +247,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
handler := withMiddleware(mux, mw)
|
||||
handler = withRBAC(handler, authCfg.gates, authCfg.loginPath)
|
||||
handler = withTenant(handler, st.tenant)
|
||||
handler = withStripTrailingSlash(handler)
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
@@ -641,6 +642,21 @@ func withTenant(next http.Handler, cfg tenant.Config) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// withStripTrailingSlash rewrites request URLs so that paths like /post/foo/
|
||||
// are routed as /post/foo. The root path "/" is left unchanged.
|
||||
func withStripTrailingSlash(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
if len(path) > 1 && path[len(path)-1] == '/' {
|
||||
r.URL.Path = strings.TrimRight(path, "/")
|
||||
if r.URL.Path == "" {
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func withMiddleware(next http.Handler, mw middleware.Config) http.Handler {
|
||||
h := next
|
||||
if mw.BodyLimit != nil && *mw.BodyLimit > 0 {
|
||||
|
||||
@@ -152,3 +152,65 @@ func TestNewHandlerIconsRedirectSitemap404(t *testing.T) {
|
||||
t.Fatalf("home missing icon link: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrailingSlashNormalization(t *testing.T) {
|
||||
page := map[string]any{"title": "Home", "intro": "<p>home</p>"}
|
||||
a := app.New(map[string]any{"page": page})
|
||||
about := map[string]any{"title": "About", "intro": "<h1>About</h1>", "_route": "/about"}
|
||||
var err error
|
||||
a, err = app.Route(a, "/about", about)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Dynamic route: /post/{slug}
|
||||
post := map[string]any{"title": "Post", "intro": "<h1>Post</h1>", "_route": "/post/{slug}"}
|
||||
a, err = app.Route(a, "/post/{slug}", post)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
h, err := httpx.NewHandler(a, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Static route: /about should work with and without trailing slash.
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
|
||||
t.Fatalf("about (no slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about/", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
|
||||
t.Fatalf("about (trailing slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Dynamic route: /post/hello should work with and without trailing slash.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/post/hello", nil))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("post/hello (no slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/post/hello/", nil))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("post/hello (trailing slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Root path "/" should still work.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Home") {
|
||||
t.Fatalf("root code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Query string should be preserved after trailing slash strip.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about/?foo=bar", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
|
||||
t.Fatalf("about (trailing slash + query) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user