-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
144 lines (115 loc) · 2.71 KB
/
router.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
package gittp
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"path"
"strings"
)
type router struct {
check func(RequestInfo) (bool, int)
}
// IsGitRequest is a helper to ensure this is a git request
func IsGitRequest(path string) bool {
action := strings.TrimRight(strings.TrimLeft(path, "/"), "/")
if !strings.Contains(action, "git-") &&
!strings.Contains(action, "info/") &&
!strings.Contains(action, "HEAD") &&
!strings.Contains(action, "objects/") {
return false
}
return true
}
func (rt router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cleanPath := strings.TrimRight(strings.TrimLeft(r.URL.Path, "/"), "/")
args := strings.Split(cleanPath, "/")
uname := args[0]
repo := cleanRepoName(args[1])
if !IsGitRequest(cleanPath) {
http.Error(w, "not found", http.StatusNotFound)
}
authHead := r.Header.Get("Authorization")
if len(authHead) == 0 {
w.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
auths := strings.Fields(authHead)
if len(auths) != 2 || auths[0] != "Basic" {
fmt.Println("middle")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
authUsername, authPassword, err := basicAuthDecode(auths[1])
if err != nil {
fmt.Println(err)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
shouldSend, code := rt.check(RequestInfo{
RepoOwner: uname,
RepoName: repo,
Username: authUsername,
Password: authPassword,
})
if !shouldSend {
http.Error(w, "", code)
return
}
serve(&gitContext{
w: w,
r: r,
})
}
func cleanRepoName(name string) string {
var clean string
clean = strings.TrimSuffix(name, ".git")
clean = strings.TrimSuffix(clean, ".wiki")
return clean
}
func getGitRepoPath(dir string) (string, error) {
if !strings.HasSuffix(dir, ".git") {
dir += ".git"
}
filename := path.Join(settings.RepoRootPath, dir)
if _, err := os.Stat(filename); os.IsNotExist(err) {
return "", err
}
return filename, nil
}
func serve(ctx *gitContext) {
for _, route := range routes {
reqPath := strings.ToLower(ctx.r.URL.Path)
m := route.GetMatches(reqPath)
if m == nil {
continue
}
if !route.IsMethod(ctx.r.Method) {
ctx.NotFound()
return
}
file := strings.TrimPrefix(reqPath, m[1]+"/")
dir, err := getGitRepoPath(m[1])
if err != nil {
ctx.NotFound()
return
}
route.handler(serviceHandler{
w: ctx.w,
r: ctx.r,
file: file,
dir: dir,
})
return
}
ctx.NotFound()
}
func basicAuthDecode(encoded string) (string, string, error) {
s, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return "", "", err
}
auth := strings.SplitN(string(s), ":", 2)
return auth[0], auth[1], nil
}