Documentation
¶
Index ¶
- func ClientIP(r *http.Request) string
- func DecodeJSON(r *http.Request, v any) error
- func FromTrustedProxy(r *http.Request) bool
- func InternalServerError(w http.ResponseWriter, cause ...error)
- func IsHTTPS(r *http.Request) bool
- func IsLikelyResponseWriteFailure(err error) bool
- func JSON(w http.ResponseWriter, data any, statusCode int)
- func JSONError(w http.ResponseWriter, message string, statusCode int)
- func ParseTrustedProxies(values []string) ([]netip.Prefix, error)
- func SendJSON(w http.ResponseWriter, statusCode int, data any)
- func SetTrustedProxies(prefixes []netip.Prefix)
- func TemplateExecutionError(w http.ResponseWriter, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientIP ¶
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 ¶
DecodeJSON decodes JSON request body
func FromTrustedProxy ¶
FromTrustedProxy - whether the connection's peer is a trusted proxy (SetTrustedProxies), whose headers about the visitor may be believed.
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 ¶
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 ¶
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 JSONError ¶
func JSONError(w http.ResponseWriter, message string, statusCode int)
JSONError writes a JSON error response
func ParseTrustedProxies ¶
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 ¶
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.