github.com/rentongzhang/docker@v1.8.2-rc1/pkg/urlutil/urlutil.go (about)

     1  package urlutil
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  var (
     9  	validPrefixes = map[string][]string{
    10  		"url":       {"http://", "https://"},
    11  		"git":       {"git://", "github.com/", "git@"},
    12  		"transport": {"tcp://", "udp://", "unix://"},
    13  	}
    14  	urlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$")
    15  )
    16  
    17  // IsURL returns true if the provided str is an HTTP(S) URL.
    18  func IsURL(str string) bool {
    19  	return checkURL(str, "url")
    20  }
    21  
    22  // IsGitURL returns true if the provided str is a git repository URL.
    23  func IsGitURL(str string) bool {
    24  	if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) {
    25  		return true
    26  	}
    27  	return checkURL(str, "git")
    28  }
    29  
    30  // IsGitTransport returns true if the provided str is a git transport by inspecting
    31  // the prefix of the string for known protocols used in git.
    32  func IsGitTransport(str string) bool {
    33  	return IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
    34  }
    35  
    36  // IsTransportURL returns true if the provided str is a transport (tcp, udp, unix) URL.
    37  func IsTransportURL(str string) bool {
    38  	return checkURL(str, "transport")
    39  }
    40  
    41  func checkURL(str, kind string) bool {
    42  	for _, prefix := range validPrefixes[kind] {
    43  		if strings.HasPrefix(str, prefix) {
    44  			return true
    45  		}
    46  	}
    47  	return false
    48  }