From ca3d23bc70fb5ef2f3333c9ae3cdcfd1db306c66 Mon Sep 17 00:00:00 2001 From: Tw Date: Thu, 21 Jul 2016 13:56:30 +0800 Subject: [PATCH] proxy: fix hyphen issue when parsing target fix issue #948 Signed-off-by: Tw --- caddyhttp/proxy/setup_test.go | 20 ++++++++++++++++++++ caddyhttp/proxy/upstream.go | 15 ++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/caddyhttp/proxy/setup_test.go b/caddyhttp/proxy/setup_test.go index 949104dab..7dff3bbc8 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 7ba33ef58..cf43728eb 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 }