github.com/ttys3/engine@v17.12.1-ce-rc2+incompatible/pkg/urlutil/urlutil.go (about) 1 // Package urlutil provides helper function to check urls kind. 2 // It supports http urls, git urls and transport url (tcp://, …) 3 package urlutil 4 5 import ( 6 "regexp" 7 "strings" 8 ) 9 10 var ( 11 validPrefixes = map[string][]string{ 12 "url": {"http://", "https://"}, 13 "git": {"git://", "github.com/", "git@"}, 14 "transport": {"tcp://", "tcp+tls://", "udp://", "unix://", "unixgram://"}, 15 } 16 urlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$") 17 ) 18 19 // IsURL returns true if the provided str is an HTTP(S) URL. 20 func IsURL(str string) bool { 21 return checkURL(str, "url") 22 } 23 24 // IsGitURL returns true if the provided str is a git repository URL. 25 func IsGitURL(str string) bool { 26 if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) { 27 return true 28 } 29 return checkURL(str, "git") 30 } 31 32 // IsTransportURL returns true if the provided str is a transport (tcp, tcp+tls, udp, unix) URL. 33 func IsTransportURL(str string) bool { 34 return checkURL(str, "transport") 35 } 36 37 func checkURL(str, kind string) bool { 38 for _, prefix := range validPrefixes[kind] { 39 if strings.HasPrefix(str, prefix) { 40 return true 41 } 42 } 43 return false 44 }