1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package git
import (
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"sync"
"github.com/golang/gddo/httputil"
grpccodes "google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
)
func GetInfoRefsHandler(a *api.API) http.Handler {
return repoPreAuthorizeHandler(a, handleGetInfoRefs)
}
func handleGetInfoRefs(rw http.ResponseWriter, r *http.Request, a *api.Response) {
responseWriter := NewHttpResponseWriter(rw)
// Log 0 bytes in because we ignore the request body (and there usually is none anyway).
defer responseWriter.Log(r, 0)
rpc := getService(r)
if !(rpc == "git-upload-pack" || rpc == "git-receive-pack") {
// The 'dumb' Git HTTP protocol is not supported
http.Error(responseWriter, "Not Found", 404)
return
}
responseWriter.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", rpc))
responseWriter.Header().Set("Cache-Control", "no-cache")
gitProtocol := r.Header.Get("Git-Protocol")
offers := []string{"gzip", "identity"}
encoding := httputil.NegotiateContentEncoding(r, offers)
if err := handleGetInfoRefsWithGitaly(r.Context(), responseWriter, a, rpc, gitProtocol, encoding); err != nil {
status := grpcstatus.Convert(err)
err = fmt.Errorf("handleGetInfoRefs: %v", err)
if status != nil && status.Code() == grpccodes.Unavailable {
helper.CaptureAndFail(responseWriter, r, err, "The git server, Gitaly, is not available at this time. Please contact your administrator.", http.StatusServiceUnavailable)
} else {
helper.Fail500(responseWriter, r, err)
}
}
}
func handleGetInfoRefsWithGitaly(ctx context.Context, responseWriter *HttpResponseWriter, a *api.Response, rpc, gitProtocol, encoding string) error {
ctx, smarthttp, err := gitaly.NewSmartHTTPClient(ctx, a.GitalyServer)
if err != nil {
return err
}
infoRefsResponseReader, err := smarthttp.InfoRefsResponseReader(ctx, &a.Repository, rpc, gitConfigOptions(a), gitProtocol)
if err != nil {
return err
}
var w io.WriteCloser = nopCloser{responseWriter}
if encoding == "gzip" {
gzWriter := getGzWriter(responseWriter)
defer putGzWriter(gzWriter)
w = gzWriter
responseWriter.Header().Set("Content-Encoding", "gzip")
}
if _, err = io.Copy(w, infoRefsResponseReader); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
return nil
}
var gzipPool = &sync.Pool{New: func() interface{} {
// Invariant: the inner writer is io.Discard. We do not want to retain
// response writers of past requests in the pool.
return gzip.NewWriter(io.Discard)
}}
func getGzWriter(w io.Writer) *gzip.Writer {
gzWriter := gzipPool.Get().(*gzip.Writer)
gzWriter.Reset(w)
return gzWriter
}
func putGzWriter(w *gzip.Writer) {
w.Reset(io.Discard) // Maintain pool invariant
gzipPool.Put(w)
}
type nopCloser struct{ io.Writer }
func (nc nopCloser) Close() error { return nil }
|