MCPcopy Index your code
hub / github.com/cloudflare/cloudflared / ValidateHostname

Function ValidateHostname

validation/validation.go:29–60  ·  view source on GitHub ↗
(hostname string)

Source from the content-addressed store, hash-verified

27)
28
29func ValidateHostname(hostname string) (string, error) {
30 if hostname == "" {
31 return "", nil
32 }
33 // users gives url(contains schema) not just hostname
34 if strings.Contains(hostname, ":") || strings.Contains(hostname, "%3A") {
35 unescapeHostname, err := url.PathUnescape(hostname)
36 if err != nil {
37 return "", fmt.Errorf("Hostname(actually a URL) %s has invalid escape characters %s", hostname, unescapeHostname)
38 }
39 hostnameToURL, err := url.Parse(unescapeHostname)
40 if err != nil {
41 return "", fmt.Errorf("Hostname(actually a URL) %s has invalid format %s", hostname, hostnameToURL)
42 }
43 asciiHostname, err := idna.ToASCII(hostnameToURL.Hostname())
44 if err != nil {
45 return "", fmt.Errorf("Hostname(actually a URL) %s has invalid ASCII encdoing %s", hostname, asciiHostname)
46 }
47 return asciiHostname, nil
48 }
49
50 asciiHostname, err := idna.ToASCII(hostname)
51 if err != nil {
52 return "", fmt.Errorf("Hostname %s has invalid ASCII encdoing %s", hostname, asciiHostname)
53 }
54 hostnameToURL, err := url.Parse(asciiHostname)
55 if err != nil {
56 return "", fmt.Errorf("Hostname %s is not valid", hostnameToURL)
57 }
58 return hostnameToURL.RequestURI(), nil
59
60}
61
62// ValidateUrl returns a validated version of `originUrl` with a scheme prepended (by default http://).
63// Note: when originUrl contains a scheme, the path is removed:

Callers 4

sshGenFunction · 0.92
TunnelCommandFunction · 0.92
TestValidateHostnameFunction · 0.85
validateUrlStringFunction · 0.85

Calls 2

ErrorfMethod · 0.80
HostnameMethod · 0.45

Tested by 1

TestValidateHostnameFunction · 0.68