0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-04-03 23:22:39 +02:00

fix framework

This commit is contained in:
wxiaoguang 2026-04-03 10:23:26 +08:00
parent 94af7e2adc
commit 54bace18d0
2 changed files with 23 additions and 4 deletions

View File

@ -55,6 +55,7 @@ func (g *RouterPathGroup) MatchPattern(methods string, pattern *RouterPathGroupP
type routerPathParam struct {
name string
patSepEnd bool
captureGroup int
}
@ -93,7 +94,15 @@ func (p *routerPathMatcher) matchPath(chiCtx *chi.Context, path string) bool {
}
for i, pm := range paramMatches {
groupIdx := p.params[i].captureGroup * 2
chiCtx.URLParams.Add(p.params[i].name, path[pm[groupIdx]:pm[groupIdx+1]])
if pm[groupIdx] == -1 || pm[groupIdx+1] == -1 {
chiCtx.URLParams.Add(p.params[i].name, "")
continue
}
val := path[pm[groupIdx]:pm[groupIdx+1]]
if p.params[i].patSepEnd {
val = strings.TrimSuffix(val, "/")
}
chiCtx.URLParams.Add(p.params[i].name, val)
}
return true
}
@ -145,10 +154,15 @@ func patternRegexp(pattern string, h ...any) *RouterPathGroupPattern {
// it is not used so no need to implement it now
param := routerPathParam{}
if partExp == "*" {
re = append(re, "(.*?)/?"...)
partExp = ".*?"
if lastEnd < len(pattern) && pattern[lastEnd] == '/' {
lastEnd++ // the "*" pattern is able to handle the last slash, so skip it
partExp += "/"
param.patSepEnd = true
lastEnd++
}
re = append(re, '(')
re = append(re, partExp...)
re = append(re, ')', '?')
} else {
partExp = util.IfZero(partExp, "[^/]+")
re = append(re, '(')

View File

@ -100,7 +100,8 @@ func TestPathProcessor(t *testing.T) {
chiCtx := chi.NewRouteContext()
chiCtx.RouteMethod = "GET"
p := newRouterPathMatcher("GET", patternRegexp(pattern), http.NotFound)
assert.True(t, p.matchPath(chiCtx, uri), "use pattern %s to process uri %s", pattern, uri)
shouldProcess := expectedPathParams != nil
assert.Equal(t, shouldProcess, p.matchPath(chiCtx, uri), "use pattern %s to process uri %s", pattern, uri)
assert.Equal(t, expectedPathParams, chiURLParamsToMap(chiCtx), "use pattern %s to process uri %s", pattern, uri)
}
@ -113,6 +114,10 @@ func TestPathProcessor(t *testing.T) {
testProcess("/<p1:*>/<p2>", "/a", map[string]string{"p1": "", "p2": "a"})
testProcess("/<p1:*>/<p2>", "/a/b", map[string]string{"p1": "a", "p2": "b"})
testProcess("/<p1:*>/<p2>", "/a/b/c", map[string]string{"p1": "a/b", "p2": "c"})
testProcess("/<p1:*>/part/<p2>", "/a/part/c", map[string]string{"p1": "a", "p2": "c"})
testProcess("/<p1:*>/part/<p2>", "/part/c", map[string]string{"p1": "", "p2": "c"})
testProcess("/<p1:*>/part/<p2>", "/a/other-part/c", nil)
testProcess("/<p1:*>-part/<p2>", "/a-other-part/c", map[string]string{"p1": "a-other", "p2": "c"})
}
func TestRouter(t *testing.T) {