Skip to content

Commit

Permalink
Fixup server based consideration for multiple hosts validation #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Nov 9, 2020
1 parent bb42be3 commit ddcb015
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
34 changes: 30 additions & 4 deletions config/runtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,18 @@ func validatePortHosts(conf *config.Gateway, configuredPort int) error {
// validate the format, validating for a valid host or port is out of scope.
validFormat := regexp.MustCompile(`^([a-z0-9.-]+|\*)(:\*|:\d{1,5})?$`)

portMap := map[int]string{}
type hosts map[string]bool
type ports map[int]hosts

portMap := make(ports)
isHostsMandatory := len(conf.Server) > 1

for _, srv := range conf.Server {
if isHostsMandatory && len(srv.Hosts) == 0 {
return fmt.Errorf("hosts attribute is mandatory for multiple servers: %q", srv.Name)
}

srvPortMap := make(ports)
for _, host := range srv.Hosts {
if !validFormat.MatchString(host) {
return fmt.Errorf("host format is invalid: %q", host)
Expand All @@ -308,11 +318,27 @@ func validatePortHosts(conf *config.Gateway, configuredPort int) error {
return err
}

if h, ok := portMap[po]; ok && h == ho {
return fmt.Errorf("conflict: host %q already defined for port: %d", ho, po)
if _, ok := srvPortMap[po]; !ok {
srvPortMap[po] = make(hosts)
}

portMap[po] = ho
srvPortMap[po][ho] = true
}

// srvPortsMap contains all unique host port combinations
// of current server and should not exist multiple.
for po, ho := range srvPortMap {
if _, ok := portMap[po]; !ok {
portMap[po] = make(hosts)
}

for h := range ho {
if _, ok := portMap[po][h]; ok {
return fmt.Errorf("conflict: host %q already defined for port: %d", h, po)
}

portMap[po][h] = true
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions config/runtime/server_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestServer_validatePortHosts(t *testing.T) {
},
}, 8080,
},
true,
false,
},
{
"Same host/port in two servers with *",
Expand Down Expand Up @@ -86,8 +86,8 @@ func TestServer_validatePortHosts(t *testing.T) {
args{
&config.Gateway{
Server: []*config.Server{
{Hosts: []string{"example.com"}},
{Hosts: []string{"example.com"}},
{Hosts: []string{"example.com", "couper.io"}},
{Hosts: []string{"example.com", "couper.io"}},
},
}, 8080,
},
Expand All @@ -105,6 +105,18 @@ func TestServer_validatePortHosts(t *testing.T) {
},
true,
},
{
"Same port /w different host in two servers",
args{
&config.Gateway{
Server: []*config.Server{
{Hosts: []string{"*", "example.com:9090"}},
{Hosts: []string{"couper.io:9090"}},
},
}, 8080,
},
false,
},
{
"Host is mandatory for multiple servers",
args{
Expand Down
2 changes: 1 addition & 1 deletion server/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (m *Mux) FindHandler(req *http.Request) http.Handler {

func (m *Mux) match(root *pathpattern.Node, req *http.Request) (*pathpattern.Node, []string) {
matchPath := req.Method + " " + req.URL.Path
// no hosts are configured, lookup /wo hostPath first
// no hosts are configured, lookup w/o hostPath first
node, paramValues := root.Match(matchPath)
if node == nil {
hostPath := pathpattern.PathFromHost(req.Host, false)
Expand Down

0 comments on commit ddcb015

Please sign in to comment.