Skip to content

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

routers/api/packages/api.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,10 @@ func CommonRoutes() *web.Route {
520520
r.Get("", rpm.DownloadPackageFile)
521521
r.Delete("", reqPackageAccess(perm.AccessModeWrite), rpm.DeletePackageFile)
522522
})
523-
r.Get("/repodata/{filename}", rpm.GetRepositoryFile)
523+
r.Group("/repodata/{filename}", func() {
524+
r.Head("", rpm.CheckRepositoryFileExistence)
525+
r.Get("", rpm.GetRepositoryFile)
526+
})
524527
}, reqPackageAccess(perm.AccessModeRead))
525528
r.Group("/rubygems", func() {
526529
r.Get("/specs.4.8.gz", rubygems.EnumeratePackages)

routers/api/packages/rpm/rpm.go

+24
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ func GetRepositoryKey(ctx *context.Context) {
5757
})
5858
}
5959

60+
func CheckRepositoryFileExistence(ctx *context.Context) {
61+
pv, err := rpm_service.GetOrCreateRepositoryVersion(ctx, ctx.Package.Owner.ID)
62+
if err != nil {
63+
apiError(ctx, http.StatusInternalServerError, err)
64+
return
65+
}
66+
67+
pf, err := packages_model.GetFileForVersionByName(ctx, pv.ID, ctx.Params("filename"), packages_model.EmptyFileKey)
68+
if err != nil {
69+
if errors.Is(err, util.ErrNotExist) {
70+
ctx.Status(http.StatusNotFound)
71+
} else {
72+
apiError(ctx, http.StatusInternalServerError, err)
73+
}
74+
return
75+
}
76+
77+
ctx.SetServeHeaders(&context.ServeHeaderOptions{
78+
Filename: pf.Name,
79+
LastModified: pf.CreatedUnix.AsLocalTime(),
80+
})
81+
ctx.Status(http.StatusOK)
82+
}
83+
6084
// Gets a pre-generated repository metadata file
6185
func GetRepositoryFile(ctx *context.Context) {
6286
pv, err := rpm_service.GetOrCreateRepositoryVersion(ctx, ctx.Package.Owner.ID)

tests/integration/api_packages_rpm_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,18 @@ gpgkey=%sapi/packages/%s/rpm/repository.key`, user.Name, user.Name, setting.AppN
149149

150150
url := rootURL + "/repodata"
151151

152-
req := NewRequest(t, "GET", url+"/dummy.xml")
152+
req := NewRequest(t, "HEAD", url+"/dummy.xml")
153+
MakeRequest(t, req, http.StatusNotFound)
154+
155+
req = NewRequest(t, "GET", url+"/dummy.xml")
153156
MakeRequest(t, req, http.StatusNotFound)
154157

155158
t.Run("repomd.xml", func(t *testing.T) {
156159
defer tests.PrintCurrentTest(t)()
157160

161+
req = NewRequest(t, "HEAD", url+"/repomd.xml")
162+
MakeRequest(t, req, http.StatusOK)
163+
158164
req = NewRequest(t, "GET", url+"/repomd.xml")
159165
resp := MakeRequest(t, req, http.StatusOK)
160166

0 commit comments

Comments
 (0)