mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Fix parsing of proto/port
Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
parent
ed7fb3bbda
commit
658a9d0f47
2 changed files with 28 additions and 12 deletions
28
nat/nat.go
28
nat/nat.go
|
@ -61,21 +61,25 @@ func (p Port) Int() int {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits a port in the format of port/proto
|
// Splits a port in the format of proto/port
|
||||||
func SplitProtoPort(rawPort string) (string, string) {
|
func SplitProtoPort(rawPort string) (string, string) {
|
||||||
parts := strings.Split(rawPort, "/")
|
var port string
|
||||||
l := len(parts)
|
var proto string
|
||||||
if l == 0 {
|
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
if l == 1 {
|
|
||||||
if rawPort == "" {
|
|
||||||
return "", "" // ""/tcp is not valid, ever
|
|
||||||
}
|
|
||||||
|
|
||||||
return "tcp", rawPort
|
parts := strings.Split(rawPort, "/")
|
||||||
|
|
||||||
|
if len(parts) == 0 || parts[0] == "" { // we have "" or ""/
|
||||||
|
port = ""
|
||||||
|
proto = ""
|
||||||
|
} else { // we have # or #/ or #/...
|
||||||
|
port = parts[0]
|
||||||
|
if len(parts) > 1 && parts[1] != "" {
|
||||||
|
proto = parts[1] // we have #/...
|
||||||
|
} else {
|
||||||
|
proto = "tcp" // we have # or #/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return parts[1], parts[0]
|
return proto, port
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateProto(proto string) bool {
|
func validateProto(proto string) bool {
|
||||||
|
|
|
@ -84,6 +84,18 @@ func TestSplitProtoPort(t *testing.T) {
|
||||||
if proto != "tcp" || port != "1234" {
|
if proto != "tcp" || port != "1234" {
|
||||||
t.Fatal("tcp is not the default protocol for portspec '1234'")
|
t.Fatal("tcp is not the default protocol for portspec '1234'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
proto, port = SplitProtoPort("1234/")
|
||||||
|
|
||||||
|
if proto != "tcp" || port != "1234" {
|
||||||
|
t.Fatal("parsing '1234/' yielded:" + port + "/" + proto)
|
||||||
|
}
|
||||||
|
|
||||||
|
proto, port = SplitProtoPort("/tcp")
|
||||||
|
|
||||||
|
if proto != "" || port != "" {
|
||||||
|
t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParsePortSpecs(t *testing.T) {
|
func TestParsePortSpecs(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue