Commit ca3d23bc authored by Tw's avatar Tw

proxy: fix hyphen issue when parsing target

fix issue #948
Signed-off-by: default avatarTw <tw19881113@gmail.com>
parent beae16f0
...@@ -111,6 +111,26 @@ func TestSetup(t *testing.T) { ...@@ -111,6 +111,26 @@ func TestSetup(t *testing.T) {
"http://localhost:8085": {}, "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) c := caddy.NewTestController("http", test.input)
err := setup(c) err := setup(c)
......
...@@ -171,10 +171,15 @@ func parseUpstream(u string) ([]string, error) { ...@@ -171,10 +171,15 @@ func parseUpstream(u string) ([]string, error) {
if colonIdx != -1 && colonIdx != protoIdx { if colonIdx != -1 && colonIdx != protoIdx {
us := u[:colonIdx] us := u[:colonIdx]
ports := u[len(us)+1:] ue := ""
if separators := strings.Count(ports, "-"); separators > 1 { portsEnd := len(u)
return nil, fmt.Errorf("port range [%s] is invalid", ports) if nextSlash := strings.Index(u[colonIdx:], "/"); nextSlash != -1 {
} else if separators == 1 { portsEnd = colonIdx + nextSlash
ue = u[portsEnd:]
}
ports := u[len(us)+1 : portsEnd]
if separators := strings.Count(ports, "-"); separators == 1 {
portsStr := strings.Split(ports, "-") portsStr := strings.Split(ports, "-")
pIni, err := strconv.Atoi(portsStr[0]) pIni, err := strconv.Atoi(portsStr[0])
if err != nil { if err != nil {
...@@ -192,7 +197,7 @@ func parseUpstream(u string) ([]string, error) { ...@@ -192,7 +197,7 @@ func parseUpstream(u string) ([]string, error) {
hosts := []string{} hosts := []string{}
for p := pIni; p <= pEnd; p++ { 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 return hosts, nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment