TsgcWebSocketHTTPServer › Events › OnBeforeCommand
Fires before OnCommandGet or OnCommandOther so the request can be screened, authorized, or short-circuited with a 401 response.
property OnBeforeCommand: TsgcWSOnBeforeCommand;
// TsgcWSOnBeforeCommand = procedure(const aConnection: TsgcWSConnection; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; var aOptions: TsgcHTTPCommandOptions) of object
—
OnBeforeCommand runs for every HTTP request before the server dispatches it to OnCommandGet or OnCommandOther. Inspect ARequestInfo (Document, Params, Headers, RemoteIP...) to decide how the request must be handled and assign aOptions accordingly. Include hcoUnauthorized to reject the request immediately with a 401 response (useful to gate an endpoint without writing any response yourself), include hcoAuthorizedBasic, hcoAuthorizedOAuth2 or hcoAuthorizedJWT to treat the request as already authorized so the check for that scheme is skipped, or leave the set empty to let the normal command handlers run. The event is the recommended place to mix public and authenticated endpoints on the same server. It fires once per request on HTTP/1.1, HTTP/2 and HTTP/3. An HTTP/3 request has no connection object behind it, so aConnection is nil on that transport.
procedure OnBeforeCommand(const aConnection: TsgcWSConnection; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo; var aOptions: TsgcHTTPCommandOptions);
begin
if aRequestInfo.Document = '/public' then
aOptions := [hcoAuthorizedBasic];
end;