Question

I wrote a web service using content-negotiation with ISAPI using Delphi XE4.

My code contains

ARequest.GetFieldByName('Accept-Language')

which outputs the correct value if I use a standalone server (Indy Bridge), but it is empty if I use an ISAPI DLL inside Apache.

Is there any way I can access this header field with ISAPI in Apache?

Was it helpful?

Solution

Since ISAPI is kind-of a successor to CGI, the 'default' HTTP headers get converted to CGI-style parameters, so you need to request HTTP_ACCEPT_LANGUAGE using the extension control block's GetServerVariable. Like so:

function GetVar(pecb: PEXTENSION_CONTROL_BLOCK; const key:AnsiString):AnsiString;
var
  l:cardinal;
begin
  l:=$10000;
  SetLength(Result,l);
  if not(pecb.GetServerVariable(pecb.ConnID,PAnsiChar(key),PAnsiChar(Result),l)) then
    if GetLastError=ERROR_INVALID_INDEX then l:=1 else RaiseLastOSError;
  SetLength(Result,l-1);
end; 

//
GetVar(ecb,'HTTP_ACCEPT_LANGUAGE')

OTHER TIPS

I used following function to make it work on Apache and the standalone EXE:

function GetHTTPHeader(ARequest: TWebRequest; AHeaderName: AnsiString): AnsiString;

  function ConvertToCGIStyle(AStr: AnsiString): AnsiString;
  var
    tmp: string;
  begin
    tmp := string(AStr); // "tmp" used to avoid Unicode warnings
    tmp := UpperCase(tmp);
    tmp := StringReplace(tmp, '-', '_', [rfReplaceAll]);
    tmp := 'HTTP_' + tmp;
    result := AnsiString(tmp);
  end;

begin
  // will work on Indy Standalone EXE
  result := ARequest.GetFieldByName(AHeaderName);

  if result = '' then
  begin
    // will work on Apache ISAPI DLL
    AHeaderName := ConvertToCGIStyle(AHeaderName);
    result := ARequest.GetFieldByName(AHeaderName);
  end;
end;

GetHTTPHeader(ARequest, 'Accept-Language');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top