@@ -32,43 +32,31 @@ import (
32
32
"code.gitea.io/gitea/modules/structs"
33
33
"code.gitea.io/gitea/modules/util"
34
34
repo_service "code.gitea.io/gitea/services/repository"
35
+
36
+ "github.com/go-chi/cors"
35
37
)
36
38
37
- // httpBase implementation git smart HTTP protocol
38
- func httpBase (ctx * context.Context ) (h * serviceHandler ) {
39
+ func HTTPGitEnabledHandler (ctx * context.Context ) {
39
40
if setting .Repository .DisableHTTPGit {
40
41
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" ))
46
43
}
44
+ }
47
45
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
+ })
70
52
}
53
+ return func (next http.Handler ) http.Handler {
54
+ return next
55
+ }
56
+ }
71
57
58
+ // httpBase implementation git smart HTTP protocol
59
+ func httpBase (ctx * context.Context ) (h * serviceHandler ) {
72
60
username := ctx .Params (":username" )
73
61
reponame := strings .TrimSuffix (ctx .Params (":reponame" ), ".git" )
74
62
0 commit comments