urlx

package module
v0.0.0-...-5e86549 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2023 License: MIT Imports: 12 Imported by: 0

README

URLx

Golang pkg for URL parsing and normalization.

  1. Parsing URL (GoDoc)
  2. Normalizing URL (GoDoc)
  3. Splitting host:port from URL (GoDoc)
  4. Resolving IP address from URL (GoDoc)

GoDoc Travis

Parsing URL

The urlx.Parse() is compatible with the same function from net/url pkg, but has slightly different behavior. It enforces default scheme and favors absolute URLs over relative paths.

Difference between urlx and net/url
github.com/goware/urlx net/url
urlx.Parse("example.com")

&url.URL{ Scheme: "http", Host: "example.com", Path: "", }

url.Parse("example.com")

&url.URL{ Scheme: "", Host: "", Path: "example.com", }

urlx.Parse("localhost:8080")

&url.URL{ Scheme: "http", Host: "localhost:8080", Path: "", Opaque: "", }

url.Parse("localhost:8080")

&url.URL{ Scheme: "localhost", Host: "", Path: "", Opaque: "8080", }

urlx.Parse("user.local:8000/path")

&url.URL{ Scheme: "http", Host: "user.local:8000", Path: "/path", Opaque: "", }

url.Parse("user.local:8000/path")

&url.URL{ Scheme: "user.local", Host: "", Path: "", Opaque: "8000/path", }

Usage
import "github.com/goware/urlx"

func main() {
    url, _ := urlx.Parse("example.com")
    // url.Scheme == "http"
    // url.Host == "example.com"

    fmt.Print(url)
    // Prints http://example.com
}

Normalizing URL

The urlx.Normalize() function normalizes the URL using the predefined subset of Purell flags.

Usage
import "github.com/goware/urlx"

func main() {
    url, _ := urlx.Parse("localhost:80///x///y/z/../././index.html?b=y&a=x#t=20")
    normalized, _ := urlx.Normalize(url)

    fmt.Print(normalized)
    // Prints http://localhost/x/y/index.html?a=x&b=y#t=20
}

Splitting host:port from URL

The urlx.SplitHostPort() is compatible with the same function from net pkg, but has slightly different behavior. It doesn't remove brackets from [IPv6] host.

Usage
import "github.com/goware/urlx"

func main() {
    url, _ := urlx.Parse("localhost:80")
    host, port, _ := urlx.SplitHostPort(url)

    fmt.Print(host)
    // Prints localhost

    fmt.Print(port)
    // Prints 80
}

Resolving IP address from URL

The urlx.Resolve() is compatible with ResolveIPAddr() from net.

Usage
url, _ := urlx.Parse("localhost")
ip, _ := urlx.Resolve(url)

fmt.Print(ip)
// Prints 127.0.0.1

License

URLx is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Normalize

func Normalize(u *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() funcs.

func Parse

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

func ParseUnsafe

func ParseUnsafe(rawURL string) (u *url.URL)
func RealLink(link string) (real string)

делает запрос и смотрит если прокси

func RealLinkClear

func RealLinkClear(link string) (real string, err error)

делает запрос и смотрит если прокси

func RealLinkFasthttp

func RealLinkFasthttp(link string) (real string)

делает запрос и смотрит если прокси

func RemoveURLParams

func RemoveURLParams(u *url.URL)

func Resolve

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

Resolve resolves the URL host to its IP address.

func ResolveString

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

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

func SplitHostPort

func SplitHostPort(u *url.URL) (host, port string, err 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