The status/1 protocol separates replay-safe queries from operations that change session state. A complete early STATUS query can be answered safely. Its MARK command requires an entire line received after the handshake.
Keep early delivery disabled for protocols that require every request to arrive after the handshake. For this status service, enable it on the TLS engine and specify the maximum number of early bytes a client may send.
uses stDERUtils, StreamSec.Mobile.StreamSecII, StreamSec.Mobile.TlsInternalServer, StreamSec.Mobile.AsyncTLSServer; type tStatusQueryEvent = procedure(aEarly: Boolean) of object;
procedure ConfigureStatusEarlyData(aEngine: TsmSimpleTLSInternalServer; aReplayCheck: TTLSCheckEarlyDataReplayEvent; aMaxBytes: Cardinal); begin aEngine.OnTLSCheckEarlyDataReplay := aReplayCheck; if Assigned(aReplayCheck) and (aMaxBytes > 0) then begin aEngine.Options.MaxEarlyDataSize := aMaxBytes; aEngine.Options.EarlyData := edmAccept; end else begin aEngine.Options.EarlyData := edmDisabled; aEngine.Options.MaxEarlyDataSize := 0; end; end;
function tStatusProtocol.GetToken: OctetString; begin Result := 'status/1'; end; function tStatusProtocol.GetDescription: string; begin Result := 'Read status and mark a session'; end; function tStatusProtocol.CreateSession(const aTransport: iAppTransport): iAppSession; begin Result := tStatusSession.Create(aTransport, fOnStatus); end; procedure RegisterStatusProtocol(aServer: TsmAsyncTLSServer; aOnStatus: tStatusQueryEvent); begin aServer.Protocols.AddProtocol(tStatusProtocol.Create(aOnStatus)); end;
Call RegisterStatusProtocol(Server) before activating the listener. The protocol advertises status/1 through ALPN. Load the server identity and configure the listener as in Run an asynchronous TLS server.
From OnConfigureEngine, call ConfigureStatusEarlyData(aEngine, CheckStatusReplay, 1024). Supply your replay-policy method as CheckStatusReplay. The engine advertises the 1024-byte allowance in new tickets and calls the method with the ticket identity and ClientHello hash before approving early data. The method can set Accept := False to decline early delivery while allowing ordinary resumption. To disable early delivery entirely, use ConfigureStatusEarlyData(aEngine, nil).
The default in-memory store atomically consumes a ticket once. Independent engines have separate ticket stores by default. A reconnect must reach the issuing engine to use its ticket. Across engines that share tickets, the store must enforce single use across all of them for early delivery.
The listener holds accepted 0-RTT bytes until it verifies the client's Finished and opens the application session. It delivers those bytes to EarlyDataReceived before any ordinary input. If the handshake fails or the client disconnects before Finished, the listener discards the buffered bytes without opening a session or calling the application.
type tStatusSession = class(TInterfacedObject, iAppSession) private fTransport: iAppTransport; fOnStatus: tStatusQueryEvent; fLine: RawByteString; fStartedEarly, fMarked, fClosed: Boolean; procedure Reply(const aText: RawByteString); procedure FinishLine(aEarly: Boolean); procedure Receive(aData: Pointer; aLen: Integer; aEarly: Boolean); public constructor Create(const aTransport: iAppTransport; aOnStatus: tStatusQueryEvent); procedure Opened; procedure DataReceived(aData: Pointer; aLen: Integer); procedure EarlyDataReceived(aData: Pointer; aLen: Integer); procedure Closed(aGraceful: Boolean); function Instance: TObject; end;
Create one session object per connection. The constructor retains its transport and the optional status notification. Reply sends an ASCII line followed by CRLF through the transport. Release the transport in Closed, as in the asynchronous echo server.
procedure tStatusSession.Receive(aData: Pointer; aLen: Integer; aEarly: Boolean); var lByte: PAnsiChar; i: Integer; begin lByte := PAnsiChar(aData); for i := 1 to aLen do begin if fClosed then Exit; if Length(fLine) = 16 then begin fClosed := True; fTransport.Close(False); Exit; end; if Length(fLine) = 0 then fStartedEarly := aEarly; fLine := fLine + lByte^; if lByte^ = #10 then begin FinishLine(aEarly); fLine := ''; end; Inc(lByte); end; end; procedure tStatusSession.EarlyDataReceived(aData: Pointer; aLen: Integer); begin Receive(aData, aLen, True); end; procedure tStatusSession.DataReceived(aData: Pointer; aLen: Integer); begin Receive(aData, aLen, False); end;
After the handshake creates the protocol session, the listener delivers buffered early bytes to EarlyDataReceived before delivering ordinary bytes to DataReceived. A callback can contain a partial line or several lines. Each session keeps its own bounded line buffer. fStartedEarly records where the first byte arrived, including when the CR and LF arrive separately.
procedure tStatusSession.FinishLine(aEarly: Boolean); begin if fStartedEarly <> aEarly then Reply('REFUSED') else if fLine = 'STATUS'#13#10 then begin if Assigned(fOnStatus) then fOnStatus(aEarly); if fMarked then Reply('MARKED') else Reply('READY'); end else if (not aEarly) and (fLine = 'MARK'#13#10) then begin fMarked := True; Reply('MARKED'); end else Reply('REFUSED'); end;
The server answers STATUS with READY until that session has been marked. It replies REFUSED to an early MARK and to any line that starts early and ends in ordinary data. A complete ordinary MARK sets the flag and subsequent status queries return MARKED. Choose early operations whose repetition is harmless to your application.
The client example checks whether TLS accepted the early flight. A REFUSED reply here means the bytes reached the server and its application declined the command.
TsmAsyncTLSServer · StreamSec.Mobile.AsyncTLSServer · StreamSec.Mobile.StreamSecII · StreamSec.Mobile.TlsInternalServer