You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

url.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package url
  2. import (
  3. "regexp"
  4. )
  5. var (
  6. isSchemeRegExp = regexp.MustCompile(`^[^:]+://`)
  7. scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})(?:\/|:))?(?P<path>[^\\].*\/[^\\].*)$`)
  8. )
  9. // MatchesScheme returns true if the given string matches a URL-like
  10. // format scheme.
  11. func MatchesScheme(url string) bool {
  12. return isSchemeRegExp.MatchString(url)
  13. }
  14. // MatchesScpLike returns true if the given string matches an SCP-like
  15. // format scheme.
  16. func MatchesScpLike(url string) bool {
  17. return scpLikeUrlRegExp.MatchString(url)
  18. }
  19. // FindScpLikeComponents returns the user, host, port and path of the
  20. // given SCP-like URL.
  21. func FindScpLikeComponents(url string) (user, host, port, path string) {
  22. m := scpLikeUrlRegExp.FindStringSubmatch(url)
  23. return m[1], m[2], m[3], m[4]
  24. }
  25. // IsLocalEndpoint returns true if the given URL string specifies a
  26. // local file endpoint. For example, on a Linux machine,
  27. // `/home/user/src/go-git` would match as a local endpoint, but
  28. // `https://github.com/src-d/go-git` would not.
  29. func IsLocalEndpoint(url string) bool {
  30. return !MatchesScheme(url) && !MatchesScpLike(url)
  31. }