mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
modules/web/mux.go routes the 1333 routes. chi is no longer imported anywhere in
our source.
A tree over path segments, each node trying children by specificity: exact
segment, then patterned segments in registration order, then a trailing
wildcard. That ordering is the contract — "/user/settings" must beat
"/{username}" or a user named "settings" takes over the settings page — and
matching BACKTRACKS, because a segment can match here and fail deeper while the
next candidate at this level still has to be tried.
Four segment shapes, all of which this codebase actually uses:
users static
{id} whole segment (577)
{sha:[a-f0-9]+} regexp-constrained (43)
PACKAGES{format} parameter mid-segment (CRAN indexes)
* trailing wildcard (131)
A plain {name} carries no regexp. It is the overwhelming majority, and it does
not need one to match a whole segment.
The tests are the point of this commit, not the code. Each case is a way a
router can be wrong that a passing build will not reveal: static beating a
param, a regexp declining and the NEXT candidate winning (/repo/zzz is not a
sha, but "repo" is a legal username), a wildcard capturing the remainder,
"/*" matching "/" itself, HEAD falling back to GET, and — the one that matters
most — a failed branch leaving NO captured parameters behind for the branch
tried next.
Three things this turned up while wiring it in:
Mount had to strip its prefix. A sub-router mounted at "/v1" writes its
patterns relative to it, so handing it the full path matches nothing.
chi_middleware.GetHead was deleted rather than reimplemented: Mux.handlerFor
already falls back from HEAD to GET, so keeping it would have been two
implementations of one rule.
ChiRoutePathHandler is now RoutePathHandler and sets RouteContext.RoutePath.
It exists so a "%2f" inside a parameter routes as one segment — without it a
branch called "feat/x" splits and matches the wrong handler.
The vendored middleware tests used chi to register two routes each; they use a
small stdlib ServeMux shim now. modules/web imports modules/binding, so those
tests cannot import web back.
github.com/go-chi/chi/v5 remains in go.mod as an INDIRECT dependency of
markbates/goth/gothic, which drives every OAuth2 login. That is goth pulling it,
not us.
Everything green; integration compile gate green.
Co-authored-by: Hanzo Dev <[email protected]>
185 lines
5.7 KiB
Go
185 lines
5.7 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/container"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
type RouterPathGroup struct {
|
|
r *Router
|
|
pathParam string
|
|
matchers []*routerPathMatcher
|
|
}
|
|
|
|
func (g *RouterPathGroup) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
|
rc := GetRouteContext(req.Context())
|
|
path := rc.Param(g.pathParam)
|
|
for _, m := range g.matchers {
|
|
if m.matchPath(rc, path) {
|
|
rc.RoutePatterns = append(rc.RoutePatterns, m.pattern)
|
|
executeMiddlewaresHandler(resp, req, m.middlewares, m.handlerFunc)
|
|
return
|
|
}
|
|
}
|
|
g.r.mux.NotFoundHandler().ServeHTTP(resp, req)
|
|
}
|
|
|
|
type RouterPathGroupPattern struct {
|
|
pattern string
|
|
re *regexp.Regexp
|
|
params []routerPathParam
|
|
middlewares []any
|
|
}
|
|
|
|
// MatchPath matches the request method, and uses regexp to match the path.
|
|
// The pattern uses "<...>" to define path parameters, for example, "/<name>" (different from chi router)
|
|
// It is only designed to resolve some special cases that chi router can't handle.
|
|
// For most cases, it shouldn't be used because it needs to iterate all rules to find the matched one (inefficient).
|
|
func (g *RouterPathGroup) MatchPath(methods, pattern string, h ...any) {
|
|
g.MatchPattern(methods, g.PatternRegexp(pattern), h...)
|
|
}
|
|
|
|
func (g *RouterPathGroup) MatchPattern(methods string, pattern *RouterPathGroupPattern, h ...any) {
|
|
g.matchers = append(g.matchers, newRouterPathMatcher(methods, pattern, h...))
|
|
}
|
|
|
|
type routerPathParam struct {
|
|
name string
|
|
pathSepEnd bool
|
|
captureGroup int
|
|
}
|
|
|
|
type routerPathMatcher struct {
|
|
methods container.Set[string]
|
|
pattern string
|
|
re *regexp.Regexp
|
|
params []routerPathParam
|
|
middlewares []middlewareProvider
|
|
handlerFunc http.HandlerFunc
|
|
}
|
|
|
|
func (p *routerPathMatcher) matchPath(rc *RouteContext, path string) bool {
|
|
if !p.methods.Contains(rc.RouteMethod) {
|
|
return false
|
|
}
|
|
if !strings.HasPrefix(path, "/") {
|
|
path = "/" + path
|
|
}
|
|
pathMatches := p.re.FindStringSubmatchIndex(path) // Golang regexp match pairs [start, end, start, end, ...]
|
|
if pathMatches == nil {
|
|
return false
|
|
}
|
|
var paramMatches [][]int
|
|
for i := 2; i < len(pathMatches); {
|
|
paramMatches = append(paramMatches, []int{pathMatches[i], pathMatches[i+1]})
|
|
pmIdx := len(paramMatches) - 1
|
|
end := pathMatches[i+1]
|
|
i += 2
|
|
for ; i < len(pathMatches); i += 2 {
|
|
if pathMatches[i] >= end {
|
|
break
|
|
}
|
|
paramMatches[pmIdx] = append(paramMatches[pmIdx], pathMatches[i], pathMatches[i+1])
|
|
}
|
|
}
|
|
for i, pm := range paramMatches {
|
|
groupIdx := p.params[i].captureGroup * 2
|
|
if pm[groupIdx] == -1 || pm[groupIdx+1] == -1 {
|
|
rc.SetParam(p.params[i].name, "")
|
|
continue
|
|
}
|
|
val := path[pm[groupIdx]:pm[groupIdx+1]]
|
|
if p.params[i].pathSepEnd {
|
|
val = strings.TrimSuffix(val, "/")
|
|
}
|
|
rc.SetParam(p.params[i].name, val)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func isValidMethod(name string) bool {
|
|
switch name {
|
|
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodHead, http.MethodOptions, http.MethodConnect, http.MethodTrace:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func newRouterPathMatcher(methods string, patternRegexp *RouterPathGroupPattern, h ...any) *routerPathMatcher {
|
|
middlewares, handlerFunc, hasPreMiddlewares := wrapMiddlewareAndHandler(nil, patternRegexp.middlewares, h)
|
|
if hasPreMiddlewares {
|
|
panic("pre-middlewares are not supported in router path matcher")
|
|
}
|
|
p := &routerPathMatcher{methods: make(container.Set[string]), middlewares: middlewares, handlerFunc: handlerFunc}
|
|
for method := range strings.SplitSeq(methods, ",") {
|
|
method = strings.TrimSpace(method)
|
|
if !isValidMethod(method) {
|
|
panic("invalid HTTP method: " + method)
|
|
}
|
|
p.methods.Add(method)
|
|
}
|
|
p.pattern, p.re, p.params = patternRegexp.pattern, patternRegexp.re, patternRegexp.params
|
|
return p
|
|
}
|
|
|
|
func patternRegexp(pattern string, h ...any) *RouterPathGroupPattern {
|
|
p := &RouterPathGroupPattern{middlewares: slices.Clone(h)}
|
|
re := []byte{'^'}
|
|
lastEnd := 0
|
|
for lastEnd < len(pattern) {
|
|
start := strings.IndexByte(pattern[lastEnd:], '<')
|
|
if start == -1 {
|
|
re = append(re, regexp.QuoteMeta(pattern[lastEnd:])...)
|
|
break
|
|
}
|
|
end := strings.IndexByte(pattern[lastEnd+start:], '>')
|
|
if end == -1 {
|
|
panic("invalid pattern: " + pattern)
|
|
}
|
|
re = append(re, regexp.QuoteMeta(pattern[lastEnd:lastEnd+start])...)
|
|
partName, partExp, _ := strings.Cut(pattern[lastEnd+start+1:lastEnd+start+end], ":")
|
|
lastEnd += start + end + 1
|
|
|
|
// TODO: it could support to specify a "capture group" for the name, for example: "/<name[2]:(\d)-(\d)>"
|
|
// it is not used so no need to implement it now
|
|
param := routerPathParam{}
|
|
if partExp == "*" {
|
|
// "<part:*>" is a shorthand for optionally matching any string (but not greedy)
|
|
partExp = ".*?"
|
|
if lastEnd < len(pattern) && pattern[lastEnd] == '/' {
|
|
// if this param part ends with path separator "/", then consider it together: "(.*?/)"
|
|
partExp += "/"
|
|
param.pathSepEnd = true
|
|
lastEnd++
|
|
}
|
|
re = append(re, '(')
|
|
re = append(re, partExp...)
|
|
re = append(re, ')', '?') // the wildcard matching is optional
|
|
} else {
|
|
// the pattern is user-provided regexp, defaults to a path part (separated by "/")
|
|
partExp = util.IfZero(partExp, "[^/]+")
|
|
re = append(re, '(')
|
|
re = append(re, partExp...)
|
|
re = append(re, ')')
|
|
}
|
|
param.name = partName
|
|
p.params = append(p.params, param)
|
|
}
|
|
re = append(re, '$')
|
|
p.pattern, p.re = pattern, regexp.MustCompile(string(re))
|
|
return p
|
|
}
|
|
|
|
func (g *RouterPathGroup) PatternRegexp(pattern string, h ...any) *RouterPathGroupPattern {
|
|
return patternRegexp(pattern, h...)
|
|
}
|