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

requestbody: Allow overwriting remote address

An experimental feature, let's see if it's useful.

Matthew Holt committed Jan 11, 2021 at 13:35 UTC 0bf2046da7f2f5bf1b7d9fa055ae28de9a06ddaf
1 file changed +9
modules/caddyhttp/requestbody/requestbody.go
+9
@@ -29,6 +29,11 @@ func init() {
29 type RequestBody struct {
30 // The maximum number of bytes to allow reading from the body by a later handler.
31 MaxSize int64 `json:"max_size,omitempty"`
32 +
33 + // Overwrites the remote address from which the request came. This is destructive;
34 + // handlers later in the chain will not be able to recover the true originating
35 + // address of the request. EXPERIMENTAL: May get changed or removed later.
36 + RemoteAddress string `json:"remote_address,omitempty"`
37 }
38
39 // CaddyModule returns the Caddy module information.
@@ -40,6 +45,10 @@ func (RequestBody) CaddyModule() caddy.ModuleInfo {
45 }
46
47 func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
48 + if rb.RemoteAddress != "" {
49 + repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
50 + r.RemoteAddr = repl.ReplaceAll(rb.RemoteAddress, "")
51 + }
52 if r.Body == nil {
53 return next.ServeHTTP(w, r)
54 }