Tema: Html,cgi
Ver Mensaje Individual
  #2  
Antiguo 28-01-2007
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
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
  // Escribimos las cabeceras
  StdWriteln('Content-type: text/html');
  // Linea en blanco para separar las cabeceras del contenido
  StdWriteln(EmptyStr);
  // Escribimos el contenido
  Writeln('< html >< h1 >Hola mundo< /h1 >< /html >'); // Quita los espacios en blanco
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
  // Escribimos las cabeceras
  StdWriteln('Content-type: text/html');
  // Linea en blanco para separar las cabeceras del contenido
  StdWriteln(EmptyStr);
  // Escribimos el contenido
  Writeln(Format('< html >%s< /html >',[GetEnvironmentStr('< br >' + #13#10)])); // Quita los espacios en blanco
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;

// Por ejemplo
Writeln(Format('< html >%s< /html >',[GetEnvVar('QUERY_STRING')]));  // Quita los espacios en blanco

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.
Responder Con Cita