Commit d746b959 authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #950 from tw4452852/proxy_parse

proxy: fix hyphen issue when parsing target
parents cf3ce491 ca3d23bc
......@@ -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)
......
......@@ -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
}
......
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