-


Input Queries - 2


unit DrBobCGI;

{$I-}

interface

var

ContentLength: Integer = 0;

 

function Value(const Field: ShortString): ShortString;

{ use this function to get the CGI inputquery values }

 

implementation

uses

SysUtils, Windows;

 

var

Data: String = '';

 

function Value(const Field: ShortString): ShortString;

var

i: Integer;

begin

Result := '';

i := Pos(Field+'=',Data);

if i > 0 then

begin

Inc(i,Length(Field)+1);

while Data[i] <> '&' do

begin

Result := Result + Data[i];

Inc(i)

end

end

end {Value};

 

var

P: PChar;

i: Integer;

Str: ShortString;

 

type

TRequestMethod = (Unknown,Get,Post);

var

RequestMethod: TRequestMethod = Unknown;

 

initialization

P := GetEnvironmentStrings;

while P^ <> #0 do

begin

Str := StrPas(P);

if Pos('REQUEST_METHOD=',Str) > 0 then

begin

Delete(Str,1,Pos('=',Str));

if Str = 'POST' then RequestMethod := Post

else

if Str = 'GET' then RequestMethod := Get

end;

if Pos('CONTENT_LENGTH=',Str) = 1 then

begin

Delete(Str,1,Pos('=',Str));

ContentLength := StrToInt(Str)

end;

if Pos('QUERY_STRING=',Str) > 0 then

begin

Delete(Str,1,Pos('=',Str));

SetLength(Data,Length(Str)+1);

Data := Str

end;

Inc(P, StrLen(P)+1)

end;

if RequestMethod = Post then

begin

SetLength(Data,ContentLength+1);

for i:=1 to ContentLength do read(Data[i]);

Data[ContentLength+1] := '&';

{ if IOResult <> 0 then { skip }

end;

i := 0;

while i < Length(Data) do

begin

Inc(i);

if Data[i] = '+' then Data[i] := ' ';

if (Data[i] = '%') then { special code }

begin

Str := '$00';

Str[2] := Data[i+1];

Str[3] := Data[i+2];