(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler)
| 122 | } |
| 123 | |
| 124 | func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { |
| 125 | repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) |
| 126 | |
| 127 | // defer work until a variable is actually evaluated by using replacer's Map callback |
| 128 | repl.Map(func(key string) (any, bool) { |
| 129 | // return early if the variable is not even a configured destination |
| 130 | destIdx := slices.Index(h.Destinations, key) |
| 131 | if destIdx < 0 { |
| 132 | return nil, false |
| 133 | } |
| 134 | |
| 135 | input := repl.ReplaceAll(h.Source, "") |
| 136 | |
| 137 | // find the first mapping matching the input and return |
| 138 | // the requested destination/output value |
| 139 | for _, m := range h.Mappings { |
| 140 | output := m.Outputs[destIdx] |
| 141 | if output == nil { |
| 142 | continue |
| 143 | } |
| 144 | outputStr := caddy.ToString(output) |
| 145 | |
| 146 | // evaluate regular expression if configured |
| 147 | if m.re != nil { |
| 148 | var result []byte |
| 149 | matches := m.re.FindStringSubmatchIndex(input) |
| 150 | if matches == nil { |
| 151 | continue |
| 152 | } |
| 153 | result = m.re.ExpandString(result, outputStr, input, matches) |
| 154 | return string(result), true |
| 155 | } |
| 156 | |
| 157 | // otherwise simple string comparison |
| 158 | if input == m.Input { |
| 159 | return repl.ReplaceAll(outputStr, ""), true |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // fall back to default if no match or if matched nil value |
| 164 | if len(h.Defaults) > destIdx { |
| 165 | return repl.ReplaceAll(h.Defaults[destIdx], ""), true |
| 166 | } |
| 167 | |
| 168 | return nil, true |
| 169 | }) |
| 170 | |
| 171 | return next.ServeHTTP(w, r) |
| 172 | } |
| 173 | |
| 174 | // Mapping describes a mapping from input to outputs. |
| 175 | type Mapping struct { |
nothing calls this directly
no test coverage detected