Browse Source

repo/http: add CORS headers to allow clone/push from browser agents (#4970)

pull/5126/head
William Hilton 7 years ago committed by jc
parent
commit
6a185e94b9
  1. 2
      cmd/web.go
  2. 12
      routes/repo/http.go

2
cmd/web.go

@ -637,8 +637,10 @@ func runWeb(c *cli.Context) error {
// e.g. with or without ".git" suffix.
m.Group("/:reponame([\\d\\w-_\\.]+\\.git$)", func() {
m.Get("", ignSignIn, context.RepoAssignment(), context.RepoRef(), repo.Home)
m.Options("/*", ignSignInAndCsrf, repo.HTTPContexter(), repo.HTTP)
m.Route("/*", "GET,POST", ignSignInAndCsrf, repo.HTTPContexter(), repo.HTTP)
})
m.Options("/:reponame/*", ignSignInAndCsrf, repo.HTTPContexter(), repo.HTTP)
m.Route("/:reponame/*", "GET,POST", ignSignInAndCsrf, repo.HTTPContexter(), repo.HTTP)
})
// ***** END: Repository *****

12
routes/repo/http.go

@ -56,6 +56,18 @@ func askCredentials(c *context.Context, status int, text string) {
func HTTPContexter() macaron.Handler {
return func(c *context.Context) {
if len(setting.HTTP.AccessControlAllowOrigin) > 0 {
// Set CORS headers for browser-based git clients
c.Resp.Header().Set("Access-Control-Allow-Origin", setting.HTTP.AccessControlAllowOrigin)
c.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Handle preflight OPTIONS request
if c.Req.Method == "OPTIONS" {
c.Status(http.StatusOK)
return
}
}
ownerName := c.Params(":username")
repoName := strings.TrimSuffix(c.Params(":reponame"), ".git")
repoName = strings.TrimSuffix(repoName, ".wiki")

Loading…
Cancel
Save