Move HttpBackend function to Http to reduce function calls when git smart http requests (#9057)
This commit is contained in:
parent
51ed4cc063
commit
d5261b9aab
1 changed files with 47 additions and 50 deletions
|
@ -277,11 +277,56 @@ func HTTP(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPBackend(ctx, &serviceConfig{
|
w := ctx.Resp
|
||||||
|
r := ctx.Req.Request
|
||||||
|
cfg := &serviceConfig{
|
||||||
UploadPack: true,
|
UploadPack: true,
|
||||||
ReceivePack: true,
|
ReceivePack: true,
|
||||||
Env: environ,
|
Env: environ,
|
||||||
})(ctx.Resp, ctx.Req.Request)
|
}
|
||||||
|
|
||||||
|
for _, route := range routes {
|
||||||
|
r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name
|
||||||
|
if m := route.reg.FindStringSubmatch(r.URL.Path); m != nil {
|
||||||
|
if setting.Repository.DisableHTTPGit {
|
||||||
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
_, err := w.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if route.method != r.Method {
|
||||||
|
if r.Proto == "HTTP/1.1" {
|
||||||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
_, err := w.Write([]byte("Method Not Allowed"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_, err := w.Write([]byte("Bad Request"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
file := strings.Replace(r.URL.Path, m[1]+"/", "", 1)
|
||||||
|
dir, err := getGitRepoPath(m[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err.Error())
|
||||||
|
ctx.NotFound("Smart Git HTTP", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
route.handler(serviceHandler{cfg, w, r, dir, file, cfg.Env})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.NotFound("Smart Git HTTP", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
type serviceConfig struct {
|
type serviceConfig struct {
|
||||||
|
@ -522,51 +567,3 @@ func getGitRepoPath(subdir string) (string, error) {
|
||||||
|
|
||||||
return fpath, nil
|
return fpath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPBackend middleware for git smart HTTP protocol
|
|
||||||
func HTTPBackend(ctx *context.Context, cfg *serviceConfig) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
for _, route := range routes {
|
|
||||||
r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name
|
|
||||||
if m := route.reg.FindStringSubmatch(r.URL.Path); m != nil {
|
|
||||||
if setting.Repository.DisableHTTPGit {
|
|
||||||
w.WriteHeader(http.StatusForbidden)
|
|
||||||
_, err := w.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if route.method != r.Method {
|
|
||||||
if r.Proto == "HTTP/1.1" {
|
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
||||||
_, err := w.Write([]byte("Method Not Allowed"))
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err.Error())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
_, err := w.Write([]byte("Bad Request"))
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
file := strings.Replace(r.URL.Path, m[1]+"/", "", 1)
|
|
||||||
dir, err := getGitRepoPath(m[1])
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err.Error())
|
|
||||||
ctx.NotFound("HTTPBackend", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
route.handler(serviceHandler{cfg, w, r, dir, file, cfg.Env})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.NotFound("HTTPBackend", nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Reference in a new issue