Documentation
¶
Overview ¶
Package httphelpers provides shared HTTP handler prelude helpers used across server sub-packages. This breaks the import cycle between server/ and its sub-packages (queryhandlers, confighandlers, etc.) that previously required copy-pasting these helpers.
The method gate and body decoder delegate to the webhttp prelude primitives (RequireMethod, LimitBody, MaxJSONBody) so subflux inherits the RFC-9110 Allow header on 405s and the MaxBytesReader overflow-to-400 behavior, while keeping subflux's own error-code taxonomy for the response envelope.
Index ¶
- Constants
- func DecodeJSONBody(w http.ResponseWriter, r *http.Request, dst any, maxBytes int64) bool
- func RequireGET(w http.ResponseWriter, r *http.Request) bool
- func RequireMethod(w http.ResponseWriter, r *http.Request, method string) bool
- func RequirePOST(w http.ResponseWriter, r *http.Request) bool
Constants ¶
const MaxDefaultBodySize = webhttp.MaxJSONBody
MaxDefaultBodySize is the default JSON body size cap for most handlers (1 MiB), aliased to webhttp.MaxJSONBody so the limit has a single definition.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSONBody ¶
DecodeJSONBody decodes a JSON request body into dst under a size cap, via webhttp.DecodeJSONInto (http.MaxBytesReader cap + single-value decode + trailing-data rejection): a body exceeding maxBytes, malformed JSON, or data after the first value fails the decode rather than being silently accepted. A maxBytes of 0 or less uses MaxDefaultBodySize. Writes subflux's 400 envelope (code "bad_request") and returns false on any decode failure.
func RequireGET ¶
func RequireGET(w http.ResponseWriter, r *http.Request) bool
RequireGET is sugar for RequireMethod(w, r, http.MethodGet).
func RequireMethod ¶
RequireMethod checks that the request method matches the expected value. On a mismatch it delegates to webhttp.RequireMethod, which sets the RFC-9110 Allow header to the permitted method and writes subflux's {error,code,request_id} 405 envelope (code "method_not_allowed"), then returns false.
func RequirePOST ¶
func RequirePOST(w http.ResponseWriter, r *http.Request) bool
RequirePOST is sugar for RequireMethod(w, r, http.MethodPost).
Types ¶
This section is empty.