Help us shape the future of Go! Take the 2026 Go Developer Survey

httpx

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package httpx sends catalog requests and records the responses exactly: no automatic compression, no connection reuse, no proxies from the environment, and no connection to a host that is not allowed.

Index

Constants

View Source
const DefaultUserAgent = "driftgate/1"

DefaultUserAgent is sent unless the configuration sets another. It does not carry the DriftGate version, so captures stay comparable across releases.

View Source
const MaxBody = 256 << 20

MaxBody bounds a response body so a runaway endpoint cannot exhaust memory.

Variables

View Source
var ErrHostNotAllowed = errors.New("host not allowed")

ErrHostNotAllowed is returned for a connection to a host outside the allow-list.

View Source
var LocalHosts = []string{"localhost", "127.0.0.0/8", "::1/128"}

LocalHosts is an allow-list of the local system only.

Functions

func Decode

func Decode(body []byte, encoding string, max int64) (out []byte, ok bool)

Decode undoes the Content-Encoding of a body, the last encoding applied first: gzip and deflate (zlib, or raw as some servers send it); identity and no encoding leave it as it is. The decoded body is capped at max, so a small body that expands without end is not read past it. ok is false for an encoding it cannot decode (br, zstd), a body that does not decode, or one over max. The buffer is sized from the size gzip records, so a large body is not grown into twice its size.

func FreePool

func FreePool()

FreePool unmaps the mapping kept for a later body.

func Headers

func Headers(h http.Header) [][2]string

Headers turns a header map into sorted, lower-case pairs with sensitive values masked. Go does not keep the received order of header names; the comparison ignores that order anyway.

func MaskHeader

func MaskHeader(name, value string) string

MaskHeader hides secrets in header values while keeping their format, so allow-list formats still match: letters become x, digits 0. Besides the headers that carry credentials by definition (Authorization, Cookie, Set-Cookie's value), a header named as a credential (X-CSRF-Token, X-Api-Key, devise_token_auth's Access-Token, Token, Api-Key) is masked, except the standard ones in plainHeaders, and in a header that holds a URL (Location, Content-Location, Link, Refresh) the values of query and fragment parameters named or shaped as credentials (token=, signature=, X-Amz-Signature=, #access_token=) and path segments shaped as one (a reset link's token).

func Unreleased

func Unreleased() int

Unreleased is the number of bodies read off the heap whose results are not released yet: after a capture, 0.

Types

type Allow

type Allow struct {

	// LookupIP, when set, resolves names instead of the system resolver
	// (tests).
	LookupIP func(ctx context.Context, network, host string) ([]net.IP, error)
	// contains filtered or unexported fields
}

Allow decides which hosts may be contacted.

func NewAllow

func NewAllow(entries []string, anyHost bool) (*Allow, error)

NewAllow builds the allow-list from host names and CIDR ranges. An empty list means local and private addresses; anyHost lifts every restriction.

func (*Allow) Check

func (a *Allow) Check(ctx context.Context, host string) error

Check reports an error unless the host is allowed: named in the list, or every address it resolves to inside an allowed range.

func (*Allow) Dial

func (a *Allow) Dial(ctx context.Context, network, addr string) (net.Conn, error)

Dial connects only to allowed hosts. A host named in the allow-list may resolve anywhere; any other name must resolve to an allowed address, and the connection goes to that checked address (no second lookup to rebind).

func (*Allow) DialContext

func (a *Allow) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext connects only to allowed hosts, as the client does; for a transport of its own, such as the recording proxy's.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client sends catalog requests.

func New

func New(o Options) (*Client, error)

New builds a client.

func (*Client) Do

func (c *Client) Do(ctx context.Context, r catalog.Request, token string) Result

Do sends a request. Transport failures (refused connection, timeout, host not allowed) are returned as a record with Error set, not as an error.

func (*Client) URL

func (c *Client) URL(r catalog.Request) (string, error)

URL builds the full URL of a request, filling {name} placeholders from params. The path must start with "/", and the URL must keep the scheme and host of the base URL: the identity's token goes with the request. An error names the path as the catalog writes it, never with values filled in (they may come from a private body).

type Options

type Options struct {
	BaseURL         string
	UserAgent       string
	Timeout         time.Duration
	FollowRedirects bool
	Allow           *Allow
	// OffHeap reads a body that is not small outside the Go heap
	// (readOffHeap); the caller then releases every Result.
	OffHeap bool
}

Options configure a client.

type Result

type Result struct {
	Record   *snapshot.Record
	Body     []byte
	Duration time.Duration
	// contains filtered or unexported fields
}

Result is one sent request.

func (Result) OffHeap

func (r Result) OffHeap() bool

OffHeap reports a body read outside the Go heap: dropping it leaves no garbage to collect.

func (Result) Release

func (r Result) Release()

Release gives back the memory of a body read outside the Go heap (Options.OffHeap); the body must not be used after it. It does nothing for any other result.

Jump to

Keyboard shortcuts

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