Marc Guillot
Marc Guillot

Reputation: 6455

Add a Custom Header on a TISAPIRequest (Delphi 10.1 Datasnap Server)

Do you know how to manually add a custom header on a TISAPIRequest ?.

This class (or the most generic TWebRequest) doesn't expose a RawHeaders property to allow adding new customized Headers when needed.

PS: I have a dirty solution for when my WebRequest is a TIdHTTPAppRequest (Datasnap standalone server), because then I can create a Helper Class to access to its private FRequestInfo property, and from there gain access to a RawHeaders, which I can use to add a new header. But I only use standalone servers for development and testing, the production environment must run on IIS servers.

TIdHTTPAppRequestHelper = class helper for TIdHTTPAppRequest
  public
    function GetRequestInfo: TIdEntityHeaderInfo;
  end;

implementation

function TIdHTTPAppRequestHelper.GetRequestInfo: TIdEntityHeaderInfo;
begin
  Result := FRequestInfo;
end;

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var Token: string;
begin
  Response.SetCustomHeader('Access-Control-Allow-Origin','*');

  Token := Request.Query;

  if Copy(Token, 1, 10) = 'dssession=' then begin
    if Request is TIdHTTPAppRequest then begin
      TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.AddValue('Pragma', Token);
    end;
  end;

  if FServerFunctionInvokerAction <> nil then
    FServerFunctionInvokerAction.Enabled := AllowServerFunctionInvoker;
end;

It is possible to write a similar code to get the same result (to add a custom header to your WebRequest) when the WebRequest is a TISAPIRequest instead of THTTPAppRequest ?.

Thank you.

Upvotes: 1

Views: 1283

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595295

TISAPIRequest has a public ECB property, which returns a pointer to the ISAPI EXTENSION_CONTROL_BLOCK structure that represents the underlying request data. However, the ECB does not allow you to alter the request headers in any way, only read values from them. You can set custom response headers via the ECB, though.

The only way I can find to add/modify custom request header values in ISAPI is to write an ISAPI Filter DLL, which is outside the scope of TISAPIRequest handling. Inside the DLL's exported HttpFilterProc() function, the SF_NOTIFY_PREPROC_HEADERS notification provides an HTTP_FILTER_PREPROC_HEADERS structure that contains pointers to AddHeader() and SetHeader() functions for adding/modifying request header values.

Upvotes: 2

Related Questions