在TISAPIRequest(Delphi 10.1 Datasnap Server)上添加自定义标头 [英] Add a Custom Header on a TISAPIRequest (Delphi 10.1 Datasnap Server)

查看:68
本文介绍了在TISAPIRequest(Delphi 10.1 Datasnap Server)上添加自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道如何在TISAPIRequest上手动添加自定义标头吗?

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

此类(或最通用的TWebRequest)没有公开RawHeaders属性,以允许在需要时添加新的自定义Header.

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

PS:对于WebRequest是TIdHTTPAppRequest(Datasnap独立服务器)时,我有一个肮脏的解决方案,因为这样我就可以创建一个Helper类来访问其私有的FRequestInfo属性,并从那里获得对RawHeaders的访问权限.可以用来添加新的标题.但是我只使用独立服务器进行开发和测试,生产环境必须在IIS服务器上运行.

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;

当WebRequest是TISAPIRequest而不是THTTPAppRequest?时,可以编写类似的代码来获得相同的结果(向WebRequest添加自定义标头).

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 ?.

谢谢.

推荐答案

TISAPIRequest 有一个公开的 ECB 属性,该属性返回指向ISAPI EXTENSION_CONTROL_BLOCK 结构,表示基础请求数据.但是, ECB 不允许您以任何方式更改请求标头,而只能从其中读取值.不过,您可以通过 ECB 设置自定义响应标题.

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.

我能找到的在ISAPI中添加/修改自定义请求标头值的唯一方法是编写 ISAPI筛选器DLL ,不在 TISAPIRequest 处理范围之内.在DLL的导出的 HttpFilterProc() 函数内部, SF_NOTIFY_PREPROC_HEADERS 通知提供了 HTTP_FILTER_PREPROC_HEADERS 结构,其中包含指向 AddHeader() SetHeader() 用于添加/修改请求标头值的函数.

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.

这篇关于在TISAPIRequest(Delphi 10.1 Datasnap Server)上添加自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆