-
Notifications
You must be signed in to change notification settings - Fork 8
/
scheme.go
40 lines (33 loc) · 996 Bytes
/
scheme.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package urlutil
import (
"regexp"
"strings"
)
const uriSchemePattern string = `^(?i)([a-z][0-9a-z\-\+.]+)://`
var rxScheme *regexp.Regexp = regexp.MustCompile(uriSchemePattern)
// https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
// URIHasScheme returns a boolean true or false if the string
// has a URI scheme.
func URIHasScheme(uri string) bool {
scheme := URIScheme(uri)
return len(scheme) > 0
}
// URIScheme extracts the URI scheme from a string. It returns
// an empty string if none is encountered.
func URIScheme(uri string) string {
uri = strings.TrimSpace(uri)
m := rxScheme.FindAllStringSubmatch(uri, -1)
if len(m) > 0 && len(m[0]) == 2 {
return strings.TrimSpace(m[0][1])
}
return ""
}
func IsHTTP(uri string, inclHTTP, inclHTTPS bool) bool {
try := strings.ToLower(strings.TrimSpace(uri))
if strings.Index(try, "http://") == 0 && inclHTTP {
return true
} else if strings.Index(try, "https://") == 0 && inclHTTPS {
return true
}
return false
}