Documentation ¶
Index ¶
- Variables
- type EmptyParserOption
- type IPv4Addr
- type IPv6Addr
- type NameValuePair
- type Parser
- type ParserOption
- func WithAcceptInvalidCodepoints() ParserOption
- func WithAllowSettingPathForNonBaseUrl() ParserOption
- func WithCollapseConsecutiveSlashes() ParserOption
- func WithEncodingOverride(cm *charmap.Charmap) ParserOption
- func WithFailOnValidationError() ParserOption
- func WithFragmentPathPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
- func WithLaxHostParsing() ParserOption
- func WithPathPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
- func WithPercentEncodeSinglePercentSign() ParserOption
- func WithPostParseHostFunc(f func(url *Url, host string) string) ParserOption
- func WithPreParseHostFunc(f func(url *Url, host string) string) ParserOption
- func WithQueryPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
- func WithReportValidationErrors() ParserOption
- func WithSkipEqualsForEmptySearchParamsValue() ParserOption
- func WithSkipTrailingSlashNormalization() ParserOption
- func WithSkipWindowsDriveLetterNormalization() ParserOption
- func WithSpecialFragmentPathPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
- func WithSpecialQueryPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
- func WithSpecialSchemes(special map[string]string) ParserOption
- type PercentEncodeSet
- func (p *PercentEncodeSet) ByteShouldBeEncoded(b byte) bool
- func (p *PercentEncodeSet) Clear(bytes ...uint) *PercentEncodeSet
- func (p *PercentEncodeSet) RuneNotInSet(r rune) bool
- func (p *PercentEncodeSet) RuneShouldBeEncoded(r rune) bool
- func (p *PercentEncodeSet) Set(bytes ...uint) *PercentEncodeSet
- type SearchParams
- func (s *SearchParams) Append(name, value string)
- func (s *SearchParams) Delete(name string)
- func (s *SearchParams) Get(name string) string
- func (s *SearchParams) GetAll(name string) []string
- func (s *SearchParams) Has(name string) bool
- func (s *SearchParams) Iterate(f func(pair *NameValuePair))
- func (s *SearchParams) QueryEscape(st string, output *strings.Builder)
- func (s *SearchParams) Set(name, value string)
- func (s *SearchParams) Sort()
- func (s *SearchParams) SortAbsolute()
- func (s *SearchParams) String() string
- type State
- type Url
- func (u *Url) DecodedPort() int
- func (u *Url) Fragment() string
- func (u *Url) Hash() string
- func (u *Url) Host() string
- func (u *Url) Hostname() string
- func (u *Url) Href(excludeFragment bool) string
- func (u *Url) IsIPv4() bool
- func (u *Url) IsIPv6() bool
- func (u *Url) IsSpecialScheme() bool
- func (u *Url) OpaquePath() bool
- func (u *Url) Parse(ref string) (*Url, error)
- func (u *Url) Password() string
- func (u *Url) Pathname() string
- func (u *Url) Port() string
- func (u *Url) Protocol() string
- func (u *Url) Query() string
- func (u *Url) Scheme() string
- func (u *Url) Search() string
- func (u *Url) SearchParams() *SearchParams
- func (u *Url) SetHash(fragment string)
- func (u *Url) SetHost(host string)
- func (u *Url) SetHostname(host string)
- func (u *Url) SetPassword(password string)
- func (u *Url) SetPathname(path string)
- func (u *Url) SetPort(port string)
- func (u *Url) SetProtocol(scheme string)
- func (u *Url) SetSearch(query string)
- func (u *Url) SetUsername(username string)
- func (u *Url) String() string
- func (u *Url) Username() string
- func (u *Url) ValidationErrors() []error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ASCIIAlpha = bitset.New(0x7a)
var ASCIIAlphanumeric = bitset.New(0x7a)
var ASCIIDigit = bitset.New(0x39)
var ASCIIHexDigit = bitset.New(0x66)
var ASCIITabOrNewline = bitset.New(0x0d).Set(0x09).Set(0x0a).Set(0x0d)
var C0OrSpacePercentEncodeSet = NewPercentEncodeSet(0x21)
var C0PercentEncodeSet = NewPercentEncodeSet(0x20)
var C0control = bitset.New(0x1f)
var C0controlOrSpace = bitset.New(0x20).Set(0x20)
var ForbiddenDomainCodePoint = ForbiddenHostCodePoint.Clone().Set(0x25).Set(0x7f)
var ForbiddenHostCodePoint = bitset.New(0x7c).Set(0x00).Set(0x09).Set(0x0a).Set(0x0d).Set(0x20).
Set(0x23).Set(0x2f).Set(0x3a).Set(0x3c).Set(0x3e).Set(0x3f).Set(0x40).Set(0x5b).
Set(0x5c).Set(0x5d).Set(0x5e).Set(0x7c)
var FragmentPercentEncodeSet = C0OrSpacePercentEncodeSet.Set(0x22, 0x3c, 0x3e, 0x60)
var HostPercentEncodeSet = C0OrSpacePercentEncodeSet.Set(0x23)
var PathPercentEncodeSet = QueryPercentEncodeSet.Set(0x3f, 0x60, 0x7b, 0x7d)
var QueryPercentEncodeSet = C0OrSpacePercentEncodeSet.Set(0x22, 0x23, 0x3C, 0x3E)
var SpecialQueryPercentEncodeSet = QueryPercentEncodeSet.Set(0x27)
var UserInfoPercentEncodeSet = PathPercentEncodeSet.Set(0x2f, 0x3a, 0x3b, 0x3d, 0x40, 0x5b, 0x5c, 0x5d, 0x5e, 0x7c)
Functions ¶
This section is empty.
Types ¶
type EmptyParserOption ¶
type EmptyParserOption struct{}
EmptyParserOption does not alter the parser configuration. It can be embedded in another structure to build custom parser options.
type NameValuePair ¶
type NameValuePair struct {
Name, Value string
}
type Parser ¶
type Parser interface { Parse(rawUrl string) (*Url, error) ParseRef(rawUrl, ref string) (*Url, error) BasicParser(urlOrRef string, base *Url, url *Url, stateOverride State) (*Url, error) PercentEncodeString(s string, tr *PercentEncodeSet) string NewUrl() *Url }
func NewParser ¶
func NewParser(opts ...ParserOption) Parser
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { p := url.NewParser(url.WithAcceptInvalidCodepoints(), url.WithCollapseConsecutiveSlashes()) u, err := p.Parse("http://example*.com/a//d?b#c") if err == nil { fmt.Println(u) } }
Output: http://example*.com/a/d?b#c
type ParserOption ¶
type ParserOption interface {
// contains filtered or unexported methods
}
ParserOption configures how we parse a URL.
func WithAcceptInvalidCodepoints ¶
func WithAcceptInvalidCodepoints() ParserOption
WithAcceptInvalidCodepoints percent encodes values which are not valid UTF-8.
This API is EXPERIMENTAL.
func WithAllowSettingPathForNonBaseUrl ¶
func WithAllowSettingPathForNonBaseUrl() ParserOption
WithAllowSettingPathForNonBaseUrl allows to set path for a url which cannot be a base url. WhathWg standard says this should be illegal
This API is EXPERIMENTAL.
func WithCollapseConsecutiveSlashes ¶
func WithCollapseConsecutiveSlashes() ParserOption
WithCollapseConsecutiveSlashes collapses consecutive slashes in path into one (e.g. http://example.com//foo///bar => http://example.com/foo/bar).
func WithEncodingOverride ¶
func WithEncodingOverride(cm *charmap.Charmap) ParserOption
WithEncodingOverride allows to set an encoding other than UTF-8 when parsing.
This API is EXPERIMENTAL.
func WithFailOnValidationError ¶
func WithFailOnValidationError() ParserOption
WithFailOnValidationError makes the parser throw an error on non fatal validation errors.
func WithFragmentPathPercentEncodeSet ¶
func WithFragmentPathPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
WithFragmentPathPercentEncodeSet allows to set an alternative set of characters to percent encode in fragment component when scheme is not special.
This API is EXPERIMENTAL.
func WithLaxHostParsing ¶
func WithLaxHostParsing() ParserOption
WithLaxHostParsing ignores some decoding errors and returns the host as is.
This API is EXPERIMENTAL.
func WithPathPercentEncodeSet ¶
func WithPathPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
WithPathPercentEncodeSet allows to set an alternative set of characters to percent encode in path component.
This API is EXPERIMENTAL.
func WithPercentEncodeSinglePercentSign ¶
func WithPercentEncodeSinglePercentSign() ParserOption
WithPercentEncodeSinglePercentSign percent encodes a '%' which is not followed by two hexadecimal digits instead of complaining about invalid percent encoding.
This API is EXPERIMENTAL.
func WithPostParseHostFunc ¶
func WithPostParseHostFunc(f func(url *Url, host string) string) ParserOption
WithPostParseHostFunc is a function which allows manipulation of host string after it is parsed. It is called only if the host isn't an IP address.
This API is EXPERIMENTAL.
func WithPreParseHostFunc ¶
func WithPreParseHostFunc(f func(url *Url, host string) string) ParserOption
WithPreParseHostFunc is a function which allows manipulation of host string before it is parsed.
This API is EXPERIMENTAL.
func WithQueryPercentEncodeSet ¶
func WithQueryPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
WithQueryPercentEncodeSet allows to set an alternative set of characters to percent encode in query component when scheme is not special.
This API is EXPERIMENTAL.
func WithReportValidationErrors ¶
func WithReportValidationErrors() ParserOption
WithReportValidationErrors records all non fatal validation errors so that they can be fetchd by a call to....
func WithSkipEqualsForEmptySearchParamsValue ¶ added in v0.2.0
func WithSkipEqualsForEmptySearchParamsValue() ParserOption
WithSkipEqualsForEmptySearchParamsValue skips writing '=' when setting an empty value for a search parameter.
e.g. url.SearchParams().Set("name", "") gives 'http://...?name' instead of 'http://...?name='
This API is EXPERIMENTAL.
func WithSkipTrailingSlashNormalization ¶
func WithSkipTrailingSlashNormalization() ParserOption
WithSkipTrailingSlashNormalization skips normalizing of empty paths.
This API is EXPERIMENTAL.
func WithSkipWindowsDriveLetterNormalization ¶
func WithSkipWindowsDriveLetterNormalization() ParserOption
WithSkipWindowsDriveLetterNormalization skips conversion of 'C|' to 'C:'. WhathWg standard says only a normalized Windows drive letter is conforming.
This API is EXPERIMENTAL.
func WithSpecialFragmentPathPercentEncodeSet ¶
func WithSpecialFragmentPathPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
WithSpecialFragmentPathPercentEncodeSet allows to set an alternative set of characters to percent encode in fragment component when scheme is special.
This API is EXPERIMENTAL.
func WithSpecialQueryPercentEncodeSet ¶
func WithSpecialQueryPercentEncodeSet(encodeSet *PercentEncodeSet) ParserOption
WithSpecialQueryPercentEncodeSet allows to set an alternative set of characters to percent encode in query component when scheme is special.
This API is EXPERIMENTAL.
func WithSpecialSchemes ¶
func WithSpecialSchemes(special map[string]string) ParserOption
WithSpecialSchemes allows overriding the notion of special schemes. special is a map of 'scheme' => 'default port'
WhatWg standard removed gopher from special schemes. This is how you add it back:
special := map[string]string{ "ftp": "21", "file": "", "http": "80", "https": "443", "ws": "80", "wss": "443", "gopher": "70", }
This API is EXPERIMENTAL.
type PercentEncodeSet ¶
type PercentEncodeSet struct {
// contains filtered or unexported fields
}
func NewPercentEncodeSet ¶
func NewPercentEncodeSet(allBelow int32, bytes ...uint) *PercentEncodeSet
func (*PercentEncodeSet) ByteShouldBeEncoded ¶
func (p *PercentEncodeSet) ByteShouldBeEncoded(b byte) bool
func (*PercentEncodeSet) Clear ¶
func (p *PercentEncodeSet) Clear(bytes ...uint) *PercentEncodeSet
func (*PercentEncodeSet) RuneNotInSet ¶
func (p *PercentEncodeSet) RuneNotInSet(r rune) bool
func (*PercentEncodeSet) RuneShouldBeEncoded ¶
func (p *PercentEncodeSet) RuneShouldBeEncoded(r rune) bool
func (*PercentEncodeSet) Set ¶
func (p *PercentEncodeSet) Set(bytes ...uint) *PercentEncodeSet
type SearchParams ¶ added in v0.4.0
type SearchParams struct {
// contains filtered or unexported fields
}
SearchParams represents a set of query parameters.
func (*SearchParams) Append ¶ added in v0.4.0
func (s *SearchParams) Append(name, value string)
Append appends a new name/value pair to the search parameters.
func (*SearchParams) Delete ¶ added in v0.4.0
func (s *SearchParams) Delete(name string)
Delete deletes the given search parameter, and its associated value(s), from the search parameters.
func (*SearchParams) Get ¶ added in v0.4.0
func (s *SearchParams) Get(name string) string
Get returns the first value associated with the given search parameter name.
func (*SearchParams) GetAll ¶ added in v0.4.0
func (s *SearchParams) GetAll(name string) []string
GetAll returns all the values associated with the given search parameter name.
func (*SearchParams) Has ¶ added in v0.4.0
func (s *SearchParams) Has(name string) bool
Has returns true if the search parameters contains a parameter with the given name.
func (*SearchParams) Iterate ¶ added in v0.4.0
func (s *SearchParams) Iterate(f func(pair *NameValuePair))
Iterate iterates over the search parameters.
func (*SearchParams) QueryEscape ¶ added in v0.4.0
func (s *SearchParams) QueryEscape(st string, output *strings.Builder)
func (*SearchParams) Set ¶ added in v0.4.0
func (s *SearchParams) Set(name, value string)
Set sets the value associated with name to value. It replaces any existing values associated with name.
func (*SearchParams) Sort ¶ added in v0.4.0
func (s *SearchParams) Sort()
Sort sorts the search parameters by name.
func (*SearchParams) SortAbsolute ¶ added in v0.4.0
func (s *SearchParams) SortAbsolute()
SortAbsolute sorts the search parameters by name and value.
func (*SearchParams) String ¶ added in v0.4.0
func (s *SearchParams) String() string
type State ¶ added in v0.5.0
type State int
const ( NoState State = iota StateSchemeStart StateScheme StateNoScheme StateOpaquePath StateSpecialRelativeOrAuthority StateSpecialAuthoritySlashes StateSpecialAuthorityIgnoreSlashes StatePathOrAuthority StateAuthority StateHost StateHostname StateFile StateFileHost StateFileSlash StatePort StatePath StatePathStart StateQuery StateFragment StateRelative StateRelativeSlash )
type Url ¶
type Url struct {
// contains filtered or unexported fields
}
Url represents a URL.
func (*Url) DecodedPort ¶
func (*Url) Fragment ¶
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Fragment()) }
Output: c
func (*Url) Hash ¶
Hash implements WHATWG url api (https://url.spec.whatwg.org/#api)
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Hash()) }
Output: #c
func (*Url) Host ¶
Host implements WHATWG url api (https://url.spec.whatwg.org/#api)
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Host()) }
Output: example.com
func (*Url) Hostname ¶
Hostname implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) Href ¶
Href implements WHATWG url api (https://url.spec.whatwg.org/#api) If excludeFragment is true, the fragment component will be excluded from the output.
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Href(false)) // http://example.com/a?b#c fmt.Println(u.Href(true)) // http://example.com/a?b }
Output: http://example.com/a?b#c http://example.com/a?b
func (*Url) IsSpecialScheme ¶
func (*Url) OpaquePath ¶ added in v0.5.0
OpaquePath tells if the path is opaque (https://url.spec.whatwg.org/#url-opaque-path)
func (*Url) Password ¶
Password implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) Pathname ¶
Pathname implements WHATWG url api (https://url.spec.whatwg.org/#api)
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Pathname()) }
Output: /a
func (*Url) Port ¶
Port implements WHATWG url api (https://url.spec.whatwg.org/#api)
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Port()) }
Output:
func (*Url) Protocol ¶
Protocol implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) Query ¶
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Query()) }
Output: b
func (*Url) Scheme ¶
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Scheme()) }
Output: http
func (*Url) Search ¶
Search implements WHATWG url api (https://url.spec.whatwg.org/#api)
Example ¶
package main import ( "fmt" "github.com/nlnwa/whatwg-url/url" ) func main() { u, _ := url.Parse("http://example.com:80/a?b#c") fmt.Println(u.Search()) }
Output: ?b
func (*Url) SearchParams ¶
func (u *Url) SearchParams() *SearchParams
SearchParams implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetHash ¶
SetHash implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetHost ¶
SetHost implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetHostname ¶
SetHostname implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetPassword ¶
SetPassword implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetPathname ¶
SetPathname implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetPort ¶
SetPort implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetProtocol ¶
SetProtocol implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetSearch ¶
SetSearch implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) SetUsername ¶
SetUsername implements WHATWG url api (https://url.spec.whatwg.org/#api)
func (*Url) Username ¶
Username implements WHATWG url api (https://url.spec.whatwg.org/#api)