TsgcWebSocketHTTPServerEvents › OnBeforeCommand

OnBeforeCommand Event

Fires before OnCommandGet or OnCommandOther so the request can be screened, authorized, or short-circuited with a 401 response.

Syntax

property OnBeforeCommand: TsgcWSOnBeforeCommand;
// TsgcWSOnBeforeCommand = procedure(const aConnection: TsgcWSConnection; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; var aOptions: TsgcHTTPCommandOptions) of object

Default Value

Remarks

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 to force Basic authentication, 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.

Example


procedure OnBeforeCommand(const aConnection: TsgcWSConnection; ARequestInfo: TIdHTTPRequestInfo;
  AResponseInfo: TIdHTTPResponseInfo; var aOptions: TsgcHTTPCommandOptions);
begin
  if aRequestInfo.Document = '/public' then
    aOptions := [hcoAuthorizedBasic];
end;

Back to Events