@marcel91200 / caddy Mirror · read-only / commits / 6f0f159b
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

caddyhttp: Add {http.request.body} placeholder

Matthew Holt committed Jul 16, 2020 at 19:25 UTC 6f0f159ba56adeb6e2cbbb408651419b87f20856
2 files changed +22
modules/caddyhttp/app.go
+1
@@ -47,6 +47,7 @@ func init() {
47 //
48 // Placeholder | Description
49 // ------------|---------------
50 +// `{http.request.body}` | The request body (⚠️ inefficient; use only for debugging)
51 // `{http.request.cookie.*}` | HTTP request cookie
52 // `{http.request.header.*}` | Specific request header field
53 // `{http.request.host.labels.*}` | Request host labels (0-based from right); e.g. for foo.example.com: 0=com, 1=example, 2=foo
modules/caddyhttp/replacer.go
+21
@@ -15,6 +15,7 @@
15 package caddyhttp
16
17 import (
18 + "bytes"
19 "context"
20 "crypto/ecdsa"
21 "crypto/ed25519"
@@ -25,6 +26,8 @@ import (
26 "crypto/x509"
27 "encoding/asn1"
28 "fmt"
29 + "io"
30 + "io/ioutil"
31 "net"
32 "net/http"
33 "net/textproto"
@@ -136,6 +139,24 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
139 return dir, true
140 case "http.request.uri.query":
141 return req.URL.RawQuery, true
142 + case "http.request.body":
143 + if req.Body == nil {
144 + return "", true
145 + }
146 + // normally net/http will close the body for us, but since we
147 + // are replacing it with a fake one, we have to ensure we close
148 + // the real body ourselves when we're done
149 + defer req.Body.Close()
150 + // read the request body into a buffer (can't pool because we
151 + // don't know its lifetime and would have to make a copy anyway)
152 + buf := new(bytes.Buffer)
153 + _, err := io.Copy(buf, req.Body)
154 + if err != nil {
155 + return "", true
156 + }
157 + // replace real body with buffered data
158 + req.Body = ioutil.NopCloser(buf)
159 + return buf.String(), true
160
161 // original request, before any internal changes
162 case "http.request.orig_method":