Skip to content

Commit 270fa6d

Browse files
lunnytechknowlogick
authored andcommitted
Support CORS headers to git smart http protocol (#5719)
1 parent 5a081c7 commit 270fa6d

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

Diff for: custom/conf/app.ini.sample

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ 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+
; WARNING: This maybe harmful to you website if you do not give it a right value.
36+
ACCESS_CONTROL_ALLOW_ORIGIN =
3437
; Force ssh:// clone url instead of scp-style uri when default SSH port is used
3538
USE_COMPAT_SSH_URI = false
3639

Diff for: docs/content/doc/advanced/config-cheat-sheet.en-us.md

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
6262
HTTP protocol.
6363
- `USE_COMPAT_SSH_URI`: **false**: Force ssh:// clone url instead of scp-style uri when
6464
default SSH port is used.
65+
- `ACCESS_CONTROL_ALLOW_ORIGIN`: **\<empty\>**: Value for Access-Control-Allow-Origin header,
66+
default is not to present. **WARNING**: This maybe harmful to you website if you do not
67+
give it a right value.
6568

6669
### Repository - Pull Request (`repository.pull-request`)
6770
- `WORK_IN_PROGRESS_PREFIXES`: **WIP:,\[WIP\]**: List of prefixes used in Pull Request

Diff for: modules/setting/setting.go

+20-18
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 {

Diff for: routers/repo/http.go

+12
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)