Skip to content

Commit 84ff353

Browse files
committed
fix
1 parent e07ddcb commit 84ff353

File tree

3 files changed

+63
-97
lines changed

3 files changed

+63
-97
lines changed

modules/packages/arch/metadata.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"regexp"
1414
"strconv"
1515
"strings"
16+
"sync"
1617

1718
"code.gitea.io/gitea/modules/packages"
1819
"code.gitea.io/gitea/modules/util"
@@ -36,14 +37,32 @@ const (
3637
RepositoryVersion = "_repository"
3738
)
3839

39-
var (
40-
reName = regexp.MustCompile(`^[a-zA-Z0-9@._+-]+$`)
41-
reVer = regexp.MustCompile(`^[a-zA-Z0-9:_.+]+-+[0-9]+$`)
42-
reOptDep = regexp.MustCompile(`^[a-zA-Z0-9@._+-]+([<>]?=?([0-9]+:)?[a-zA-Z0-9@._+-]+)?(:.*)?$`)
43-
rePkgVer = regexp.MustCompile(`^[a-zA-Z0-9@._+-]+([<>]?=?([0-9]+:)?[a-zA-Z0-9@._+-]+)?$`)
40+
type GlobalVarType struct {
41+
reName *regexp.Regexp
42+
reVer *regexp.Regexp
43+
reOptDep *regexp.Regexp
44+
rePkgVer *regexp.Regexp
4445

45-
maxMagicLength = 0
46-
magics = map[string]struct {
46+
maxMagicLength int
47+
magics map[string]struct {
48+
magic []byte
49+
archiver func() archiver.Reader
50+
}
51+
52+
ArchPkgOrSig *regexp.Regexp
53+
ArchDBOrSig *regexp.Regexp
54+
}
55+
56+
var GlobalVar = sync.OnceValue[*GlobalVarType](func() *GlobalVarType {
57+
v := GlobalVarType{
58+
reName: regexp.MustCompile(`^[a-zA-Z0-9@._+-]+$`),
59+
reVer: regexp.MustCompile(`^[a-zA-Z0-9:_.+]+-+[0-9]+$`),
60+
reOptDep: regexp.MustCompile(`^[a-zA-Z0-9@._+-]+([<>]?=?([0-9]+:)?[a-zA-Z0-9@._+-]+)?(:.*)?$`),
61+
rePkgVer: regexp.MustCompile(`^[a-zA-Z0-9@._+-]+([<>]?=?([0-9]+:)?[a-zA-Z0-9@._+-]+)?$`),
62+
ArchPkgOrSig: regexp.MustCompile(`^.*\.pkg\.tar\.\w+(\.sig)*$`),
63+
ArchDBOrSig: regexp.MustCompile(`^.*.db(\.tar\.gz)*(\.sig)*$`),
64+
}
65+
v.magics = map[string]struct {
4766
magic []byte
4867
archiver func() archiver.Reader
4968
}{
@@ -66,15 +85,11 @@ var (
6685
},
6786
},
6887
}
69-
)
70-
71-
func init() {
72-
for _, i := range magics {
73-
if nLen := len(i.magic); nLen > maxMagicLength {
74-
maxMagicLength = nLen
75-
}
88+
for _, i := range v.magics {
89+
v.maxMagicLength = max(v.maxMagicLength, len(i.magic))
7690
}
77-
}
91+
return &v
92+
})
7893

7994
type Package struct {
8095
Name string `json:"name"`
@@ -124,7 +139,7 @@ func ParsePackage(r *packages.HashedBuffer) (*Package, error) {
124139
return nil, err
125140
}
126141

127-
header := make([]byte, maxMagicLength)
142+
header := make([]byte, GlobalVar().maxMagicLength)
128143
_, err = r.Read(header)
129144
if err != nil {
130145
return nil, err
@@ -136,7 +151,7 @@ func ParsePackage(r *packages.HashedBuffer) (*Package, error) {
136151

137152
var tarball archiver.Reader
138153
var tarballType string
139-
for tarType, info := range magics {
154+
for tarType, info := range GlobalVar().magics {
140155
if bytes.Equal(header[:len(info.magic)], info.magic) {
141156
tarballType = tarType
142157
tarball = info.archiver()
@@ -272,6 +287,8 @@ func ParsePackageInfo(compressType string, r io.Reader) (*Package, error) {
272287

273288
// ValidatePackageSpec Arch package validation according to PKGBUILD specification.
274289
func ValidatePackageSpec(p *Package) error {
290+
gv := GlobalVar()
291+
reName, reVer, reOptDep, rePkgVer := gv.reName, gv.reVer, gv.reOptDep, gv.rePkgVer
275292
if !reName.MatchString(p.Name) {
276293
return util.NewInvalidArgumentErrorf("invalid package name")
277294
}

routers/api/packages/api.go

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -137,66 +137,10 @@ func CommonRoutes() *web.Router {
137137
})
138138
}, reqPackageAccess(perm.AccessModeRead))
139139
r.Group("/arch", func() {
140-
r.Group("/repository.key", func() {
141-
r.Head("", arch.GetRepositoryKey)
142-
r.Get("", arch.GetRepositoryKey)
143-
})
144-
145-
r.Methods("HEAD,GET,PUT,DELETE", "*", func(ctx *context.Context) {
146-
pathGroups := strings.Split(strings.Trim(ctx.PathParam("*"), "/"), "/")
147-
groupLen := len(pathGroups)
148-
isGetHead := ctx.Req.Method == "HEAD" || ctx.Req.Method == "GET"
149-
isPut := ctx.Req.Method == "PUT"
150-
isDelete := ctx.Req.Method == "DELETE"
151-
if isGetHead {
152-
if groupLen < 2 {
153-
ctx.Status(http.StatusNotFound)
154-
return
155-
}
156-
if groupLen == 2 {
157-
ctx.SetPathParam("group", "")
158-
ctx.SetPathParam("arch", pathGroups[0])
159-
ctx.SetPathParam("file", pathGroups[1])
160-
} else {
161-
ctx.SetPathParam("group", strings.Join(pathGroups[:groupLen-2], "/"))
162-
ctx.SetPathParam("arch", pathGroups[groupLen-2])
163-
ctx.SetPathParam("file", pathGroups[groupLen-1])
164-
}
165-
arch.GetPackageOrDB(ctx)
166-
return
167-
} else if isPut {
168-
ctx.SetPathParam("group", strings.Join(pathGroups, "/"))
169-
reqPackageAccess(perm.AccessModeWrite)(ctx)
170-
if ctx.Written() {
171-
return
172-
}
173-
arch.PushPackage(ctx)
174-
return
175-
} else if isDelete {
176-
if groupLen < 3 {
177-
ctx.Status(http.StatusBadRequest)
178-
return
179-
}
180-
if groupLen == 3 {
181-
ctx.SetPathParam("group", "")
182-
ctx.SetPathParam("package", pathGroups[0])
183-
ctx.SetPathParam("version", pathGroups[1])
184-
ctx.SetPathParam("arch", pathGroups[2])
185-
} else {
186-
ctx.SetPathParam("group", strings.Join(pathGroups[:groupLen-3], "/"))
187-
ctx.SetPathParam("package", pathGroups[groupLen-3])
188-
ctx.SetPathParam("version", pathGroups[groupLen-2])
189-
ctx.SetPathParam("arch", pathGroups[groupLen-1])
190-
}
191-
reqPackageAccess(perm.AccessModeWrite)(ctx)
192-
if ctx.Written() {
193-
return
194-
}
195-
arch.RemovePackage(ctx)
196-
return
197-
}
198-
ctx.Status(http.StatusNotFound)
199-
})
140+
r.Methods("HEAD,GET", "/repository.key", arch.GetRepositoryKey)
141+
r.Methods("HEAD,GET", "*", arch.GetPackageOrDB)
142+
r.Methods("PUT", "*", reqPackageAccess(perm.AccessModeWrite), arch.PushPackage)
143+
r.Methods("DELETE", "*", reqPackageAccess(perm.AccessModeWrite), arch.RemovePackage)
200144
}, reqPackageAccess(perm.AccessModeRead))
201145
r.Group("/cargo", func() {
202146
r.Group("/api/v1/crates", func() {

routers/api/packages/arch/arch.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io"
1111
"net/http"
1212
"path/filepath"
13-
"regexp"
1413
"strings"
1514

1615
packages_model "code.gitea.io/gitea/models/packages"
@@ -24,11 +23,6 @@ import (
2423
arch_service "code.gitea.io/gitea/services/packages/arch"
2524
)
2625

27-
var (
28-
archPkgOrSig = regexp.MustCompile(`^.*\.pkg\.tar\.\w+(\.sig)*$`)
29-
archDBOrSig = regexp.MustCompile(`^.*.db(\.tar\.gz)*(\.sig)*$`)
30-
)
31-
3226
func apiError(ctx *context.Context, status int, obj any) {
3327
helper.LogAndProcessError(ctx, status, obj, func(message string) {
3428
ctx.PlainText(status, message)
@@ -53,7 +47,7 @@ func refreshLocker(ctx *context.Context, group string) (globallock.ReleaseFunc,
5347
}
5448

5549
func PushPackage(ctx *context.Context) {
56-
group := ctx.PathParam("group")
50+
group := ctx.PathParam("*")
5751
releaser, err := refreshLocker(ctx, group)
5852
if err != nil {
5953
apiError(ctx, http.StatusInternalServerError, err)
@@ -175,13 +169,18 @@ func PushPackage(ctx *context.Context) {
175169
}
176170

177171
func GetPackageOrDB(ctx *context.Context) {
178-
var (
179-
file = ctx.PathParam("file")
180-
group = ctx.PathParam("group")
181-
arch = ctx.PathParam("arch")
182-
)
172+
pathFields := strings.Split(strings.Trim(ctx.PathParam("*"), "/"), "/")
173+
pathFieldsLen := len(pathFields)
174+
if pathFieldsLen < 2 {
175+
ctx.Status(http.StatusBadRequest)
176+
return
177+
}
178+
179+
group := strings.Join(pathFields[:pathFieldsLen-2], "/")
180+
arch := pathFields[pathFieldsLen-2]
181+
file := pathFields[pathFieldsLen-1]
183182

184-
if archPkgOrSig.MatchString(file) {
183+
if arch_module.GlobalVar().ArchPkgOrSig.MatchString(file) {
185184
pkg, u, pf, err := arch_service.GetPackageFile(ctx, group, file, ctx.Package.Owner.ID)
186185
if err != nil {
187186
if errors.Is(err, util.ErrNotExist) {
@@ -195,7 +194,7 @@ func GetPackageOrDB(ctx *context.Context) {
195194
return
196195
}
197196

198-
if archDBOrSig.MatchString(file) {
197+
if arch_module.GlobalVar().ArchDBOrSig.MatchString(file) {
199198
pkg, u, pf, err := arch_service.GetPackageDBFile(ctx, group, arch, ctx.Package.Owner.ID,
200199
strings.HasSuffix(file, ".sig"))
201200
if err != nil {
@@ -215,12 +214,18 @@ func GetPackageOrDB(ctx *context.Context) {
215214
}
216215

217216
func RemovePackage(ctx *context.Context) {
218-
var (
219-
group = ctx.PathParam("group")
220-
pkg = ctx.PathParam("package")
221-
ver = ctx.PathParam("version")
222-
pkgArch = ctx.PathParam("arch")
223-
)
217+
pathFields := strings.Split(strings.Trim(ctx.PathParam("*"), "/"), "/")
218+
pathFieldsLen := len(pathFields)
219+
if pathFieldsLen < 3 {
220+
ctx.Status(http.StatusBadRequest)
221+
return
222+
}
223+
224+
group := strings.Join(pathFields[:pathFieldsLen-3], "/")
225+
pkg := pathFields[pathFieldsLen-3]
226+
ver := pathFields[pathFieldsLen-2]
227+
pkgArch := pathFields[pathFieldsLen-1]
228+
224229
releaser, err := refreshLocker(ctx, group)
225230
if err != nil {
226231
apiError(ctx, http.StatusInternalServerError, err)

0 commit comments

Comments
 (0)