Documentation
¶
Overview ¶
Package whatwgurl implements the URL Standard (https://url.spec.whatwg.org/): the basic URL parser with the state overrides of the URL API's setters, the host parser with domain to ASCII (UTS #46, Unicode 17.0.0), the URL serializer, origins, and application/x-www-form-urlencoded.
Inputs are strings of scalar values in valid UTF-8; a caller holding UTF-16 replaces lone surrogates with U+FFFD before it calls in.
The parser works on the bytes of the UTF-8 input. That is equivalent to the standard's code points: no code point above U+007F is a delimiter, and every percent-encode set holds them all, so UTF-8 percent-encoding a code point percent-encodes its bytes.
Index ¶
- Variables
- func AppendForm(dst []byte, pairs []Pair) []byte
- type Options
- type Pair
- type URL
- func (u *URL) Clone() *URL
- func (u *URL) Fragment() (f string, ok bool)
- func (u *URL) HasOpaquePath() bool
- func (u *URL) Hash() string
- func (u *URL) Host() string
- func (u *URL) Hostname() string
- func (u *URL) Href() string
- func (u *URL) Origin() string
- func (u *URL) Password() string
- func (u *URL) Pathname() string
- func (u *URL) Port() string
- func (u *URL) Protocol() string
- func (u *URL) Query() (q string, ok bool)
- func (u *URL) Scheme() string
- func (u *URL) Search() string
- func (u *URL) SetHash(v string)
- func (u *URL) SetHost(v string)
- func (u *URL) SetHostname(v string)
- func (u *URL) SetPassword(v string)
- func (u *URL) SetPathname(v string)
- func (u *URL) SetPort(v string)
- func (u *URL) SetProtocol(v string)
- func (u *URL) SetQuery(q string, ok bool)
- func (u *URL) SetSearch(v string)
- func (u *URL) SetUsername(v string)
- func (u *URL) String() string
- func (u *URL) Username() string
Constants ¶
This section is empty.
Variables ¶
var ErrInvalid = errors.New("whatwgurl: invalid URL")
ErrInvalid is the error of a parse that fails.
Functions ¶
func AppendForm ¶
AppendForm appends the application/x-www-form-urlencoded serialization (URL Standard §5.2) of pairs to dst. Names and values must be valid UTF-8.
Types ¶
type Options ¶
type Options struct {
// NFC, when not nil, returns the Unicode Normalization Form C of s. Domain
// to ASCII (UTS #46) normalizes a host with non-ASCII input to NFC after
// mapping, and fails a label that Punycode decodes to a string that is not
// in NFC. Without NFC both steps are skipped; that changes the result only
// for a host whose mapped input is not in NFC. NFC is called only while
// parsing a host that holds non-ASCII input, on the goroutine that parses.
NFC func(s string) string
}
Options holds what the parser cannot do on its own. The zero value, and a nil *Options, parse without Unicode normalization.
A URL keeps the Options it was parsed with, and its setters use them.
type Pair ¶
type Pair struct{ Name, Value string }
Pair is a name-value tuple of application/x-www-form-urlencoded.
type URL ¶
type URL struct {
// contains filtered or unexported fields
}
URL is a URL record. Its fields are kept as the serialization and the offsets of the components in it, so the getters slice and do not allocate.
func Parse ¶
Parse runs the basic URL parser on input against base (nil: no base) with the zero Options.
func (*URL) HasOpaquePath ¶
HasOpaquePath reports whether u's path is opaque.
func (*URL) Host ¶
Host returns u's host, serialized, and ':' and the port when it has one; "" when the host is null.
func (*URL) Origin ¶
Origin returns the ASCII serialization of u's origin (HTML §7.1.1), "null" for an opaque origin.
func (*URL) SetQuery ¶
SetQuery sets u's query to q, or to null when !ok, as URLSearchParams's update steps do; q must be ASCII that needs no percent-encoding, such as AppendForm's output. (The URL Standard no longer strips trailing spaces from an opaque path here: the parser percent-encodes a space before a query or fragment instead.)