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://, …)
|
2018-02-05 16:05:59 -05:00
|
|
|
package urlutil // import "github.com/docker/docker/pkg/urlutil"
|
2014-11-24 18:10:37 -05:00
|
|
|
|
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{
|
2018-05-30 05:06:06 -04:00
|
|
|
"url": {"http://", "https://"},
|
|
|
|
|
|
|
|
// The github.com/ prefix is a special case used to treat context-paths
|
|
|
|
// starting with `github.com` as a git URL if the given path does not
|
|
|
|
// exist locally. The "github.com/" prefix is kept for backward compatibility,
|
|
|
|
// and is a legacy feature.
|
|
|
|
//
|
|
|
|
// Going forward, no additional prefixes should be added, and users should
|
|
|
|
// be encouraged to use explicit URLs (https://github.com/user/repo.git) instead.
|
2015-05-21 16:20:25 -04:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|