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

感情迁移 提交于 2019-12-13 17:13:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/38901343/add-a-custom-header-on-a-tisapirequest-delphi-10-1-datasnap-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!