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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package starlarkmw
import (
"context"
"fmt"
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/starlarkmw/internal/lib"
caddyscript "github.com/caddyserver/caddy/v2/pkg/caddyscript/lib"
"github.com/starlight-go/starlight/convert"
"go.starlark.net/starlark"
)
func init() {
caddy.RegisterModule(StarlarkMW{})
}
// StarlarkMW represents a middleware responder written in starlark
type StarlarkMW struct {
Script string `json:"script"`
serveHTTP *starlark.Function
setup *starlark.Function
thread *starlark.Thread
loadMiddleware *lib.LoadMiddleware
execute *lib.Execute
globals *starlark.StringDict
gctx caddy.Context
rctx caddy.Context
rcancel context.CancelFunc
}
// CaddyModule returns the Caddy module information.
func (StarlarkMW) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.starlark",
New: func() caddy.Module { return new(StarlarkMW) },
}
}
// ServeHTTP responds to an http request with starlark.
func (s *StarlarkMW) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
var mwcancel context.CancelFunc
var mwctx caddy.Context
// call setup() to prepare the middleware chain if it is defined
if s.setup != nil {
mwctx, mwcancel = caddy.NewContext(s.gctx)
defer mwcancel()
s.loadMiddleware.Ctx = mwctx
args := starlark.Tuple{caddyscript.HTTPRequest{Req: r}}
_, err := starlark.Call(new(starlark.Thread), s.setup, args, nil)
if err != nil {
return fmt.Errorf("starlark setup(), %v", err)
}
}
// dynamically build middleware chain for each request
stack := caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
wr, err := convert.ToValue(w)
if err != nil {
return fmt.Errorf("cannot convert response writer to starlark value")
}
args := starlark.Tuple{wr, caddyscript.HTTPRequest{Req: r}}
v, err := starlark.Call(new(starlark.Thread), s.serveHTTP, args, nil)
if err != nil {
return fmt.Errorf("starlark serveHTTP(), %v", err)
}
// if a responder type was returned from starlark we should run it otherwise it
// is expected to handle the request
if resp, ok := v.(lib.ResponderModule); ok {
return resp.Instance.ServeHTTP(w, r)
}
return nil
})
// TODO :- make middlewareResponseWriter exported and wrap w with that
var mid []caddyhttp.Middleware
for _, m := range s.execute.Modules {
mid = append(mid, func(next caddyhttp.HandlerFunc) caddyhttp.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
return m.Instance.ServeHTTP(w, r, next)
}
})
}
for i := len(mid) - 1; i >= 0; i-- {
stack = mid[i](stack)
}
s.execute.Modules = nil
return stack(w, r)
}
// Cleanup cleans up any modules loaded during the creation of a starlark route.
func (s *StarlarkMW) Cleanup() error {
s.rcancel()
return nil
}
// Provision sets up the starlark env.
func (s *StarlarkMW) Provision(ctx caddy.Context) error {
// store global context
s.gctx = ctx
// setup context for cleaning up any modules loaded during starlark script parsing phase
rctx, cancel := caddy.NewContext(ctx)
s.rcancel = cancel
// setup starlark global env
env := make(starlark.StringDict)
loadMiddleware := lib.LoadMiddleware{}
loadResponder := lib.LoadResponder{
Ctx: rctx,
}
execute := lib.Execute{}
lr := starlark.NewBuiltin("loadResponder", loadResponder.Run)
lr = lr.BindReceiver(&loadResponder)
env["loadResponder"] = lr
lm := starlark.NewBuiltin("loadMiddleware", loadMiddleware.Run)
lm = lm.BindReceiver(&loadMiddleware)
env["loadMiddleware"] = lm
ex := starlark.NewBuiltin("execute", execute.Run)
ex = ex.BindReceiver(&execute)
env["execute"] = ex
// import caddyscript lib
env["time"] = caddyscript.Time{}
env["regexp"] = caddyscript.Regexp{}
// configure starlark
thread := new(starlark.Thread)
s.thread = thread
// run starlark script
globals, err := starlark.ExecFile(thread, "", s.Script, env)
if err != nil {
return fmt.Errorf("starlark exec file: %v", err.Error())
}
// extract defined methods to setup middleware chain and responder, setup is not required
var setup *starlark.Function
if _, ok := globals["setup"]; ok {
setup, ok = globals["setup"].(*starlark.Function)
if !ok {
return fmt.Errorf("setup function not defined in starlark script")
}
}
serveHTTP, ok := globals["serveHTTP"].(*starlark.Function)
if !ok {
return fmt.Errorf("serveHTTP function not defined in starlark script")
}
s.setup = setup
s.serveHTTP = serveHTTP
s.loadMiddleware = &loadMiddleware
s.execute = &execute
s.globals = &globals
return nil
}
|