Documentation
¶
Overview ¶
Package httpx holds shared HTTP building blocks for the server.
Index ¶
- func ClientIP(r *http.Request, trusted []netip.Prefix) netip.Addr
- func ReadJSON(r *http.Request, limit int64, v any) error
- func RequireSameOrigin(public *url.URL, next http.Handler) http.Handler
- func SecurityHeaders(next http.Handler) http.Handler
- func TooMany(w http.ResponseWriter, retryAfter time.Duration)
- func UserAgent(r *http.Request) string
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type Limiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientIP ¶
ClientIP returns the peer address, or, when the peer is a trusted proxy, the rightmost untrusted X-Forwarded-For hop (leftmost if all are trusted). A malformed hop ends the walk and falls back to the peer address.
func ReadJSON ¶
ReadJSON decodes exactly one JSON value of at most limit bytes, rejecting unknown fields.
func RequireSameOrigin ¶
RequireSameOrigin rejects state-changing requests whose Origin is not exactly the scheme and host (with port) of public; a missing Origin is rejected.
func SecurityHeaders ¶
SecurityHeaders sets the fixed response security headers.
func TooMany ¶
func TooMany(w http.ResponseWriter, retryAfter time.Duration)
TooMany writes 429 with Retry-After (whole seconds, at least 1).
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is a set of token buckets keyed by string; a fresh key starts full.
func NewLimiter ¶
NewLimiter allows perMinute requests per minute per key, with burst perMinute.