|
|
|
@ -32,43 +32,31 @@ import (
|
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/cors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// httpBase implementation git smart HTTP protocol
|
|
|
|
|
func httpBase(ctx *context.Context) (h *serviceHandler) {
|
|
|
|
|
func HTTPGitEnabledHandler(ctx *context.Context) {
|
|
|
|
|
if setting.Repository.DisableHTTPGit {
|
|
|
|
|
ctx.Resp.WriteHeader(http.StatusForbidden)
|
|
|
|
|
_, err := ctx.Resp.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(err.Error())
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
_, _ = ctx.Resp.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(setting.Repository.AccessControlAllowOrigin) > 0 {
|
|
|
|
|
allowedOrigin := setting.Repository.AccessControlAllowOrigin
|
|
|
|
|
// Set CORS headers for browser-based git clients
|
|
|
|
|
ctx.Resp.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
|
|
|
|
|
ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent")
|
|
|
|
|
|
|
|
|
|
// Handle preflight OPTIONS request
|
|
|
|
|
if ctx.Req.Method == "OPTIONS" {
|
|
|
|
|
if allowedOrigin == "*" {
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
|
} else if allowedOrigin == "null" {
|
|
|
|
|
ctx.Status(http.StatusForbidden)
|
|
|
|
|
} else {
|
|
|
|
|
origin := ctx.Req.Header.Get("Origin")
|
|
|
|
|
if len(origin) > 0 && origin == allowedOrigin {
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Status(http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
func CorsHandler() func(next http.Handler) http.Handler {
|
|
|
|
|
if setting.Repository.AccessControlAllowOrigin != "" {
|
|
|
|
|
return cors.Handler(cors.Options{
|
|
|
|
|
AllowedOrigins: []string{setting.Repository.AccessControlAllowOrigin},
|
|
|
|
|
AllowedHeaders: []string{"Content-Type", "Authorization", "User-Agent"},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
|
return next
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// httpBase implementation git smart HTTP protocol
|
|
|
|
|
func httpBase(ctx *context.Context) (h *serviceHandler) {
|
|
|
|
|
username := ctx.Params(":username")
|
|
|
|
|
reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
|
|
|
|
|
|
|
|
|
|