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 ¶
- Variables
- func Decline(conn net.Conn, p *profile.Spec) error
- func Head(p *profile.Spec, req *http.Request) ([]hpack.Field, error)
- type Conn
- func (c *Conn) CanTakeNewRequest() bool
- func (c *Conn) Close() error
- func (c *Conn) Connect(ctx context.Context, req *http.Request, protocol string) (*http.Response, *Stream, error)
- func (c *Conn) ConnectProtocol() bool
- func (c *Conn) Done() <-chan struct{}
- func (c *Conn) GoAway()
- func (c *Conn) IdleSince() (time.Time, bool)
- func (c *Conn) OnClose(f func())
- func (c *Conn) RoundTrip(req *http.Request) (*http.Response, error)
- type Stream
Constants ¶
This section is empty.
Variables ¶
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.
Functions ¶
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 ¶
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 ¶
CanTakeNewRequest reports whether new requests can start on c.
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 ¶
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 ¶
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.
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 ¶
Close resets the stream (RST_STREAM NO_ERROR), unless both sides ended it already.
func (*Stream) MaxData ¶
MaxData is the most one DATA frame carries now: the profile's cap, or the peer's frame size if smaller.