httputil

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP - the visitor's address. The connection's peer, unless it is a trusted proxy (SetTrustedProxies): then the nearest address of X-Forwarded-For that isn't one, read from the right (the entries on the left are the client's to write), else X-Real-IP. A client talking to the application directly can't set its address this way.

func DecodeJSON

func DecodeJSON(r *http.Request, v any) error

DecodeJSON decodes JSON request body

func InternalServerError

func InternalServerError(w http.ResponseWriter, cause ...error)

InternalServerError sends HTTP 500. If cause is set and indicates a failure while writing the response body to the client (timeout, broken pipe, reset), the status line may already be sent; in that case it does nothing to avoid a superfluous WriteHeader (e.g. after template streaming stops mid-body).

func IsHTTPS

func IsHTTPS(r *http.Request) bool

IsHTTPS - whether the visitor came over HTTPS: the connection's TLS, or X-Forwarded-Proto from a trusted proxy (a client talking to the application directly can't claim it).

func IsLikelyResponseWriteFailure

func IsLikelyResponseWriteFailure(err error) bool

IsLikelyResponseWriteFailure reports whether err is probably a failure writing the response to the client (timeout, reset, broken pipe), as opposed to a template or application error before any bytes were sent.

func JSON

func JSON(w http.ResponseWriter, data any, statusCode int)

JSON writes a JSON response

func JSONError

func JSONError(w http.ResponseWriter, message string, statusCode int)

JSONError writes a JSON error response

func ParseTrustedProxies

func ParseTrustedProxies(values []string) ([]netip.Prefix, error)

ParseTrustedProxies - the proxies' addresses or networks ("10.0.0.5", "172.16.0.0/12"), for SetTrustedProxies.

func SendJSON

func SendJSON(w http.ResponseWriter, statusCode int, data any)

SendJSON is an alias for JSON with flipped parameter order for convenience

func SetTrustedProxies

func SetTrustedProxies(prefixes []netip.Prefix)

SetTrustedProxies sets the proxies in front of the site (the config's trusted_proxies); none - loopback only.

func TemplateExecutionError

func TemplateExecutionError(w http.ResponseWriter, err error)

TemplateExecutionError sends a minimal HTML 500 page with a message for the visitor; err itself is for the caller's log (its text names files and templates). Does nothing if err looks like a client disconnect / write failure.

Types

This section is empty.

Jump to

Keyboard shortcuts

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