httputils

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 13 Imported by: 3 modules, 7 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httputils provides helpers for HTTP handlers: writing error responses with logging and telemetry, inspecting request host and client address, and hashing basic auth passwords.

Each status-specific helper such as NotFoundServerError or InternalServerError delegates to ServerError:

if err != nil {
	httputils.InternalServerError(l, w, r, err)
	return
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadGatewayServerError added in v0.30.0

func BadGatewayServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

BadGatewayServerError responds with status http.StatusBadGateway using ServerError.

func BadRequestServerError

func BadRequestServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

BadRequestServerError responds with status http.StatusBadRequest using ServerError.

func ConflictServerError added in v0.30.0

func ConflictServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

ConflictServerError responds with status http.StatusConflict using ServerError.

func ExpectationFailedServerError added in v0.30.0

func ExpectationFailedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

ExpectationFailedServerError responds with status http.StatusExpectationFailed using ServerError.

func FailedDependencyServerError added in v0.30.0

func FailedDependencyServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

FailedDependencyServerError responds with status http.StatusFailedDependency using ServerError.

func ForbiddenServerError added in v0.30.0

func ForbiddenServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

ForbiddenServerError responds with status http.StatusForbidden using ServerError.

func GatewayTimeoutServerError added in v0.30.0

func GatewayTimeoutServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

GatewayTimeoutServerError responds with status http.StatusGatewayTimeout using ServerError.

func GetRemoteAddr added in v0.9.6

func GetRemoteAddr(r *http.Request) string

GetRemoteAddr returns the client IP address of r. It prefers the X-Real-IP, True-Client-Ip and the first X-Forwarded-For entry, in that order, and falls back to the host part of http.Request.RemoteAddr. These headers are client-controlled unless set by a trusted proxy.

func GetRequestDomain added in v0.2.0

func GetRequestDomain(r *http.Request) string

GetRequestDomain returns the host returned by GetRequestHost with any port removed.

func GetRequestHost added in v0.2.0

func GetRequestHost(r *http.Request) string

GetRequestHost returns the request's host, preferring the X-Forwarded-Host header, then http.Request.Host for non-absolute request URLs, and the URL host otherwise. The result may include a port.

func GoneServerError added in v0.30.0

func GoneServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

GoneServerError responds with status http.StatusGone using ServerError.

func HTTPVersionNotSupportedServerError added in v0.30.0

func HTTPVersionNotSupportedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

HTTPVersionNotSupportedServerError responds with status http.StatusHTTPVersionNotSupported using ServerError.

func HashBasicAuthPassword added in v0.2.0

func HashBasicAuthPassword(v []byte) ([]byte, error)

HashBasicAuthPassword returns the bcrypt hash of the password v using bcrypt.DefaultCost, suitable for storing basic auth credentials.

func InsufficientStorageServerError added in v0.30.0

func InsufficientStorageServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

InsufficientStorageServerError responds with status http.StatusInsufficientStorage using ServerError.

func InternalServerError

func InternalServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

InternalServerError responds with status http.StatusInternalServerError using ServerError.

func InternalServiceTooEarly added in v0.9.0

func InternalServiceTooEarly(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

InternalServiceTooEarly responds with status http.StatusTooEarly using ServerError.

func InternalServiceUnavailable added in v0.9.0

func InternalServiceUnavailable(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

InternalServiceUnavailable responds with status http.StatusServiceUnavailable using ServerError.

func LengthRequiredServerError added in v0.30.0

func LengthRequiredServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

LengthRequiredServerError responds with status http.StatusLengthRequired using ServerError.

func LockedServerError added in v0.30.0

func LockedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

LockedServerError responds with status http.StatusLocked using ServerError.

func LoopDetectedServerError added in v0.30.0

func LoopDetectedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

LoopDetectedServerError responds with status http.StatusLoopDetected using ServerError.

func MethodNotAllowedServerError added in v0.24.0

func MethodNotAllowedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

MethodNotAllowedServerError responds with status http.StatusMethodNotAllowed using ServerError.

func MisdirectedRequestServerError added in v0.30.0

func MisdirectedRequestServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

MisdirectedRequestServerError responds with status http.StatusMisdirectedRequest using ServerError.

func NetworkAuthenticationRequiredServerError added in v0.30.0

func NetworkAuthenticationRequiredServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

NetworkAuthenticationRequiredServerError responds with status http.StatusNetworkAuthenticationRequired using ServerError.

func NotAcceptableServerError added in v0.30.0

func NotAcceptableServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

NotAcceptableServerError responds with status http.StatusNotAcceptable using ServerError.

func NotExtendedServerError added in v0.30.0

func NotExtendedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

NotExtendedServerError responds with status http.StatusNotExtended using ServerError.

func NotFoundServerError

func NotFoundServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

NotFoundServerError responds with status http.StatusNotFound using ServerError.

func NotImplementedServerError added in v0.30.0

func NotImplementedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

NotImplementedServerError responds with status http.StatusNotImplemented using ServerError.

func PaymentRequiredServerError added in v0.30.0

func PaymentRequiredServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

PaymentRequiredServerError responds with status http.StatusPaymentRequired using ServerError.

func PreconditionFailedServerError added in v0.30.0

func PreconditionFailedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

PreconditionFailedServerError responds with status http.StatusPreconditionFailed using ServerError.

func PreconditionRequiredServerError added in v0.30.0

func PreconditionRequiredServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

PreconditionRequiredServerError responds with status http.StatusPreconditionRequired using ServerError.

func ProxyAuthRequiredServerError added in v0.30.0

func ProxyAuthRequiredServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

ProxyAuthRequiredServerError responds with status http.StatusProxyAuthRequired using ServerError.

func RequestEntityTooLargeServerError added in v0.24.0

func RequestEntityTooLargeServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

RequestEntityTooLargeServerError responds with status http.StatusRequestEntityTooLarge using ServerError.

func RequestHeaderFieldsTooLargeServerError added in v0.30.0

func RequestHeaderFieldsTooLargeServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

RequestHeaderFieldsTooLargeServerError responds with status http.StatusRequestHeaderFieldsTooLarge using ServerError.

func RequestTimeoutServerError added in v0.30.0

func RequestTimeoutServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

RequestTimeoutServerError responds with status http.StatusRequestTimeout using ServerError.

func RequestURITooLongServerError added in v0.30.0

func RequestURITooLongServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

RequestURITooLongServerError responds with status http.StatusRequestURITooLong using ServerError.

func RequestedRangeNotSatisfiableServerError added in v0.30.0

func RequestedRangeNotSatisfiableServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

RequestedRangeNotSatisfiableServerError responds with status http.StatusRequestedRangeNotSatisfiable using ServerError.

func ServerError

func ServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, code int, errs ...error)

ServerError writes a plain-text response with the given status code and its http.StatusText as body, if errs joined by errors.Join is non-nil.

The error is recorded on the current span and its type added to the otelhttp labeler, if any. If r carries a request log labeler (see httplog.InjectLabelerIntoRequest), the error is added to it; otherwise it is logged at error level through l, unless l is nil.

When errs is empty or contains only nil errors, nothing is written to w.

func TeapotServerError added in v0.30.0

func TeapotServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

TeapotServerError responds with status http.StatusTeapot using ServerError.

func TooManyRequestsServerError added in v0.30.0

func TooManyRequestsServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

TooManyRequestsServerError responds with status http.StatusTooManyRequests using ServerError.

func UnauthorizedServerError

func UnauthorizedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

UnauthorizedServerError responds with status http.StatusUnauthorized using ServerError.

func UnavailableForLegalReasonsServerError added in v0.30.0

func UnavailableForLegalReasonsServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

UnavailableForLegalReasonsServerError responds with status http.StatusUnavailableForLegalReasons using ServerError.

func UnprocessableEntityServerError added in v0.30.0

func UnprocessableEntityServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

UnprocessableEntityServerError responds with status http.StatusUnprocessableEntity using ServerError.

func UnsupportedMediaTypeServerError added in v0.30.0

func UnsupportedMediaTypeServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

UnsupportedMediaTypeServerError responds with status http.StatusUnsupportedMediaType using ServerError.

func UpgradeRequiredServerError added in v0.30.0

func UpgradeRequiredServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

UpgradeRequiredServerError responds with status http.StatusUpgradeRequired using ServerError.

func VariantAlsoNegotiatesServerError added in v0.30.0

func VariantAlsoNegotiatesServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, errs ...error)

VariantAlsoNegotiatesServerError responds with status http.StatusVariantAlsoNegotiates using ServerError.

Types

This section is empty.

Jump to

Keyboard shortcuts

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