Skip to content

Commit

Permalink
do not default non-existant http variables to empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JensErat committed Feb 7, 2023
1 parent 83f0005 commit 2d8fba4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
27 changes: 11 additions & 16 deletions modules/caddyhttp/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,28 @@ 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
if strings.HasPrefix(key, reqCookieReplPrefix) {
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.*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -293,18 +290,16 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
raw := GetVar(req.Context(), varName)
// variables can be dynamic, so always return true
// even when it may not be set; treat as empty then
return raw, true
return raw, raw != nil
}
}

if w != nil {
// 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
}
}

Expand Down
6 changes: 1 addition & 5 deletions modules/caddyhttp/replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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'",
Expand Down

0 comments on commit 2d8fba4

Please sign in to comment.