mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-24 04:02:38 +02:00
add comments, remove package-specific test (framework already gets corrected)
This commit is contained in:
parent
35873d4b81
commit
6b5ffb7ecb
@ -55,7 +55,7 @@ func (g *RouterPathGroup) MatchPattern(methods string, pattern *RouterPathGroupP
|
|||||||
|
|
||||||
type routerPathParam struct {
|
type routerPathParam struct {
|
||||||
name string
|
name string
|
||||||
patSepEnd bool
|
pathSepEnd bool
|
||||||
captureGroup int
|
captureGroup int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ func (p *routerPathMatcher) matchPath(chiCtx *chi.Context, path string) bool {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
val := path[pm[groupIdx]:pm[groupIdx+1]]
|
val := path[pm[groupIdx]:pm[groupIdx+1]]
|
||||||
if p.params[i].patSepEnd {
|
if p.params[i].pathSepEnd {
|
||||||
val = strings.TrimSuffix(val, "/")
|
val = strings.TrimSuffix(val, "/")
|
||||||
}
|
}
|
||||||
chiCtx.URLParams.Add(p.params[i].name, val)
|
chiCtx.URLParams.Add(p.params[i].name, val)
|
||||||
@ -154,16 +154,19 @@ func patternRegexp(pattern string, h ...any) *RouterPathGroupPattern {
|
|||||||
// it is not used so no need to implement it now
|
// it is not used so no need to implement it now
|
||||||
param := routerPathParam{}
|
param := routerPathParam{}
|
||||||
if partExp == "*" {
|
if partExp == "*" {
|
||||||
|
// "<part:*>" is a shorthand for optionally matching any string (but not greedy)
|
||||||
partExp = ".*?"
|
partExp = ".*?"
|
||||||
if lastEnd < len(pattern) && pattern[lastEnd] == '/' {
|
if lastEnd < len(pattern) && pattern[lastEnd] == '/' {
|
||||||
|
// if this param part ends with path separator "/", then consider it together: "(.*?/)"
|
||||||
partExp += "/"
|
partExp += "/"
|
||||||
param.patSepEnd = true
|
param.pathSepEnd = true
|
||||||
lastEnd++
|
lastEnd++
|
||||||
}
|
}
|
||||||
re = append(re, '(')
|
re = append(re, '(')
|
||||||
re = append(re, partExp...)
|
re = append(re, partExp...)
|
||||||
re = append(re, ')', '?')
|
re = append(re, ')', '?') // the wildcard matching is optional
|
||||||
} else {
|
} else {
|
||||||
|
// the pattern is user-provided regexp, defaults to a path part (separated by "/")
|
||||||
partExp = util.IfZero(partExp, "[^/]+")
|
partExp = util.IfZero(partExp, "[^/]+")
|
||||||
re = append(re, '(')
|
re = append(re, '(')
|
||||||
re = append(re, partExp...)
|
re = append(re, partExp...)
|
||||||
|
|||||||
@ -473,12 +473,6 @@ func CommonRoutes() *web.Router {
|
|||||||
g.MatchPath("GET", "/<group:*>/repodata/<filename>", rpm.GetRepositoryFile)
|
g.MatchPath("GET", "/<group:*>/repodata/<filename>", rpm.GetRepositoryFile)
|
||||||
g.MatchPath("PUT", "/<group:*>/upload", reqPackageAccess(perm.AccessModeWrite), rpm.UploadPackageFile)
|
g.MatchPath("PUT", "/<group:*>/upload", reqPackageAccess(perm.AccessModeWrite), rpm.UploadPackageFile)
|
||||||
// this URL pattern is only used internally in the RPM index, it is generated by us, the filename part is not really used (can be anything)
|
// this URL pattern is only used internally in the RPM index, it is generated by us, the filename part is not really used (can be anything)
|
||||||
// ~~Order is important here: the more specific long route (with filename) must come first.~~
|
|
||||||
// ~~Otherwise, when a package name contains "package",~~
|
|
||||||
// ~~* request path: ".../rpm/package/gitea-package/1.0.2-1/x86_64/any-file-name"~~
|
|
||||||
// ~~* it also matches "/rpm/<group:*>/package/<name>/<version>/<architecture>",~~
|
|
||||||
// TODO: the MatchPath has been fixed, the order is not important anymore
|
|
||||||
// As long as the CI passes, we can remove the comments and the new test case (not needed anymore)
|
|
||||||
g.MatchPath("HEAD,GET", "/<group:*>/package/<name>/<version>/<architecture>/<filename>", rpm.DownloadPackageFile)
|
g.MatchPath("HEAD,GET", "/<group:*>/package/<name>/<version>/<architecture>/<filename>", rpm.DownloadPackageFile)
|
||||||
g.MatchPath("HEAD,GET", "/<group:*>/package/<name>/<version>/<architecture>", rpm.DownloadPackageFile)
|
g.MatchPath("HEAD,GET", "/<group:*>/package/<name>/<version>/<architecture>", rpm.DownloadPackageFile)
|
||||||
g.MatchPath("DELETE", "/<group:*>/package/<name>/<version>/<architecture>", reqPackageAccess(perm.AccessModeWrite), rpm.DeletePackageFile)
|
g.MatchPath("DELETE", "/<group:*>/package/<name>/<version>/<architecture>", reqPackageAccess(perm.AccessModeWrite), rpm.DeletePackageFile)
|
||||||
|
|||||||
@ -15,7 +15,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
|
||||||
"code.gitea.io/gitea/models/packages"
|
"code.gitea.io/gitea/models/packages"
|
||||||
"code.gitea.io/gitea/models/unittest"
|
"code.gitea.io/gitea/models/unittest"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
@ -165,29 +164,6 @@ gpgkey=%sapi/packages/%s/rpm/repository.key`,
|
|||||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/package/%s/%s/%s/any-file-name", groupURL, packageName, packageVersion, packageArchitecture))
|
req = NewRequest(t, "GET", fmt.Sprintf("%s/package/%s/%s/%s/any-file-name", groupURL, packageName, packageVersion, packageArchitecture))
|
||||||
resp = MakeRequest(t, req, http.StatusOK)
|
resp = MakeRequest(t, req, http.StatusOK)
|
||||||
assert.Equal(t, content, resp.Body.Bytes())
|
assert.Equal(t, content, resp.Body.Bytes())
|
||||||
|
|
||||||
// Test with "package" in the name (reproduces bug if fix not applied)
|
|
||||||
pvs, err := packages.GetVersionsByPackageType(t.Context(), user.ID, packages.TypeRpm)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
require.NotEmpty(t, pvs)
|
|
||||||
|
|
||||||
// Rename package to "gitea-package" in DB to test collision
|
|
||||||
_, err = db.GetEngine(t.Context()).Exec("UPDATE package SET name = ?, lower_name = ? WHERE id = ?", "gitea-package", "gitea-package", pvs[0].PackageID)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
// Rename package_file to match new name
|
|
||||||
_, err = db.GetEngine(t.Context()).Exec("UPDATE package_file SET name = ?, lower_name = ? WHERE version_id = ?", "gitea-package-1.0.2-1.x86_64.rpm", "gitea-package-1.0.2-1.x86_64.rpm", pvs[0].ID)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
// Restore name
|
|
||||||
db.GetEngine(t.Context()).Exec("UPDATE package SET name = ?, lower_name = ? WHERE id = ?", packageName, strings.ToLower(packageName), pvs[0].PackageID)
|
|
||||||
db.GetEngine(t.Context()).Exec("UPDATE package_file SET name = ?, lower_name = ? WHERE version_id = ?", fmt.Sprintf("%s-%s.%s.rpm", packageName, packageVersion, packageArchitecture), strings.ToLower(fmt.Sprintf("%s-%s.%s.rpm", packageName, packageVersion, packageArchitecture)), pvs[0].ID)
|
|
||||||
}()
|
|
||||||
|
|
||||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/package/%s/%s/%s/any-file-name", groupURL, "gitea-package", packageVersion, packageArchitecture))
|
|
||||||
resp = MakeRequest(t, req, http.StatusOK)
|
|
||||||
assert.Equal(t, content, resp.Body.Bytes())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Repository", func(t *testing.T) {
|
t.Run("Repository", func(t *testing.T) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user