Home > Enterprise >  Read request headers in ISAPI DLL
Read request headers in ISAPI DLL

Time:09-17

I'm writing an ISAPI DLL, using Delphi 10.4.2 and IIS 10.

The configuration, the content, request-response, the debugging, all working fine.

But, I can't read the request 's custom Headers. The test request has come from Postman.

In TWebModule1.WebModule1DefaultHandlerAction, the request is inherited from Web.Win.IsapiHTTP.TISAPIRequest.

I'm using the Web.Win.IsapiHTTP.TISAPIRequest.GetFieldByName() method, as mentioned in the Embarcadero documentation.

I've added <add name="Access-Control-Allow-Origin" value="*" />  to the configuration file on the server side.

I feel that I'm missing something.

For example, this returns with empty content, but from the client-side I sent it, every GetFieldByName returns with an empty string.

TWebModule1.WebModule1DefaultHandlerAction..
begin
  Response.statuscode := 200;
  response.Content := Request.GetFieldByName('ic_Something');
  Handled := true;
 end;

CodePudding user response:

Per ISAPI Server Variables, you need to use either HEADER_<HeaderName> or HTTP_<HeaderName> when retrieving a custom header:

Variable Description
HEADER_<HeaderName>

IIS 5.1 and earlier: This server variable is not available.
The value stored in the header <HeaderName>. Any header other than those listed in this table must be preceded by "HEADER_" in order for the ServerVariables collection to retrieve its value. This is useful for retrieving custom headers.

Note: Unlike HTTP_<HeaderName>, all characters in HEADER_<HeaderName> are interpreted as-is. For example, if you specify HEADER_MY_HEADER, the server searches for a request header named MY_HEADER.
HTTP_<HeaderName> The value stored in the header <HeaderName>. Any header other than those listed in this table must be preceded by "HTTP_" in order for the ServerVariables collection to retrieve its value. This is useful for retrieving custom headers.

Note: The server interprets any underscore (_) characters in <HeaderName> as dashes in the actual header. For example, if you specify HTTP_MY_HEADER, the server searches for a request header named MY-HEADER.

For example:

TWebModule1.WebModule1DefaultHandlerAction..
begin
  Response.statuscode := 200; 
  Response.Content := Request.GetFieldByName('HEADER_ic_Something');
  Handled := true; 
end;

UPDATE: apparently GetFieldByName() already looks for HTTP_<HeaderName> for you. But per the above documentation, that will search for an HTTP header named ic-Something, whereas HEADER_ic_Something will search for ic_Something instead. So use whichever one is more appropriate for your needs.

CodePudding user response:

To read all the custom headers from the request in ISAPI you must specify ALL_RAW as the field name:

TWebModule1.WebModule1DefaultHandlerAction
var
  CustomHeaders: string;
begin
  CustomHeaders := Request.GetFieldByName('ALL_RAW');
end;
  • Related