diff --git a/caddyhttp/proxy/setup_test.go b/caddyhttp/proxy/setup_test.go index 949104da..7dff3bbc 100644 --- a/caddyhttp/proxy/setup_test.go +++ b/caddyhttp/proxy/setup_test.go @@ -111,6 +111,26 @@ func TestSetup(t *testing.T) { "http://localhost:8085": {}, }, }, + // test #10 test hyphen without port range + { + "proxy / http://localhost:8001/a--b", + false, + map[string]struct{}{ + "http://localhost:8001/a--b": {}, + }, + }, + // test #11 test hyphen with port range + { + "proxy / http://localhost:8001-8005/a--b", + false, + map[string]struct{}{ + "http://localhost:8001/a--b": {}, + "http://localhost:8002/a--b": {}, + "http://localhost:8003/a--b": {}, + "http://localhost:8004/a--b": {}, + "http://localhost:8005/a--b": {}, + }, + }, } { c := caddy.NewTestController("http", test.input) err := setup(c) diff --git a/caddyhttp/proxy/upstream.go b/caddyhttp/proxy/upstream.go index 7ba33ef5..cf43728e 100644 --- a/caddyhttp/proxy/upstream.go +++ b/caddyhttp/proxy/upstream.go @@ -171,10 +171,15 @@ func parseUpstream(u string) ([]string, error) { if colonIdx != -1 && colonIdx != protoIdx { us := u[:colonIdx] - ports := u[len(us)+1:] - if separators := strings.Count(ports, "-"); separators > 1 { - return nil, fmt.Errorf("port range [%s] is invalid", ports) - } else if separators == 1 { + ue := "" + portsEnd := len(u) + if nextSlash := strings.Index(u[colonIdx:], "/"); nextSlash != -1 { + portsEnd = colonIdx + nextSlash + ue = u[portsEnd:] + } + ports := u[len(us)+1 : portsEnd] + + if separators := strings.Count(ports, "-"); separators == 1 { portsStr := strings.Split(ports, "-") pIni, err := strconv.Atoi(portsStr[0]) if err != nil { @@ -192,7 +197,7 @@ func parseUpstream(u string) ([]string, error) { hosts := []string{} for p := pIni; p <= pEnd; p++ { - hosts = append(hosts, fmt.Sprintf("%s:%d", us, p)) + hosts = append(hosts, fmt.Sprintf("%s:%d%s", us, p, ue)) } return hosts, nil }