url

package module
v0.0.0-...-9c7c343 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: Apache-2.0 Imports: 4 Imported by: 1

README

Go Reference

URL validation

Comprehensive URL validation suitable for sanitizing/normalizing untrusted inputs.

Usage

go get github.com/jbrekelmans/go-url@latest

import jurl "github.com/jbrekelmans/go-url"

u, err := jurl.ValidateURL("https://bla@github.com:443//?foo=bar#xyz", jurl.ValidateURLOptions{
    Abs:                      jurl.NewBool(true),  // Require the URL to be absolute
    AllowedSchemes:           []string{"https"},   // Only allow scheme https
    NormalizePort:            jurl.NewBool(false), // Remove port if it is the default port for the URL.
    StripFragment:            true,
    StripQuery:               true,                // Remove query string.
    StripPathTrailingSlashes: true,
    StripUser:                true,
})
if err != nil {
    log.Fatal(err)
}
log.Printf("%v", u) // Outputs https://github.com

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBool

func NewBool(b bool) *bool

NewBool returns a pointer to a boolean with value b.

func NormalizePort

func NormalizePort(u *url.URL, preferExplicitPort bool, schemeDefaultPorts func(scheme string) int) error

NormalizePort normalizes the port of u. If u's port is a named port then an error is returned. If u's port is the default port for scheme u.Scheme and preferExplicitPort is false then u's port is removed. If u does not have a port and preferExplicitPort is true then u's port is set to the default port for scheme u.Scheme. The default port for scheme x is defined by schemeDefaultPorts(x). If schemeDefaultPorts does not define a port of scheme x then it should return -1. If schemeDefaultPorts(u.Scheme) < 0 then NormalizePort returns an error (because the default port is undefined). If schemeDefaultPorts is nil then NormalizePort behaves as if schemeDefaultPorts is set to SchemeDefaultPorts. If u is not absolute then NormalizePort does not modify u.

func SchemeDefaultPorts

func SchemeDefaultPorts(s string) int

SchemeDefaultPorts returns the default port for scheme s. If no default port is defined for scheme s then returns -1.

func ValidateURL

func ValidateURL(s string, opts ValidateURLOptions) (*url.URL, error)

ValidateURL parses and validates a URI/URL. Let u be the *url.URL as defined by url.Parse(s). ValidateURL returns u if and only if no error occurs. If opts.Abs != nil and u.IsAbs() != *opts.Abs then an error is returned. If len(opts.AllowedSchemes) > 0 and !u.IsAbs() and u.Scheme is not in opts.AllowedSchemes then an error is returned. If opts.NormalizePort != nil then:

  1. ValidateURL calls NormalizePort(u, *opts.NormalizePort, opts.SchemeDefaultPorts).
  2. ValidateURL considers named ports invalid and if u.IsAbs() and u.Port() is not an integer then an error is returned.

If opts.StripPathTrailingSlashes then the longest trailing sequence of forward slashes is trimmed from u.Path and u.RawPath.

Unless opts.StripPathTrailingSlashesNoPercentEncoded is true, percent encoded forward slashes are also included in this sequence.

For other options see source code.

Types

type ValidateURLOptions

type ValidateURLOptions struct {
	Abs                                      *bool
	AllowedSchemes                           []string
	NormalizePort                            *bool
	SchemeDefaultPorts                       func(scheme string) int
	StripFragment                            bool
	StripQuery                               bool
	StripPathTrailingSlashes                 bool
	StripPathTrailingSlashesNoPercentEncoded bool
	StripUser                                bool
	StripUserPassword                        bool
	User                                     *bool
	UserPassword                             *bool
}

ValidateURLOptions represents a set of URL validation options accepted by ValidateURL.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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