@marcel91200 / caddy Mirror · read-only / commits / 0fc47e83
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 14, 05:15 UTC

map: Apply default if mapped output is nil

Matthew Holt committed Oct 2, 2020 at 15:23 UTC 0fc47e8357af5ccd6f800819722229b1a279e5b5
3 files changed +23 -14
caddytest/integration/map_test.go
+2 -2
@@ -18,7 +18,7 @@ func TestMap(t *testing.T) {
18 localhost:9080 {
19
20 map {http.request.method} {dest-1} {dest-2} {
21 - default unknown
21 + default unknown1 unknown2
22 ~G.T get-called
23 POST post-called foobar
24 }
@@ -30,7 +30,7 @@ func TestMap(t *testing.T) {
30 `, "caddyfile")
31
32 // act and assert
33 - tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called ")
33 + tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called unknown2")
34 tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called foobar")
35 }
36
modules/caddyhttp/map/caddyfile.go
+6 -3
@@ -70,7 +70,10 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
70
71 // every other line maps one input to one or more outputs
72 in := h.Val()
73 - outs := h.RemainingArgs()
73 + var outs []interface{}
74 + for _, out := range h.RemainingArgs() {
75 + outs = append(outs, out)
76 + }
77
78 // cannot have more outputs than destinations
79 if len(outs) > len(handler.Destinations) {
@@ -78,9 +81,9 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
81 }
82
83 // for convenience, can have fewer outputs than destinations, but the
81 - // underlying handler won't accept that, so we fill in empty values
84 + // underlying handler won't accept that, so we fill in nil values
85 for len(outs) < len(handler.Destinations) {
83 - outs = append(outs, "")
86 + outs = append(outs, nil)
87 }
88
89 // create the mapping
modules/caddyhttp/map/map.go
+15 -9
@@ -16,7 +16,6 @@ package maphandler
16
17 import (
18 "fmt"
19 - "log"
19 "net/http"
20 "regexp"
21 "strings"
@@ -40,14 +39,15 @@ type Handler struct {
39 // Source is the placeholder from which to get the input value.
40 Source string `json:"source,omitempty"`
41
43 - // Destinations are the placeholders in which to store the outputs.
42 + // Destinations are the names of placeholders in which to store the outputs.
43 Destinations []string `json:"destinations,omitempty"`
44
45 // Mappings from source values (inputs) to destination values (outputs).
46 // The first matching mapping will be applied.
47 Mappings []Mapping `json:"mappings,omitempty"`
48
50 - // If no mappings match, the default value will be applied (optional).
49 + // If no mappings match or if the mapped output is null/nil, the associated
50 + // default output will be applied (optional).
51 Defaults []string
52 }
53
@@ -125,20 +125,26 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
125 // find the first mapping matching the input and return
126 // the requested destination/output value
127 for _, m := range h.Mappings {
128 - log.Printf("MAPPING: %+v", m)
128 if m.re != nil {
129 if m.re.MatchString(input) {
131 - return m.Outputs[destIdx], true
130 + if output := m.Outputs[destIdx]; output == nil {
131 + break
132 + } else {
133 + return output, true
134 + }
135 }
136 continue
137 }
138 if input == m.Input {
136 - log.Printf("RETURNING: %s", m.Outputs[destIdx])
137 - return m.Outputs[destIdx], true
139 + if output := m.Outputs[destIdx]; output == nil {
140 + break
141 + } else {
142 + return output, true
143 + }
144 }
145 }
146
141 - // fall back to default if no match
147 + // fall back to default if no match or if matched nil value
148 if len(h.Defaults) > destIdx {
149 return h.Defaults[destIdx], true
150 }
@@ -171,7 +177,7 @@ type Mapping struct {
177
178 // Upon a match with the input, each output is positionally correlated
179 // with each destination of the parent handler.
174 - Outputs []string `json:"outputs,omitempty"`
180 + Outputs []interface{} `json:"outputs,omitempty"`
181
182 re *regexp.Regexp
183 }