Browse Source

Able to set custom Access-Control-Allow-Origin header (#3987)

Added new config option '[http] ACCESS_CONTROL_ALLOW_ORIGIN'.
pull/4068/head
Unknwon 8 years ago
parent
commit
5e01ecbc05
No known key found for this signature in database
GPG Key ID: FB9F411CDD69BEC1
  1. 12
      cmd/web.go
  2. 4
      conf/app.ini
  3. 2
      gogs.go
  4. 48
      modules/bindata/bindata.go
  5. 5
      modules/context/context.go
  6. 24
      modules/setting/setting.go
  7. 2
      templates/.VERSION

12
cmd/web.go

@ -116,7 +116,7 @@ func newMacaron() *macaron.Macaron {
if setting.EnableGzip {
m.Use(gzip.Gziper())
}
if setting.Protocol == setting.FCGI {
if setting.Protocol == setting.SCHEME_FCGI {
m.SetURLPrefix(setting.AppSubUrl)
}
m.Use(macaron.Static(
@ -640,7 +640,7 @@ func runWeb(ctx *cli.Context) error {
}
var listenAddr string
if setting.Protocol == setting.UNIX_SOCKET {
if setting.Protocol == setting.SCHEME_UNIX_SOCKET {
listenAddr = fmt.Sprintf("%s", setting.HTTPAddr)
} else {
listenAddr = fmt.Sprintf("%s:%s", setting.HTTPAddr, setting.HTTPPort)
@ -649,14 +649,14 @@ func runWeb(ctx *cli.Context) error {
var err error
switch setting.Protocol {
case setting.HTTP:
case setting.SCHEME_HTTP:
err = http.ListenAndServe(listenAddr, m)
case setting.HTTPS:
case setting.SCHEME_HTTPS:
server := &http.Server{Addr: listenAddr, TLSConfig: &tls.Config{MinVersion: tls.VersionTLS10}, Handler: m}
err = server.ListenAndServeTLS(setting.CertFile, setting.KeyFile)
case setting.FCGI:
case setting.SCHEME_FCGI:
err = fcgi.Serve(nil, m)
case setting.UNIX_SOCKET:
case setting.SCHEME_UNIX_SOCKET:
os.Remove(listenAddr)
var listener *net.UnixListener

4
conf/app.ini

@ -142,6 +142,10 @@ ENABLE_GZIP = false
; Landing page for non-logged users, can be "home" or "explore"
LANDING_PAGE = home
[http]
; Value for Access-Control-Allow-Origin header, default is not to present
ACCESS_CONTROL_ALLOW_ORIGIN =
; Define allowed algorithms and their minimum key length (use -1 to disable a type)
[ssh.minimum_key_sizes]
ED25519 = 256

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting"
)
const APP_VER = "0.9.123.0128"
const APP_VER = "0.9.124.0128"
func init() {
setting.AppVer = APP_VER

48
modules/bindata/bindata.go

File diff suppressed because one or more lines are too long

5
modules/context/context.go

@ -156,6 +156,11 @@ func Contexter() macaron.Handler {
},
Org: &Organization{},
}
if len(setting.HTTP.AccessControlAllowOrigin) > 0 {
ctx.Header().Set("Access-Control-Allow-Origin", setting.HTTP.AccessControlAllowOrigin)
}
// Compute current URL for real-time change language.
ctx.Data["Link"] = setting.AppSubUrl + strings.TrimSuffix(ctx.Req.URL.Path, "/")

24
modules/setting/setting.go

@ -34,10 +34,10 @@ import (
type Scheme string
const (
HTTP Scheme = "http"
HTTPS Scheme = "https"
FCGI Scheme = "fcgi"
UNIX_SOCKET Scheme = "unix"
SCHEME_HTTP Scheme = "http"
SCHEME_HTTPS Scheme = "https"
SCHEME_FCGI Scheme = "fcgi"
SCHEME_UNIX_SOCKET Scheme = "unix"
)
type LandingPage string
@ -74,6 +74,10 @@ var (
LandingPageURL LandingPage
UnixSocketPermission uint32
HTTP struct {
AccessControlAllowOrigin string
}
SSH struct {
Disabled bool `ini:"DISABLE_SSH"`
StartBuiltinServer bool `ini:"START_SSH_SERVER"`
@ -388,15 +392,15 @@ func NewContext() {
AppSubUrl = strings.TrimSuffix(url.Path, "/")
AppSubUrlDepth = strings.Count(AppSubUrl, "/")
Protocol = HTTP
Protocol = SCHEME_HTTP
if sec.Key("PROTOCOL").String() == "https" {
Protocol = HTTPS
Protocol = SCHEME_HTTPS
CertFile = sec.Key("CERT_FILE").String()
KeyFile = sec.Key("KEY_FILE").String()
} else if sec.Key("PROTOCOL").String() == "fcgi" {
Protocol = FCGI
Protocol = SCHEME_FCGI
} else if sec.Key("PROTOCOL").String() == "unix" {
Protocol = UNIX_SOCKET
Protocol = SCHEME_UNIX_SOCKET
UnixSocketPermissionRaw := sec.Key("UNIX_SOCKET_PERMISSION").MustString("666")
UnixSocketPermissionParsed, err := strconv.ParseUint(UnixSocketPermissionRaw, 8, 32)
if err != nil || UnixSocketPermissionParsed > 0777 {
@ -557,7 +561,9 @@ func NewContext() {
}
}
if err = Cfg.Section("ui").MapTo(&UI); err != nil {
if err = Cfg.Section("http").MapTo(&HTTP); err != nil {
log.Fatal(4, "Fail to map HTTP settings: %v", err)
} else if err = Cfg.Section("ui").MapTo(&UI); err != nil {
log.Fatal(4, "Fail to map UI settings: %v", err)
} else if err = Cfg.Section("markdown").MapTo(&Markdown); err != nil {
log.Fatal(4, "Fail to map Markdown settings: %v", err)

2
templates/.VERSION

@ -1 +1 @@
0.9.123.0128
0.9.124.0128
Loading…
Cancel
Save