@@ -30,6 +30,7 @@ import (
3030
3131 zerr "zotregistry.io/zot/errors"
3232 "zotregistry.io/zot/pkg/api/constants"
33+ zcommon "zotregistry.io/zot/pkg/common"
3334 gqlPlayground "zotregistry.io/zot/pkg/debug/gqlplayground"
3435 debug "zotregistry.io/zot/pkg/debug/swagger"
3536 ext "zotregistry.io/zot/pkg/extensions"
@@ -53,10 +54,6 @@ func NewRouteHandler(c *Controller) *RouteHandler {
5354 return rh
5455}
5556
56- func allowedMethods (method string ) []string {
57- return []string {http .MethodOptions , method }
58- }
59-
6057func (rh * RouteHandler ) SetupRoutes () {
6158 prefixedRouter := rh .c .Router .PathPrefix (constants .RoutePrefix ).Subrouter ()
6259 prefixedRouter .Use (AuthHandler (rh .c ))
@@ -75,11 +72,11 @@ func (rh *RouteHandler) SetupRoutes() {
7572 // https://github.com/opencontainers/distribution-spec/blob/main/spec.md#endpoints
7673 {
7774 prefixedRouter .HandleFunc (fmt .Sprintf ("/{name:%s}/tags/list" , zreg .NameRegexp .String ()),
78- rh .ListTags ).Methods (allowedMethods ("GET" )... )
75+ rh .ListTags ).Methods (zcommon . AllowedMethods ("GET" )... )
7976 prefixedRouter .HandleFunc (fmt .Sprintf ("/{name:%s}/manifests/{reference}" , zreg .NameRegexp .String ()),
80- rh .CheckManifest ).Methods (allowedMethods ("HEAD" )... )
77+ rh .CheckManifest ).Methods (zcommon . AllowedMethods ("HEAD" )... )
8178 prefixedRouter .HandleFunc (fmt .Sprintf ("/{name:%s}/manifests/{reference}" , zreg .NameRegexp .String ()),
82- rh .GetManifest ).Methods (allowedMethods ("GET" )... )
79+ rh .GetManifest ).Methods (zcommon . AllowedMethods ("GET" )... )
8380 prefixedRouter .HandleFunc (fmt .Sprintf ("/{name:%s}/manifests/{reference}" , zreg .NameRegexp .String ()),
8481 rh .UpdateManifest ).Methods ("PUT" )
8582 prefixedRouter .HandleFunc (fmt .Sprintf ("/{name:%s}/manifests/{reference}" , zreg .NameRegexp .String ()),
@@ -102,13 +99,13 @@ func (rh *RouteHandler) SetupRoutes() {
10299 rh .DeleteBlobUpload ).Methods ("DELETE" )
103100 // support for OCI artifact references
104101 prefixedRouter .HandleFunc (fmt .Sprintf ("/{name:%s}/referrers/{digest}" , zreg .NameRegexp .String ()),
105- rh .GetReferrers ).Methods (allowedMethods ("GET" )... )
102+ rh .GetReferrers ).Methods (zcommon . AllowedMethods ("GET" )... )
106103 prefixedRouter .HandleFunc (constants .ExtCatalogPrefix ,
107- rh .ListRepositories ).Methods (allowedMethods ("GET" )... )
104+ rh .ListRepositories ).Methods (zcommon . AllowedMethods ("GET" )... )
108105 prefixedRouter .HandleFunc (constants .ExtOciDiscoverPrefix ,
109- rh .ListExtensions ).Methods (allowedMethods ("GET" )... )
106+ rh .ListExtensions ).Methods (zcommon . AllowedMethods ("GET" )... )
110107 prefixedRouter .HandleFunc ("/" ,
111- rh .CheckVersionSupport ).Methods (allowedMethods ("GET" )... )
108+ rh .CheckVersionSupport ).Methods (zcommon . AllowedMethods ("GET" )... )
112109 }
113110
114111 // support for ORAS artifact reference types (alpha 1) - image signature use case
@@ -146,6 +143,13 @@ func (rh *RouteHandler) SetupRoutes() {
146143// @Produce json
147144// @Success 200 {string} string "ok".
148145func (rh * RouteHandler ) CheckVersionSupport (response http.ResponseWriter , request * http.Request ) {
146+ response .Header ().Set ("Access-Control-Allow-Methods" , "HEAD,GET,POST,OPTIONS" )
147+ response .Header ().Set ("Access-Control-Allow-Headers" , "Authorization,content-type" )
148+
149+ if request .Method == http .MethodOptions {
150+ return
151+ }
152+
149153 response .Header ().Set (constants .DistAPIVersion , "registry/2.0" )
150154 // NOTE: compatibility workaround - return this header in "allowed-read" mode to allow for clients to
151155 // work correctly
@@ -178,6 +182,13 @@ type ImageTags struct {
178182// @Failure 404 {string} string "not found"
179183// @Failure 400 {string} string "bad request".
180184func (rh * RouteHandler ) ListTags (response http.ResponseWriter , request * http.Request ) {
185+ response .Header ().Set ("Access-Control-Allow-Methods" , "HEAD,GET,POST,OPTIONS" )
186+ response .Header ().Set ("Access-Control-Allow-Headers" , "Authorization,content-type" )
187+
188+ if request .Method == http .MethodOptions {
189+ return
190+ }
191+
181192 vars := mux .Vars (request )
182193
183194 name , ok := vars ["name" ]
@@ -301,6 +312,13 @@ func (rh *RouteHandler) ListTags(response http.ResponseWriter, request *http.Req
301312// @Failure 404 {string} string "not found"
302313// @Failure 500 {string} string "internal server error".
303314func (rh * RouteHandler ) CheckManifest (response http.ResponseWriter , request * http.Request ) {
315+ response .Header ().Set ("Access-Control-Allow-Methods" , "HEAD,GET,POST,OPTIONS" )
316+ response .Header ().Set ("Access-Control-Allow-Headers" , "Authorization,content-type" )
317+
318+ if request .Method == http .MethodOptions {
319+ return
320+ }
321+
304322 vars := mux .Vars (request )
305323 name , ok := vars ["name" ]
306324
@@ -367,6 +385,13 @@ type ExtensionList struct {
367385// @Failure 500 {string} string "internal server error"
368386// @Router /v2/{name}/manifests/{reference} [get].
369387func (rh * RouteHandler ) GetManifest (response http.ResponseWriter , request * http.Request ) {
388+ response .Header ().Set ("Access-Control-Allow-Methods" , "HEAD,GET,POST,OPTIONS" )
389+ response .Header ().Set ("Access-Control-Allow-Headers" , "Authorization,content-type" )
390+
391+ if request .Method == http .MethodOptions {
392+ return
393+ }
394+
370395 vars := mux .Vars (request )
371396 name , ok := vars ["name" ]
372397
@@ -468,6 +493,13 @@ func getReferrers(ctx context.Context, routeHandler *RouteHandler,
468493// @Failure 500 {string} string "internal server error"
469494// @Router /v2/{name}/references/{digest} [get].
470495func (rh * RouteHandler ) GetReferrers (response http.ResponseWriter , request * http.Request ) {
496+ response .Header ().Set ("Access-Control-Allow-Methods" , "HEAD,GET,POST,OPTIONS" )
497+ response .Header ().Set ("Access-Control-Allow-Headers" , "Authorization,content-type" )
498+
499+ if request .Method == http .MethodOptions {
500+ return
501+ }
502+
471503 vars := mux .Vars (request )
472504
473505 name , ok := vars ["name" ]
@@ -1501,6 +1533,13 @@ type RepositoryList struct {
15011533// @Failure 500 {string} string "internal server error"
15021534// @Router /v2/_catalog [get].
15031535func (rh * RouteHandler ) ListRepositories (response http.ResponseWriter , request * http.Request ) {
1536+ response .Header ().Set ("Access-Control-Allow-Methods" , "HEAD,GET,POST,OPTIONS" )
1537+ response .Header ().Set ("Access-Control-Allow-Headers" , "Authorization,content-type" )
1538+
1539+ if request .Method == http .MethodOptions {
1540+ return
1541+ }
1542+
15041543 combineRepoList := make ([]string , 0 )
15051544
15061545 subStore := rh .c .StoreController .SubStore
@@ -1560,6 +1599,13 @@ func (rh *RouteHandler) ListRepositories(response http.ResponseWriter, request *
15601599// @Success 200 {object} api.ExtensionList
15611600// @Router /v2/_oci/ext/discover [get].
15621601func (rh * RouteHandler ) ListExtensions (w http.ResponseWriter , r * http.Request ) {
1602+ w .Header ().Set ("Access-Control-Allow-Methods" , "HEAD,GET,POST,OPTIONS" )
1603+ w .Header ().Set ("Access-Control-Allow-Headers" , "Authorization,content-type" )
1604+
1605+ if r .Method == http .MethodOptions {
1606+ return
1607+ }
1608+
15631609 extensionList := ext .GetExtensions (rh .c .Config )
15641610
15651611 WriteJSON (w , http .StatusOK , extensionList )
0 commit comments