]> source.dussan.org Git - gitea.git/commitdiff
Restore CORS on git smart http protocol (#16496) (#16506)
authorzeripath <art27@cantab.net>
Wed, 21 Jul 2021 14:03:02 +0000 (15:03 +0100)
committerGitHub <noreply@github.com>
Wed, 21 Jul 2021 14:03:02 +0000 (15:03 +0100)
Backport #16496

Unfortunately the chi changes have resulted in the CORS headers for the
git smart http protocol going missing.

This is mostly because the OPTIONS method is not being handled by
httpBase anymore.

This PR adds a GetOptions, PostOptions and Options methods to web
handler to allow OPTIONS method requests to still reach the httpBase
function.

Fix #16350
Close #16491

Signed-off-by: Andrew Thornton <art27@cantab.net>
modules/web/route.go
routers/web/web.go

index 319d08f5981c169f2f21a972f5b2567f6c812044..3c6513da626ce2226d8b1a43c0cfbbf17d996aa7 100644 (file)
@@ -269,6 +269,26 @@ func (r *Route) Get(pattern string, h ...interface{}) {
        r.R.Get(r.getPattern(pattern), Wrap(middlewares...))
 }
 
+// Options delegate options method
+func (r *Route) Options(pattern string, h ...interface{}) {
+       var middlewares = r.getMiddlewares(h)
+       r.R.Options(r.getPattern(pattern), Wrap(middlewares...))
+}
+
+// GetOptions delegate get and options method
+func (r *Route) GetOptions(pattern string, h ...interface{}) {
+       var middlewares = r.getMiddlewares(h)
+       r.R.Get(r.getPattern(pattern), Wrap(middlewares...))
+       r.R.Options(r.getPattern(pattern), Wrap(middlewares...))
+}
+
+// PostOptions delegate post and options method
+func (r *Route) PostOptions(pattern string, h ...interface{}) {
+       var middlewares = r.getMiddlewares(h)
+       r.R.Post(r.getPattern(pattern), Wrap(middlewares...))
+       r.R.Options(r.getPattern(pattern), Wrap(middlewares...))
+}
+
 // Head delegate head method
 func (r *Route) Head(pattern string, h ...interface{}) {
        var middlewares = r.getMiddlewares(h)
index 7a47f479c0da305bc859258c164a708151effcf9..9baef52962c9d62c3ad8ea43838833bcead131d2 100644 (file)
@@ -1006,17 +1006,17 @@ func RegisterRoutes(m *web.Route) {
                        }, ignSignInAndCsrf, lfsServerEnabled)
 
                        m.Group("", func() {
-                               m.Post("/git-upload-pack", repo.ServiceUploadPack)
-                               m.Post("/git-receive-pack", repo.ServiceReceivePack)
-                               m.Get("/info/refs", repo.GetInfoRefs)
-                               m.Get("/HEAD", repo.GetTextFile("HEAD"))
-                               m.Get("/objects/info/alternates", repo.GetTextFile("objects/info/alternates"))
-                               m.Get("/objects/info/http-alternates", repo.GetTextFile("objects/info/http-alternates"))
-                               m.Get("/objects/info/packs", repo.GetInfoPacks)
-                               m.Get("/objects/info/{file:[^/]*}", repo.GetTextFile(""))
-                               m.Get("/objects/{head:[0-9a-f]{2}}/{hash:[0-9a-f]{38}}", repo.GetLooseObject)
-                               m.Get("/objects/pack/pack-{file:[0-9a-f]{40}}.pack", repo.GetPackFile)
-                               m.Get("/objects/pack/pack-{file:[0-9a-f]{40}}.idx", repo.GetIdxFile)
+                               m.PostOptions("/git-upload-pack", repo.ServiceUploadPack)
+                               m.PostOptions("/git-receive-pack", repo.ServiceReceivePack)
+                               m.GetOptions("/info/refs", repo.GetInfoRefs)
+                               m.GetOptions("/HEAD", repo.GetTextFile("HEAD"))
+                               m.GetOptions("/objects/info/alternates", repo.GetTextFile("objects/info/alternates"))
+                               m.GetOptions("/objects/info/http-alternates", repo.GetTextFile("objects/info/http-alternates"))
+                               m.GetOptions("/objects/info/packs", repo.GetInfoPacks)
+                               m.GetOptions("/objects/info/{file:[^/]*}", repo.GetTextFile(""))
+                               m.GetOptions("/objects/{head:[0-9a-f]{2}}/{hash:[0-9a-f]{38}}", repo.GetLooseObject)
+                               m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.pack", repo.GetPackFile)
+                               m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.idx", repo.GetIdxFile)
                        }, ignSignInAndCsrf)
 
                        m.Head("/tasks/trigger", repo.TriggerTask)