TsgcWebSocketServer_HTTPAPIイベント › OnHTTPRequest

OnHTTPRequest イベント

サーバーが HTTP リクエストを受信したときにアプリケーションがレスポンスを構築できるよう発生します。

構文

property OnHTTPRequest: TsgcWSHTTPAPIRequestEvent;
// TsgcWSHTTPAPIRequestEvent = procedure(aConnection: TsgcWSConnection_HTTPAPI; const aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse) of object

デフォルト値

解説

OnHTTPRequest は、HTTP.SYS がサーバーに配信するすべての HTTP リクエスト (GET、POST、PUT、DELETE、HEAD...) のメインエントリーポイントです。aRequestInfo はデコードされたリクエスト (Document、Method、Headers、ContentType、Content、QueryParams、Cookies、ContentLength、AuthExists/AuthUsername/AuthPassword、Stream) を公開し、aResponseInfo はアプリケーションが返したいレスポンスを収集します: ResponseNo、ContentText (またはバイナリ/ファイルレスポンス用の ContentStream / FileName)、ContentType、CustomHeaders、Date/Expires/LastModified、CacheControl を必要に応じて設定してください。aResponseInfo を変更しない場合はデフォルトの HTTP 200 が返され、ResponseNo に 404/500/... を設定するとエラー状態を示すことができます。このイベントは WebSocket アップグレードリクエストには発生しません。それらは OnConnect / OnHandshake を通じてルーティングされます。

使用例


procedure OnHTTPRequest(aConnection: TsgcWSConnection_HTTPAPI;
  const aRequestInfo: THttpServerRequest;
  var aResponseInfo: THttpServerResponse);
begin
  if aRequestInfo.Method = 'GET' then
  begin
    if aRequestInfo.Document = '/test.html' then
    begin
      aResponseInfo.ResponseNo := 200;
      aResponseInfo.ContentText := 'OK';
      aResponseInfo.ContentType := 'text/html; charset=UTF-8';
    end
    else
      aResponseInfo.ResponseNo := 404;
  end
  else
    aResponseInfo.ResponseNo := 500;
end;

イベントに戻る