-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
78 lines (66 loc) · 1.71 KB
/
handler.go
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
package main
import (
"fmt"
"net/http"
"net/http/cgi"
)
func unauthorized(w http.ResponseWriter, reason string) {
w.Header().Set("WWW-Authenticate", `Basic realm="Login Please"`)
w.WriteHeader(401)
content := "401 Unauthorized\n"
if reason != "" {
content += reason + "\n"
}
w.Write([]byte(content))
}
func auth(w http.ResponseWriter, req *http.Request) bool {
if config.Username == "" {
return true
}
username, password, ok := req.BasicAuth()
if !ok {
unauthorized(w, "")
return false
}
if username != config.Username {
unauthorized(w, "Unknown user")
return false
}
if password != config.Password {
unauthorized(w, "Wrong Password")
return false
}
return true
}
func spawn(w http.ResponseWriter, req *http.Request) {
ch := cgi.Handler{
Path: config.Bin,
Dir: `.`,
Env: func() (env []string) {
env = append(env, fmt.Sprintf("REMOTE_USER=%s", config.Username))
env = append(env, "GIT_HTTP_EXPORT_ALL=")
env = append(env, fmt.Sprintf("GIT_PROJECT_ROOT=%s", config.Root))
return
}(),
}
// net/http/cgi/host.go:122
// Chunked request bodies are not supported by CGI.
//
// error: RPC failed; HTTP 400 curl 22 The requested URL returned error: 400
// fatal: the remote end hung up unexpectedly
//
// https://github.com/git/git/blob/master/Documentation/config/http.txt#L216
// https://gitlab.com/gitlab-org/gitlab/-/issues/17649
// https://github.com/golang/go/issues/5613
fixChunked(req)
ch.ServeHTTP(w, req)
}
func fixChunked(req *http.Request) {
if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == `chunked` {
// hacking!
req.TransferEncoding = nil
req.Header.Set(`Transfer-Encoding`, `chunked`)
// let cgi use Body.
req.ContentLength = -1
}
}