urlutil

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHost = errors.New("invalid host")

Error for an authority whose host cannot be read: unbalanced brackets, brackets around anything but an IPv6 or IPvFuture literal, or text NFKC turns into delimiters

View Source
var ErrPortRange = errors.New("port out of range 0-65535")

Error for a port number above 65535

Functions

func BaseHost

func BaseHost(baseURL string) (string, error)

Lowercased host of a base URL that may lack a scheme, trailing dots removed, empty when blank or hostless, an ErrInvalidHost error for an authority Split refuses

func BaseHostMatches

func BaseHostMatches(baseURL, domain string) (bool, error)

Whether the host of a base URL is the domain or one of its subdomains, compared without case or trailing dots

func BaseOrigin

func BaseOrigin(baseURL string) (scheme, host string, port int, err error)

Lowercased scheme, host and effective port of a base URL, 443 or 80 standing in for an absent http(s) port, all empty with port 0 when hostless or the port is not a number up to 65535

func BasePath

func BasePath(baseURL string) (string, error)

Lowercased path of a base URL without trailing slashes, empty for a bare host, an ErrInvalidHost error for an authority Split refuses

func EncodeQuery

func EncodeQuery(pairs [][2]string) string

Query string of name value pairs in the order given, names and values escaped as url.QueryEscape does

func Escape

func Escape(s, keep string) string

s percent-encoded byte by byte with upper case hex, ASCII letters, digits, -._~ and the ASCII bytes of keep left as they are

func FilePath

func FilePath(fileURL string) (string, error)

Local path a file URL names, percent escapes decoded; an empty, localhost or own host name authority is local, another one is a UNC share on Windows and a *FileURLError elsewhere, as is a scheme other than file

func FileURL

func FileURL(path string) string

file URL of a local path made absolute first, file:///C:/... for a Windows drive path and file://server/share/... for a UNC path

func FormatHost

func FormatHost(host string) string

Host as it goes in a URL authority: surrounding space trimmed, an IPv6 literal bracketed with its zone percent escaped as %25

func LookupFilePath

func LookupFilePath(ctx context.Context, fileURL string) (string, error)

Local path a file URL names like FilePath, an authority that resolves to a loopback address or one of this machine's interface addresses also counting as local

func Port

func Port(u *url.URL) (int, bool, error)

Port number of a parsed URL, false when it names none, an error wrapping ErrPortRange for a number past 65535

func QueryPairs

func QueryPairs(query string) [][2]string

Name value pairs of a query string in their order, a plus sign read as a space, a malformed escape kept as text and a name without = given an empty value

func Unescape

func Unescape(s string) string

s with every %XX escape decoded to its byte, any other percent sign and every other byte kept as it is

Types

type FileURLError

type FileURLError struct {
	URL, Reason string
}

Error for a URL that names no file on this machine: another scheme, or a file URL naming another host

func (*FileURLError) Error

func (e *FileURLError) Error() string

Reason followed by the quoted URL

type Parts

type Parts struct {
	Scheme, Authority, Path, Query, Fragment string
}

Components of a URL reference cut at its delimiters, nothing decoded, the scheme lowercased and the authority holding any user info and port

func Split

func Split(ref string) (Parts, error)

Reference cut into scheme, authority, path, query and fragment the way RFC 3986 appendix B reads any text, after dropping leading C0 controls and spaces and every tab, CR and LF; an ErrInvalidHost error only for bad IP literal brackets or a non-ASCII authority that NFKC normalization gives URL delimiters

func (Parts) Hostname

func (p Parts) Hostname() string

Host of the authority, user info and port dropped, IP literal brackets removed, lowercased with an IPv6 zone kept as written

func (Parts) Port

func (p Parts) Port() (int, bool, error)

Port number of the authority, false when it names none, an error when it is not a decimal number, one wrapping ErrPortRange past 65535

func (Parts) String

func (p Parts) String() string

Reference put back together from its components, // written before a non-empty authority or a path that starts with //, an empty query or fragment left out

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL