Provision sets up m.
(ctx caddy.Context)
| 120 | |
| 121 | // Provision sets up m. |
| 122 | func (m *MatchExpression) Provision(ctx caddy.Context) error { |
| 123 | m.log = ctx.Logger() |
| 124 | |
| 125 | // replace placeholders with a function call - this is just some |
| 126 | // light (and possibly naïve) syntactic sugar |
| 127 | m.expandedExpr = placeholderRegexp.ReplaceAllString(m.Expr, placeholderExpansion) |
| 128 | |
| 129 | // as a second pass, we'll strip the escape character from an escaped |
| 130 | // placeholder, so that it can be used as an input to other CEL functions |
| 131 | m.expandedExpr = escapedPlaceholderRegexp.ReplaceAllString(m.expandedExpr, escapedPlaceholderExpansion) |
| 132 | |
| 133 | // our type adapter expands CEL's standard type support |
| 134 | m.ta = celTypeAdapter{} |
| 135 | |
| 136 | // initialize the CEL libraries from the Matcher implementations which |
| 137 | // have been configured to support CEL. |
| 138 | matcherLibProducers := []CELLibraryProducer{} |
| 139 | for _, info := range caddy.GetModules("http.matchers") { |
| 140 | p, ok := info.New().(CELLibraryProducer) |
| 141 | if ok { |
| 142 | matcherLibProducers = append(matcherLibProducers, p) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // add the matcher name to the context so that the matcher name |
| 147 | // can be used by regexp matchers being provisioned |
| 148 | ctx = ctx.WithValue(MatcherNameCtxKey, m.Name) |
| 149 | |
| 150 | // Assemble the compilation and program options from the different library |
| 151 | // producers into a single cel.Library implementation. |
| 152 | matcherEnvOpts := []cel.EnvOption{} |
| 153 | matcherProgramOpts := []cel.ProgramOption{} |
| 154 | for _, producer := range matcherLibProducers { |
| 155 | l, err := producer.CELLibrary(ctx) |
| 156 | if err != nil { |
| 157 | return fmt.Errorf("error initializing CEL library for %T: %v", producer, err) |
| 158 | } |
| 159 | matcherEnvOpts = append(matcherEnvOpts, l.CompileOptions()...) |
| 160 | matcherProgramOpts = append(matcherProgramOpts, l.ProgramOptions()...) |
| 161 | } |
| 162 | matcherLib := cel.Lib(NewMatcherCELLibrary(matcherEnvOpts, matcherProgramOpts)) |
| 163 | |
| 164 | // create the CEL environment |
| 165 | env, err := cel.NewEnv( |
| 166 | cel.Function(CELPlaceholderFuncName, cel.SingletonBinaryBinding(m.caddyPlaceholderFunc), cel.Overload( |
| 167 | CELPlaceholderFuncName+"_httpRequest_string", |
| 168 | []*cel.Type{httpRequestObjectType, cel.StringType}, |
| 169 | cel.AnyType, |
| 170 | )), |
| 171 | cel.Variable(CELRequestVarName, httpRequestObjectType), |
| 172 | cel.CustomTypeAdapter(m.ta), |
| 173 | ext.Strings(), |
| 174 | ext.Bindings(), |
| 175 | ext.Lists(), |
| 176 | ext.Math(), |
| 177 | matcherLib, |
| 178 | ) |
| 179 | if err != nil { |