url

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: Apache-2.0 Imports: 13 Imported by: 77

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ASCIIAlpha = bitset.New(0x7a)
View Source
var ASCIIAlphanumeric = bitset.New(0x7a)
View Source
var ASCIIDigit = bitset.New(0x39)
View Source
var ASCIIHexDigit = bitset.New(0x66)
View Source
var ASCIITabOrNewline = bitset.New(0x0d).Set(0x09).Set(0x0a).Set(0x0d)
View Source
var C0OrSpacePercentEncodeSet = NewPercentEncodeSet(0x21)
View Source
var C0PercentEncodeSet = NewPercentEncodeSet(0x20)
View Source
var C0control = bitset.New(0x1f)
View Source
var C0controlOrSpace = bitset.New(0x20).Set(0x20)
View Source
var ForbiddenDomainCodePoint = ForbiddenHostCodePoint.Clone().Set(0x25).Set(0x7f)
View Source
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)
View Source
var FragmentPercentEncodeSet = C0OrSpacePercentEncodeSet.Set(0x22, 0x3c, 0x3e, 0x60)
View Source
var HostPercentEncodeSet = C0OrSpacePercentEncodeSet.Set(0x23)
View Source
var PathPercentEncodeSet = QueryPercentEncodeSet.Set(0x3f, 0x60, 0x7b, 0x7d)
View Source
var QueryPercentEncodeSet = C0OrSpacePercentEncodeSet.Set(0x22, 0x23, 0x3C, 0x3E)
View Source
var SpecialQueryPercentEncodeSet = QueryPercentEncodeSet.Set(0x27)
View Source
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 IPv4Addr

type IPv4Addr uint32

func (IPv4Addr) String

func (address IPv4Addr) String() string

type IPv6Addr

type IPv6Addr [8]uint16

func (*IPv6Addr) String

func (address *IPv6Addr) String() string

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 Parse

func Parse(rawUrl string) (*Url, error)

func ParseRef

func ParseRef(rawUrl, ref string) (*Url, error)

func (*Url) DecodedPort

func (u *Url) DecodedPort() int

func (*Url) Fragment

func (u *Url) Fragment() string
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

func (u *Url) Hash() string

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

func (u *Url) Host() string

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

func (u *Url) Hostname() string

Hostname implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) Href

func (u *Url) Href(excludeFragment bool) string

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

func (u *Url) IsIPv4() bool

func (*Url) IsIPv6

func (u *Url) IsIPv6() bool

func (*Url) IsSpecialScheme

func (u *Url) IsSpecialScheme() bool

func (*Url) OpaquePath added in v0.5.0

func (u *Url) OpaquePath() bool

OpaquePath tells if the path is opaque (https://url.spec.whatwg.org/#url-opaque-path)

func (*Url) Parse

func (u *Url) Parse(ref string) (*Url, error)

func (*Url) Password

func (u *Url) Password() string

Password implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) Pathname

func (u *Url) Pathname() string

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

func (u *Url) Port() string

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

func (u *Url) Protocol() string

Protocol implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) Query

func (u *Url) Query() string
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

func (u *Url) Scheme() string
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

func (u *Url) Search() string

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

func (u *Url) SetHash(fragment string)

SetHash implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetHost

func (u *Url) SetHost(host string)

SetHost implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetHostname

func (u *Url) SetHostname(host string)

SetHostname implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetPassword

func (u *Url) SetPassword(password string)

SetPassword implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetPathname

func (u *Url) SetPathname(path string)

SetPathname implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetPort

func (u *Url) SetPort(port string)

SetPort implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetProtocol

func (u *Url) SetProtocol(scheme string)

SetProtocol implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetSearch

func (u *Url) SetSearch(query string)

SetSearch implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) SetUsername

func (u *Url) SetUsername(username string)

SetUsername implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) String

func (u *Url) String() string

func (*Url) Username

func (u *Url) Username() string

Username implements WHATWG url api (https://url.spec.whatwg.org/#api)

func (*Url) ValidationErrors

func (u *Url) ValidationErrors() []error

Jump to

Keyboard shortcuts

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