Skip to content

Commit

Permalink
!!! BUGFIX: if multiple servers are defined, ensure the correct one i…
Browse files Browse the repository at this point in the history
…s taken (based on port)

before this change, the servers were chosen randomly. UGH - nasty!!!
  • Loading branch information
skurfuerst committed May 21, 2023
1 parent 137d0eb commit 7a68b23
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions subscribe/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,13 @@ func (s *Subscribe) handler(msg *nats.Msg) {
func (s *Subscribe) matchServer(servers map[string]*caddyhttp.Server, req *http.Request) (*caddyhttp.Server, error) {
repl := caddy.NewReplacer()
for _, server := range servers {
if !hasListenerAddress(server, req.URL) {
// listener address (host/port) does not match for server, so we can continue with next server.
continue
}
r := caddyhttp.PrepareRequest(req, repl, nil, server)
for _, route := range server.Routes {

if route.MatcherSets.AnyMatch(r) {
return server, nil
}
Expand All @@ -138,6 +143,33 @@ func (s *Subscribe) matchServer(servers map[string]*caddyhttp.Server, req *http.
return nil, fmt.Errorf("no server matched for the current url: %s", req.URL)
}

// hasListenerAddress is modelled after the same function in caddyhttp/server, but a bit simplified.
func hasListenerAddress(s *caddyhttp.Server, fullAddr *url.URL) bool {
laddrs, err := caddy.ParseNetworkAddress(fullAddr.Host)
if err != nil {
return false
}
if laddrs.PortRangeSize() != 1 {
return false // TODO: support port ranges
}

for _, lnAddr := range s.Listen {
thisAddrs, err := caddy.ParseNetworkAddress(lnAddr)
if err != nil {
continue
}
if thisAddrs.Network != laddrs.Network {
continue
}

if (laddrs.StartPort <= thisAddrs.EndPort) &&
(laddrs.StartPort >= thisAddrs.StartPort) {
return true
}
}
return false
}

func (s *Subscribe) prepareRequest(method string, rawURL string, body io.Reader, header nats.Header) (*http.Request, error) {
u, err := url.Parse(rawURL)
if err != nil {
Expand Down

0 comments on commit 7a68b23

Please sign in to comment.