2015-07-25 04:35:07 -04:00
|
|
|
// Package urlutil provides helper function to check urls kind.
|
|
|
|
// It supports http urls, git urls and transport url (tcp://, …)
|
2014-11-24 18:10:37 -05:00
|
|
|
package urlutil
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
2014-11-24 18:10:37 -05:00
|
|
|
|
|
|
|
var (
|
2015-05-21 16:20:25 -04:00
|
|
|
validPrefixes = map[string][]string{
|
|
|
|
"url": {"http://", "https://"},
|
|
|
|
"git": {"git://", "github.com/", "git@"},
|
2016-03-29 06:21:41 -04:00
|
|
|
"transport": {"tcp://", "tcp+tls://", "udp://", "unix://", "unixgram://"},
|
2014-11-24 18:10:37 -05:00
|
|
|
}
|
2015-04-24 18:12:45 -04:00
|
|
|
urlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$")
|
2014-11-24 18:10:37 -05:00
|
|
|
)
|
|
|
|
|
2015-05-21 16:20:25 -04:00
|
|
|
// IsURL returns true if the provided str is an HTTP(S) URL.
|
|
|
|
func IsURL(str string) bool {
|
|
|
|
return checkURL(str, "url")
|
|
|
|
}
|
|
|
|
|
2014-11-24 18:10:37 -05:00
|
|
|
// IsGitURL returns true if the provided str is a git repository URL.
|
|
|
|
func IsGitURL(str string) bool {
|
2015-04-24 18:12:45 -04:00
|
|
|
if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) {
|
2014-11-24 18:10:37 -05:00
|
|
|
return true
|
|
|
|
}
|
2015-05-21 16:20:25 -04:00
|
|
|
return checkURL(str, "git")
|
2014-11-24 18:10:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsGitTransport returns true if the provided str is a git transport by inspecting
|
|
|
|
// the prefix of the string for known protocols used in git.
|
|
|
|
func IsGitTransport(str string) bool {
|
|
|
|
return IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
|
|
|
|
}
|
2015-05-21 16:20:25 -04:00
|
|
|
|
2016-02-08 12:29:01 -05:00
|
|
|
// IsTransportURL returns true if the provided str is a transport (tcp, tcp+tls, udp, unix) URL.
|
2015-05-21 16:20:25 -04:00
|
|
|
func IsTransportURL(str string) bool {
|
|
|
|
return checkURL(str, "transport")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkURL(str, kind string) bool {
|
|
|
|
for _, prefix := range validPrefixes[kind] {
|
|
|
|
if strings.HasPrefix(str, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|