-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.go
140 lines (127 loc) · 4.25 KB
/
git.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package triton
import (
"compress/gzip"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
)
const (
gitPathInfoRefs = "info/refs"
gitPathHead = "HEAD"
gitServiceQuery = "service"
gitUploadPack = "git-upload-pack"
gitUploadPackResult = "application/x-git-upload-pack-result"
gitUploadPackAdvertisement = "application/x-git-upload-pack-advertisement"
gitCommand = "git"
gitUploadPackCommand = "upload-pack"
gitStatelessOption = "--stateless-rpc"
gitAdvertiseOption = "--advertise-refs"
gitServiceAdvertisementHeader = "# service="
gitPacketFlush = "0000"
httpHeaderContentType = "Content-Type"
httpHeaderContentEncoding = "Content-Encoding"
httpHeaderExpires = "Expires"
httpHeaderPragma = "Pragma"
httpHeaderCacheControl = "Cache-Control"
httpHeaderContentLength = "Content-Length"
httpHeaderLastModified = "Last-Modified"
gzipEncoding = "gzip"
plainText = "text/plain"
neverExpire = "Fri, 01 Jan 1980 00:00:00 GMT"
noCachePragma = "no-cache"
noCacheControl = "no-cache, max-age=0, must-revalidate"
)
func addGitHandlers(mux *http.ServeMux, gitDir string, fullPathGitDir string) {
mux.HandleFunc(gitDir, func(wr http.ResponseWriter, req *http.Request) {
serveGitRequest(wr, req, fullPathGitDir)
})
mux.HandleFunc(gitDir+gitPathInfoRefs, func(wr http.ResponseWriter, req *http.Request) {
serveInfoRefs(wr, req, gitDir)
})
mux.HandleFunc(gitDir+gitPathHead, func(wr http.ResponseWriter, req *http.Request) {
serveTextFileNoCaching(wr, req, fullPathGitDir+gitPathHead)
})
}
func serveGitRequest(wr http.ResponseWriter, req *http.Request, path string) {
// TODO: Metrics
fmt.Println("serveGitRequest", req.RequestURI, path)
serviceType := req.URL.Query().Get(gitServiceQuery)
if gitUploadPack == serviceType {
serveUploadPack(wr, req, path)
} else {
wr.WriteHeader(http.StatusBadRequest)
}
}
func serveInfoRefs(wr http.ResponseWriter, req *http.Request, path string) {
fmt.Println("serveInfoRefs", path)
packCmd := exec.Command(gitCommand, gitUploadPackCommand, gitStatelessOption, gitAdvertiseOption, path)
packCmd.Dir = path
if refs, err := packCmd.Output(); err != nil {
// TODO
fmt.Println("packCmd", err)
} else {
noCaching(wr)
wr.Header().Set(httpHeaderContentType, gitUploadPackAdvertisement)
wr.WriteHeader(http.StatusOK)
wr.Write(gitPacketString(gitServiceAdvertisementHeader + gitUploadPackCommand + "\n"))
wr.Write([]byte(gitPacketFlush))
wr.Write(refs)
}
}
func serveUploadPack(wr http.ResponseWriter, req *http.Request, path string) {
body := req.Body
if gzipEncoding == req.Header.Get(httpHeaderContentEncoding) {
if gzipBody, err := gzip.NewReader(body); err != nil {
// TODO
fmt.Println("gzip", err)
return
} else {
body = gzipBody
}
}
wr.Header().Set(httpHeaderContentType, gitUploadPackResult)
packCmd := exec.Command(gitCommand, gitUploadPackCommand, gitStatelessOption, path)
packCmd.Dir = path
packCmd.Stdin = body
packCmd.Stdout = wr
if err := packCmd.Run(); err != nil {
// TODO
fmt.Println("packCmd", err)
}
}
func serveTextFileNoCaching(wr http.ResponseWriter, req *http.Request, file string) {
fmt.Println("serveTextFileNoCaching", file)
noCaching(wr)
serveFile(plainText, wr, req, file)
}
func serveFile(contentType string, wr http.ResponseWriter, req *http.Request, file string) {
fileStats, err := os.Stat(file)
if os.IsNotExist(err) {
// TODO
fmt.Println("serveFile", err)
return
} else if err != nil {
// TODO
fmt.Println("serveFile", err)
return
}
wr.Header().Set(httpHeaderContentType, contentType)
wr.Header().Set(httpHeaderContentLength, strconv.Itoa(int(fileStats.Size())))
wr.Header().Set(httpHeaderLastModified, fileStats.ModTime().Format(http.TimeFormat))
http.ServeFile(wr, req, file)
}
func gitPacketString(str string) []byte {
strLen := int64(len(str)) + 4
octalLen := strconv.FormatInt(strLen, 16)
padding := 4 - len(octalLen)%4
octalLen = strings.Repeat("0", padding) + octalLen
return []byte(octalLen + str)
}
func noCaching(wr http.ResponseWriter) {
wr.Header().Set(httpHeaderExpires, neverExpire)
wr.Header().Set(httpHeaderPragma, noCachePragma)
wr.Header().Set(httpHeaderCacheControl, noCacheControl)
}