mirror of https://github.com/gogs/gogs.git
repo/http: add CORS headers to allow clone/push from browser agents (#4970)
parent
ba0a78da2a
commit
6a185e94b9
|
@ -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 *****
|
||||
|
|
|
@ -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…
Reference in New Issue