gonormalizer

package module
v0.0.0-...-cb6e050 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: MIT Imports: 6 Imported by: 1

README

gonormalizer

Go Reference Go Report Card

A simple and easy to use URL Normalisation package for GO.

Note: This package does not do URL sanitization or Validation. It's important and recommended to do URL validation before passing url's as function parameters, the package only deals with normalization. If you are using this package in server context it's upto you to perform validation to prevent from various attacks. More information related to normalization can be found here:

Normalize
RFC3986

Install

go get github.com/rosshhun/gonormalizer

Usage/Examples

import (
	"github.com/rosshhun/gonormalizer"
)

func main(){
	u, err := gonormalizer.ForceHttps("http://example.com/")
	if err != nil{
		fmt.Prinltln(err)
	}
	fmt.Prinltln(u)
	//=>https://example.com/
}

Function Signatures

functionName(string) (string, error)
Parameter: string

Pass url as a string to function parameter, it's important to validate the url, with regex or other libraries Available.

Return Type: string

The value of String return type is a 'url' if there are no errors occured, if there is an error occured then the value of the string return type will be "".

Return Type: error

The value of Error return type is a 'nil' if there are no errors occured, if there is an error then the value of error return type will be a custom erorr.

functionName(string, string) (string, error)
Parameter1: string

Pass url as a string to function parameter, it's important to validate the url, with regex or other libraries Available.

Parameter2: string

Pass port number or protocol as a string to function parameter, it's important to validate the url, with regex or other libraries Available.

Return Type: string

The value of String return type is a 'url' if there are no errors occured, if there is an error occured then the value of the string return type will be "".

Return Type: error

The value of Error return type is a 'nil' if there are no errors occured, if there is an error then the value of error return type will be a custom erorr.

functionName(string) bool
Parameter: string

Pass url as a string to function parameter, it's important to validate the url, with regex or other libraries Available.

Return Type: bool

The value of bool return type can be a 'true/fale'

functionName(string) string
Parameter: string

Pass url as a string to function parameter, it's important to validate the url, with regex or other libraries Available.

Return Type: string

The value of String return type is a 'modified url'.

List of Functions

func AddPort(u string, p string) (string, error)
func AddProtocol(u string, p string) (string, error)
func AddTrailingDot(u string) (string, error)
func AddTrailingSlash(u string) (string, error)
func DefaultProtocol(u string) (string, error)
func ForceHttp(u string) (string, error)
func ForceHttps(u string) (string, error)
func IsEmpty(u string) bool
func IsValid(u string) bool
func LowerCase(u string) string
func Normalize(s string) (string, error)
func RemoveTrailingDot(u string) (string, error)
func RemoveTrailingSlash(u string) (string, error)
func Scheme(u string) (string, error)
func StripAuthentication(u string) (string, error)
func StripHash(u string) (string, error)
func StripPort(u string) (string, error)
func StripProtocol(u string) (string, error)
func StripTextFragment(u string) (string, error)
func StripWWW(u string) (string, error)
func TrimURL(u string) string

Examples

AddPort
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.AddPort("http://example.com/", "80")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

AddProtocol
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.AddProtocol("example.com", "https")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

AddTrailingDot
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.AddTrailingDot("example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

AddTrailingSlash
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.AddTrailingSlash("https://example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

DefaultProtocol
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.DefaultProtocol("example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

ForceHttp
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.ForceHttp("https://example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

ForceHttps
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.ForceHttps("http://example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

IsEmpty
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u := gonormalizer.IsEmpty("https://example.com")
	fmt.Println(u)
}

IsValid
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u := gonormalizer.IsValid("https://example.com")
	fmt.Println(u)
}
LowerCase
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u := gonormalizer.LowerCase("HtTPs://example.com")
	fmt.Println(u)
}

Normalize
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.Normalize("//example.com")
	if err != nil{
		fmt.Println(err)
	}
	fmt.Println(u)
}

StripTrailingDot
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripTrailingDot("https://example.com.")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

StripTrailingSlash
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripTrailingSlash("https://example.com/")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

Scheme
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.Scheme("https://example.com/")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

StripAuthentication
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripAuthentication("user:password@example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}
StripHash
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripHash("https://example.com#")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}
StripPort
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripPort("https://example.com:80")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

StripProtocol
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripProtocol("https://example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

StripTextFragment
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripTextFragment("https://example.com#*~text339-+")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

StripWWW
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u, err := gonormalizer.StripWWW("https://www.example.com")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}

TrimURL
package main

import (
	"fmt"

	"github.com/rosshhun/gonormalizer"
)

func main() {
	u := gonormalizer.TrimURL("    https://www.example.com    ")
	fmt.Println(u)
}
Notes

Documentation is available here: pkg.go.dev.

Support

If you do have a contribution to the package, feel free to create a Pull Request or an Issue.

What to contribute

If you don't know what to do, there are some features and functions that need to be done

  • Refactor code
  • Edit docs and README: spellcheck, grammar and typo check
  • Implement benchmarking
  • Implement batch of examples
  • Look at forks for new features and fixes
Advice

Feel free to create what you want, but keep in mind when you implement new features:

  • Code must be clear and readable, names of variables/constants clearly describes what they are doing
  • Public functions must be documented and described in source file and added to README.md to the list of available functions
  • There are must be unit-tests for any new functions and improvements

Documentation

Index

Constants

View Source
const (
	URLPort        = `(:(\d{1,5}))`
	URL            = `^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?`
	HTTP_REGEXP    = `^(?:[a-zA-Z.-]+:)?\/\/`
	WWW_REGEXP     = `(www\.)`
	FHTTP_REGEXP   = `^(?:https:)`
	FHTTPS_REGEXP  = `^(?:http:)`
	SHASH_REGEXP   = `(#.*)`
	SAUTH_REGEXP   = `^((?:\w+:)?\/\/)?[^@/]+@`
	TAIL_REGEXP    = `\/$`
	TAILDOT_REGEXP = `\.$`
	COLON_REGEXP   = `:$`
)

Basic regular expressions for validating strings

Variables

This section is empty.

Functions

func AddPort

func AddPort(u string, p string) (string, error)

AddPort attaches the specified port to the end of URL Accepts URL and Port Number as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string with port number and nil In case of error return is empty string with a customized error

func AddProtocol

func AddProtocol(u string, p string) (string, error)

AddProtocol attaches the specified Protocol to the URL Accepts URL and Protocol as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string with protocol and nil In case of error return is empty string with a customized error

func AddTrailingDot

func AddTrailingDot(u string) (string, error)

AddTrailingDot attaches the dot (.) to the end of URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string and nil In case of error return is empty string with a customized error

func AddTrailingSlash

func AddTrailingSlash(u string) (string, error)

AddTrailingSlash attaches the / to the end of URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string and nil In case of error return is empty string with a customized error

func DefaultProtocol

func DefaultProtocol(u string) (string, error)

DefaultProtocol attaches the http:// to the URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string and nil In case of error return is empty string with a customized error

func ForceHttp

func ForceHttp(u string) (string, error)

ForceHttp converts the URL from https to http Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string and nil In case of error return is empty string with a customized error

func ForceHttps

func ForceHttps(u string) (string, error)

ForceHttps converts the URL from http to https Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string and nil In case of error return is empty string with a customized error

func IsEmpty

func IsEmpty(u string) bool

IsEmpty checks URL is empty or not IsEmpty is used by every function in the library Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns true, else false

func IsValid

func IsValid(u string) bool

IsValid checks if URL is in format with the URL Pattern IsValid is used by every function in the library Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns true, else false

func LowerCase

func LowerCase(u string) string

LowerCase checks if the passed URL is in lowercase Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then it returns the lowercase string

func Normalize

func Normalize(s string) (string, error)

Normalize is a generalized method for normalizing URL Accepts URL as a string argument Returns the normalized string and nil In case of error return is a unambiguous string with a customized error

func Scheme

func Scheme(u string) (string, error)

Scheme presents us with the scheme or portocol of the URL internally Scheme uses url.Parse and scheme functions from url package Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func StripAuthentication

func StripAuthentication(u string) (string, error)

StripAuthentication removes authentication from the end of URL Expected input format "user:password@@example.com", "https://user:password@@example.com" Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func StripHash

func StripHash(u string) (string, error)

StripHash removes the # and contents after #example from URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func StripPort

func StripPort(u string) (string, error)

StripPort detaches the port from URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then returns the modified string with port number and nil In case of error return is empty string with a customized error

func StripProtocol

func StripProtocol(u string) (string, error)

StripProtocol removes the protocol from URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func StripTextFragment

func StripTextFragment(u string) (string, error)

StripTextFragment removes text fragments from the end of URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func StripTrailingDot

func StripTrailingDot(u string) (string, error)

StripTrailingDot removes the dot (.) from the end of URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func StripTrailingSlash

func StripTrailingSlash(u string) (string, error)

StripTrailingSlash removes TrailingSlash / from the end of URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func StripWWW

func StripWWW(u string) (string, error)

StripWWW removes the www. from URL Accepts URL as a string argument if string matches the patterns (pattern is regular expression) then Returns the modified string and nil In case of error return is empty string with a customized error

func TrimURL

func TrimURL(u string) string

TrimURL checks if the passed string URL have any spaces to left or right side Accepts URL as a string argument returns modified string

Types

This section is empty.

Jump to

Keyboard shortcuts

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