Documentation
¶
Overview ¶
Package uri provides helpers for parsing, validating, and inspecting URLs and hostnames, including scheme/protocol handling, local-network detection, and IANA top-level-domain validation.
Index ¶
- Constants
- func GuessProtocolForHostname(hostname string) string
- func GuessSchemeForHostname(hostname string) string
- func Host(url string) string
- func Hostname(value string) string
- func IsLocalHostname(hostname string) bool
- func IsLocalURL(url string) bool
- func IsLoopback(domain string) bool
- func IsPublicIP(ip net.IP) bool
- func IsPublicIPAddress(value string) bool
- func IsSchemeValid(scheme string) bool
- func IsValidHostname(hostname string) bool
- func IsValidIP4Address(ip string) bool
- func IsValidIP6Address(ip string) bool
- func IsValidIPAddress(ip string) bool
- func IsValidTLD(tld string) bool
- func IsValidURL(uri string) bool
- func NormalizeHost(value string) string
- func NotLocalHostname(hostname string) bool
- func NotLocalURL(url string) bool
- func NotLoopback(domain string) bool
- func NotPublicIP(ip net.IP) bool
- func NotPublicIPAddress(value string) bool
- func NotSchemeValid(scheme string) bool
- func NotValidHostname(hostname string) bool
- func NotValidIPAddress(ip string) bool
- func NotValidTLD(tld string) bool
- func NotValidURL(uri string) bool
- func ParseURL(uri string) (*url.URL, error)
- func Path(uri string) string
- func PathAndQuery(uri string) string
- func PrependProtocol(uri string) string
- func Protocol(uri string) string
- func RefreshTLDs()
- func Scheme(uri string) string
- func ValidateHostname(hostname string) error
- func ValidateTLD(tld string) error
- func ValidateURL(uri string) error
Constants ¶
const ProtocolHTTP = "http://"
ProtocolHTTP represents the "http://" protocol string (including the :// suffix)
const ProtocolHTTPS = "https://"
ProtocolHTTPS represents the "https://" protocol string (including the :// suffix)
const ProtocolSuffix = "://"
ProtocolSuffix represents the "://" suffix that follows the protocol name
const SchemeHTTP = "http"
SchemeHTTP represents the "http" network scheme (without the :// suffix)
const SchemeHTTPS = "https"
SchemeHTTPS represents the "https" network scheme (without the :// suffix)
Variables ¶
This section is empty.
Functions ¶
func GuessProtocolForHostname ¶
GuessProtocolForHostname returns the correct protocol for a given hostname. Hosts on the public network always use HTTPS, and hosts on the local network always use HTTP.
func GuessSchemeForHostname ¶
GuessSchemeForHostname returns the correct scheme for a given hostname. Hosts on the public network always use HTTPS, and hosts on the local network always use HTTP.
func Hostname ¶
Hostname returns ONLY the hostname, removing the protocol, port, path, and querystring from a hostname
func IsLocalHostname ¶
IsLocalHostname returns TRUE if the hostname is a local domain — a loopback or mDNS/internal name, or any IP address that is not publicly routable.
func IsLocalURL ¶
IsLocalURL returns TRUE if the URL contains a local hostname
func IsLoopback ¶
IsLoopback returns TRUE if the provided domain is a loopback address. This matches loopback hostnames ("localhost", the RFC 6761 ".localhost" TLD, and common /etc/hosts aliases) as well as any loopback IP, which covers the whole 127.0.0.0/8 block, ::1, and aliases such as "::ffff:127.0.0.1".
func IsPublicIP ¶ added in v0.0.7
IsPublicIP reports whether ip is a globally-routable public address that is safe to connect to. It returns FALSE for loopback, private, link-local (including the 169.254.169.254 cloud-metadata endpoint), unspecified, multicast, broadcast, carrier-grade NAT, and other special-use ranges.
Use this to defend against SSRF: validate the resolved IP at connection time (e.g. in a net.Dialer Control hook) before connecting to a user-supplied host.
func IsPublicIPAddress ¶ added in v0.0.7
IsPublicIPAddress reports whether the given string is a valid IP address that is also a public, safe-to-connect address. An unparseable value returns FALSE.
func IsSchemeValid ¶
IsSchemeValid returns TRUE if the scheme is http or https
func IsValidHostname ¶
IsValidHostname returns TRUE if the provided value contains a valid hostname.
func IsValidIP4Address ¶
IsValidIP4Address checks if the given string is a valid IPv4 address.
func IsValidIP6Address ¶
IsValidIP6Address checks if the given string is a valid IPv6 address.
func IsValidIPAddress ¶
IsValidIPAddress checks if the given string is a valid IP address (either IPv4 or IPv6).
func IsValidTLD ¶
IsValidTLD returns TRUE if the provided "top level domain" is found in the IANA list.
func NormalizeHost ¶ added in v0.1.1
NormalizeHost extracts the bare hostname from any URL-ish value, regardless of its shape: a bare hostname, a "host:port", a full "scheme://" URL, a value with userinfo ("user@host"), or a bracketed IPv6 literal ("[::1]:8080"). The result is lower-cased. Unlike Hostname, the result MAY contain colons, because an IPv6 address (e.g. "::1") is returned in its canonical, unbracketed form.
func NotLocalHostname ¶
NotLocalHostname returns TRUE if the hostname is NOT a local domain
func NotLocalURL ¶
NotLocalURL returns TRUE if the URL does NOT contain a local hostname
func NotLoopback ¶
NotLoopback returns TRUE if the provided domain is NOT a loopback address (otherserver.com, 192.168.0.5)
func NotPublicIP ¶ added in v0.0.7
NotPublicIP returns TRUE if ip is NOT a public, safe-to-connect address.
func NotPublicIPAddress ¶ added in v0.0.7
NotPublicIPAddress returns TRUE if the given string is NOT a public IP address.
func NotSchemeValid ¶
NotSchemeValid returns TRUE if the scheme is NOT http or https
func NotValidHostname ¶ added in v0.0.3
NotValidHostname returns TRUE if the provided value does NOT contain a valid hostname.
func NotValidIPAddress ¶ added in v0.0.3
NotValidIPAddress checks if the given string is NOT a valid IP address.
func NotValidTLD ¶
NotValidTLD returns TRUE if the provided "top level domain" is NOT found in the IANA list.
func NotValidURL ¶
NotValidURL returns TRUE if the URL is NOT valid
func ParseURL ¶
ParseURL is a drop-in replacement for url.Parse that requires a valid hostname and http/https scheme.
func PathAndQuery ¶
PathAndQuery returns the path and query portions of a URL
func PrependProtocol ¶
PrependProtocol adds the correct protocol to the beginning of a URL if needed.
func Protocol ¶
Protocol returns the protocol portion of a URL. If the URL is not valid, then an empty string is returned.
func RefreshTLDs ¶
func RefreshTLDs()
RefreshTLDs loads the most recent TLD list from the IANA website.
func Scheme ¶
Scheme returns the scheme portion of a URL (without the "://" suffix), or an empty string if the URL cannot be parsed.
func ValidateHostname ¶
ValidateHostname validates a hostname and returns an error describing any issues found.
func ValidateTLD ¶
ValidateTLD returns an error if the provided top-level domain is empty or is not found in the IANA list.
func ValidateURL ¶
ValidateURL checks to see that a URL properly formatted, has a valid hostname, and uses http or https as the scheme.
Types ¶
This section is empty.