Git Product home page Git Product logo

urlx's Introduction

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.

urlx's People

Contributors

0xrishabh avatar aidarkhanov avatar omohayui avatar syntaqx avatar vojtechvitek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.