httpx

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package httpx holds the HTTP plumbing shared by notifycat's handlers. For the signed-webhook receivers it owns only the parts that are identical across providers — capping and reading the body once, and replaying a fresh body reader downstream — and delegates authentication to a provider-supplied callback. The signing scheme itself (which headers, what HMAC, which error message) stays with each provider, because those genuinely differ (GitHub signs the raw body; Slack signs a timestamped base string).

It also holds the JSON and plain-text writers, which send non-cacheable responses, PrefersText, which reads the Accept header, and Route, which pairs a ServeMux pattern with its handler so a domain can hand its routes to the composition root.

Index

Constants

View Source
const (
	CacheControlHeader = "Cache-Control"
	NoStore            = "no-store"
)

Response caching headers set on every credential-bearing response.

View Source
const (
	AcceptHeader      = "Accept"
	ContentTypeHeader = "Content-Type"
	JSONMediaType     = "application/json"
	TextMediaType     = "text/plain"
	TextContentType   = TextMediaType + "; charset=utf-8"
	QualityParameter  = "q"
	MediaWildcard     = "*"
)

Content negotiation headers and media types.

View Source
const AllowHeader = "Allow"

AllowHeader lists the methods a 405 response accepts.

Variables

This section is empty.

Functions

func PrefersText added in v0.30.0

func PrefersText(request *http.Request) bool

PrefersText reports whether the Accept header ranks text/plain strictly above application/json.

func Signature

func Signature(maxBytes int64, authenticate Authenticate) func(http.Handler) http.Handler

Signature returns an HTTP middleware that rejects bodies larger than maxBytes (413), reads the body once, runs authenticate (which rejects unauthenticated requests itself), and otherwise replays a fresh body reader to next so downstream handlers read the verified bytes without touching the raw stream.

func WriteError added in v0.29.0

func WriteError(w http.ResponseWriter, status int, code string)

WriteError writes {"error": code} with the status.

func WriteJSON added in v0.29.0

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

WriteJSON writes body as a non-cacheable JSON response.

func WriteNoContent added in v0.29.0

func WriteNoContent(w http.ResponseWriter)

WriteNoContent writes a non-cacheable 204.

func WriteText added in v0.30.0

func WriteText(w http.ResponseWriter, status int, text string)

WriteText writes text and a trailing newline as a non-cacheable plain-text response.

Types

type Authenticate

type Authenticate func(w http.ResponseWriter, r *http.Request, body []byte) bool

Authenticate verifies a request from its (already-read) raw body. On failure it writes the appropriate error response to w and returns false; on success it writes nothing and returns true. Keeping the failure response here — rather than returning an error the skeleton renders — lets each provider preserve its own status codes and messages (e.g. "missing signature" vs "invalid signature").

type Route added in v0.29.0

type Route struct {
	Pattern string
	Handler http.Handler
}

Route pairs a ServeMux pattern with its handler.

Jump to

Keyboard shortcuts

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