utils

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2021 License: Apache-2.0 Imports: 12 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultHTTPClient = &http.Client{
	Timeout:   defaultHTTPTimeout,
	Transport: HTTPTransportWithTLSClientConfig(DefaultTLSConfig()),
}

DefaultHTTPClient is a http.Client with a timeout set.

View Source
var DefaultHTTPUserAgent = "Kopano-Konnect/" + version.Version

DefaultHTTPUserAgent is the User-Agent Header which should be used when making HTTP requests.

View Source
var InsecureHTTPClient = &http.Client{
	Timeout:   defaultHTTPTimeout,
	Transport: HTTPTransportWithTLSClientConfig(InsecureSkipVerifyTLSConfig()),
}

InsecureHTTPClient is a http.Client with a timeout set and with TLS verification disabled.

Functions

func DecodeURLSchema added in v0.30.0

func DecodeURLSchema(dst interface{}, src map[string][]string) error

DecodeURLSchema decodes request for mdata in to the provided dst url struct.

func DefaultTLSConfig

func DefaultTLSConfig() *tls.Config

DefaultTLSConfig returns a new tls.Config.

func DescribeError

func DescribeError(err error) error

DescribeError returns a wrapped version for errors which contain additional fields. The wrapped version contains all fields as a string value. Use this for general purpose logging of rich errors.

func ErrorAsFields

func ErrorAsFields(err error) map[string]interface{}

ErrorAsFields returns a mapping of all fields of the provided error.

func HTTPTransportWithTLSClientConfig

func HTTPTransportWithTLSClientConfig(tlsClientConfig *tls.Config) *http.Transport

HTTPTransportWithTLSClientConfig creates a new http.Transport with sane default settings using the provided tls.Config.

func InsecureSkipVerifyTLSConfig

func InsecureSkipVerifyTLSConfig() *tls.Config

InsecureSkipVerifyTLSConfig returns a new tls.Config which does skip TLS verification.

func IsRequestFromTrustedSource

func IsRequestFromTrustedSource(req *http.Request, ips []*net.IP, nets []*net.IPNet) (bool, error)

IsRequestFromTrustedSource checks if the provided requests remote address is one either one of the provided ips or in one of the provided networks.

func OriginFromRequestHeaders

func OriginFromRequestHeaders(header http.Header) string

OriginFromRequestHeaders tries to find information about the origin from the provided http.Header. It first looks into the Origin header field and if that is not found it looks into the Referer header field. If both are not found an empty string is returned.

func WriteErrorPage

func WriteErrorPage(rw http.ResponseWriter, code int, title string, message string)

WriteErrorPage create a formatted error page response containing the provided information and writes it to the provided http.ResponseWriter.

func WriteJSON

func WriteJSON(rw http.ResponseWriter, code int, data interface{}, contentType string) error

WriteJSON marshals the provided data as JSON and writes it to the provided http.ResponseWriter using the provided HTTP status code and content-type. the nature of this function is that it always writes a HTTP response header. Thus it makes no sense to write another header on error. Resulting errors should be logged and the connection should be closes as it is non-functional.

func WriteRedirect

func WriteRedirect(rw http.ResponseWriter, code int, uri *url.URL, params interface{}, asFragment bool) error

WriteRedirect crates a URL out of the provided uri and params and writes a a HTTP response with the provided HTTP status code to the provided http.ResponseWriter incliding HTTP caching headers to prevent caching. If asFragment is true, the provided params are added as URL fragment, otherwise they replace the query. If params is nil, the provided uri is taken as is.

Types

type ErrorWithDescription

type ErrorWithDescription interface {
	error
	Description() string
}

ErrorWithDescription is an interface binding the standard error inteface with a description.

Jump to

Keyboard shortcuts

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