summaryrefslogtreecommitdiffstats
path: root/routers/repo/http.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-11-22 00:24:43 +0800
committertechknowlogick <techknowlogick@gitea.io>2019-11-21 11:24:43 -0500
commitd5261b9aab83138e31cbe02b1da9adc658ed400f (patch)
tree79c8a22bcd0d9e16c915eef316b5791d9f317b99 /routers/repo/http.go
parent51ed4cc0636bc5004e68e7f851fd49b62a0adabf (diff)
downloadgitea-d5261b9aab83138e31cbe02b1da9adc658ed400f.tar.gz
gitea-d5261b9aab83138e31cbe02b1da9adc658ed400f.zip
Move HttpBackend function to Http to reduce function calls when git smart http requests (#9057)
Diffstat (limited to 'routers/repo/http.go')
-rw-r--r--routers/repo/http.go97
1 files changed, 47 insertions, 50 deletions
diff --git a/routers/repo/http.go b/routers/repo/http.go
index d41c63ba35..63cce0f713 100644
--- a/routers/repo/http.go
+++ b/routers/repo/http.go
@@ -277,11 +277,56 @@ func HTTP(ctx *context.Context) {
}
}
- HTTPBackend(ctx, &serviceConfig{
+ w := ctx.Resp
+ r := ctx.Req.Request
+ cfg := &serviceConfig{
UploadPack: true,
ReceivePack: true,
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 {
@@ -522,51 +567,3 @@ func getGitRepoPath(subdir string) (string, error) {
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)
- }
-}