mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-03 23:09:57 -05:00
proxy: Fix 502 errors for requests without headers (#2188)
* Fix 502 errors for requests without headers * Add unexported roundRobinPolicier We have to preserve state for fallback mode of Header policy, so it's required to save state in some variable
This commit is contained in:
parent
3e0695ee31
commit
d3b731e925
2 changed files with 16 additions and 9 deletions
|
@ -183,6 +183,8 @@ type Header struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var roundRobinPolicier RoundRobin
|
||||||
|
|
||||||
// Select selects the host based on hashing the header value
|
// Select selects the host based on hashing the header value
|
||||||
func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||||
if r.Name == "" {
|
if r.Name == "" {
|
||||||
|
@ -190,7 +192,8 @@ func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||||
}
|
}
|
||||||
val := request.Header.Get(r.Name)
|
val := request.Header.Get(r.Name)
|
||||||
if val == "" {
|
if val == "" {
|
||||||
return nil
|
// fallback to RoundRobin policy in case no Header in request
|
||||||
|
return roundRobinPolicier.Select(pool, request)
|
||||||
}
|
}
|
||||||
return hostByHashing(pool, val)
|
return hostByHashing(pool, val)
|
||||||
}
|
}
|
||||||
|
|
|
@ -320,21 +320,25 @@ func TestUriPolicy(t *testing.T) {
|
||||||
func TestHeaderPolicy(t *testing.T) {
|
func TestHeaderPolicy(t *testing.T) {
|
||||||
pool := testPool()
|
pool := testPool()
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
Name string
|
||||||
Policy *Header
|
Policy *Header
|
||||||
RequestHeaderName string
|
RequestHeaderName string
|
||||||
RequestHeaderValue string
|
RequestHeaderValue string
|
||||||
NilHost bool
|
NilHost bool
|
||||||
HostIndex int
|
HostIndex int
|
||||||
}{
|
}{
|
||||||
{&Header{""}, "", "", true, 0},
|
{"empty config", &Header{""}, "", "", true, 0},
|
||||||
{&Header{""}, "Affinity", "somevalue", true, 0},
|
{"empty config+header+value", &Header{""}, "Affinity", "somevalue", true, 0},
|
||||||
{&Header{""}, "Affinity", "", true, 0},
|
{"empty config+header", &Header{""}, "Affinity", "", true, 0},
|
||||||
|
|
||||||
{&Header{"Affinity"}, "", "", true, 0},
|
{"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 1},
|
||||||
{&Header{"Affinity"}, "Affinity", "somevalue", false, 1},
|
{"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 2},
|
||||||
{&Header{"Affinity"}, "Affinity", "somevalue2", false, 0},
|
{"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 0},
|
||||||
{&Header{"Affinity"}, "Affinity", "somevalue3", false, 2},
|
|
||||||
{&Header{"Affinity"}, "Affinity", "", true, 0},
|
{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue", false, 1},
|
||||||
|
{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue2", false, 0},
|
||||||
|
{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue3", false, 2},
|
||||||
|
{"hash route with empty value", &Header{"Affinity"}, "Affinity", "", false, 1},
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, test := range tests {
|
for idx, test := range tests {
|
||||||
|
|
Loading…
Add table
Reference in a new issue