httpx

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0 modules, 0 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 httpx holds shared HTTP building blocks for the server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request, trusted []netip.Prefix) netip.Addr

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

func ReadJSON(r *http.Request, limit int64, v any) error

ReadJSON decodes exactly one JSON value of at most limit bytes, rejecting unknown fields.

func RequireSameOrigin

func RequireSameOrigin(public *url.URL, next http.Handler) http.Handler

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

func SecurityHeaders(next http.Handler) http.Handler

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).

func UserAgent

func UserAgent(r *http.Request) string

UserAgent returns the request's User-Agent for persistence: at most 256 bytes, with invalid UTF-8 (including a rune cut by the limit) removed (Postgres rejects it).

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON writes v as JSON with the given status.

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

func NewLimiter(perMinute int, now func() time.Time) *Limiter

NewLimiter allows perMinute requests per minute per key, with burst perMinute.

func (*Limiter) Allow

func (l *Limiter) Allow(key string) bool

Allow consumes one token for key if available.

Jump to

Keyboard shortcuts

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