Skip to content

Commit 3e2b152

Browse files
committed
Support CORS headers to git smart http protocol
1 parent 6564564 commit 3e2b152

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

custom/conf/app.ini.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ PULL_REQUEST_QUEUE_LENGTH = 1000
3131
PREFERRED_LICENSES = Apache License 2.0,MIT License
3232
; Disable the ability to interact with repositories using the HTTP protocol
3333
DISABLE_HTTP_GIT = false
34+
; Value for Access-Control-Allow-Origin header, default is not to present
35+
ACCESS_CONTROL_ALLOW_ORIGIN =
3436
; Force ssh:// clone url instead of scp-style uri when default SSH port is used
3537
USE_COMPAT_SSH_URI = false
3638

modules/setting/setting.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,16 @@ var (
201201

202202
// Repository settings
203203
Repository = struct {
204-
AnsiCharset string
205-
ForcePrivate bool
206-
DefaultPrivate string
207-
MaxCreationLimit int
208-
MirrorQueueLength int
209-
PullRequestQueueLength int
210-
PreferredLicenses []string
211-
DisableHTTPGit bool
212-
UseCompatSSHURI bool
204+
AnsiCharset string
205+
ForcePrivate bool
206+
DefaultPrivate string
207+
MaxCreationLimit int
208+
MirrorQueueLength int
209+
PullRequestQueueLength int
210+
PreferredLicenses []string
211+
DisableHTTPGit bool
212+
AccessControlAllowOrigin string
213+
UseCompatSSHURI bool
213214

214215
// Repository editor settings
215216
Editor struct {
@@ -237,15 +238,16 @@ var (
237238
WorkInProgressPrefixes []string
238239
} `ini:"repository.pull-request"`
239240
}{
240-
AnsiCharset: "",
241-
ForcePrivate: false,
242-
DefaultPrivate: RepoCreatingLastUserVisibility,
243-
MaxCreationLimit: -1,
244-
MirrorQueueLength: 1000,
245-
PullRequestQueueLength: 1000,
246-
PreferredLicenses: []string{"Apache License 2.0,MIT License"},
247-
DisableHTTPGit: false,
248-
UseCompatSSHURI: false,
241+
AnsiCharset: "",
242+
ForcePrivate: false,
243+
DefaultPrivate: RepoCreatingLastUserVisibility,
244+
MaxCreationLimit: -1,
245+
MirrorQueueLength: 1000,
246+
PullRequestQueueLength: 1000,
247+
PreferredLicenses: []string{"Apache License 2.0,MIT License"},
248+
DisableHTTPGit: false,
249+
AccessControlAllowOrigin: "",
250+
UseCompatSSHURI: false,
249251

250252
// Repository editor settings
251253
Editor: struct {

routers/repo/http.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ import (
2727

2828
// HTTP implmentation git smart HTTP protocol
2929
func HTTP(ctx *context.Context) {
30+
if len(setting.Repository.AccessControlAllowOrigin) > 0 {
31+
// Set CORS headers for browser-based git clients
32+
ctx.Resp.Header().Set("Access-Control-Allow-Origin", setting.Repository.AccessControlAllowOrigin)
33+
ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent")
34+
35+
// Handle preflight OPTIONS request
36+
if ctx.Req.Method == "OPTIONS" {
37+
ctx.Status(http.StatusOK)
38+
return
39+
}
40+
}
41+
3042
username := ctx.Params(":username")
3143
reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
3244

0 commit comments

Comments
 (0)