urlx

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT, MIT Imports: 8 Imported by: 0

README

github.com/evg4b/uncors/pkg/urlx

Golang pkg for URL parsing and normalization. Repository forked from goware/urlx specifically for uncors project.

Changes:

  • Removed default http scheme
  • Symbol * allowed in urls

License

github.com/evg4b/uncors/pkg/urlx is licensed under the MIT License.

Documentation

Overview

Package urlx parses and normalizes URLs. It can also resolve hostname to an IP address.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyHost   = errors.New("empty host")
	ErrEmptyPort   = errors.New("empty port")
	ErrInvalidHost = errors.New("invalid host")
	ErrEmptyURL    = errors.New("empty url")
)

Functions

func Normalize

func Normalize(parsedURL *url.URL) (string, error)

Normalize returns normalized URL string. Behavior: 1. Remove unnecessary host dots. 2. Remove default port (http://localhost:80 becomes http://localhost). 3. Remove duplicate slashes. 4. Remove unnecessary dots from path. 5. Sort query parameters. 6. Decode host IP into decimal numbers. 7. Handle escape values. 8. Decode Punycode domains into UTF8 representation.

func NormalizeString

func NormalizeString(rawURL string) (string, error)

NormalizeString returns normalized URL string. It's a shortcut for Parse() and Normalize() functions.

func Parse

func Parse(rawURL string) (*url.URL, error)

Parse parses raw URL string into the net/url URL struct. It uses the url.Parse() internally, but it slightly changes its behavior:

  1. It forces the default scheme and port to http
  2. It favors absolute paths over relative ones, thus "example.com" is parsed into url.Host instead of url.Path.
  3. It lowercase's the Host (not only the Scheme).

func ParseWithDefaultScheme

func ParseWithDefaultScheme(rawURL string, scheme string) (*url.URL, error)

func Resolve

func Resolve(parsedURL *url.URL) (*net.IPAddr, error)

Resolve resolves the URL host to its IP address.

func ResolveString

func ResolveString(rawURL string) (*net.IPAddr, error)

ResolveString resolves the URL host to its IP address. It's a shortcut for Parse() and Resolve() functions.

func SplitHostPort

func SplitHostPort(parsedURL *url.URL) (string, string, error)

SplitHostPort splits network address of the form "host:port" into host and port. Unlike net.SplitHostPort(), it doesn't remove brackets from [IPv6] host, and it accepts net/url.URL struct instead of a string.

func URIEncode

func URIEncode(uri string) (string, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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