Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mobile.HTTPSClient

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)

Classes, interfaces and records

classeHTTPSClientError

inherits from eTLSClientError

(no public members)

interfaceiHttpExchange

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;
Waits for the response head. False on timeout - which is NOT a failure: call it again, or Cancel. Test Failed to tell a timeout from a dead exchange.
function Read(var aBuf; aCount: Integer; aMSec: Cardinal): Integer;
Reads body octets into aBuf. FOUR OUTCOMES, and they are deliberately all distinct, because collapsing any two of them turns a truncated or failed download into an apparently complete one: > 0 that many octets were read. Full description »
function ReadAll(aMSec: Cardinal): TBytes;
Reads the whole remaining body into one buffer, bounded by Limits.BodyBytes. Unlike Read this RAISES rather than returning a sentinel: an eHTTPSClientError on a timeout and on a failed exchange, so a caller that ignores the distinction cannot silently receive a truncated body. Only for bodies you are content to hold entirely in memory; stream with Read when you are not.
function Response: tHttpResponse;
Status and headers; valid once WaitForHead has returned True. Owned by the exchange.
procedure Cancel;
Abandons the request: a response not yet complete fails (FailText says it was cancelled) and its HTTP/2 or HTTP/3 stream is reset, an upload still being written stops, and on HTTP/1.1 the connection is not reused. Safe from another thread. A response that has already completed stays readable, body included, and keeps its receive-window credit: DiscardBody drops it.
function Failed: Boolean; function FailText: string; function Retryable: Boolean;
True when the failure was "this connection could not carry it", so the same request may be re-issued on a fresh connection.
procedure DiscardBody;
Drops the part of the response body you have not read and gives its receive-window credit back - for every response whose body you do not want, a 404 page for example. Full description »

interfaceiHttpUploadExchange

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;
True while the body may still be written. False once a final write was accepted, or the upload was cancelled, failed or stopped by the server, or the client disconnected.
function RequestBodyCapacity: Integer;
How many bytes TrySendBody would accept now, at most cHttpsUploadChunkBytes. Only a hint: it can change before the next call. 0 when the body is closed, the queue is full or the client is busy; a final empty write may still be accepted while the queue is full.
function TrySendBody(const aData: TBytes; aEndStream: Boolean): Boolean;
Offers the next piece of the body, at most cHttpsUploadChunkBytes; aEndStream True makes it the last. Full description »
function WaitForBodyCapacity(aMinBytes: Integer; aMSec: Cardinal): Boolean;
Waits until TrySendBody can take at least aMinBytes (1 to cHttpsUploadChunkBytes), for at most aMSec (0 only tests). False: if RequestBodyOpen is still True it was a timeout - wait again; otherwise the body is closed (a final write, Cancel, a failure, or the server stopped it) and the response or FailText tells you how it ended.

interfaceiHttpUploadTrailersExchange

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;

classtHTTPClientLimits

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;
Largest assembled response body for one exchange. A backstop rather than the primary defence: on h2 the receive window is the real control, because credit is withheld until the consumer drains.
property HeaderBytes: Integer read fHeaderBytes write SetHeaderBytes default cHttpsDefaultHeaderBytes;
HTTP/1.1 only: the complete status+header block, each chunk size/extension line and the complete trailer section must each fit this bound. Check it incrementally before buffering bytes from an unterminated peer.
property Http3QpackTableCapacity: Integer read fHttp3QpackTableCapacity write SetHttp3QpackTableCapacity default 0;
HTTP/3 header compression (QPACK) limits, used only with hcpHttp3. Http3QpackTableCapacity (0..1048576 bytes) and Http3QpackBlockedStreams (0..100, how many responses may wait for table updates) are announced to the server. Full description »
property Http3QpackBlockedStreams: Integer read fHttp3QpackBlockedStreams write SetHttp3QpackBlockedStreams default 0; property Http3MaxQpackBlockedBytes: Integer read fHttp3MaxQpackBlockedBytes write SetHttp3MaxQpackBlockedBytes default 262144;

classTsmCustomHTTPSClient

(no public members)

classtHttpsExchange

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;
Returns the current value of the fResponse field.
procedure Cancel; function Failed: Boolean;
Returns True if fState equals hxsFailed; otherwise False.
function FailText: string;
Returns the current value of the fFailText field.
function Retryable: Boolean;
Returns the current value of the fRetryable field.
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;

classtHttpsPumpThread

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);

classTsmCustomHTTPSClient

inherits from TsmCustomTLSClient

constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Open;
Opens the connection and negotiates the wire. Called implicitly by the request methods, so an application normally never calls it. » member page
procedure Connect; override; procedure Disconnect; override; function GetChannelBinding(out aBinding: TBytes): Boolean; override; procedure ClearAltSvcCache;
Forgets every HTTP/3 alternative learned so far, in a shared cache for every client using it. Call it when the network or the trust configuration changes. A live HTTP/3 connection is not closed. » member page
property AltSvcCache: iHttpAltSvcCache read GetAltSvcCache write SetAltSvcCache;
Where the HTTP/3 alternatives learned with hcpHttp3 are kept. Each client starts with a private cache. Full description »
function BeginRequest(const aMethod, aURL: string; const aHeaders: tHttpHeaders; const aBody: TBytes): iHttpExchange;
Starts a request and returns immediately. Several may be outstanding at once when the connection settled on h2. » member page
function BeginUpload(const aMethod, aURL: string; const aHeaders: tHttpHeaders): iHttpUploadExchange; overload;
Starts a request whose body you then write through the returned iHttpUploadExchange (see there for the call sequence). Full description »
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;
The same, with trailer fields after the body. aTrailerNames lists the fields you may send as trailers (sent as the Trailer field; an empty list is allowed). End with TrySendTrailers on the result's iHttpUploadTrailersExchange, or with an ordinary final TrySendBody, which sends no trailer fields. HTTP/1.1 then always sends the body chunked, even with aContentLength, which is still enforced. » member page
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;
BeginRequest, then wait for the head and read the whole body. The caller owns the returned response and must free it. Full description »
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;
The wire ALPN settled on for the live connection. » member page
property WireFeedCount: Int64 read GetWireFeedCount;
How many times the pump has handed inbound plaintext to a wire engine, and how many octets in total. Both are written only by the pump. » member page
property WireBytesIn: Int64 read GetWireBytesIn; property Protocols: tHttpClientProtocols read fProtocols write SetProtocols default [hcpHttp11, hcpHttp2];
WHICH application protocols to offer in the TLS ALPN extension - not which to prefer. This is a SET: a set has no order, so the members are present or absent and nothing here expresses a ranking. Full description »
property Limits: tHTTPClientLimits read fLimits write SetLimits;
What this client will accept FROM a server - see tHTTPClientLimits, where each bound says which hole it closes. Assigning copies the values; the sub-object itself is owned here. » member page
property Http3AttemptTimeout: Cardinal read fHttp3AttemptTimeout write SetHttp3AttemptTimeout default cHttpsDefaultHttp3AttemptTimeout;
Milliseconds a QUIC connection attempt (see Protocols) may take before the client connects over TCP instead; the request is sent only after either has connected. A shorter ConnectTimeout or an earlier DeadlineTick cuts it shorter. Default 1000; 0 raises. Change it only while disconnected; it raises otherwise. » member page
property UserAgent: string read fUserAgent write fUserAgent;
Sent as the User-Agent request field on both wires. Set it to '' to omit the field entirely rather than to send an empty one. » member page
property MaxRedirects: Integer read fMaxRedirects write fMaxRedirects default cHttpsDefaultMaxRedirect;
How many redirect hops Execute will follow before giving up. Exceeding it RAISES rather than returning the last 3xx, because a redirect loop is a fault and returning the final hop would present it as an answer. BeginRequest never follows anything - a redirect is a property of the Execute convenience, not of an exchange. » member page
property RedirectPolicy: tHttpRedirectPolicy read fRedirect write fRedirect default hrpSafeMethods;
WHICH methods Execute will follow a 3xx for. hrpSafeMethods (the default) follows GET and HEAD only. Full description »
property KeepAlive: Boolean read fKeepAlive write fKeepAlive default True;
HTTP/1.1 only, and only advisory: it selects the Connection field this client sends - keep-alive (the default) or close. HTTP/2 has no such field and ignores this entirely; a connection there is retired by GOAWAY or by the transport, never by a request header. Setting it False does not close the connection after a response - it asks the SERVER to, and a server is free to keep it open. » member page

classTsmHTTPSClient — component reference

inherits from TsmCustomHTTPSClient

Self-contained: creates and owns its TLS configuration component.

property TLSServer: TsmSimpleTLSInternalServer read GetTLSServer write SetOwnTLSServer;

classTsmAdvancedHTTPSClient — component reference

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;

Types (enumerations, sets, aliases)

enumtHttpClientWire

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.

  tHttpClientWire = (hcwNone, hcwHttp11, hcwHttp2, hcwHttp3);

enumtHttpClientProtocol

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.

  tHttpClientProtocol  = (hcpHttp11, hcpHttp2, hcpHttp3);

settHttpClientProtocols

  tHttpClientProtocols = set of tHttpClientProtocol;

enumtHttpRedirectPolicy

Which methods Execute follows a 3xx for - see the RedirectPolicy property, which documents the per-status behaviour and the cross-origin rules.

  tHttpRedirectPolicy = (hrpSafeMethods, hrpNever, hrpAll);

enumtHttpExchangeState

  tHttpExchangeState = (hxsPending, hxsHead, hxsBody, hxsDone, hxsFailed);