whatwgurl

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("whatwgurl: invalid URL")

ErrInvalid is the error of a parse that fails.

Functions

func AppendForm

func AppendForm(dst []byte, pairs []Pair) []byte

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.

func (*Options) Parse

func (o *Options) Parse(input string, base *URL) (*URL, error)

Parse runs the basic URL parser on input against base (nil: no base) and returns the URL record, or ErrInvalid for failure. The URL keeps o for its setters.

type Pair

type Pair struct{ Name, Value string }

Pair is a name-value tuple of application/x-www-form-urlencoded.

func ParseForm

func ParseForm(s string) []Pair

ParseForm runs the application/x-www-form-urlencoded parser (URL Standard §5.1) on the UTF-8 encoding s. A name or value holds U+FFFD for bytes that do not decode as UTF-8 after percent-decoding.

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

func Parse(input string, base *URL) (*URL, error)

Parse runs the basic URL parser on input against base (nil: no base) with the zero Options.

func (*URL) Clone

func (u *URL) Clone() *URL

Clone returns a copy of u.

func (*URL) Fragment

func (u *URL) Fragment() (f string, ok bool)

Fragment returns u's fragment; ok is false when it is null.

func (*URL) HasOpaquePath

func (u *URL) HasOpaquePath() bool

HasOpaquePath reports whether u's path is opaque.

func (*URL) Hash

func (u *URL) Hash() string

Hash returns '#' and u's fragment, or "" when it is null or empty.

func (*URL) Host

func (u *URL) Host() string

Host returns u's host, serialized, and ':' and the port when it has one; "" when the host is null.

func (*URL) Hostname

func (u *URL) Hostname() string

Hostname returns u's host, serialized; "" when it is null.

func (*URL) Href

func (u *URL) Href() string

Href returns the serialization of u.

func (*URL) Origin

func (u *URL) Origin() string

Origin returns the ASCII serialization of u's origin (HTML §7.1.1), "null" for an opaque origin.

func (*URL) Password

func (u *URL) Password() string

Password returns u's password.

func (*URL) Pathname

func (u *URL) Pathname() string

Pathname returns u's path, serialized.

func (*URL) Port

func (u *URL) Port() string

Port returns u's port, serialized; "" when it is null.

func (*URL) Protocol

func (u *URL) Protocol() string

Protocol returns u's scheme followed by ':'.

func (*URL) Query

func (u *URL) Query() (q string, ok bool)

Query returns u's query; ok is false when it is null.

func (*URL) Scheme

func (u *URL) Scheme() string

Scheme returns u's scheme.

func (*URL) Search

func (u *URL) Search() string

Search returns '?' and u's query, or "" when it is null or empty.

func (*URL) SetHash

func (u *URL) SetHash(v string)

SetHash runs the hash setter.

func (*URL) SetHost

func (u *URL) SetHost(v string)

SetHost runs the host setter.

func (*URL) SetHostname

func (u *URL) SetHostname(v string)

SetHostname runs the hostname setter.

func (*URL) SetPassword

func (u *URL) SetPassword(v string)

SetPassword runs the password setter.

func (*URL) SetPathname

func (u *URL) SetPathname(v string)

SetPathname runs the pathname setter.

func (*URL) SetPort

func (u *URL) SetPort(v string)

SetPort runs the port setter.

func (*URL) SetProtocol

func (u *URL) SetProtocol(v string)

SetProtocol runs the protocol setter.

func (*URL) SetQuery

func (u *URL) SetQuery(q string, ok bool)

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.)

func (*URL) SetSearch

func (u *URL) SetSearch(v string)

SetSearch runs the search setter. The caller updates a URLSearchParams object from Query afterwards.

func (*URL) SetUsername

func (u *URL) SetUsername(v string)

SetUsername runs the username setter.

func (*URL) String

func (u *URL) String() string

String returns the serialization of u.

func (*URL) Username

func (u *URL) Username() string

Username returns u's username.

Jump to

Keyboard shortcuts

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