/
cleanpath.go
47 lines (40 loc) · 931 Bytes
/
cleanpath.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
package dataflow
import (
"path"
"strings"
)
const (
httpProto = "http://"
httpsProto = "https://"
)
func cleanPaths(rv string) string {
if rv == "" {
return rv
}
if strings.HasPrefix(rv, httpProto) || strings.HasPrefix(rv, httpsProto) {
var proto, domainPath, query string
protoEndIdx := strings.Index(rv, "//") + 2
queryIdx := strings.Index(rv, "?")
proto = rv[:protoEndIdx]
if queryIdx != -1 {
domainPath = rv[protoEndIdx:queryIdx]
query = rv[queryIdx:]
} else if rv != proto {
domainPath = rv[protoEndIdx:]
}
if domainPath != "" {
appendSlash := len(domainPath) > 0 && domainPath[len(domainPath)-1] == '/'
domainPath = path.Clean(domainPath)
if appendSlash {
domainPath += "/"
}
}
return proto + domainPath + query
}
appendSlash := len(rv) > 0 && rv[len(rv)-1] == '/'
cleanPath := path.Clean(rv)
if appendSlash {
return cleanPath + "/"
}
return cleanPath
}