diff --git a/modules/caddyhttp/replacer.go b/modules/caddyhttp/replacer.go index e89c502b6e9e..a304d81ccee4 100644 --- a/modules/caddyhttp/replacer.go +++ b/modules/caddyhttp/replacer.go @@ -62,19 +62,17 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo if req != nil { // query string parameters if strings.HasPrefix(key, reqURIQueryReplPrefix) { - vals := req.URL.Query()[key[len(reqURIQueryReplPrefix):]] + vals, found := req.URL.Query()[key[len(reqURIQueryReplPrefix):]] // always return true, since the query param might // be present only in some requests - return strings.Join(vals, ","), true + return strings.Join(vals, ","), found } // request header fields if strings.HasPrefix(key, reqHeaderReplPrefix) { field := key[len(reqHeaderReplPrefix):] - vals := req.Header[textproto.CanonicalMIMEHeaderKey(field)] - // always return true, since the header field might - // be present only in some requests - return strings.Join(vals, ","), true + vals, found := req.Header[textproto.CanonicalMIMEHeaderKey(field)] + return strings.Join(vals, ","), found } // cookies @@ -82,11 +80,10 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo name := key[len(reqCookieReplPrefix):] for _, cookie := range req.Cookies() { if strings.EqualFold(name, cookie.Name) { - // always return true, since the cookie might - // be present only in some requests return cookie.Value, true } } + return nil, false } // http.request.tls.* @@ -161,7 +158,7 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo return id.String(), true case "http.request.body": if req.Body == nil { - return "", true + return "", false } // normally net/http will close the body for us, but since we // are replacing it with a fake one, we have to ensure we close @@ -219,11 +216,11 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo // convert to integer then compute prefix bits, err := strconv.Atoi(bitsStr) if err != nil { - return "", true + return "", false } prefix, err := addr.Prefix(bits) if err != nil { - return "", true + return "", false } return prefix.String(), true } @@ -291,10 +288,8 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo if strings.HasPrefix(key, varsReplPrefix) { varName := key[len(varsReplPrefix):] tbl := req.Context().Value(VarsCtxKey).(map[string]any) - raw := tbl[varName] - // variables can be dynamic, so always return true - // even when it may not be set; treat as empty then - return raw, true + raw, found := tbl[varName] + return raw, found } } @@ -302,10 +297,8 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo // response header fields if strings.HasPrefix(key, respHeaderReplPrefix) { field := key[len(respHeaderReplPrefix):] - vals := w.Header()[textproto.CanonicalMIMEHeaderKey(field)] - // always return true, since the header field might - // be present only in some responses - return strings.Join(vals, ","), true + vals, found := w.Header()[textproto.CanonicalMIMEHeaderKey(field)] + return strings.Join(vals, ","), found } } diff --git a/modules/caddyhttp/replacer_test.go b/modules/caddyhttp/replacer_test.go index 44c6f2a89a72..a03a4f0f81c0 100644 --- a/modules/caddyhttp/replacer_test.go +++ b/modules/caddyhttp/replacer_test.go @@ -107,10 +107,6 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV get: "http.request.remote.host/24,32", expect: "192.168.159.0/24", }, - { - get: "http.request.remote.host/999", - expect: "", - }, { get: "http.request.remote.port", expect: "1234", @@ -207,7 +203,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV } { actual, got := repl.GetString(tc.get) if !got { - t.Errorf("Test %d: Expected to recognize the placeholder name, but didn't", i) + t.Errorf("Test %d: Expected to recognize the placeholder name %q, but didn't", i, tc.get) } if actual != tc.expect { t.Errorf("Test %d: Expected %s to be '%s' but got '%s'",