-
Notifications
You must be signed in to change notification settings - Fork 56
/
pathTransformHandler.go
56 lines (48 loc) · 1.24 KB
/
pathTransformHandler.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
package serverHandler
import (
"mjpclab.dev/ghfs/src/util"
"net/http"
)
type pathTransformHandler struct {
prefixes []string
nextHandler http.Handler
}
func stripUrlPrefix(urlPathDir, urlPath, prefix string) string {
if len(urlPath) == len(prefix) {
return "/"
} else if len(prefix) <= 1 {
return urlPathDir
} else {
return urlPathDir[len(prefix):]
}
}
func (transformer pathTransformHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
urlPath := util.CleanUrlPath(r.URL.Path)
var urlPathDir string
if len(urlPath) > 1 && r.URL.Path[len(r.URL.Path)-1] == '/' {
urlPathDir = urlPath + "/"
} else {
urlPathDir = urlPath
}
r.RequestURI = urlPathDir
if len(r.URL.RawQuery) > 0 {
r.RequestURI += "?" + r.URL.RawQuery
}
if len(transformer.prefixes) == 0 {
r.URL.Path = urlPathDir
transformer.nextHandler.ServeHTTP(w, r)
return
}
for _, prefix := range transformer.prefixes {
if !util.HasUrlPrefixDir(urlPath, prefix) {
continue
}
r.URL.Path = stripUrlPrefix(urlPathDir, urlPath, prefix)
transformer.nextHandler.ServeHTTP(w, r)
return
}
defaultHandler.ServeHTTP(w, r)
}
func newPathTransformHandler(prefixes []string, handler http.Handler) http.Handler {
return pathTransformHandler{prefixes, handler}
}