httpx

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package httpx holds the small HTTP helpers the mock servers and the webhook package share.

Index

Constants

View Source
const DefaultWebhookTimeout = 10 * time.Second

DefaultWebhookTimeout is how long a webhook waits for its answer when nothing sets its timeout.

Variables

This section is empty.

Functions

func IsToken

func IsToken(s string) bool

IsToken reports whether s is an HTTP token, as header names and methods are.

func ListenAddr

func ListenAddr(addr string) bool

ListenAddr reports whether addr is an address a server can listen on: empty, a port, or host:port.

func NewDialer

func NewDialer(allow, deny []netip.Prefix) *net.Dialer

NewDialer returns a dialer of the connections a server opens, with the limits of http.DefaultTransport's, which refuses an address in deny, or outside allow when allow isn't empty, and the unspecified address with either set, with vlatte.ErrOutboundRefused. It checks the address it connects to, once resolved.

func NewWebhookTransport

func NewWebhookTransport(allow, deny []netip.Prefix) *http.Transport

NewWebhookTransport returns the transport a server gives webhooks when its config has none. It is the server's: Close closes its idle connections. It has the limits of http.DefaultTransport, built here rather than copied, as a program may have replaced http.DefaultTransport, and dials as NewDialer does with allow and deny.

func ParsePrefixes

func ParsePrefixes(list []string) ([]netip.Prefix, error)

ParsePrefixes parses the address ranges of an outbound setting, each in CIDR form, for NewDialer.

func Wait

func Wait(ctx context.Context, d time.Duration) error

Wait waits for d, or until ctx ends, and then returns ctx's error.

Types

type AcceptRange

type AcceptRange struct {
	MediaType string
	Q         float64
}

AcceptRange is a media range of an Accept header, such as application/json, text/* or */*, and its weight.

func ParseAccept

func ParseAccept(header []string) []AcceptRange

ParseAccept parses the media ranges of Accept headers, in the order they come, leaving out those it can't parse and those whose weight isn't one RFC 9110 allows, from 0 to 1.

func (AcceptRange) Match

func (r AcceptRange) Match(mediaType string) int

Match tells how specifically r matches mediaType: 3 by name, 2 by type, as text/*, 1 as */*, 0 not.

type Preference

type Preference struct {
	Q           float64
	Specificity int
	Index       int
}

Preference is how much the ranges of Accept headers want a media type: Q is the weight of the range that matches it, Specificity tells how specifically that range matches, as AcceptRange.Match does, and Index is where the range sits among the ranges. A media type no range matches has a zero Preference.

func Prefer

func Prefer(ranges []AcceptRange, names ...string) Preference

Prefer returns how much ranges want a media type with one of names: the most specific range that matches a name decides, as RFC 9110 reads Accept, the heaviest of those when several are as specific, and the first of those when they weigh the same.

Jump to

Keyboard shortcuts

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