@marcel91200 / caddy Mirror · read-only / commits / 55b4c12e
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 20:30 UTC

map: Evaluate placeholders in output vals (#4650)

Matthew Holt committed Mar 21, 2022 at 17:05 UTC 55b4c12e0404347828ed691594d1f8ae8228c598
2 files changed +21
modules/caddyhttp/map/map.go
+4
@@ -145,6 +145,10 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
145 return string(result), true
146 }
147 if input == m.Input {
148 + if outputStr, ok := output.(string); ok {
149 + // NOTE: if the output has a placeholder that has the same key as the input, this is infinite recursion
150 + return repl.ReplaceAll(outputStr, ""), true
151 + }
152 return output, true
153 }
154 }
modules/caddyhttp/map/map_test.go
+17
@@ -82,6 +82,22 @@ func TestHandler(t *testing.T) {
82 "output": "3",
83 },
84 },
85 + {
86 + reqURI: "/foo",
87 + handler: Handler{
88 + Source: "{http.request.uri.path}",
89 + Destinations: []string{"{output}"},
90 + Mappings: []Mapping{
91 + {
92 + Input: "/foo",
93 + Outputs: []interface{}{"{testvar}"},
94 + },
95 + },
96 + },
97 + expect: map[string]interface{}{
98 + "output": "testing",
99 + },
100 + },
101 } {
102 if err := tc.handler.Provision(caddy.Context{}); err != nil {
103 t.Fatalf("Test %d: Provisioning handler: %v", i, err)
@@ -92,6 +108,7 @@ func TestHandler(t *testing.T) {
108 t.Fatalf("Test %d: Creating request: %v", i, err)
109 }
110 repl := caddyhttp.NewTestReplacer(req)
111 + repl.Set("testvar", "testing")
112 ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
113 req = req.WithContext(ctx)
114