native

package
v0.4.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: 34 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 native is the pure-Go engine: the one place that assembles the request path (pool, dial, handshake, then h1 or h2 by ALPN, or h3 over QUIC), routing each request to its pool key and retrying on another connection a request nothing of which was sent.

HTTP/2 coalescing (when Config.Coalesce), HTTP/3 discovery and fallback, and WebSocket transports follow the profile's connections, http3 and websocket sections; see docs/concepts/http3.md, docs/concepts/websockets.md and decisions 0023, 0028 and 0037. Plan reuses RoundTrip's own steps and changes nothing (decision 0034).

Index

Constants

View Source
const (
	HTTP1 = "HTTP/1.1"
	HTTP2 = "HTTP/2.0"
	HTTP3 = "HTTP/3.0"
)

The protocols, as a Plan names them: as responses' Proto does.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Profile is the browser to imitate. New takes ownership: it must be
	// valid (profile.Parse or Validate) and never changed afterwards.
	Profile *profile.Spec
	RootCAs *x509.CertPool
	// DialTimeout bounds the connect (through a proxy: to the proxy, and the
	// proxy's handshake) and, separately, the TLS handshake, on top of the
	// request context's deadline. Zero means 30 seconds.
	DialTimeout time.Duration
	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
	// LocalAddrs are the local addresses the engine's sockets bind to
	// (dial.Dialer.LocalAddrs); checked by the caller.
	LocalAddrs []netip.Addr
	// Resolver looks host names up: for dials without DialContext, and to
	// find another host's HTTP/2 connection a request may take
	// (Coalesce, profile.CoalesceOnLookup). Nil uses
	// net.DefaultResolver.
	Resolver *net.Resolver
	// Proxy picks each request's proxy, as http.Transport's does; nil, or
	// a nil URL, connects directly.
	Proxy func(*http.Request) (*url.URL, error)
	// DisableHTTP3 keeps every request on TCP, whatever the profile's
	// http3 section and servers' Alt-Svc say.
	DisableHTTP3 bool
	// AltSvc keeps the origins' HTTP/3 alternatives, shared with every
	// engine given the same cache; nil gives the engine one of its own.
	AltSvc *altsvc.Cache
	// BrowserFallback keeps the browser's timing where QUIC fails: requests
	// wait on an unanswered 0-RTT connection until it gives up
	// (ZeroRTTHolds, Fallback.Hold), and on a failed attempt until the race
	// delay. Otherwise they go on TCP once the race delay (or, without
	// one, the first probe timeout) has passed with no answer, and at once
	// when the attempt failed.
	BrowserFallback bool
	// BrowserDialDelay holds a QUIC dial a request waits for back as the
	// browser does (http3.discovery.dialDelayMs, resumedDialDelayMs).
	// Otherwise it starts at once (decision 0050).
	BrowserDialDelay bool
	// Coalesce shares HTTP/2 connections between hosts as the profile says
	// (connections.coalesce). Otherwise each host has its own connection:
	// no lookup for another host's, and no connection opened and declined.
	// The root package decides the default (decision 0037).
	Coalesce bool
	// MaxConnsPerHost is the most HTTP/1.1 connections open at once to one
	// host; zero means the profile's connections.http1PerHost.
	MaxConnsPerHost int
	// IdleConnTimeout is how long an idle connection is kept (HTTP/1.1,
	// HTTP/2, HTTP/3); zero means 90 seconds, negative never closes one
	// for its idleness. Unused proxies' connections close early either
	// way (proxyUse).
	IdleConnTimeout time.Duration
	// MaxIdleProxies, when set, is how many of the most recently used
	// proxies keep their idle connections and their TLS sessions, 407s
	// and HTTP/3 state (forgetProxy), the others losing both as soon as
	// they fall out (proxyUse). Zero means keepProxies for connections
	// and rememberProxies for state, or any proxy used in the last
	// proxyUnused. Negative is an error.
	MaxIdleProxies int
	// HTTP3OverSOCKS5 lets requests through a socks5 or socks5h proxy use
	// HTTP/3, over a SOCKS5 UDP association with the proxy (ListenUDPVia),
	// as they would without the proxy. Otherwise, and through any other
	// proxy, they stay on TCP (browsers never use HTTP/3 through a proxy).
	HTTP3OverSOCKS5 bool
	// HTTP3ReadBufferSize is the receive buffer QUIC sockets ask for: zero
	// asks for dial.DefaultHTTP3ReadBufferSize (Chrome's), negative keeps the
	// OS's default.
	HTTP3ReadBufferSize int
	// HTTP3ACKEvery, when set, is how many ack-eliciting packets a QUIC
	// connection acknowledges at once after the profile's warm-up, in place
	// of the profile's quic.ack.every; at least that, at most maxACKEvery
	// (decision 0043).
	HTTP3ACKEvery int
	// BrowserProxyAuth gives an HTTP proxy the URL's credentials as the
	// profile's browser does: only once the proxy has answered 407
	// (proxy.connectCredentials, forwardCredentials). Otherwise they go
	// from the first CONNECT or forwarded request, placed as the
	// browser places them unasked (decision 0039).
	BrowserProxyAuth bool
	// VerifyConnection, when set, checks a verified connection for host on
	// every TLS and QUIC handshake (resumed ones included) and before a request
	// coalesces onto another host's HTTP/2 connection. An error fails the
	// handshake or keeps the request off that connection.
	VerifyConnection func(host string, cs tls.ConnectionState) error
	// OnConnect, when set, is told each TCP connect's duration
	// (dial.Dialer.OnConnect), which the rtt client hint estimates from.
	OnConnect func(time.Duration)
}

Config configures the engine.

type Engine

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

Engine is the pure-Go engine: pooled connections, one dial path, uTLS handshakes and HTTP/1.1, HTTP/2 or HTTP/3 as the profile's browser chooses. It is safe for concurrent use.

func New

func New(cfg Config) (*Engine, error)

New builds an engine for cfg.Profile, which it takes ownership of: the profile must not change afterwards, and engines given the same profile share what they compile from it.

func (*Engine) CloseIdleConnections

func (e *Engine) CloseIdleConnections()

CloseIdleConnections closes idle HTTP/1.1 connections, and HTTP/2 and HTTP/3 connections with no requests in flight.

func (*Engine) Handshake

func (e *Engine) Handshake(ctx context.Context, rawURL string) (*tls.ConnectionState, error)

Handshake connects to rawURL's host as a request would, with the profile's TLS hello, and returns the verified TLS state. It sends no request and closes the connection.

func (*Engine) OpenWebSocket

func (e *Engine) OpenWebSocket(req *http.Request, key string) (*WebSocket, error)

OpenWebSocket sends req, a WebSocket handshake with its headers resolved, as the profile's browser opens a socket: an extended CONNECT stream on the pool's HTTP/2 connection when the server allows it, else a new unpooled HTTP/1.1 connection. key is the Sec-WebSocket-Key (HTTP/1.1 only). See docs/concepts/websockets.md.

func (*Engine) Plan

func (e *Engine) Plan(req *http.Request) (*Plan, error)

Plan returns what RoundTrip would write for req over each protocol it may take now, built by the same code the connections use. It dials, looks up and changes nothing; its error is one RoundTrip would return for any protocol. See decision 0034.

func (*Engine) RoundTrip

func (e *Engine) RoundTrip(req *http.Request) (resp *http.Response, err error)

RoundTrip sends req on a pooled or new connection. It closes req.Body, on errors too.

type Field

type Field struct {
	Name  string
	Value string
}

Field is one header line as a connection writes it.

type Head struct {
	Line   string
	Fields []Field
	Err    error
}

Head is a request's head over one protocol: the HTTP/1.1 request line (Line; empty otherwise) and header lines in order. Err, when set, is why the request cannot go over the protocol; Line and Fields are then empty.

type Plan

type Plan struct {
	// Protocols are the protocols the request may take now, likeliest
	// first; several when the choice is made while connecting.
	Protocols []string
	HTTP2     Head
	HTTP1     Head
	// HTTP3 is nil when the engine speaks no HTTP/3 (the profile has
	// none, or Config.DisableHTTP3).
	HTTP3 *Head
}

Plan is what the engine would write for a request.

type WebSocket

type WebSocket struct {
	// Response is the server's answer to the handshake. Its body is
	// readable only when the handshake failed (not 101, or not 2xx over
	// HTTP/2); closing it ends the transport.
	Response *http.Response
	// Conn carries the socket once the handshake succeeded, and Reader
	// reads what the server sent on it (which may have come with the
	// handshake's response).
	Conn   ws.Transport
	Reader io.Reader
	// H2 is set for an extended CONNECT stream (RFC 8441), whose DATA
	// frames carry at most MaxData bytes.
	H2      bool
	MaxData int
}

WebSocket is the transport a WebSocket handshake opened, and the server's answer.

Jump to

Keyboard shortcuts

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