uri

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2022 License: MIT Imports: 8 Imported by: 3

README

opinionated URI parsing library heavily inspired by fasthttp and the standard net/url libraries. a fasthttp-like API, with stronger encode checking and opaque support of net/url.

how is it opinionated? paths are automatically normalized by a minimal allocation path library i wrote, so the URI.Path() will be the shortest possible version of the raw input, with .. and extra / removed. if you need the raw input path that is still available via the URI.RawPath() member.

benchmarks faster than both net/url and fasthttp -- run them yourself if you'd like to compare

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadPercentEncode = errors.New("uri: bad percent encoding")
	ErrShouldBeEscaped  = errors.New("uri: byte should be escaped")
)

Percent encode parsing errors

View Source
var (
	ErrContainsCtrlByte = errors.New("uri: contains ascii ctrl byte")
	ErrInvalidScheme    = errors.New("uri: invalid scheme")
	ErrInvalidIPLiteral = errors.New("uri: invalid ip literal")
	ErrInvalidPort      = errors.New("uri: invalid port after host")
)

URI parsing errors

Functions

func AuthUnescape

func AuthUnescape(into *[]byte, b []byte) error

func FragmentUnescape

func FragmentUnescape(into *[]byte, b []byte) error

func HasAsciiControlBytes

func HasAsciiControlBytes(b []byte) bool

HasAsciiControlBytes returns whether a byte slice contains ASCII control bytes

func HostUnescape

func HostUnescape(into *[]byte, b []byte) error

func IsValidPercentEncoding

func IsValidPercentEncoding(b []byte) (bool, error)

IsValidPercentEncoding returns error on bad percent encoding within the supplied byte slice. Bool returned is whether you need to escape the slice. This only checks for valid path percent-encoding

func PathEscape

func PathEscape(into *[]byte, b []byte)

func PathUnescape

func PathUnescape(into *[]byte, b []byte) error

func QueryEscape

func QueryEscape(into *[]byte, b []byte)

func QueryUnescape

func QueryUnescape(into *[]byte, b []byte) error

func ReleaseQueryParams

func ReleaseQueryParams(params *QueryParams)

ReleaseQueryParams resets and releases a QueryParams object back to the pool

func ReleaseURI

func ReleaseURI(uri *URI)

ReleaseURI resets and releases a URI object back to the pool

func ZoneUnescape

func ZoneUnescape(into *[]byte, b []byte) error

Types

type Bytes

type Bytes []byte

Bytes is the simplest possible implementation of bytes.Bytes, with convenient length and capacity methods

func (Bytes) Bytes

func (b Bytes) Bytes() []byte

func (Bytes) Cap

func (b Bytes) Cap() int

func (Bytes) Len

func (b Bytes) Len() int

func (Bytes) String

func (b Bytes) String() string

func (Bytes) StringPtr

func (b Bytes) StringPtr() string

type QueryParams

type QueryParams struct {
	// contains filtered or unexported fields
}

QueryParams represents parsed query parameters

func AcquireQueryParams

func AcquireQueryParams() *QueryParams

AcquireQueryParams acquires a QueryParams object from the global pool

func NewParams

func NewParams() QueryParams

NewParams returns a new initialized QueryParams object

func (*QueryParams) Add

func (q *QueryParams) Add(key []byte, value []byte, join byte)

Add adds the supplied value to the parameter at supplied key, using the supplied 'join' as a separator byte. A new parameter will be created if necessary

func (*QueryParams) AddString

func (q *QueryParams) AddString(key string, value string, join byte)

AddString adds the supplied value to the parameter at supplied key, using the supplied 'join' as a separator byte. A new parameter will be created if necessary

func (*QueryParams) CopyTo

func (q *QueryParams) CopyTo(dst *QueryParams)

CopyTo copies the current state of QueryParams into the other object

func (*QueryParams) Delete

func (q *QueryParams) Delete(key []byte)

Delete deletes the paramater with key from the QueryParams object

func (*QueryParams) DeleteString

func (q *QueryParams) DeleteString(key string)

DeleteString deletes the paramater with key from the QueryParams object

func (*QueryParams) Get

func (q *QueryParams) Get(key []byte) (Bytes, bool)

Get attempts to return the param value for supplied key

func (*QueryParams) GetString

func (q *QueryParams) GetString(key string) (Bytes, bool)

GetString attempts to return the param value for supplied key

func (*QueryParams) Len

func (q *QueryParams) Len() int

Len returns the number of query key-value pairs

func (*QueryParams) Params

func (q *QueryParams) Params() bytes.Bytes

Params returns the the current state of the QueryParams object as bytes.Bytes

func (*QueryParams) Parse

func (q *QueryParams) Parse(b []byte) error

Parse parses the supplied query bytes into the QueryParams object, returning error on invalid percent encoding

func (*QueryParams) ParseString

func (q *QueryParams) ParseString(s string) error

ParseString parses the supplied query bytes into the QueryParams object, returning error on invalid percent encoding

func (*QueryParams) Reset

func (q *QueryParams) Reset()

Reset resets the QueryParams object

func (*QueryParams) Set

func (q *QueryParams) Set(key []byte, value []byte)

Set sets the parameter with key, to the supplied value. A new parameter will be created if necessary

func (*QueryParams) SetString

func (q *QueryParams) SetString(key string, value string)

SetString sets the parameter with key, to the supplied value. A new parameter will be created if necessary

func (*QueryParams) Sort

func (q *QueryParams) Sort()

Sort sorts the query parameters alphabetically by key

type URI

type URI struct {
	// contains filtered or unexported fields
}

URI represents a parsed URI structure

func AcquireURI

func AcquireURI() *URI

AcquireURI acquires a URI object from the global pool

func New

func New() URI

New returns a newly allocated URI object

func (*URI) CopyTo

func (u *URI) CopyTo(dst *URI)

CopyTo copies the current URI data to the supplied URI

func (*URI) Fragment

func (u *URI) Fragment() bytes.Bytes

Fragment returns the fragment byte buffer

func (*URI) FullURI

func (u *URI) FullURI() bytes.Bytes

FullURI returns the full URI of form: {Scheme}://{Host}/{Path}?{Query}#{Fragment} or {Scheme}:{Opaque}?{Query}#{Fragment}

func (*URI) Host

func (u *URI) Host() bytes.Bytes

Host returns the host byte buffer

func (*URI) Opaque

func (u *URI) Opaque() bytes.Bytes

Opaque returns the opaque byte buffer

func (*URI) Parse

func (u *URI) Parse(uri []byte) error

Parse attempts to parse supplied URI bytes into the receiving object

func (*URI) ParseRequest

func (u *URI) ParseRequest(uri []byte) error

func (*URI) ParseRequestString

func (u *URI) ParseRequestString(uri string) error

func (*URI) ParseString

func (u *URI) ParseString(uri string) error

ParseString attempts to parse supplied URI string into the receiving object

func (*URI) Password

func (u *URI) Password() bytes.Bytes

Password returns the password byte buffer

func (*URI) Path

func (u *URI) Path() bytes.Bytes

Path returns the normalized path byte buffer

func (*URI) Query

func (u *URI) Query() *QueryParams

Query returns the query parameters, parsing raw query if necessary

func (*URI) RawPath

func (u *URI) RawPath() bytes.Bytes

RawPath returns the raw path (i.e. not normalized) byte buffer

func (*URI) RawQuery

func (u *URI) RawQuery() bytes.Bytes

RawQuery returns the raw query byte buffer

func (*URI) RequestURI

func (u *URI) RequestURI() bytes.Bytes

RequestURI returns the request URI of form: /{Path}?{Query}

func (*URI) Reset

func (u *URI) Reset()

func (*URI) Scheme

func (u *URI) Scheme() bytes.Bytes

Scheme returns the schem byte buffer

func (*URI) SetFragment

func (u *URI) SetFragment(b []byte)

SetFragment sets the fragment byte buffer to the supplied bytes

func (*URI) SetFragmentString

func (u *URI) SetFragmentString(s string)

SetFragmentString sets the fragment byte buffer to the supplied string

func (*URI) SetHost

func (u *URI) SetHost(b []byte)

SetHost sets the host byte buffer to the supplied bytes, formatted to lowercase

func (*URI) SetHostString

func (u *URI) SetHostString(s string)

SetHostString sets the host byte buffer to the supplied string, formatted to lowercase

func (*URI) SetOpaque

func (u *URI) SetOpaque(b []byte)

SetOpaque sets the opaque byte buffer to the supplied bytes

func (*URI) SetOpaqueString

func (u *URI) SetOpaqueString(s string)

SetOpaqueString sets the host byte buffer to the supplied string

func (*URI) SetPassword

func (u *URI) SetPassword(b []byte)

SetPassword sets the password byte buffer to the supplied bytes

func (*URI) SetPasswordString

func (u *URI) SetPasswordString(s string)

SetPasswordStrings sets the password byte buffer to the suppied string

func (*URI) SetPath

func (u *URI) SetPath(b []byte)

SetPath sets the raw path byte buffer to supplied bytes, and sets the normalized path buffer to the normalized form of this

func (*URI) SetPathString

func (u *URI) SetPathString(s string)

SetPathString sets the raw path byte buffer to supplied string, and sets the normalized path buffer to the normalized form of this

func (*URI) SetQuery

func (u *URI) SetQuery(b []byte)

SetQuery sets the query byte buffer to the supplied bytes

func (*URI) SetQueryString

func (u *URI) SetQueryString(s string)

SetQueryString sets the query byte buffer to the supplied string

func (*URI) SetScheme

func (u *URI) SetScheme(b []byte)

Scheme sets the scheme byte buffer to the supplied bytes, formatted to lowercase

func (*URI) SetSchemeString

func (u *URI) SetSchemeString(s string)

SetSchemeString sets the scheme byte buffer to the supplied string, formatted to lowercase

func (*URI) SetUsername

func (u *URI) SetUsername(b []byte)

SetUsername sets the username byte buffer to the supplied bytes

func (*URI) SetUsernameString

func (u *URI) SetUsernameString(s string)

SetUsernameString sets the username byte buffer to the supplied string

func (*URI) ToURL

func (u *URI) ToURL() url.URL

ToURL copies the current URI data to a url.URL object valid for the duration that the current URI data is valid (until next parse)

func (*URI) Username

func (u *URI) Username() bytes.Bytes

Username returns the username byte buffer

Jump to

Keyboard shortcuts

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