Un cgi no es mas que un programa que recibe los datos por la entrada estándar y tiene que escribir la respuesta por la salida estándar. Además el servidor puede pasar al cgi otras variables como Variables de Entorno al ejecutar el cgi.
Aunque delphi creo que cuenta con librerías especificas para hacer cgis, un cgi simple se puede hacer con unas pocas lineas de código.
Por ejemplo:
Código Delphi
[-]
program Prueba;
uses Windows, SysUtils;
procedure StdWrite(Str: String);
var
StdOutput: THandle;
begin
StdOutput:= GetStdHandle(STD_OUTPUT_HANDLE);
if (StdOutput <> INVALID_HANDLE_VALUE) and (StdOutput <> 0) then
FileWrite(StdOutput,PChar(Str)^,Length(Str));
end;
procedure StdWriteln(Str: String);
begin
StdWrite(Str + #13#10);
end;
begin
StdWriteln('Content-type: text/html');
StdWriteln(EmptyStr);
Writeln('< html >< h1 >Hola mundo< /h1 >< /html >'); end.
O en este otro ejemplo, que muestra todas las variables de entorno que el servidor le pasa al cgi.
Código Delphi
[-]
program Prueba2;
uses Windows, SysUtils;
procedure StdWrite(Str: String);
var
StdOutput: THandle;
begin
StdOutput:= GetStdHandle(STD_OUTPUT_HANDLE);
if (StdOutput <> INVALID_HANDLE_VALUE) and (StdOutput <> 0) then
FileWrite(StdOutput,PChar(Str)^,Length(Str));
end;
procedure StdWriteln(Str: String);
begin
StdWrite(Str + #13#10);
end;
function GetEnvironmentStr(Sep: String): String;
var
P: PChar;
Str: String;
begin
Result:= EmptyStr;
P:= GetEnvironmentStrings;
if P <> nil then
begin
Str:= String(P);
while Str <> EmptyStr do
begin
Result:= Result + Str + Sep;
inc(P,Length(Str)+1);
Str:= String(P);
end;
FreeEnvironmentStrings(P);
end;
end;
begin
StdWriteln('Content-type: text/html');
StdWriteln(EmptyStr);
Writeln(Format('< html >%s< /html >',[GetEnvironmentStr('< br >' + #13#10)])); end.
También preguntas como envía el navegador los datos al cgi, normalmente se usan formularios para eso. Hay dos métodos GET y POST, el primero manda los diferentes campos codificados en un mismo string. Para obtener ese texto puedes usar la variable de entorno QUERY_STRING, haciendo algo como esto:
Código Delphi
[-]
function GetEnvVar(Nombre: string): string;
var
Str: PChar;
Len: Integer;
begin
Len:= GetEnvironmentVariable(PChar(Nombre),nil,0);
if Len > 0 then
begin
GetMem(Str,Len+1);
try
GetEnvironmentVariable(PChar(Nombre),Str,Len);
Result:= String(Str);
finally
FreeMem(Str);
end;
end else Result:= EmptyStr;
end;
Writeln(Format('< html >%s< /html >',[GetEnvVar('QUERY_STRING')]));
El método POST, sin embargo puede mandar cualquier tipo de información, texto, imágenes, archivos, etc. y el cgi lo recibe a través de su entrada estándar, pero eso ya es otra historia
, para empezar creo que esto es suficiente.