@@ -32,43 +32,31 @@ import (
3232 "code.gitea.io/gitea/modules/structs"
3333 "code.gitea.io/gitea/modules/util"
3434 repo_service "code.gitea.io/gitea/services/repository"
35+
36+ "github.com/go-chi/cors"
3537)
3638
37- // httpBase implementation git smart HTTP protocol
38- func httpBase (ctx * context.Context ) (h * serviceHandler ) {
39+ func HTTPGitEnabledHandler (ctx * context.Context ) {
3940 if setting .Repository .DisableHTTPGit {
4041 ctx .Resp .WriteHeader (http .StatusForbidden )
41- _ , err := ctx .Resp .Write ([]byte ("Interacting with repositories by HTTP protocol is not allowed" ))
42- if err != nil {
43- log .Error (err .Error ())
44- }
45- return
42+ _ , _ = ctx .Resp .Write ([]byte ("Interacting with repositories by HTTP protocol is not allowed" ))
4643 }
44+ }
4745
48- if len (setting .Repository .AccessControlAllowOrigin ) > 0 {
49- allowedOrigin := setting .Repository .AccessControlAllowOrigin
50- // Set CORS headers for browser-based git clients
51- ctx .Resp .Header ().Set ("Access-Control-Allow-Origin" , allowedOrigin )
52- ctx .Resp .Header ().Set ("Access-Control-Allow-Headers" , "Content-Type, Authorization, User-Agent" )
53-
54- // Handle preflight OPTIONS request
55- if ctx .Req .Method == "OPTIONS" {
56- if allowedOrigin == "*" {
57- ctx .Status (http .StatusOK )
58- } else if allowedOrigin == "null" {
59- ctx .Status (http .StatusForbidden )
60- } else {
61- origin := ctx .Req .Header .Get ("Origin" )
62- if len (origin ) > 0 && origin == allowedOrigin {
63- ctx .Status (http .StatusOK )
64- } else {
65- ctx .Status (http .StatusForbidden )
66- }
67- }
68- return
69- }
46+ func CorsHandler () func (next http.Handler ) http.Handler {
47+ if setting .Repository .AccessControlAllowOrigin != "" {
48+ return cors .Handler (cors.Options {
49+ AllowedOrigins : []string {setting .Repository .AccessControlAllowOrigin },
50+ AllowedHeaders : []string {"Content-Type" , "Authorization" , "User-Agent" },
51+ })
7052 }
53+ return func (next http.Handler ) http.Handler {
54+ return next
55+ }
56+ }
7157
58+ // httpBase implementation git smart HTTP protocol
59+ func httpBase (ctx * context.Context ) (h * serviceHandler ) {
7260 username := ctx .Params (":username" )
7361 reponame := strings .TrimSuffix (ctx .Params (":reponame" ), ".git" )
7462
0 commit comments