0

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?

Sam
  • 7,252
  • 16
  • 46
  • 65
Daniel Marschall
  • 3,739
  • 2
  • 28
  • 67

2 Answers2

1

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')
Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
  • Thanks for your help! Your answer was a bit confusing because I have no access to the PECB through TWebRequest. But simply Request.GetFieldByName('HTTP_ACCEPT_LANGUAGE') did work on Apache. However, it did not work for the standalone EXE because only Request.GetFieldByName('Accept-Language') works there. So I have written a small function which tries both (see my own answer below). Can you please tell me if this is a clean solution? Thanks. – Daniel Marschall Apr 18 '14 at 11:16
0

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');
Daniel Marschall
  • 3,739
  • 2
  • 28
  • 67