Feature notes: HTTPS application layer notes (HTTP/1.1, h2, h3)
inherits from Exception
(no public members)
(no public members)
(no public members)
ONE MATCHED REST REQUEST, as a handler sees it: the template captures, the parsed JSON body, the authenticated principal, and the underlying iHttpRequest for everything else (headers, cookies, session, the TLS facts). Valid ONLY during the handler call, like the request it wraps.
function GetHttp: iHttpRequest;
function GetJson: tJsonValue;
function GetPrincipal: string;
function GetParamCount: Integer;
function GetParamName(aIndex: Integer): string;
function GetParamByName(const aName: string): string;
function QueryValue(const aName: string; const aDefault: string = ''): string;
property Http: iHttpRequest read GetHttp;
property Json: tJsonValue read GetJson;
property Principal: string read GetPrincipal;
property ParamCount: Integer read GetParamCount;
property ParamNames[aIndex: Integer]: string read GetParamName;
property Params[const aName: string]: string read GetParamByName; default;
THE ANSWER, with the JSON conveniences a REST handler wants. Everything still lands on the wrapped iHttpResponse - reachable as Http for the cases these helpers do not cover (extra headers, files). Every helper SETS THE STATUS EXPLICITLY, so there is no promotion ambiguity: what you send is what goes out.
function GetHttp: iHttpResponse;
procedure SendJson(aJson: tJsonValue; aStatus: Integer = 200; aOwns: Boolean = True);
procedure SendCreated(const aLocation: string; aJson: tJsonValue = nil; aOwns: Boolean = True);
procedure SendNoContent;
procedure SendProblem(aStatus: Integer; const aTitle: string = ''; const aDetail: string = '');
property Http: iHttpResponse read GetHttp;
WHAT AN APPLICATION IMPLEMENTS - the REST-typed sibling of iHttpRequestHandler, registered per method + template with AddRoute. One instance serves every connection, so it must be safe to call from several threads at once; both arguments die when the call returns. Raising is safe: the dispatcher answers a generic 500 problem and reports the real error to OnRequestError.
procedure HandleRest(const aRequest: iRestRequest; const aResponse: iRestResponse);
THE IDEMPOTENCY SEAM - the server-side half of the client's IdempotencyHeader. The dispatcher consults it for every UNSAFE request (anything but GET/HEAD/OPTIONS/TRACE) that carries a key, scoping the key by method and path so one client key cannot collide across endpoints.
The contract is a reservation: BeginRequest answering rivNew RESERVES the key, and the dispatcher then always either CompleteRequest (the answer is stored and replayed to duplicates) or AbandonRequest (the reservation is dropped and a retry executes afresh). 5xx answers are ABANDONED, never stored - a transient failure must not become the key's permanent answer.
Implementations MUST be safe to call from several threads at once; the bundled one (NewRestIdempotencyStore) locks itself, expires entries on a TTL and evicts the oldest completed entry past its cap.
function BeginRequest(const aKey: string; out aStatus: Integer; out aContentType: string; out aBody: TBytes): tRestIdempotencyVerdict;
procedure CompleteRequest(const aKey: string; aStatus: Integer; const aContentType: string; const aBody: TBytes);
procedure AbandonRequest(const aKey: string);
ONE ROUTE: a method, a parsed template, a handler. Built and owned by the dispatcher; declared here only because the dispatcher's route list needs the type. Application code never touches it.
constructor Create(const aMethod, aTemplate: string; const aHandler: iRestHandler; aAuthExempt: Boolean);
function MatchesSegments(const aSegs: array of string): Boolean;
function SameShape(const aOther: tRestRoute): Boolean;
property Method: string read fMethod;
property AuthExempt: Boolean read fAuthExempt;
property Handler: iRestHandler read fHandler;
inherits from TInterfacedObject
implements iHttpRequestHandler
THE ENGINE. An iHttpRequestHandler that owns the route table and runs the pipeline documented at the top of the unit. Transport-free: tests drive HandleRequest with hand-built tHttpServerRequest/tHttpServerResponse objects and no socket exists anywhere.
LIFETIME: refcounted. The component keeps one interface reference for the engine's whole life and hands the same reference to the router's prefix mount; nothing ever calls Free on it. Code using the engine directly should do the same - hold it as iHttpRequestHandler (or assign it to one immediately) and let the last reference free it.
Routes are NOT locked: register them before the server goes Active. The callbacks fire with EventSender as Sender - the component sets itself.
constructor Create;
destructor Destroy; override;
procedure AddRoute(const aMethod, aTemplate: string; const aHandler: iRestHandler; aAuthExempt: Boolean = False);
procedure AddRouteEvent(const aMethod, aTemplate: string; aEvent: tRestRequestEvent; aAuthExempt: Boolean = False);
procedure ClearRoutes;
procedure HandleRequest(const aRequest: iHttpRequest; const aResponse: iHttpResponse);
property BasePath: string read fBasePath write SetBasePath;
property EventSender: TObject read fEventSender write fEventSender;
property OnAuthenticate: tRestAuthenticateEvent read fOnAuthenticate write fOnAuthenticate;
property OnError: tRestErrorEvent read fOnError write fOnError;
property IdempotencyStore: iRestIdempotencyStore read fIdempotencyStore write fIdempotencyStore;
property IdempotencyHeader: string read fIdempotencyHeader write fIdempotencyHeader;
property AuthChallenge: string read fAuthChallenge write fAuthChallenge;
inherits from TComponent
THE COMPONENT. Drop one, point Protocol at either HTTPS protocol component (the property is typed at their common base), register routes, done - the canonical wiring shares ONE router between TsmHTTPSv1dot1 and TsmHTTPSv2, so mounting on either component's router serves both wires.
The association binds in Loaded, exactly like Protocol's own Server reference: during DFM streaming the referenced component may not exist yet. Runtime assignment binds immediately. MountOn is the escape hatch for a router that belongs to no component - a test, a hand-built server.
The seams are virtual-method-plus-event pairs, the same contract as TsmCustomRESTClient: subclass and override, or drop an instance and assign the event - both work, and an override that calls inherited keeps the event working.
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AddRoute(const aMethod, aTemplate: string; const aHandler: iRestHandler; aAuthExempt: Boolean = False);
procedure AddRouteEvent(const aMethod, aTemplate: string; aEvent: tRestRequestEvent; aAuthExempt: Boolean = False);
procedure MountOn(const aRouter: iHttpRouter);
procedure Unmount;
property Dispatcher: tRestDispatcher read fDispatcher;
property IdempotencyStore: iRestIdempotencyStore read GetIdempotencyStore write SetIdempotencyStore;
property Protocol: TsmCustomHTTPSProtocol read fProtocol write SetProtocol;
property BasePath: string read fBasePath write SetBasePath;
property IdempotencyHeader: string read GetIdempotencyHeader write SetIdempotencyHeader;
property AuthChallenge: string read GetAuthChallenge write SetAuthChallenge;
property OnAuthenticate: tRestAuthenticateEvent read fOnAuthenticate write fOnAuthenticate;
property OnRequestError: tRestErrorEvent read fOnRequestError write fOnRequestError;
inherits from TsmCustomRESTServer
(no public members)
The event way to answer a route - AddRouteEvent. A DISTINCT name rather than an AddRoute overload, deliberately: Delphi cannot resolve a bare method reference across an interface-vs-method-pointer overload (E2250).
THE AUTH SEAM. Fired for every matched route that is not AuthExempt, BEFORE the body is parsed - an unauthenticated peer's body is never worth parsing. aAllow ARRIVES False: the moment a policy is wired in, the default is deny, and the handler's job is to prove the request in. Set aPrincipal to whatever names the caller - it travels to the handler as aRequest.Principal. Everything needed for the decision is on aRequest: the authorization header via Http.Headers, the mTLS peer via Http.Connection (authorize on PeerIssuer + PeerSerialNumber, as that interface documents - not on a DN string). A refusal answers 401 with a lowercase www-authenticate carrying AuthChallenge.
A handler raised. aError is the real exception - log it here; the wire got a 500 problem with a generic detail and nothing else. Runs on the I/O thread that ran the handler, so treat it as logging, not as work.
What the store found for a key when a request began.