From e73b117332a5606a986ee7e03e6f4b0cea7871c9 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 14 Sep 2019 13:25:26 -0600 Subject: reverse_proxy: Ability to mutate headers; set upstream placeholders --- modules/caddyhttp/reverseproxy/hosts.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'modules/caddyhttp/reverseproxy/hosts.go') diff --git a/modules/caddyhttp/reverseproxy/hosts.go b/modules/caddyhttp/reverseproxy/hosts.go index 1c0fae3..a8349bd 100644 --- a/modules/caddyhttp/reverseproxy/hosts.go +++ b/modules/caddyhttp/reverseproxy/hosts.go @@ -16,6 +16,8 @@ package reverseproxy import ( "fmt" + "net" + "strings" "sync/atomic" "github.com/caddyserver/caddy/v2" @@ -173,6 +175,27 @@ type DialInfo struct { // The address to dial. Follows the same // semantics and rules as net.Dial. Address string + + // Host and Port are components of Address, + // pre-split for convenience. + Host, Port string +} + +// NewDialInfo creates and populates a DialInfo +// for the given network and address. It splits +// the address into host and port values if the +// network type supports them, or uses the whole +// address as the port if splitting fails. +func NewDialInfo(network, address string) DialInfo { + var addrHost, addrPort string + if !strings.Contains(network, "unix") { + var err error + addrHost, addrPort, err = net.SplitHostPort(address) + if err != nil { + addrHost = address // assume there was no port + } + } + return DialInfo{network, address, addrHost, addrPort} } // String returns the Caddy network address form -- cgit v1.2.3