A consumer-facing HTTPS client for HTTP/1.1 and HTTP/2 over TLS that can also move to HTTP/3 over QUIC (see Protocols). Its TCP wire is chosen by ALPN rather than by configuration. It descends from TsmCustomTLSClient, so the connection, the handshake, the record layer, the timeouts and the ownership tiers all come from there; what this unit adds is the two application wires, the pump thread that drives them, and the request API. MULTIPLEXING IS THE POINT OF h2 AND IS PRESENT FROM THE FIRST VERSION. Execute is BeginRequest + WaitFor, not the other way round, so there is one code path rather than a simple one with a concurrent one bolted beside it. Several application threads may have requests in flight over one h2 connection; on HTTP/1.1 they serialise, because that wire has no way not to. THE THREAD MODEL, in one paragraph. One pump thread per connection owns the frame loop: it reads plaintext through the inherited transport and feeds it to whichever wire engine is live. Application threads never touch the socket - they enter the ENGINE GATE, hand work to the engine, and wait on their own exchange event. The gate is what makes HPACK safe: the dynamic table is per connection and stateful, so encoding a header block, allocating the stream id that orders it, and emitting it must be one critical section, or the peer's decoder desynchronises and every exchange on the connection dies of COMPRESSION_ERROR under load. LOCK ORDER IS ALWAYS engine gate -> transport lock, never the reverse; the base class never takes the gate, so there is no cycle to close. SCOPE. One connection per component instance: an instance talks to one origin at a time, with h2 multiplexing over that connection and HTTP/1.1 keep-alive reuse. A multi-connection pool - several connections per origin, which is what HTTP/1.1 needs for concurrency - is the next increment and is deliberately not here; nothing in this API changes when it arrives.
Feature notes: TLS implementation notes (RFC map) · HTTPS client notes (HTTP/1.1, HTTP/2, HTTP/3)
inherits from eTLSClientError
(no public members)
One request in flight; BeginRequest returns it, BeginUpload returns the iHttpUploadExchange extension. Call WaitForHead, then Response for the status and headers, then Read until it returns 0 (or ReadAll). Any application thread may use it, and Cancel may come from another thread while one waits. The exchange holds its own response: a complete one stays readable after Disconnect or after the client is freed. One that was not complete then fails, after Read has returned the bytes already received. An old exchange never affects a later connection. READ EVERY BODY TO ITS END, OR DISCARD IT. Until then a completed response keeps its body in memory and the client keeps the exchange; on HTTP/2 and HTTP/3 the unread octets keep their share of the connection's receive window, so enough unread bodies stall every response on that connection; and with hcpHttp3 the client never moves to HTTP/3. Cancel leaves a completed response as it is; DiscardBody ends all of that.
function WaitForHead(aMSec: Cardinal): Boolean;
function Read(var aBuf; aCount: Integer; aMSec: Cardinal): Integer;
function ReadAll(aMSec: Cardinal): TBytes;
function Response: tHttpResponse;
procedure Cancel;
function Failed: Boolean;
function FailText: string;
function Retryable: Boolean;
procedure DiscardBody;
inherits from iHttpExchange
A request whose body you write piece by piece; BeginUpload returns it. Send each piece with TrySendBody(Piece, False); when that returns False, call WaitForBodyCapacity and retry with the same Piece. End with a final TrySendBody(Last, True) - Last may be empty - or, when BeginUpload was given trailer names, with iHttpUploadTrailersExchange.TrySendTrailers. Read the response as on any exchange. It may arrive while the body is still open; reading it does not end the upload, Cancel does. An accepted TBytes is kept by reference until it is sent: use a new array for each piece, never refill one. TrySendBody and TrySendTrailers never wait for the network (the client's pump thread sends), and once the client's DeadlineTick has passed the upload calls raise eHTTPSClientError.
function RequestBodyOpen: Boolean;
function RequestBodyCapacity: Integer;
function TrySendBody(const aData: TBytes; aEndStream: Boolean): Boolean;
function WaitForBodyCapacity(aMinBytes: Integer; aMSec: Cardinal): Boolean;
Ends an upload begun with a BeginUpload overload that takes trailer names: the fields follow the body. Get it with Supports(Upload, iHttpUploadTrailersExchange, Final). TrySendTrailers takes the place of the final TrySendBody, so send every piece with aEndStream False first; a known length must be complete. aTrailers stays yours (the fields are copied) and may use only the declared names, not necessarily all. True: the body is closed. False: nothing was accepted for now - retry. Raises, accepting nothing, for an undeclared, forbidden or malformed field, a short body, an upload begun without trailer names, or one already closed.
function TrySendTrailers(const aTrailers: tHttpHeaders): Boolean;
inherits from TPersistent
WHAT THIS CLIENT WILL ACCEPT FROM A SERVER. A client is usually treated as the trusted end of a connection; it is not - it simply faces one attacker instead of the whole internet. 0 = unbounded, and a negative normalises to 0 on the way in.
constructor Create(aOwner: TComponent);
procedure AssignTo(Dest: TPersistent); override;
property BodyBytes: Integer read fBodyBytes write SetBodyBytes default cHttpsDefaultBodyBytes;
property HeaderBytes: Integer read fHeaderBytes write SetHeaderBytes default cHttpsDefaultHeaderBytes;
property Http3QpackTableCapacity: Integer read fHttp3QpackTableCapacity write SetHttp3QpackTableCapacity default 0;
property Http3QpackBlockedStreams: Integer read fHttp3QpackBlockedStreams write SetHttp3QpackBlockedStreams default 0;
property Http3MaxQpackBlockedBytes: Integer read fHttp3MaxQpackBlockedBytes write SetHttp3MaxQpackBlockedBytes default 262144;
(no public members)
The concrete exchange. Not exposed - callers hold iHttpExchange.
constructor Create(aOwner: TsmCustomHTTPSClient; aStreamID: UInt64);
destructor Destroy; override;
function WaitForHead(aMSec: Cardinal): Boolean;
function Read(var aBuf; aCount: Integer; aMSec: Cardinal): Integer;
function ReadAll(aMSec: Cardinal): TBytes;
function Response: tHttpResponse;
procedure Cancel;
function Failed: Boolean;
function FailText: string;
function Retryable: Boolean;
procedure DiscardBody;
function RequestBodyOpen: Boolean;
function RequestBodyCapacity: Integer;
function TrySendBody(const aData: TBytes; aEndStream: Boolean): Boolean;
function WaitForBodyCapacity(aMinBytes: Integer; aMSec: Cardinal): Boolean;
function TrySendTrailers(const aTrailers: tHttpHeaders): Boolean;
inherits from TThread
Drives the inbound half of one connection. Owns nothing; every mutation it makes to the wire engines happens inside the owner's engine gate.
constructor Create(aOwner: TsmCustomHTTPSClient);
inherits from TsmCustomTLSClient
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Open;
procedure Connect; override;
procedure Disconnect; override;
function GetChannelBinding(out aBinding: TBytes): Boolean; override;
procedure ClearAltSvcCache;
property AltSvcCache: iHttpAltSvcCache read GetAltSvcCache write SetAltSvcCache;
function BeginRequest(const aMethod, aURL: string; const aHeaders: tHttpHeaders; const aBody: TBytes): iHttpExchange;
function BeginUpload(const aMethod, aURL: string; const aHeaders: tHttpHeaders): iHttpUploadExchange; overload;
function BeginUpload(const aMethod, aURL: string; const aHeaders: tHttpHeaders; aContentLength: Int64): iHttpUploadExchange; overload;
function BeginUpload(const aMethod, aURL: string; const aHeaders: tHttpHeaders; const aTrailerNames: array of string): iHttpUploadExchange; overload;
function BeginUpload(const aMethod, aURL: string; const aHeaders: tHttpHeaders; aContentLength: Int64; const aTrailerNames: array of string): iHttpUploadExchange; overload;
function Execute(const aMethod, aURL: string; const aHeaders: tHttpHeaders; const aBody: TBytes): tHttpResponse;
function Get(const aURL: string; const aAccept: string = ''): tHttpResponse;
function Head(const aURL: string): tHttpResponse;
function Post(const aURL: string; const aBody: TBytes; const aContentType: string): tHttpResponse;
property Wire: tHttpClientWire read fWire;
property WireFeedCount: Int64 read GetWireFeedCount;
property WireBytesIn: Int64 read GetWireBytesIn;
property Protocols: tHttpClientProtocols read fProtocols write SetProtocols default [hcpHttp11, hcpHttp2];
property Limits: tHTTPClientLimits read fLimits write SetLimits;
property Http3AttemptTimeout: Cardinal read fHttp3AttemptTimeout write SetHttp3AttemptTimeout default cHttpsDefaultHttp3AttemptTimeout;
property UserAgent: string read fUserAgent write fUserAgent;
property MaxRedirects: Integer read fMaxRedirects write fMaxRedirects default cHttpsDefaultMaxRedirect;
property RedirectPolicy: tHttpRedirectPolicy read fRedirect write fRedirect default hrpSafeMethods;
property KeepAlive: Boolean read fKeepAlive write fKeepAlive default True;
inherits from TsmCustomHTTPSClient
Self-contained: creates and owns its TLS configuration component.
property TLSServer: TsmSimpleTLSInternalServer read GetTLSServer write SetOwnTLSServer;
inherits from TsmCustomHTTPSClient
Takes an EXTERNAL engine, so several clients share one set of trust anchors and one identity; nil falls back to GlobalServer.
property TLSServer;
Which wire a connection actually settled on. Reported, never set: ALPN decides it. hcwHttp11 is also what a server that offers no ALPN at all gets, which is why NegotiatedProtocol rather than the offer list is what this reads.
The application protocols that may be offered in ALPN. A SET, so membership only - see the Protocols property for why no ordering is expressed here and why the server's preference is the one that decides.
Which methods Execute follows a 3xx for - see the RedirectPolicy property, which documents the per-status behaviour and the cross-origin rules.