webutil

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: MIT Imports: 13 Imported by: 5

README

GoDoc

various web utils

This contains a number of useful libs for http servers and other web usage.

Documentation

Index

Constants

View Source
const (
	StatusBadRequest                   = HttpError(http.StatusBadRequest)
	StatusUnauthorized                 = HttpError(http.StatusUnauthorized)
	StatusPaymentRequired              = HttpError(http.StatusPaymentRequired)
	StatusForbidden                    = HttpError(http.StatusForbidden)
	StatusNotFound                     = HttpError(http.StatusNotFound)
	StatusMethodNotAllowed             = HttpError(http.StatusMethodNotAllowed)
	StatusNotAcceptable                = HttpError(http.StatusNotAcceptable)
	StatusProxyAuthRequired            = HttpError(http.StatusProxyAuthRequired)
	StatusRequestTimeout               = HttpError(http.StatusRequestTimeout)
	StatusConflict                     = HttpError(http.StatusConflict)
	StatusGone                         = HttpError(http.StatusGone)
	StatusLengthRequired               = HttpError(http.StatusLengthRequired)
	StatusPreconditionFailed           = HttpError(http.StatusPreconditionFailed)
	StatusRequestEntityTooLarge        = HttpError(http.StatusRequestEntityTooLarge)
	StatusRequestURITooLong            = HttpError(http.StatusRequestURITooLong)
	StatusUnsupportedMediaType         = HttpError(http.StatusUnsupportedMediaType)
	StatusRequestedRangeNotSatisfiable = HttpError(http.StatusRequestedRangeNotSatisfiable)
	StatusExpectationFailed            = HttpError(http.StatusExpectationFailed)
	StatusTeapot                       = HttpError(http.StatusTeapot)
	StatusMisdirectedRequest           = HttpError(http.StatusMisdirectedRequest)
	StatusUnprocessableEntity          = HttpError(http.StatusUnprocessableEntity)
	StatusLocked                       = HttpError(http.StatusLocked)
	StatusFailedDependency             = HttpError(http.StatusFailedDependency)
	StatusTooEarly                     = HttpError(http.StatusTooEarly)
	StatusUpgradeRequired              = HttpError(http.StatusUpgradeRequired)
	StatusPreconditionRequired         = HttpError(http.StatusPreconditionRequired)
	StatusTooManyRequests              = HttpError(http.StatusTooManyRequests)
	StatusRequestHeaderFieldsTooLarge  = HttpError(http.StatusRequestHeaderFieldsTooLarge)
	StatusUnavailableForLegalReasons   = HttpError(http.StatusUnavailableForLegalReasons)

	StatusInternalServerError           = HttpError(http.StatusInternalServerError)
	StatusNotImplemented                = HttpError(http.StatusNotImplemented)
	StatusBadGateway                    = HttpError(http.StatusBadGateway)
	StatusServiceUnavailable            = HttpError(http.StatusServiceUnavailable)
	StatusGatewayTimeout                = HttpError(http.StatusGatewayTimeout)
	StatusHTTPVersionNotSupported       = HttpError(http.StatusHTTPVersionNotSupported)
	StatusVariantAlsoNegotiates         = HttpError(http.StatusVariantAlsoNegotiates)
	StatusInsufficientStorage           = HttpError(http.StatusInsufficientStorage)
	StatusLoopDetected                  = HttpError(http.StatusLoopDetected)
	StatusNotExtended                   = HttpError(http.StatusNotExtended)
	StatusNetworkAuthenticationRequired = HttpError(http.StatusNetworkAuthenticationRequired)
)

Variables

This section is empty.

Functions

func ConvertPhpQuery

func ConvertPhpQuery(u url.Values) map[string]any

func EncodePhpQuery

func EncodePhpQuery(q map[string]any) string

func ErrorHandler added in v0.0.2

func ErrorHandler(f http.Handler) error

ErrorHandler returns an error that also complies with http.Handler for a given http.Handler.

func ErrorHandlerFunc added in v0.0.2

func ErrorHandlerFunc(f func(w http.ResponseWriter, req *http.Request)) error

ErrorHandlerFunc returns an error that also complies with http.Handler for a given function.

func ErrorToHttpHandler

func ErrorToHttpHandler(e error) http.Handler

func Get added in v0.2.1

func Get(url string) (io.ReadCloser, error)

Get will perform a GET on the specified URL and return an object that will read data and automatically resume the download if an error occurs. Additionally it will also check the HTTP Status and return an error if it is not successful.

If the response does not contain a Content-Length or if the server does not support Range headers, this will just perform a regular GET and won't be able to resume anything.

func HTTPStatus added in v0.0.5

func HTTPStatus(err error) int

func HttpErrorHandler deprecated

func HttpErrorHandler(code int) http.Handler

HttpErrorHandler returns a http.Handler for a given error code

Deprecated: HttpError can be directly used as a handler

func HttpHandlerToError

func HttpHandlerToError(h http.Handler) error

func IsRedirect

func IsRedirect(e error) http.Handler

func ParseDataUri

func ParseDataUri(u string) ([]byte, string, error)

ParseDataUri will parse a given data: uri and return its data and mime type.

func ParseIPPort

func ParseIPPort(ip string) *net.TCPAddr

ParseIPPort will parse an IP with optionally a port

func ParsePhpQuery

func ParsePhpQuery(q string) map[string]any

ParsePhpQuery parses PHP compatible query string, return as map[string]any

multiple cases may happen: a=b (simple) a[b]=c (object) a[]=c (array) a[b][]=c (multi levels) a[][][]=c (wtf)

func RedirectError

func RedirectError(u *url.URL) error

func RedirectErrorCode

func RedirectErrorCode(u *url.URL, code int) error

code can be one of http.StatusMovedPermanently or http.StatusFound or any 3xx http status code

func SendRedirect

func SendRedirect(w http.ResponseWriter, target string, code int)

SendRedirect is deprecated. http.Redirect() should be used instead

func ServeError

func ServeError(w http.ResponseWriter, req *http.Request, err error)

func Wrap added in v0.0.8

func Wrap(h Handler) http.Handler

Types

type Handler added in v0.0.8

type Handler interface {
	ServeHTTP(w http.ResponseWriter, req *http.Request) error
}

type HttpError

type HttpError int

func (HttpError) Error added in v0.0.4

func (e HttpError) Error() string

Error returns information about the error, including the corresponding text

func (HttpError) HTTPStatus added in v0.0.5

func (e HttpError) HTTPStatus() int

HTTPStatus returns the value set in http error

func (HttpError) ServeHTTP added in v0.0.4

func (e HttpError) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP serves the error

func (HttpError) Unwrap added in v0.2.1

func (e HttpError) Unwrap() error

Unwrap will return a common error that (somewhat) matches the received error, making it easier to check if a given response matches a specific kind of error

type Redirect added in v0.0.6

type Redirect struct {
	URL  *url.URL
	Code int
}

func (*Redirect) Error added in v0.0.6

func (e *Redirect) Error() string

func (*Redirect) HTTPStatus added in v0.0.6

func (e *Redirect) HTTPStatus() int

func (*Redirect) ServeHTTP added in v0.0.6

func (e *Redirect) ServeHTTP(w http.ResponseWriter, req *http.Request)

type WrapFunc added in v0.0.8

type WrapFunc func(w http.ResponseWriter, req *http.Request) error

func (WrapFunc) ServeHTTP added in v0.0.8

func (wf WrapFunc) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Wrapper added in v0.0.8

type Wrapper struct {
	Child Handler
}

func (*Wrapper) ServeHTTP added in v0.0.8

func (wrapper *Wrapper) ServeHTTP(w http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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