-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitipathprefix.go
More file actions
47 lines (37 loc) · 873 Bytes
/
itipathprefix.go
File metadata and controls
47 lines (37 loc) · 873 Bytes
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 itipathprefix
import (
"net/http"
"strings"
)
// PathPrefixMatcher Store the prefix to match with the request Path
type PathPrefixMatcher struct {
prefix string
}
// New is the constructor to ItiPathPrefix
func New(template string) *PathPrefixMatcher {
return &PathPrefixMatcher{
prefix: template,
}
}
// Match returns if the request can be handled by this Route.
func (t *PathPrefixMatcher) Match(req *http.Request) bool {
if req.URL.Path == t.prefix {
return true
}
req.URL.Path = strings.Trim(req.URL.Path, "/")
t.prefix = strings.Trim(t.prefix, "/")
pText := strings.Split(req.URL.Path, "/")
pPrefix := strings.Split(t.prefix, "/")
for i := range pPrefix {
if pPrefix[i] == "*" {
continue
}
if pPrefix[i] != pText[i] {
if strings.HasPrefix(pText[i], pPrefix[i]) {
return true
}
return false
}
}
return true
}