Support IPv6 address format.

Signed-off-by: Dong Chen <dongluo.chen@docker.com>
This commit is contained in:
Dong Chen 2016-03-01 16:28:24 -08:00
parent f6131d0331
commit c73f17fb6e
2 changed files with 10 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package cli
import (
"math/rand"
"net"
"regexp"
"time"
@ -11,7 +12,12 @@ import (
)
func checkAddrFormat(addr string) bool {
m, _ := regexp.MatchString("^[0-9a-zA-Z._-]+:[0-9]{1,5}$", addr)
// validate addr is in host:port form. Use net function to handle both IPv4/IPv6 cases.
_, port, err := net.SplitHostPort(addr)
if err != nil {
return false
}
m, _ := regexp.MatchString("^[0-9]{1,5}$", port)
return m
}

View File

@ -24,4 +24,7 @@ func TestCheckAddrFormat(t *testing.T) {
assert.True(t, checkAddrFormat("1.1.1.1:1111"))
assert.True(t, checkAddrFormat("hostname:1111"))
assert.True(t, checkAddrFormat("host-name_42:1111"))
assert.True(t, checkAddrFormat("[2001:db8:0:f101::3]:2375"))
assert.False(t, checkAddrFormat("2001:db8:0:f101::3:2375"))
assert.False(t, checkAddrFormat("[2001:db8:0:f101::3]:3:2375"))
}