@@ -5,73 +5,84 @@ import (
5
5
"fmt"
6
6
"io"
7
7
"net/http"
8
+ "net/url"
8
9
"os"
9
10
"regexp"
10
11
"strings"
11
12
"sync"
12
13
)
13
14
14
15
var (
15
- serviceRPCUpload = func (path string ) ( match string ) {
16
- return hasSuffix ( path , "/git-upload-pack" )
16
+ serviceRPCUpload = func (u * url. URL ) * Match {
17
+ return matchSuffix ( u . Path , "/git-upload-pack" )
17
18
}
18
- serviceRPCReceive = func (path string ) ( match string ) {
19
- return hasSuffix ( path , "/git-receive-pack" )
19
+ serviceRPCReceive = func (u * url. URL ) * Match {
20
+ return matchSuffix ( u . Path , "/git-receive-pack" )
20
21
}
21
22
22
- getInfoRefs = func (path string ) ( match string ) {
23
- return hasSuffix ( path , "/info/refs" )
23
+ getInfoRefs = func (u * url. URL ) * Match {
24
+ return matchSuffix ( u . Path , "/info/refs" )
24
25
}
25
26
26
- getHead = func (path string ) ( match string ) {
27
- return hasSuffix ( path , "/HEAD" )
27
+ getHead = func (u * url. URL ) * Match {
28
+ return matchSuffix ( u . Path , "/HEAD" )
28
29
}
29
30
30
- getAlternates = func (path string ) ( match string ) {
31
- return hasSuffix ( path , "/objects/info/alternates" )
31
+ getAlternates = func (u * url. URL ) * Match {
32
+ return matchSuffix ( u . Path , "/objects/info/alternates" )
32
33
}
33
34
34
- getHTTPAlternates = func (path string ) ( match string ) {
35
- return hasSuffix ( path , "/objects/info/http-alternates" )
35
+ getHTTPAlternates = func (u * url. URL ) * Match {
36
+ return matchSuffix ( u . Path , "/objects/info/http-alternates" )
36
37
}
37
38
38
- getInfoPacks = func (path string ) ( match string ) {
39
- return hasSuffix ( path , "/objects/info/packs" )
39
+ getInfoPacks = func (u * url. URL ) * Match {
40
+ return matchSuffix ( u . Path , "/objects/info/packs" )
40
41
}
41
42
42
43
getInfoFileRegexp = regexp .MustCompile (".*?(/objects/info/[^/]*)$" )
43
- getInfoFile = func (path string ) ( match string ) {
44
- return findStringSubmatch (path , getInfoFileRegexp )
44
+ getInfoFile = func (u * url. URL ) * Match {
45
+ return findStringSubmatch (u . Path , getInfoFileRegexp )
45
46
}
46
47
47
48
getLooseObjectRegexp = regexp .MustCompile (".*?(/objects/[0-9a-f]{2}/[0-9a-f]{38})$" )
48
- getLooseObject = func (path string ) ( match string ) {
49
- return findStringSubmatch (path , getLooseObjectRegexp )
49
+ getLooseObject = func (u * url. URL ) * Match {
50
+ return findStringSubmatch (u . Path , getLooseObjectRegexp )
50
51
}
51
52
52
53
getPackFileRegexp = regexp .MustCompile (".*?(/objects/pack/pack-[0-9a-f]{40}\\ .pack)$" )
53
- getPackFile = func (path string ) ( match string ) {
54
- return findStringSubmatch (path , getPackFileRegexp )
54
+ getPackFile = func (u * url. URL ) * Match {
55
+ return findStringSubmatch (u . Path , getPackFileRegexp )
55
56
}
56
57
57
58
getIdxFileRegexp = regexp .MustCompile (".*?(/objects/pack/pack-[0-9a-f]{40}\\ .idx)$" )
58
- getIdxFile = func (path string ) ( match string ) {
59
- return findStringSubmatch (path , getIdxFileRegexp )
59
+ getIdxFile = func (u * url. URL ) * Match {
60
+ return findStringSubmatch (u . Path , getIdxFileRegexp )
60
61
}
61
62
)
62
63
63
- func hasSuffix (path , suffix string ) (match string ) {
64
- if strings .HasSuffix (path , suffix ) {
65
- match = suffix
64
+ type Match struct {
65
+ RepoPath , FilePath string
66
+ }
67
+
68
+ func matchSuffix (path , suffix string ) * Match {
69
+ if ! strings .HasSuffix (path , suffix ) {
70
+ return nil
66
71
}
67
- return
72
+ repoPath := strings .Replace (path , suffix , "" , 1 )
73
+ filePath := strings .Replace (path , repoPath + "/" , "" , 1 )
74
+ return & Match {repoPath , filePath }
68
75
}
69
76
70
- func findStringSubmatch (path string , prefix * regexp.Regexp ) (match string ) {
71
- if m := prefix .FindStringSubmatch (path ); m != nil {
72
- match = m [1 ]
77
+ func findStringSubmatch (path string , prefix * regexp.Regexp ) * Match {
78
+ m := prefix .FindStringSubmatch (path )
79
+ if m == nil {
80
+ return nil
73
81
}
74
- return
82
+ suffix := m [1 ]
83
+ repoPath := strings .Replace (path , suffix , "" , 1 )
84
+ filePath := strings .Replace (path , repoPath + "/" , "" , 1 )
85
+ return & Match {repoPath , filePath }
75
86
}
76
87
77
88
type options struct {
@@ -151,7 +162,7 @@ func (ghx *GitHTTPXfer) SetLogger(logger Logger) {
151
162
}
152
163
153
164
func (ghx * GitHTTPXfer ) ServeHTTP (rw http.ResponseWriter , r * http.Request ) {
154
- repoPath , filePath , handler , err := ghx .matchRouting (r .Method , r .URL . Path )
165
+ repoPath , filePath , handler , err := ghx .matchRouting (r .Method , r .URL )
155
166
switch err .(type ) {
156
167
case * URLNotFoundError :
157
168
RenderNotFound (rw )
@@ -173,11 +184,12 @@ func (ghx *GitHTTPXfer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
173
184
handler (ctx )
174
185
}
175
186
176
- func (ghx * GitHTTPXfer ) matchRouting (method , path string ) (repoPath string , filePath string , handler HandlerFunc , err error ) {
177
- match , route , err := ghx .Router .Match (method , path )
187
+ func (ghx * GitHTTPXfer ) matchRouting (method string , u * url.URL ) (repoPath string , filePath string , handler HandlerFunc , err error ) {
188
+ match , route , err := ghx .Router .Match (method , u )
189
+
178
190
if err == nil {
179
- repoPath = strings . Replace ( path , match , "" , 1 )
180
- filePath = strings . Replace ( path , repoPath + "/" , "" , 1 )
191
+ repoPath = match . RepoPath
192
+ filePath = match . FilePath
181
193
handler = route .Handler
182
194
}
183
195
return
0 commit comments