-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
tree.go
42 lines (38 loc) · 825 Bytes
/
tree.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
package gorouter
import (
"net/http"
"github.com/vardius/gorouter/v4/mux"
)
func allowed(t mux.Tree, method, path string) (allow string) {
if path == "*" {
// tree roots should be http method nodes only
for _, root := range t {
if root.Name() == http.MethodOptions {
continue
}
if len(allow) == 0 {
allow = root.Name()
} else {
allow += ", " + root.Name()
}
}
} else {
// tree roots should be http method nodes only
for _, root := range t {
if root.Name() == method || root.Name() == http.MethodOptions {
continue
}
if route, _ := root.Tree().MatchRoute(path); route != nil {
if len(allow) == 0 {
allow = root.Name()
} else {
allow += ", " + root.Name()
}
}
}
}
if len(allow) > 0 {
allow += ", " + http.MethodOptions
}
return allow
}