Support CORS headers to git smart http protocol (#5719)

This commit is contained in:
Lunny Xiao 2019-01-15 05:05:27 +08:00 committed by techknowlogick
parent 5a081c7a80
commit 270fa6d63b
4 changed files with 38 additions and 18 deletions

View File

@ -31,6 +31,9 @@ PULL_REQUEST_QUEUE_LENGTH = 1000
PREFERRED_LICENSES = Apache License 2.0,MIT License PREFERRED_LICENSES = Apache License 2.0,MIT License
; Disable the ability to interact with repositories using the HTTP protocol ; Disable the ability to interact with repositories using the HTTP protocol
DISABLE_HTTP_GIT = false DISABLE_HTTP_GIT = false
; Value for Access-Control-Allow-Origin header, default is not to present
; WARNING: This maybe harmful to you website if you do not give it a right value.
ACCESS_CONTROL_ALLOW_ORIGIN =
; Force ssh:// clone url instead of scp-style uri when default SSH port is used ; Force ssh:// clone url instead of scp-style uri when default SSH port is used
USE_COMPAT_SSH_URI = false USE_COMPAT_SSH_URI = false

View File

@ -62,6 +62,9 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
HTTP protocol. HTTP protocol.
- `USE_COMPAT_SSH_URI`: **false**: Force ssh:// clone url instead of scp-style uri when - `USE_COMPAT_SSH_URI`: **false**: Force ssh:// clone url instead of scp-style uri when
default SSH port is used. default SSH port is used.
- `ACCESS_CONTROL_ALLOW_ORIGIN`: **\<empty\>**: Value for Access-Control-Allow-Origin header,
default is not to present. **WARNING**: This maybe harmful to you website if you do not
give it a right value.
### Repository - Pull Request (`repository.pull-request`) ### Repository - Pull Request (`repository.pull-request`)
- `WORK_IN_PROGRESS_PREFIXES`: **WIP:,\[WIP\]**: List of prefixes used in Pull Request - `WORK_IN_PROGRESS_PREFIXES`: **WIP:,\[WIP\]**: List of prefixes used in Pull Request

View File

@ -201,15 +201,16 @@ var (
// Repository settings // Repository settings
Repository = struct { Repository = struct {
AnsiCharset string AnsiCharset string
ForcePrivate bool ForcePrivate bool
DefaultPrivate string DefaultPrivate string
MaxCreationLimit int MaxCreationLimit int
MirrorQueueLength int MirrorQueueLength int
PullRequestQueueLength int PullRequestQueueLength int
PreferredLicenses []string PreferredLicenses []string
DisableHTTPGit bool DisableHTTPGit bool
UseCompatSSHURI bool AccessControlAllowOrigin string
UseCompatSSHURI bool
// Repository editor settings // Repository editor settings
Editor struct { Editor struct {
@ -237,15 +238,16 @@ var (
WorkInProgressPrefixes []string WorkInProgressPrefixes []string
} `ini:"repository.pull-request"` } `ini:"repository.pull-request"`
}{ }{
AnsiCharset: "", AnsiCharset: "",
ForcePrivate: false, ForcePrivate: false,
DefaultPrivate: RepoCreatingLastUserVisibility, DefaultPrivate: RepoCreatingLastUserVisibility,
MaxCreationLimit: -1, MaxCreationLimit: -1,
MirrorQueueLength: 1000, MirrorQueueLength: 1000,
PullRequestQueueLength: 1000, PullRequestQueueLength: 1000,
PreferredLicenses: []string{"Apache License 2.0,MIT License"}, PreferredLicenses: []string{"Apache License 2.0,MIT License"},
DisableHTTPGit: false, DisableHTTPGit: false,
UseCompatSSHURI: false, AccessControlAllowOrigin: "",
UseCompatSSHURI: false,
// Repository editor settings // Repository editor settings
Editor: struct { Editor: struct {

View File

@ -27,6 +27,18 @@ import (
// HTTP implmentation git smart HTTP protocol // HTTP implmentation git smart HTTP protocol
func HTTP(ctx *context.Context) { func HTTP(ctx *context.Context) {
if len(setting.Repository.AccessControlAllowOrigin) > 0 {
// Set CORS headers for browser-based git clients
ctx.Resp.Header().Set("Access-Control-Allow-Origin", setting.Repository.AccessControlAllowOrigin)
ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent")
// Handle preflight OPTIONS request
if ctx.Req.Method == "OPTIONS" {
ctx.Status(http.StatusOK)
return
}
}
username := ctx.Params(":username") username := ctx.Params(":username")
reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git") reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")