httpx

package
v2.8.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcquireDeadPeerTimeout added in v2.6.0

func AcquireDeadPeerTimeout(ctx context.Context, timeout time.Duration) (func(), error)

AcquireDeadPeerTimeout bounds how long data written to the request's connection may stay unacknowledged before the kernel tears it down, and returns a release function restoring the system default once every caller sharing the connection has released it.

Long-lived streams need this because they write on their own schedule into a connection the client may have silently abandoned: left to the defaults the kernel retransmits until tcp_retries2 is exhausted, roughly eleven minutes. TCP keepalive does not cover the case, since probes only fire while a connection is idle and a stream with an unacknowledged write in flight never is.

It is deliberately scoped to one connection rather than the listener: the same listener carries edge tunnel gRPC streams, which tolerate up to edge.TunnelStaleTimeout of silence and would be reset by a shared timeout.

The release function is always safe to call, including when acquisition failed or the request never passed through WithConn.

func ClearReadDeadline added in v2.8.1

func ClearReadDeadline(ctx context.Context)

ClearReadDeadline clears any read deadline left armed on the request's underlying connection. Safe to call from any request handler, including when the request never passed through WithConn; only the first call per connection issues the syscall.

It exists because go1.26.6 (the CVE-2026-56853 backport) arms ReadHeaderTimeout on the raw connection before sniffing the h2c preface and hands the connection to the HTTP/2 server without clearing it. The HTTP/2 server tracks liveness with timers, never read deadlines, so the stale absolute deadline survives and tears the connection down — with every stream on it — exactly ReadHeaderTimeout after accept. By the time a handler runs the headers are already parsed, so clearing the deadline does not weaken the slowloris protection the timeout is for.

func DefaultLookupIP

func DefaultLookupIP(ctx context.Context, host string) ([]net.IP, error)

func GetClientBaseURL

func GetClientBaseURL(origin, forwardedHost, forwardedProto, host, appURL string) string

GetClientBaseURL determines the client's base URL from request headers. It checks Origin, X-Forwarded-Host/Proto, and Host headers. If none provide a valid URL, it falls back to the configured appURL.

func GetIntQueryParam

func GetIntQueryParam(r *http.Request, name string, required bool) (int, error)

GetIntQueryParam reads and parses an integer query parameter from the request URL. If `required` is true and the parameter is missing, or if parsing fails, an error is returned.

func IsWebSocketUpgradeRequest added in v2.7.0

func IsWebSocketUpgradeRequest(r *http.Request) bool

IsWebSocketUpgradeRequest reports whether r is a WebSocket handshake: the Connection header contains "upgrade" (it can be a list, e.g. Firefox sends "keep-alive, Upgrade") and Upgrade is "websocket".

func NewHTTPClient

func NewHTTPClient() *http.Client

func NewHTTPClientWithTimeout

func NewHTTPClientWithTimeout(timeout time.Duration) *http.Client

func NewSafeOutboundHTTPClient

func NewSafeOutboundHTTPClient(base *http.Client, lookupIP LookupIPFunc) (*http.Client, error)

func NormalizeBaseURL added in v2.8.0

func NormalizeBaseURL(rawURL string) (string, error)

NormalizeBaseURL validates an outbound HTTP URL and strips endpoint-specific components.

func SetJSONStreamHeaders

func SetJSONStreamHeaders(headers HeaderSetter)

func ValidateOutboundHTTPURL

func ValidateOutboundHTTPURL(rawURL string) (*url.URL, error)

ValidateOutboundHTTPURL parses and validates an outbound HTTP(S) target URL. It intentionally performs syntactic hardening (scheme/host/credentials) without restricting private network ranges, because environment agents may be deployed on trusted private subnets.

func ValidateSafeRemoteURL

func ValidateSafeRemoteURL(ctx context.Context, rawURL string, lookupIP LookupIPFunc) (*url.URL, error)

func ValidateWebSocketOrigin

func ValidateWebSocketOrigin(appURL string) func(r *http.Request) bool

ValidateWebSocketOrigin validates the Origin header for WebSocket connections to prevent CSRF attacks. It checks: 1. Same-origin requests (Origin matches Host) 2. Allowed origins from appURL 3. Handles empty Origin headers (some clients don't send it)

func WithConn added in v2.6.0

func WithConn(ctx context.Context, conn net.Conn) context.Context

WithConn stores an accepted connection on its base context. Wire it up as http.Server.ConnContext so long-lived handlers can tune socket options for their own connection without affecting every other client.

Types

type HeaderSetter

type HeaderSetter interface {
	SetHeader(key string, value string)
}

type LookupIPFunc

type LookupIPFunc func(ctx context.Context, host string) ([]net.IP, error)

Jump to

Keyboard shortcuts

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