h2

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 20 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 h2 is mimicry's HTTP/2 client. It opens connections and sends requests the way the profile's browser does (preface, priorities, header order, flow control) on golang.org/x/net/http2's Framer, with header blocks encoded by internal/hpack. See decision 0001.

A fully read response is never reset: the stream waits for the peer's END_STREAM. A request the peer cannot have processed fails with ErrConnUnusable and its body unread, so the caller can retry it elsewhere.

Index

Constants

This section is empty.

Variables

View Source
var ErrConnUnusable = errors.New("h2: connection unusable")

ErrConnUnusable is returned for a request the peer cannot have processed (connection closed or going away before its HEADERS went out, or stream refused before its body was read). It is safe to retry on another connection with the same body, which RoundTrip has neither read nor closed.

View Source
var ErrStreamRefused = errors.New("h2: stream refused")

ErrStreamRefused is returned for a request whose stream the peer refused (REFUSED_STREAM) before its body was read, when the profile resends refused requests on the same connection (HTTP2.RefusedStreamSends). The connection stays usable, and the body is neither read nor closed.

Functions

func Decline

func Decline(conn net.Conn, p *profile.Spec) error

Decline sends what New sends on conn, then GOAWAY (NO_ERROR), and closes conn without acknowledging the server's SETTINGS. Firefox does this to a connection made unnecessary by coalescing; see decision 0023. conn is closed on errors too.

func Head(p *profile.Spec, req *http.Request) ([]hpack.Field, error)

Head returns the header list, pseudo-headers first, that a connection for p sends for req, or the error RoundTrip would return before sending.

Types

type Conn

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

Conn is a client HTTP/2 connection that opens like the profile's browser. It is safe for concurrent use.

func New

func New(conn net.Conn, p *profile.Spec, clock clockwork.Clock) (*Conn, error)

New opens an HTTP/2 connection on conn, which must have negotiated "h2", and sends the profile's preface. p must have HTTP2; clock drives the profile's timers.

func (*Conn) CanTakeNewRequest

func (c *Conn) CanTakeNewRequest() bool

CanTakeNewRequest reports whether new requests can start on c.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection; in-flight requests fail.

func (*Conn) Connect

func (c *Conn) Connect(ctx context.Context, req *http.Request, protocol string) (*http.Response, *Stream, error)

Connect opens an extended CONNECT stream (RFC 8441) for req, with :protocol after the profile's pseudo-headers, and returns the response once its headers arrive. A 2xx response comes with the stream, which outlives ctx until it is ended; any other response has no stream and its body is read as RoundTrip's. Errors are those of RoundTrip.

func (*Conn) ConnectProtocol

func (c *Conn) ConnectProtocol() bool

ConnectProtocol reports whether the peer sent SETTINGS_ENABLE_CONNECT_PROTOCOL 1, allowing extended CONNECT (RFC 8441).

func (*Conn) Done

func (c *Conn) Done() <-chan struct{}

Done is closed once the connection has finished: closed by either side, failed, or drained after GOAWAY.

func (*Conn) GoAway

func (c *Conn) GoAway()

GoAway sends GOAWAY (NO_ERROR) and takes no more requests. Those in flight run to completion and the connection closes after the last, or after declineLinger if idle. Firefox does this once the origin's HTTP/3 connection is up; see concepts/http3.md.

func (*Conn) IdleSince

func (c *Conn) IdleSince() (time.Time, bool)

IdleSince reports when c's last request ended (or c opened), and false while it has requests in flight.

func (*Conn) OnClose

func (c *Conn) OnClose(f func())

OnClose runs f once c has finished (Done is closed): at once, on the caller's goroutine, if it has; otherwise on the read loop as it exits. f runs with none of c's locks held.

func (*Conn) RoundTrip

func (c *Conn) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip sends req on a new stream and returns the response once its headers arrive. Closing the body early resets the stream, and the request context bounds body reads too. req.Body is closed, except with ErrConnUnusable or ErrStreamRefused, which leave it unread for the retry.

type Stream

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

Stream is an extended CONNECT stream: a byte stream in DATA frames both ways, the server's in its response's Body.

func (*Stream) Close

func (st *Stream) Close() error

Close resets the stream (RST_STREAM NO_ERROR), unless both sides ended it already.

func (*Stream) MaxData

func (st *Stream) MaxData() int

MaxData is the most one DATA frame carries now: the profile's cap, or the peer's frame size if smaller.

func (*Stream) Read

func (st *Stream) Read(p []byte) (int, error)

Read reads what the peer sent on the stream: its response's Body.

func (*Stream) Release

func (st *Stream) Release()

Release forgets the stream without sending a frame: for a stream the peer ended, when the profile does not describe how the browser ends its.

func (*Stream) Send

func (st *Stream) Send(ctx context.Context, parts [][]byte, end bool) error

Send writes each part in DATA frames of its own, cut at the profile's DATA cap and the flow-control windows; end sets END_STREAM on the last.

Jump to

Keyboard shortcuts

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