Use bool pointers for upstream options that default to true

This commit is contained in:
Joel Speed 2020-07-19 14:00:52 +01:00
parent 6b27069812
commit d43b372ca9
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB
7 changed files with 41 additions and 38 deletions

View File

@ -77,8 +77,8 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
Path: u.Path,
URI: upstreamString,
InsecureSkipTLSVerify: l.SSLUpstreamInsecureSkipVerify,
PassHostHeader: l.PassHostHeader,
ProxyWebSockets: l.ProxyWebSockets,
PassHostHeader: &l.PassHostHeader,
ProxyWebSockets: &l.ProxyWebSockets,
FlushInterval: &l.FlushInterval,
}
@ -104,8 +104,8 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
// Force defaults compatible with static responses
upstream.URI = ""
upstream.InsecureSkipTLSVerify = false
upstream.PassHostHeader = true
upstream.ProxyWebSockets = false
upstream.PassHostHeader = nil
upstream.ProxyWebSockets = nil
flush := 1 * time.Second
upstream.FlushInterval = &flush
}

View File

@ -23,6 +23,7 @@ var _ = Describe("Legacy Options", func() {
legacyOpts.LegacyUpstreams.SSLUpstreamInsecureSkipVerify = true
legacyOpts.LegacyUpstreams.Upstreams = []string{"http://foo.bar/baz", "file://var/lib/website#/bar"}
truth := true
opts.UpstreamServers = Upstreams{
{
ID: "/baz",
@ -30,8 +31,8 @@ var _ = Describe("Legacy Options", func() {
URI: "http://foo.bar/baz",
FlushInterval: &flushInterval,
InsecureSkipTLSVerify: true,
PassHostHeader: true,
ProxyWebSockets: true,
PassHostHeader: &truth,
ProxyWebSockets: &truth,
},
{
ID: "/bar",
@ -39,8 +40,8 @@ var _ = Describe("Legacy Options", func() {
URI: "file://var/lib/website#/bar",
FlushInterval: &flushInterval,
InsecureSkipTLSVerify: true,
PassHostHeader: true,
ProxyWebSockets: true,
PassHostHeader: &truth,
ProxyWebSockets: &truth,
},
}
@ -72,8 +73,8 @@ var _ = Describe("Legacy Options", func() {
Path: "/baz",
URI: validHTTP,
InsecureSkipTLSVerify: skipVerify,
PassHostHeader: passHostHeader,
ProxyWebSockets: proxyWebSockets,
PassHostHeader: &passHostHeader,
ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval,
}
@ -84,8 +85,8 @@ var _ = Describe("Legacy Options", func() {
Path: "/",
URI: emptyPathHTTP,
InsecureSkipTLSVerify: skipVerify,
PassHostHeader: passHostHeader,
ProxyWebSockets: proxyWebSockets,
PassHostHeader: &passHostHeader,
ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval,
}
@ -95,8 +96,8 @@ var _ = Describe("Legacy Options", func() {
Path: "/bar",
URI: validFileWithFragment,
InsecureSkipTLSVerify: skipVerify,
PassHostHeader: passHostHeader,
ProxyWebSockets: proxyWebSockets,
PassHostHeader: &passHostHeader,
ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval,
}
@ -109,8 +110,8 @@ var _ = Describe("Legacy Options", func() {
Static: true,
StaticCode: &validStaticCode,
InsecureSkipTLSVerify: false,
PassHostHeader: true,
ProxyWebSockets: false,
PassHostHeader: nil,
ProxyWebSockets: nil,
FlushInterval: &defaultFlushInterval,
}
@ -123,8 +124,8 @@ var _ = Describe("Legacy Options", func() {
Static: true,
StaticCode: &invalidStaticCode,
InsecureSkipTLSVerify: false,
PassHostHeader: true,
ProxyWebSockets: false,
PassHostHeader: nil,
ProxyWebSockets: nil,
FlushInterval: &defaultFlushInterval,
}

View File

@ -52,9 +52,9 @@ type Upstream struct {
// PassHostHeader determines whether the request host header should be proxied
// to the upstream server.
// Defaults to true.
PassHostHeader bool `json:"passHostHeader"`
PassHostHeader *bool `json:"passHostHeader"`
// ProxyWebSockets enables proxying of websockets to upstream servers
// Defaults to true.
ProxyWebSockets bool `json:"proxyWebSockets"`
ProxyWebSockets *bool `json:"proxyWebSockets"`
}

View File

@ -49,7 +49,7 @@ func newHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *option
// Set up a WebSocket proxy if required
var wsProxy http.Handler
if upstream.ProxyWebSockets {
if upstream.ProxyWebSockets == nil || *upstream.ProxyWebSockets {
wsProxy = newWebSocketReverseProxy(u, upstream.InsecureSkipTLSVerify)
}
@ -110,7 +110,7 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
}
// Set the request director based on the PassHostHeader option
if !upstream.PassHostHeader {
if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
setProxyUpstreamHostHeader(proxy, target)
} else {
setProxyDirector(proxy)

View File

@ -24,6 +24,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
const flushInterval5s = 5 * time.Second
const flushInterval1s = 1 * time.Second
truth := true
falsum := false
type httpUpstreamTableInput struct {
id string
@ -51,10 +53,11 @@ var _ = Describe("HTTP Upstream Suite", func() {
rw := httptest.NewRecorder()
flush := 1 * time.Second
upstream := options.Upstream{
ID: in.id,
PassHostHeader: true,
ProxyWebSockets: false,
PassHostHeader: &truth,
ProxyWebSockets: &falsum,
InsecureSkipTLSVerify: false,
FlushInterval: &flush,
}
@ -258,8 +261,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
flush := 1 * time.Second
upstream := options.Upstream{
ID: "noPassHost",
PassHostHeader: false,
ProxyWebSockets: false,
PassHostHeader: &falsum,
ProxyWebSockets: &falsum,
InsecureSkipTLSVerify: false,
FlushInterval: &flush,
}
@ -302,7 +305,7 @@ var _ = Describe("HTTP Upstream Suite", func() {
ID: "foo123",
FlushInterval: &in.flushInterval,
InsecureSkipTLSVerify: in.skipVerify,
ProxyWebSockets: in.proxyWebSockets,
ProxyWebSockets: &in.proxyWebSockets,
}
handler := newHTTPUpstreamProxy(upstream, u, in.sigData, in.errorHandler)
@ -370,8 +373,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
flush := 1 * time.Second
upstream := options.Upstream{
ID: "websocketProxy",
PassHostHeader: true,
ProxyWebSockets: true,
PassHostHeader: &truth,
ProxyWebSockets: &truth,
InsecureSkipTLSVerify: false,
FlushInterval: &flush,
}

View File

@ -73,10 +73,10 @@ func validateStaticUpstream(upstream options.Upstream) []string {
if upstream.FlushInterval != nil && *upstream.FlushInterval != time.Second {
msgs = append(msgs, fmt.Sprintf("upstream %q has flushInterval, but is a static upstream, this will have no effect.", upstream.ID))
}
if !upstream.PassHostHeader {
if upstream.PassHostHeader != nil {
msgs = append(msgs, fmt.Sprintf("upstream %q has passHostHeader, but is a static upstream, this will have no effect.", upstream.ID))
}
if !upstream.ProxyWebSockets {
if upstream.ProxyWebSockets != nil {
msgs = append(msgs, fmt.Sprintf("upstream %q has proxyWebSockets, but is a static upstream, this will have no effect.", upstream.ID))
}

View File

@ -17,6 +17,7 @@ var _ = Describe("Upstreams", func() {
flushInterval := 5 * time.Second
staticCode200 := 200
truth := true
validHTTPUpstream := options.Upstream{
ID: "validHTTPUpstream",
@ -24,11 +25,9 @@ var _ = Describe("Upstreams", func() {
URI: "http://localhost:8080",
}
validStaticUpstream := options.Upstream{
ID: "validStaticUpstream",
Path: "/validStaticUpstream",
Static: true,
PassHostHeader: true, // This would normally be defaulted
ProxyWebSockets: true, // this would normally be defaulted
ID: "validStaticUpstream",
Path: "/validStaticUpstream",
Static: true,
}
validFileUpstream := options.Upstream{
ID: "validFileUpstream",
@ -134,8 +133,8 @@ var _ = Describe("Upstreams", func() {
URI: "ftp://foo",
Static: true,
FlushInterval: &flushInterval,
PassHostHeader: false,
ProxyWebSockets: false,
PassHostHeader: &truth,
ProxyWebSockets: &truth,
InsecureSkipTLSVerify: true,
},
},