0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

proxy: fix hyphen issue when parsing target

fix issue #948

Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
Tw 2016-07-21 13:56:30 +08:00
parent beae16f07c
commit ca3d23bc70
2 changed files with 30 additions and 5 deletions

View file

@ -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)

View file

@ -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
}