httpurl

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

README

httpurl

license travis coverage report

Minimalistic Go library to make handling http URLs easier.

httpurl complements Golang's url.URL from the net/url standard library. The desire to implement httpurl stems from:

  • noticing developers are using string manipulation functions to manipulate URLs/URIs. Inevitably, we end up with bits and pieces of incorrect code (especially when mixing regular expressions and URLs for domain validation).
  • the lack of url.MustParse.
  • query manipulation requiring going back and forth from url.Query to url.RawQuery.
  • somewhat inspired by Java's OkHttpUrl library. An early attempt to use a builder pattern didn't make production code any shorter or easier to write.

Documentation

https://pkg.go.dev/github.com/alokmenghrajani/httpurl

Example

Instead of:

u, err := url.Parse("http://example.com/")
if err != nil {
    // Need to handle this error
}
q := u.Query()
q.Add("code", "1234")
u.RawQuery = q.Encode()

You can do:

u := httpurl.MustParse("http://example.com")
AddQueryParam(u, "code", 1234)

* * *

And instead of :

basePath := "https://example.com/users/%d/products/%s/"

baseURL, err := url.Parse(fmt.Sprintf(basePath, userId, productId))
if err != nil {
    // Need to handle this error
}

You can do:

basePath := "https://example.com/users/{userId}/products/{productId}/"

baseURL := httpurl.MustParse(basePath)
httpurl.ExpandPath(baseURL, httpurl.ExpandMap{
  "userId": userId,
  "productId": productId,
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPathSegment

func AddPathSegment(u *url.URL, newpath string)

AddPathSegment adds a path segment. It is safe to use this function with externally controlled data; newpath is escaped. A malicious user cannot leverage tricks involving ".." or "/". An empty newpath is ignored.

func AddQueryParam

func AddQueryParam(u *url.URL, key string, value interface{})

AddQueryParam adds the value to key in the query parameters. It appends to any existing values associated with key.

func Clone

func Clone(u *url.URL) *url.URL

func ExpandPath

func ExpandPath(u *url.URL, values ExpandMap) error

ExpandPath enables replacing a URL template with concrete values. E.g. ExpandPath(http://example.com/{a}/xyz/{b}, {"a": "foo", "b": 123}) results in http://example.com/foo/xyz/123

func IsDomain

func IsDomain(u *url.URL, domain string) bool

IsDomain checks if a domain matches a url.URL. E.g. given http://www.example.com/, IsDomain returns true only for "www.example.com".

func IsDomainOrSubdomainOf

func IsDomainOrSubdomainOf(u *url.URL, domain string) bool

IsDomainOrSubdomainOf checks if a url.URL is a domain or subdomain of a given domain. E.g. given http://www.example.com/, IsDomainOrSubdomainOf returns true for "www.example.com", "example.com", and "com".

func IsSubdomainOf

func IsSubdomainOf(u *url.URL, domain string) bool

IsSubdomainOf checks if a url.URL is a subdomain of a given domain. E.g. given http://www.example.com/, IsSubdomainOf returns true for "example.com", as well as for "com".

func MustParse

func MustParse(rawurl string) *url.URL

Parses a rawurl (must be an absolute URI) and panics in case of error.

func RemovePathSegment

func RemovePathSegment(u *url.URL, index int)

RemovePathSegment drops a path segment at a given segment, counting from 0. E.g. RemovePathSegment(http://example.com/foo/bar/xyz, 1) would result in http://example.com/foo/xyz

func SetQueryParam

func SetQueryParam(u *url.URL, key string, value interface{})

SetQueryParam sets the key to value in the query parameters. It replaces any existing values.

Types

type ExpandMap

type ExpandMap map[string]interface{}

Jump to

Keyboard shortcuts

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