mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-17 03:43:23 +02:00
fix httpBase logic
This commit is contained in:
parent
c2860491c2
commit
c2e9a7dfb8
@ -10,15 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func addOwnerRepoGitHTTPRouters(m *web.Router) {
|
func addOwnerRepoGitHTTPRouters(m *web.Router) {
|
||||||
presetGitService := func(service string) func(ctx *context.Context) {
|
|
||||||
return func(ctx *context.Context) { ctx.SetPathParam("preset-git-service", service) }
|
|
||||||
}
|
|
||||||
// Some users want to use "web-based git client" to access Gitea's repositories,
|
// Some users want to use "web-based git client" to access Gitea's repositories,
|
||||||
// so the CORS handler and OPTIONS method are used.
|
// so the CORS handler and OPTIONS method are used.
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
m.Methods("POST,OPTIONS", "/git-upload-pack", presetGitService("git-upload-pack"), repo.ServiceUploadPack)
|
m.Methods("POST,OPTIONS", "/git-upload-pack", repo.ServiceUploadPack)
|
||||||
m.Methods("POST,OPTIONS", "/git-receive-pack", presetGitService("git-receive-pack"), repo.ServiceReceivePack)
|
m.Methods("POST,OPTIONS", "/git-receive-pack", repo.ServiceReceivePack)
|
||||||
m.Methods("POST,OPTIONS", "/git-upload-archive", presetGitService("git-upload-archive"), repo.ServiceUploadArchive)
|
m.Methods("POST,OPTIONS", "/git-upload-archive", repo.ServiceUploadArchive)
|
||||||
m.Methods("GET,OPTIONS", "/info/refs", repo.GetInfoRefs)
|
m.Methods("GET,OPTIONS", "/info/refs", repo.GetInfoRefs)
|
||||||
m.Methods("GET,OPTIONS", "/HEAD", repo.GetTextFile("HEAD"))
|
m.Methods("GET,OPTIONS", "/HEAD", repo.GetTextFile("HEAD"))
|
||||||
m.Methods("GET,OPTIONS", "/objects/info/alternates", repo.GetTextFile("objects/info/alternates"))
|
m.Methods("GET,OPTIONS", "/objects/info/alternates", repo.GetTextFile("objects/info/alternates"))
|
||||||
|
|||||||
@ -30,6 +30,7 @@ import (
|
|||||||
repo_module "code.gitea.io/gitea/modules/repository"
|
repo_module "code.gitea.io/gitea/modules/repository"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/structs"
|
"code.gitea.io/gitea/modules/structs"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
"code.gitea.io/gitea/services/context"
|
"code.gitea.io/gitea/services/context"
|
||||||
repo_service "code.gitea.io/gitea/services/repository"
|
repo_service "code.gitea.io/gitea/services/repository"
|
||||||
|
|
||||||
@ -55,8 +56,9 @@ func CorsHandler() func(next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// httpBase implementation git smart HTTP protocol
|
// httpBase does the common work for git http services,
|
||||||
func httpBase(ctx *context.Context) *serviceHandler {
|
// including early response, authentication, repository lookup and permission check.
|
||||||
|
func httpBase(ctx *context.Context, optGitService ...string) *serviceHandler {
|
||||||
username := ctx.PathParam("username")
|
username := ctx.PathParam("username")
|
||||||
reponame := strings.TrimSuffix(ctx.PathParam("reponame"), ".git")
|
reponame := strings.TrimSuffix(ctx.PathParam("reponame"), ".git")
|
||||||
|
|
||||||
@ -67,8 +69,7 @@ func httpBase(ctx *context.Context) *serviceHandler {
|
|||||||
|
|
||||||
var serviceType string
|
var serviceType string
|
||||||
var isPull, receivePack bool
|
var isPull, receivePack bool
|
||||||
gitService := ctx.FormString("service", ctx.PathParam("preset-git-service"))
|
switch util.OptionalArg(optGitService) {
|
||||||
switch gitService {
|
|
||||||
case "git-receive-pack":
|
case "git-receive-pack":
|
||||||
serviceType = ServiceTypeReceivePack
|
serviceType = ServiceTypeReceivePack
|
||||||
receivePack = true
|
receivePack = true
|
||||||
@ -78,8 +79,11 @@ func httpBase(ctx *context.Context) *serviceHandler {
|
|||||||
case "git-upload-archive":
|
case "git-upload-archive":
|
||||||
serviceType = ServiceTypeUploadArchive
|
serviceType = ServiceTypeUploadArchive
|
||||||
isPull = true
|
isPull = true
|
||||||
default:
|
case "":
|
||||||
isPull = ctx.Req.Method == http.MethodHead || ctx.Req.Method == http.MethodGet
|
isPull = ctx.Req.Method == http.MethodHead || ctx.Req.Method == http.MethodGet
|
||||||
|
default: // unknown service
|
||||||
|
ctx.Resp.WriteHeader(http.StatusBadRequest)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var accessMode perm.AccessMode
|
var accessMode perm.AccessMode
|
||||||
@ -396,8 +400,12 @@ func prepareGitCmdWithAllowedService(service string, allowedServices []string) *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serviceRPC(ctx *context.Context, h *serviceHandler, service string) {
|
func serviceRPC(ctx *context.Context, service string) {
|
||||||
defer ctx.Req.Body.Close()
|
defer ctx.Req.Body.Close()
|
||||||
|
h := httpBase(ctx, "git-"+service)
|
||||||
|
if h == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
expectedContentType := fmt.Sprintf("application/x-git-%s-request", service)
|
expectedContentType := fmt.Sprintf("application/x-git-%s-request", service)
|
||||||
if ctx.Req.Header.Get("Content-Type") != expectedContentType {
|
if ctx.Req.Header.Get("Content-Type") != expectedContentType {
|
||||||
@ -458,25 +466,16 @@ const (
|
|||||||
|
|
||||||
// ServiceUploadPack implements Git Smart HTTP protocol
|
// ServiceUploadPack implements Git Smart HTTP protocol
|
||||||
func ServiceUploadPack(ctx *context.Context) {
|
func ServiceUploadPack(ctx *context.Context) {
|
||||||
h := httpBase(ctx)
|
serviceRPC(ctx, ServiceTypeUploadPack)
|
||||||
if h != nil {
|
|
||||||
serviceRPC(ctx, h, ServiceTypeUploadPack)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceReceivePack implements Git Smart HTTP protocol
|
// ServiceReceivePack implements Git Smart HTTP protocol
|
||||||
func ServiceReceivePack(ctx *context.Context) {
|
func ServiceReceivePack(ctx *context.Context) {
|
||||||
h := httpBase(ctx)
|
serviceRPC(ctx, ServiceTypeReceivePack)
|
||||||
if h != nil {
|
|
||||||
serviceRPC(ctx, h, ServiceTypeReceivePack)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServiceUploadArchive(ctx *context.Context) {
|
func ServiceUploadArchive(ctx *context.Context) {
|
||||||
h := httpBase(ctx)
|
serviceRPC(ctx, ServiceTypeUploadArchive)
|
||||||
if h != nil {
|
|
||||||
serviceRPC(ctx, h, ServiceTypeUploadArchive)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func packetWrite(str string) []byte {
|
func packetWrite(str string) []byte {
|
||||||
@ -489,7 +488,7 @@ func packetWrite(str string) []byte {
|
|||||||
|
|
||||||
// GetInfoRefs implements Git dumb HTTP
|
// GetInfoRefs implements Git dumb HTTP
|
||||||
func GetInfoRefs(ctx *context.Context) {
|
func GetInfoRefs(ctx *context.Context) {
|
||||||
h := httpBase(ctx)
|
h := httpBase(ctx, ctx.FormString("service")) // git http protocol: "?service=git-<service>"
|
||||||
if h == nil {
|
if h == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user