Skip to content

Commit d5261b9

Browse files
lunnytechknowlogick
authored andcommitted
Move HttpBackend function to Http to reduce function calls when git smart http requests (#9057)
1 parent 51ed4cc commit d5261b9

File tree

1 file changed

+47
-50
lines changed

1 file changed

+47
-50
lines changed

routers/repo/http.go

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,56 @@ func HTTP(ctx *context.Context) {
277277
}
278278
}
279279

280-
HTTPBackend(ctx, &serviceConfig{
280+
w := ctx.Resp
281+
r := ctx.Req.Request
282+
cfg := &serviceConfig{
281283
UploadPack: true,
282284
ReceivePack: true,
283285
Env: environ,
284-
})(ctx.Resp, ctx.Req.Request)
286+
}
287+
288+
for _, route := range routes {
289+
r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name
290+
if m := route.reg.FindStringSubmatch(r.URL.Path); m != nil {
291+
if setting.Repository.DisableHTTPGit {
292+
w.WriteHeader(http.StatusForbidden)
293+
_, err := w.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
294+
if err != nil {
295+
log.Error(err.Error())
296+
}
297+
return
298+
}
299+
if route.method != r.Method {
300+
if r.Proto == "HTTP/1.1" {
301+
w.WriteHeader(http.StatusMethodNotAllowed)
302+
_, err := w.Write([]byte("Method Not Allowed"))
303+
if err != nil {
304+
log.Error(err.Error())
305+
}
306+
} else {
307+
w.WriteHeader(http.StatusBadRequest)
308+
_, err := w.Write([]byte("Bad Request"))
309+
if err != nil {
310+
log.Error(err.Error())
311+
}
312+
}
313+
return
314+
}
315+
316+
file := strings.Replace(r.URL.Path, m[1]+"/", "", 1)
317+
dir, err := getGitRepoPath(m[1])
318+
if err != nil {
319+
log.Error(err.Error())
320+
ctx.NotFound("Smart Git HTTP", err)
321+
return
322+
}
323+
324+
route.handler(serviceHandler{cfg, w, r, dir, file, cfg.Env})
325+
return
326+
}
327+
}
328+
329+
ctx.NotFound("Smart Git HTTP", nil)
285330
}
286331

287332
type serviceConfig struct {
@@ -522,51 +567,3 @@ func getGitRepoPath(subdir string) (string, error) {
522567

523568
return fpath, nil
524569
}
525-
526-
// HTTPBackend middleware for git smart HTTP protocol
527-
func HTTPBackend(ctx *context.Context, cfg *serviceConfig) http.HandlerFunc {
528-
return func(w http.ResponseWriter, r *http.Request) {
529-
for _, route := range routes {
530-
r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name
531-
if m := route.reg.FindStringSubmatch(r.URL.Path); m != nil {
532-
if setting.Repository.DisableHTTPGit {
533-
w.WriteHeader(http.StatusForbidden)
534-
_, err := w.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
535-
if err != nil {
536-
log.Error(err.Error())
537-
}
538-
return
539-
}
540-
if route.method != r.Method {
541-
if r.Proto == "HTTP/1.1" {
542-
w.WriteHeader(http.StatusMethodNotAllowed)
543-
_, err := w.Write([]byte("Method Not Allowed"))
544-
if err != nil {
545-
log.Error(err.Error())
546-
}
547-
} else {
548-
w.WriteHeader(http.StatusBadRequest)
549-
_, err := w.Write([]byte("Bad Request"))
550-
if err != nil {
551-
log.Error(err.Error())
552-
}
553-
}
554-
return
555-
}
556-
557-
file := strings.Replace(r.URL.Path, m[1]+"/", "", 1)
558-
dir, err := getGitRepoPath(m[1])
559-
if err != nil {
560-
log.Error(err.Error())
561-
ctx.NotFound("HTTPBackend", err)
562-
return
563-
}
564-
565-
route.handler(serviceHandler{cfg, w, r, dir, file, cfg.Env})
566-
return
567-
}
568-
}
569-
570-
ctx.NotFound("HTTPBackend", nil)
571-
}
572-
}

0 commit comments

Comments
 (0)