This repository has been archived by the owner on Nov 24, 2017. It is now read-only.
/
helpers.go
162 lines (129 loc) · 3.5 KB
/
helpers.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Adapted from https://code.csdn.net/flycutter/git-http-backend.
package git
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
)
// General helpers
func sendFile(contentType string, hr handlerReq) {
w, r := hr.w, hr.r
reqFile := path.Join(hr.Dir, hr.File)
f, err := os.Stat(reqFile)
if os.IsNotExist(err) {
renderNotFound(w)
return
} else {
log.Print("err stating file:", err)
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", fmt.Sprintf("%d", f.Size()))
w.Header().Set("Last-Modified", f.ModTime().Format(http.TimeFormat))
http.ServeFile(w, r, reqFile)
}
func getGitDir(filePath string) (string, error) {
f := path.Join(config.ProjectRoot, filePath)
if _, err := os.Stat(f); os.IsNotExist(err) {
return "", err
}
return f, nil
}
func getServiceType(r *http.Request) string {
serviceType := r.FormValue("service")
if s := strings.HasPrefix(serviceType, "git-"); !s {
return ""
}
return strings.Replace(serviceType, "git-", "", 1)
}
func hasAccess(r *http.Request, dir string, rpc string, checkContentType bool) bool {
if checkContentType {
if r.Header.Get("Content-Type") != fmt.Sprintf("application/x-git-%s-request", rpc) {
return false
}
}
if !(rpc == "upload-pack" || rpc == "receive-pack") {
return false
}
if rpc == "receive-pack" {
return config.ReceivePack
}
if rpc == "upload-pack" {
return config.UploadPack
}
return getConfigSetting(rpc, dir)
}
func updateServerInfo(dir string) []byte {
args := []string{"update-server-info"}
return gitCommand(dir, args...)
}
func getConfigSetting(serviceName string, dir string) bool {
serviceName = strings.Replace(serviceName, "-", "", -1)
setting := getGitConfig("http."+serviceName, dir)
if serviceName == "uploadpack" {
return setting != "false"
}
return setting == "true"
}
func getGitConfig(configName string, dir string) string {
args := []string{"config", configName}
out := string(gitCommand(dir, args...))
return out[0 : len(out)-1]
}
func gitCommand(dir string, args ...string) []byte {
command := exec.Command(config.GitBinPath, args...)
command.Dir = dir
out, err := command.Output()
if err != nil {
// TODO: Properly handle error
log.Print(err)
}
return out
}
// HTTP error response functions
func renderMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
if r.Proto == "HTTP/1.1" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method Not Allowed"))
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request"))
}
}
func renderNotFound(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not Found"))
}
func renderNoAccess(w http.ResponseWriter) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Forbidden"))
}
// Packet-line helpers
func packetFlush() []byte {
return []byte("0000")
}
func packetWrite(str string) []byte {
s := strconv.FormatInt(int64(len(str)+4), 16)
if len(s)%4 != 0 {
s = strings.Repeat("0", 4-len(s)%4) + s
}
return []byte(s + str)
}
// Header writing helpers
func hdrNoCache(w http.ResponseWriter) {
w.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
}
func hdrCacheForever(w http.ResponseWriter) {
now := time.Now().Unix()
expires := now + 31536000
w.Header().Set("Date", fmt.Sprintf("%d", now))
w.Header().Set("Expires", fmt.Sprintf("%d", expires))
w.Header().Set("Cache-Control", "public, max-age=31536000")
}