1// Package urlutil provides helper function to check urls kind.
2// It supports http urls, git urls and transport url (tcp://, …)
3package urlutil // import "github.com/docker/docker/pkg/urlutil"
4
5import (
6	"regexp"
7	"strings"
8)
9
10var (
11	validPrefixes = map[string][]string{
12		"url": {"http://", "https://"},
13
14		// The github.com/ prefix is a special case used to treat context-paths
15		// starting with `github.com` as a git URL if the given path does not
16		// exist locally. The "github.com/" prefix is kept for backward compatibility,
17		// and is a legacy feature.
18		//
19		// Going forward, no additional prefixes should be added, and users should
20		// be encouraged to use explicit URLs (https://github.com/user/repo.git) instead.
21		"git":       {"git://", "github.com/", "git@"},
22		"transport": {"tcp://", "tcp+tls://", "udp://", "unix://", "unixgram://"},
23	}
24	urlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$")
25)
26
27// IsURL returns true if the provided str is an HTTP(S) URL.
28func IsURL(str string) bool {
29	return checkURL(str, "url")
30}
31
32// IsGitURL returns true if the provided str is a git repository URL.
33func IsGitURL(str string) bool {
34	if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) {
35		return true
36	}
37	return checkURL(str, "git")
38}
39
40// IsTransportURL returns true if the provided str is a transport (tcp, tcp+tls, udp, unix) URL.
41func IsTransportURL(str string) bool {
42	return checkURL(str, "transport")
43}
44
45func checkURL(str, kind string) bool {
46	for _, prefix := range validPrefixes[kind] {
47		if strings.HasPrefix(str, prefix) {
48			return true
49		}
50	}
51	return false
52}
53