uri – github.com/fredbi/uri Index | Examples | Files

package uri

import "github.com/fredbi/uri"

Package uri is meant to be an RFC 3986 compliant URI builder and parser.

This is based on the work from ttacon/uri (credits: Trey Tacon).

This fork concentrates on RFC 3986 strictness for URI parsing and validation.

Reference: https://tools.ietf.org/html/rfc3986

Tests have been augmented with test suites of URI validators in other languages: perl, python, scala, .Net.

Extra features like MySQL URIs present in the original repo have been removed.

Index

Examples

Variables

var (
	ErrNoSchemeFound    = errors.New("no scheme found in URI")
	ErrInvalidURI       = errors.New("not a valid URI")
	ErrInvalidCharacter = errors.New("invalid character in URI")
	ErrInvalidScheme    = errors.New("invalid scheme in URI")
	ErrInvalidQuery     = errors.New("invalid query string in URI")
	ErrInvalidFragment  = errors.New("invalid fragment in URI")
	ErrInvalidPath      = errors.New("invalid path in URI")
	ErrInvalidHost      = errors.New("invalid host in URI")
	ErrInvalidPort      = errors.New("invalid port in URI")
	ErrInvalidUserInfo  = errors.New("invalid userinfo in URI")
	ErrMissingHost      = errors.New("missing host in URI")
)

Validation errors.

Functions

func IsURI

func IsURI(raw string) bool

IsURI tells if a URI is valid according to RFC3986/RFC397.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/fredbi/uri"
)

func main() {
	isValid := uri.IsURI("urn://example.com?query=x#fragment/path") // true
	fmt.Println(isValid)

	isValid = uri.IsURI("//example.com?query=x#fragment/path") // false
	fmt.Println(isValid)

}

Output:

true
false

func IsURIReference

func IsURIReference(raw string) bool

IsURIReference tells if a URI reference is valid according to RFC3986/RFC397.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/fredbi/uri"
)

func main() {
	isValid := uri.IsURIReference("//example.com?query=x#fragment/path") // true
	fmt.Println(isValid)

}

Output:

true

func UsesDNSHostValidation

func UsesDNSHostValidation(scheme string) bool

UsesDNSHostValidation returns true if the provided scheme has host validation that does not follow RFC3986 (which is quite generic), but assume a valid DNS hostname instead.

See: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml

Types

type Authority

type Authority interface {
	UserInfo() string
	Host() string
	Port() string
	Path() string
	String() string
	Validate(...string) error
}

Authority information that a URI contains as specified by RFC3986.

Username and password are given by UserInfo().

type Builder

type Builder interface {
	URI() URI
	SetScheme(scheme string) Builder
	SetUserInfo(userinfo string) Builder
	SetHost(host string) Builder
	SetPort(port string) Builder
	SetPath(path string) Builder
	SetQuery(query string) Builder
	SetFragment(fragment string) Builder

	// Returns the URI this Builder represents.
	String() string
}

Builder builds URIs.

type URI

type URI interface {
	// Scheme the URI conforms to.
	Scheme() string

	// Authority information for the URI, including the "//" prefix.
	Authority() Authority

	// Query returns a map of key/value pairs of all parameters
	// in the query string of the URI.
	Query() url.Values

	// Fragment returns the fragment (component preceded by '#') in the
	// URI if there is one.
	Fragment() string

	// Builder returns a Builder that can be used to modify the URI.
	Builder() Builder

	// String representation of the URI
	String() string

	// Validate the different components of the URI
	Validate() error
}

URI represents a general RFC3986 URI.

func Parse

func Parse(raw string) (URI, error)

Parse attempts to parse a URI and returns an error if the URI is not RFC3986-compliant.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/fredbi/uri"
)

func main() {
	u, err := uri.Parse("https://example.com:8080/path")
	if err != nil {
		fmt.Println("Invalid URI")
	} else {
		fmt.Println(u.String())
	}

}

Output:

https://example.com:8080/path

func ParseReference

func ParseReference(raw string) (URI, error)

ParseReference attempts to parse a URI relative reference and returns an error if the URI is not RFC3986 compliant.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/fredbi/uri"
)

func main() {
	u, err := uri.ParseReference("//example.com/path?a=1#fragment")
	if err != nil {
		fmt.Println("Invalid URI reference")
	} else {
		fmt.Println(u.Fragment())

		params := u.Query()
		fmt.Println(params.Get("a"))
	}
}

Output:

fragment
1

Source Files

uri.go

Version
v0.1.0
Published
Nov 18, 2022
Platform
js/wasm
Imports
5 packages
Last checked
now

Tools for package owners.