MCPcopy
hub / github.com/caddyserver/caddy / CELLibrary

Method CELLibrary

modules/caddyhttp/fileserver/matcher.go:174–223  ·  view source on GitHub ↗

CELLibrary produces options that expose this matcher for use in CEL expression matchers. Example: expression file() expression file({http.request.uri.path}, '/index.php') expression file({'root': '/srv', 'try_files': [{http.request.uri.path}, '/index.php'], 'try_policy': 'first_exist', 'split_p

(ctx caddy.Context)

Source from the content-addressed store, hash-verified

172// expression file({http.request.uri.path}, '/index.php')
173// expression file({'root': '/srv', 'try_files': [{http.request.uri.path}, '/index.php'], 'try_policy': 'first_exist', 'split_path': ['.php']})
174func (MatchFile) CELLibrary(ctx caddy.Context) (cel.Library, error) {
175 requestType := cel.ObjectType("http.Request")
176
177 matcherFactory := func(data ref.Val) (caddyhttp.RequestMatcherWithError, error) {
178 values, err := caddyhttp.CELValueToMapStrList(data)
179 if err != nil {
180 return nil, err
181 }
182
183 var root string
184 if len(values["root"]) > 0 {
185 root = values["root"][0]
186 }
187
188 var fsName string
189 if len(values["fs"]) > 0 {
190 fsName = values["fs"][0]
191 }
192
193 var try_policy string
194 if len(values["try_policy"]) > 0 {
195 try_policy = values["try_policy"][0]
196 }
197
198 m := MatchFile{
199 Root: root,
200 TryFiles: values["try_files"],
201 TryPolicy: try_policy,
202 SplitPath: values["split_path"],
203 FileSystem: fsName,
204 }
205
206 err = m.Provision(ctx)
207 return m, err
208 }
209
210 envOptions := []cel.EnvOption{
211 cel.Macros(parser.NewGlobalVarArgMacro("file", celFileMatcherMacroExpander())),
212 cel.Function("file", cel.Overload("file_request_map", []*cel.Type{requestType, caddyhttp.CELTypeJSON}, cel.BoolType)),
213 cel.Function("file_request_map",
214 cel.Overload("file_request_map", []*cel.Type{requestType, caddyhttp.CELTypeJSON}, cel.BoolType),
215 cel.SingletonBinaryBinding(caddyhttp.CELMatcherRuntimeFunction("file_request_map", matcherFactory))),
216 }
217
218 programOptions := []cel.ProgramOption{
219 cel.CustomDecorator(caddyhttp.CELMatcherDecorator("file_request_map", matcherFactory)),
220 }
221
222 return caddyhttp.NewMatcherCELLibrary(envOptions, programOptions), nil
223}
224
225func celFileMatcherMacroExpander() parser.MacroExpander {
226 return func(eh parser.ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) {

Callers

nothing calls this directly

Calls 6

ProvisionMethod · 0.95
CELValueToMapStrListFunction · 0.92
CELMatcherDecoratorFunction · 0.92
NewMatcherCELLibraryFunction · 0.92

Tested by

no test coverage detected