Skip to content

Commit 4037d05

Browse files
caddyhttp: {http.request.body_base64} placeholder (#7367)
1 parent 409a072 commit 4037d05

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

modules/caddyhttp/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func init() {
5151
// Placeholder | Description
5252
// ------------|---------------
5353
// `{http.request.body}` | The request body (⚠️ inefficient; use only for debugging)
54+
// `{http.request.body_base64}` | The request body, base64-encoded (⚠️ for debugging)
5455
// `{http.request.cookie.*}` | HTTP request cookie
5556
// `{http.request.duration}` | Time up to now spent handling the request (after decoding headers from client)
5657
// `{http.request.duration_ms}` | Same as 'duration', but in milliseconds.

modules/caddyhttp/replacer.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,21 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
229229
req.Body = io.NopCloser(buf) // replace real body with buffered data
230230
return buf.String(), true
231231

232+
case "http.request.body_base64":
233+
if req.Body == nil {
234+
return "", true
235+
}
236+
// normally net/http will close the body for us, but since we
237+
// are replacing it with a fake one, we have to ensure we close
238+
// the real body ourselves when we're done
239+
defer req.Body.Close()
240+
// read the request body into a buffer (can't pool because we
241+
// don't know its lifetime and would have to make a copy anyway)
242+
buf := new(bytes.Buffer)
243+
_, _ = io.Copy(buf, req.Body) // can't handle error, so just ignore it
244+
req.Body = io.NopCloser(buf) // replace real body with buffered data
245+
return base64.StdEncoding.EncodeToString(buf.Bytes()), true
246+
232247
// original request, before any internal changes
233248
case "http.request.orig_method":
234249
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)

0 commit comments

Comments
 (0)