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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package caddyhttp
import (
"log"
"net/http"
"strings"
"bitbucket.org/lightcodelabs/caddy2"
"bitbucket.org/lightcodelabs/caddy2/internal/caddyscript"
"go.starlark.net/starlark"
)
// TODO: Matchers should probably support regex of some sort... performance trade-offs?
type (
matchHost []string
matchPath []string
matchMethod []string
matchQuery map[string][]string
matchHeader map[string][]string
matchProtocol string
matchScript string
)
func init() {
caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.host",
New: func() (interface{}, error) { return matchHost{}, nil },
})
caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.path",
New: func() (interface{}, error) { return matchPath{}, nil },
})
caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.method",
New: func() (interface{}, error) { return matchMethod{}, nil },
})
caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.query",
New: func() (interface{}, error) { return matchQuery{}, nil },
})
caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.header",
New: func() (interface{}, error) { return matchHeader{}, nil },
})
caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.protocol",
New: func() (interface{}, error) { return new(matchProtocol), nil },
})
caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.caddyscript",
New: func() (interface{}, error) { return new(matchScript), nil },
})
}
func (m matchScript) Match(r *http.Request) bool {
input := string(m)
thread := new(starlark.Thread)
env := caddyscript.MatcherEnv(r)
val, err := starlark.Eval(thread, "", input, env)
if err != nil {
log.Printf("caddyscript for matcher is invalid: attempting to evaluate expression `%v` error `%v`", input, err)
return false
}
return val.String() == "True"
}
func (m matchProtocol) Match(r *http.Request) bool {
switch string(m) {
case "grpc":
if r.Header.Get("content-type") == "application/grpc" {
return true
}
case "https":
if r.TLS != nil {
return true
}
case "http":
if r.TLS == nil {
return true
}
}
return false
}
func (m matchHost) Match(r *http.Request) bool {
for _, host := range m {
if r.Host == host {
return true
}
}
return false
}
func (m matchPath) Match(r *http.Request) bool {
for _, path := range m {
if strings.HasPrefix(r.URL.Path, path) {
return true
}
}
return false
}
func (m matchMethod) Match(r *http.Request) bool {
for _, method := range m {
if r.Method == method {
return true
}
}
return false
}
func (m matchQuery) Match(r *http.Request) bool {
for param, vals := range m {
paramVal := r.URL.Query().Get(param)
for _, v := range vals {
if paramVal == v {
return true
}
}
}
return false
}
func (m matchHeader) Match(r *http.Request) bool {
for field, vals := range m {
fieldVals := r.Header[field]
for _, fieldVal := range fieldVals {
for _, v := range vals {
if fieldVal == v {
return true
}
}
}
}
return false
}
// Interface guards
var (
_ RouteMatcher = matchHost{}
_ RouteMatcher = matchPath{}
_ RouteMatcher = matchMethod{}
_ RouteMatcher = matchQuery{}
_ RouteMatcher = matchHeader{}
_ RouteMatcher = new(matchProtocol)
_ RouteMatcher = new(matchScript)
)
|