From 2f684e42d5b8b78fe688b2f7e31d54d2333c0701 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 20 Sep 2019 13:13:49 -0600 Subject: reverse_proxy/headers: Expose header replacement ability in Caddyfile Adds header_up and header_down subdirectives to reverse_proxy --- modules/caddyhttp/reverseproxy/caddyfile.go | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'modules/caddyhttp/reverseproxy') diff --git a/modules/caddyhttp/reverseproxy/caddyfile.go b/modules/caddyhttp/reverseproxy/caddyfile.go index d8c63b4..83298d8 100644 --- a/modules/caddyhttp/reverseproxy/caddyfile.go +++ b/modules/caddyhttp/reverseproxy/caddyfile.go @@ -25,6 +25,7 @@ import ( "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" "github.com/caddyserver/caddy/v2/modules/caddyhttp" + "github.com/caddyserver/caddy/v2/modules/caddyhttp/headers" "github.com/dustin/go-humanize" ) @@ -67,6 +68,10 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) // unhealthy_status // unhealthy_latency // +// # header manipulation +// header_up [+|-] [ []] +// header_down [+|-] [ []] +// // # round trip // transport { // ... @@ -327,6 +332,46 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { } h.HealthChecks.Passive.UnhealthyLatency = caddy.Duration(dur) + case "header_up": + if h.Headers == nil { + h.Headers = new(headers.Handler) + } + if h.Headers.Request == nil { + h.Headers.Request = new(headers.HeaderOps) + } + args := d.RemainingArgs() + switch len(args) { + case 1: + headers.CaddyfileHeaderOp(h.Headers.Request, args[0], "", "") + case 2: + headers.CaddyfileHeaderOp(h.Headers.Request, args[0], args[1], "") + case 3: + headers.CaddyfileHeaderOp(h.Headers.Request, args[0], args[1], args[2]) + default: + return d.ArgErr() + } + + case "header_down": + if h.Headers == nil { + h.Headers = new(headers.Handler) + } + if h.Headers.Response == nil { + h.Headers.Response = &headers.RespHeaderOps{ + HeaderOps: new(headers.HeaderOps), + } + } + args := d.RemainingArgs() + switch len(args) { + case 1: + headers.CaddyfileHeaderOp(h.Headers.Response.HeaderOps, args[0], "", "") + case 2: + headers.CaddyfileHeaderOp(h.Headers.Response.HeaderOps, args[0], args[1], "") + case 3: + headers.CaddyfileHeaderOp(h.Headers.Response.HeaderOps, args[0], args[1], args[2]) + default: + return d.ArgErr() + } + case "transport": if !d.NextArg() { return d.ArgErr() -- cgit v1.2.3