Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Http.Http2Client

******************************************************************************

Feature notes: HTTPS application layer notes (HTTP/1.1, h2, h3)

Classes, interfaces and records

recordtHttp2ClientSendPart

Data: TBytes; Offset: Integer; EndStream: Boolean;

classtHttp2ClientStream

constructor Create(aID: Cardinal; aSendWindow, aRecvWindow: Integer); destructor Destroy; override;

classtHttp2ClientConn

constructor Create; destructor Destroy; override; procedure Opened;
Emits the client connection preface and our opening SETTINGS. Call it once, before anything else, on a transport that is already up.
procedure RefuseConnection(aErrorCode: Cardinal; const aText: string);
ENDS THE CONNECTION with GOAWAY(aErrorCode), for a requirement the engine cannot see itself - the TLS version, which RFC 9113 s9.2 sets: "Implementations of HTTP/2 MUST use TLS version 1.2 [TLS12] or higher for HTTP/2 over TLS." As a protocol failure does: every live stream is failed, not retryable, OnConnFailed reports aErrorCode and aText, and ErrorGoAwaySent turns True.
procedure Receive(aData: Pointer; aLen: Integer);
Feeds inbound octets and runs the frame loop. Everything this produces - responses, ACKs, window updates - leaves through the events before it returns.
procedure Closed;
The transport is gone. Fails every outstanding stream as h2fTransport (retryable: nothing here says the request itself was bad) and then reports the connection finished. Idempotent.
function StartRequest(const aMethod, aScheme, aAuthority, aPath: string; const aHeaders: tHttpHeaders; const aBody: TBytes; aEndStream: Boolean): Cardinal;
Opens the next stream and sends the request head, plus aBody when there is one. Returns the stream id, or 0 when the connection cannot take another request - which is not an error: the caller opens another connection. Ask CanStartRequest first if you want to know why. aEndStream False leaves the request body open for SendBody.
procedure SendBody(aStreamID: Cardinal; const aData: TBytes; aEndStream: Boolean);
Writes the next piece of an open request body, as TrySendBody does, and then calls Pump, so OnOutput runs before it returns. Does nothing for an unknown stream or one whose request body is closed; otherwise raises where TrySendBody would return False or raise.
function RequestBodyOpen(aStreamID: Cardinal): Boolean;
For a request body written piece by piece, on a stream StartRequest opened with aEndStream False: True while more body may be written.
function RequestBodyCapacity(aStreamID: Cardinal): Integer;
Bytes the send queue takes now: MaxInt when MaxBufferedSendBytes is 0, and 0 when the body is closed or cH2CliMaxQueuedSendParts pieces are queued. Not what is left of the Content-Length, nor the peer's flow-control window.
function RequestBodyPending(aStreamID: Cardinal): Boolean;
True while the body or its end is still to be framed; False once END_STREAM or the trailers have been framed, or the stream failed or was cancelled.
function TrySendBody(aStreamID: Cardinal; const aData: TBytes; aEndStream: Boolean): Boolean;
Queues all of aData, with END_STREAM when aEndStream, and returns True; or queues nothing and returns False while the queue is full - retry with the same array after Pump has sent some. Full description »
function TrySendTrailers(aStreamID: Cardinal; const aTrailers: tHttpHeaders): Boolean;
Ends an open request body with trailer fields instead of END_STREAM on DATA: Pump sends them as a final HEADERS block after the queued DATA. Full description »
procedure Pump;
Frames queued request body and trailers and hands them to OnOutput, as far as the flow-control windows allow; Receive does the same after each inbound block. One call is one bounded pass over the streams, so a long queue takes several.
property MaxBufferedSendBytes: Integer read fMaxBufferedSendBytes write SetMaxBufferedSendBytes;
Bound in bytes on queued request body across all streams, as counted by RetainedSendBytes. 0, the default, sets no byte bound (the piece count stays capped at cH2CliMaxQueuedSendParts). Full description »
property QueuedBytes: Int64 read fQueuedBytes;
The send queue across all streams: body bytes not yet framed (QueuedBytes), the full length of every array still held (RetainedSendBytes; a partly sent array counts whole), and the number of such pieces (QueuedSendParts).
property RetainedSendBytes: Int64 read fRetainedSendBytes; property QueuedSendParts: Integer read fQueuedSendParts; property OutgoingTrailerBytes: Int64 read fOutgoingTrailerBytes;
Trailer sections accepted by TrySendTrailers and not yet sent, counted as name + value + 32 bytes per field (OutgoingTrailerBytes), and the size of trailer HEADERS frames built but not yet handed to OnOutput (OutgoingTrailerWireBytes). Neither counts in QueuedBytes or RetainedSendBytes.
property OutgoingTrailerWireBytes: Int64 read fOutgoingTrailerWireBytes; procedure ReleaseBody(aStreamID: Cardinal; aBytes: Integer);
THE BACKPRESSURE VALVE, and the deliberate inversion of what the server engine does. tHttp2Conn credits the receive window the instant DATA arrives and refuses bodies that grow too large, because a server assembles a request body before dispatching it and real backpressure would deadlock an upload. Full description »
procedure CancelStream(aStreamID: Cardinal);
Abandons a stream: drops what is still queued of its request body and trailers and sends RST_STREAM(CANCEL), unless the stream has already ended in both directions. Full description »
procedure SendPing;
Sends a PING, e.g. to test a pooled connection before handing it out.
procedure BeginGracefulClose;
Announces that we will open no more streams on this connection. Streams already open run to completion; Usable goes False so the pool stops handing it out.
property Usable: Boolean read GetUsable;
False once this connection may not take another request - it has gone away in either direction, the id space or the per-connection budget is spent, or the transport is finished. The pool tests this before checkout.
function CanStartRequest: Boolean;
True when Usable AND there is room under both our own and the peer's concurrency limit right now.
property ActiveStreams: Integer read GetActiveStreams; property StreamsOpened: Int64 read fStreamsOpened; property UnproductiveTokens: Integer read fUnproductive; property FailedText: string read fFailedText;
Why the connection ended, when it ended badly.
property ErrorGoAwaySent: Boolean read fErrorGoAwaySent;
True once the engine has ended the connection with a GOAWAY of its own for an error: a protocol error or malformed frame found in Receive, a local bound, or RefuseConnection. Full description »
property OnOutput : tHttp2ClientOutputEvent read fOnOutput write fOnOutput; property OnResponseHead: tHttp2ClientHeadEvent read fOnResponseHead write fOnResponseHead; property OnResponseData: tHttp2ClientDataEvent read fOnResponseData write fOnResponseData; property OnTrailers : tHttp2ClientTrailerEvent read fOnTrailers write fOnTrailers; property OnStreamFailed: tHttp2ClientFailEvent read fOnStreamFailed write fOnStreamFailed; property OnConnFailed : tHttp2ClientConnEvent read fOnConnFailed write fOnConnFailed; property InitialWindow: Integer read fInitialWindow write fInitialWindow;
bounds. FOUR OF THESE MUST BE SET BEFORE Opened, because Opened is what puts them on the wire: InitialWindow, MaxConcurrentStreams and MaxHeaderListBytes go into the opening SETTINGS, and ConnWindow is applied by the WINDOW_UPDATE that Opened emits on stream 0. Full description »
property ConnWindow: Integer read fConnWindow write fConnWindow;
The CONNECTION receive window, which has no SETTINGS parameter - the only way to raise it past the 64 KiB default is the WINDOW_UPDATE on stream 0 that Opened emits. It bounds the sum of all concurrent downloads, so a value near InitialWindow lets one stream starve the rest.
property MaxHeaderBlockBytes: Integer read fMaxHeaderBlockBytes write fMaxHeaderBlockBytes; property MaxContinuationFrames: Integer read fMaxContinuationFrames write fMaxContinuationFrames; property MaxHeaderListBytes: Integer read fMaxHeaderListBytes write SetMaxHeaderListBytes; property MaxConcurrentStreams: Integer read fMaxConcurrentStreams write fMaxConcurrentStreams; property MaxStreamsPerConnection: Integer read fMaxStreamsPerConnection write fMaxStreamsPerConnection; property MaxUnproductiveFrames: Integer read fMaxUnproductive write SetMaxUnproductiveFrames;

Types (enumerations, sets, aliases)

enumtHttp2ClientStreamState

A CLIENT stream. h2csHalfClosedLocal is the state tHttp2Conn's enum does not have and a client lives in for most of every request: we have sent END_STREAM and are waiting for the response.

  tHttp2ClientStreamState = (h2csIdle,
                             h2csOpen,               { both halves open }
                             h2csHalfClosedLocal,    { request sent in full }
                             h2csHalfClosedRemote,   { response fully received }
                             h2csClosed);

enumtHttp2FailReason

WHY an exchange ended without a complete response. The distinction that matters to the caller is aRetryable - whether re-issuing the request on a fresh connection is safe and sensible, or whether it would just fail again.

  tHttp2FailReason = (h2fNone,
                      h2fReset,        { peer sent RST_STREAM }
                      h2fRefused,      { REFUSED_STREAM, or above a GOAWAY's last id }
                      h2fGoAway,       { connection retired under us }
                      h2fConnError,    { we or the peer broke the protocol }
                      h2fTransport,    { the connection died }
                      h2fCancelled,    { the caller abandoned it }
                      h2fLocalLimit);  { a local bound refused the response }

proctHttp2ClientOutputEvent

  tHttp2ClientOutputEvent  = procedure (aData: Pointer; aLen: Integer) of object;

proctHttp2ClientHeadEvent

A response head. aInformational marks a 1xx interim response (100-continue, 103 early hints): the exchange is NOT over and a final head will follow, so a consumer that treats it as the answer will hand the caller the wrong status.

  tHttp2ClientHeadEvent    = procedure (aStreamID: Cardinal;
                                        const aHeaders: tHttpHeaders;
                                        aInformational, aEndStream: Boolean) of object;

proctHttp2ClientDataEvent

  tHttp2ClientDataEvent    = procedure (aStreamID: Cardinal;
                                        aData: Pointer; aLen: Integer;
                                        aEndStream: Boolean) of object;

proctHttp2ClientTrailerEvent

  tHttp2ClientTrailerEvent = procedure (aStreamID: Cardinal;
                                        const aHeaders: tHttpHeaders) of object;

proctHttp2ClientFailEvent

  tHttp2ClientFailEvent    = procedure (aStreamID: Cardinal;
                                        aReason: tHttp2FailReason;
                                        aErrorCode: Cardinal;
                                        aRetryable: Boolean) of object;

proctHttp2ClientConnEvent

The connection itself is finished. Every live stream has already been failed through OnStreamFailed before this fires.

  tHttp2ClientConnEvent    = procedure (aErrorCode: Cardinal;
                                        const aText: string) of object;

Routines