Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-05

CGI application

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vannus

This may not be the right place to ask, but I'm really stumped and would much prefer FPC over PHP.

Can someone point me to how I can make a CGI app that outputs the fields in the query?

ie.
my.cgi?q=hello

and my.cgi then outputs whatever q is?

I'm using Lazarus but the deprecated CGI doesn't have any obvious way of getting the URL So I tried weblaz but got nowhere with it - TCustomCGIApplication's AResponse.Fields don't get filled with the query and AResponse.URL doesn't contain the URL for me to parse myself :(

Thanks.

Michael Van Canneyt
to Vannus
Vannus wrote 16 lines
This may not be the right place to ask, but I'm really stumped and would much prefer FPC over PHP. Can someone point me to how I can make a CGI app that outputs the fields in the query? ie. my.cgi?q=hello and my.cgi then outputs whatever q is? I'm using Lazarus but the deprecated CGI doesn't have any obvious way of getting the URL So I tried weblaz but got nowhere with it - TCustomCGIApplication's AResponse.Fields don't get filled with the query and AResponse.URL doesn't contain the URL for me to parse myself :(

You need to look at "request", not at "response".

Michael.

Graeme Geldenhuys
to Vannus: This may not be the right place to ask, but I'm really stumped and would much prefer FPC…

Vannus het geskryf:

Can someone point me to how I can make a CGI app that outputs the fields in the query?

Think of CGI apps as simple Console applications (that's pretty much what they are). Everything after the ? is simply passed in as a parameter to your program. In it's simplest form you can write results back to the user using writeln() statements.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/
Vannus
to Graeme Geldenhuys

eh - im pretty sure im using ARequest and not AResponse (im on differnt pc just now, can't check) - but i'll definetly double check when i get back!

How do i get command line vars? i tried GetENV() but it didn't understand GetEnv() (which unit is it in?)

Cheers.

Graeme Geldenhuys wrote 22 lines
Vannus het geskryf: > Can someone point me to how I can make a CGI app that outputs the fields in > the query? Think of CGI apps as simple Console applications (that's pretty much what they are). Everything after the ? is simply passed in as a parameter to your program. In it's simplest form you can write results back to the user using writeln() statements. Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Graeme Geldenhuys
to Vannus

Vannus het geskryf:

How do i get command line vars? i tried GetENV() but it didn't understand GetEnv() (which unit is it in?)

ParamCount() and ParamStr() is part of the System unit, so is always available.

GetEnvironmentVariable() is in the SysUtils unit.

Note that CGI apps use a lot of environment variables as well, so it might be beneficial if you loop through all of them and print them out so you can see what is available to you. I'm pretty sure Apache also comes with a sample CGI app which does this. Values stored are things like document root directory, server name, cgi application name etc..

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/
Carsten Bager
to Vannus: eh - im pretty sure im using ARequest and not AResponse (im on differnt pc just now,…
How do i get command line vars? i tried GetENV() but it didn't understand GetEnv() (which unit is it in?)

Here is a small cgi program. Comments are in Danish but I hope you can get the meaning. Carsten

{Program version 1.00}
program listdir2html;
{LONGSTRINGS OFF}

{$INFO +---------------------------------+}
{$INFO | Statistik projekt til Oegendahl |}
{$INFO +---------------------------------+}

uses
dos,
classes,
uInit,
sysutils, {System}
uSpStatus,

  testbruger;
var
  QUERY_STRING:shortstring;
Procedure PrintHeader(title:shortstring);
Begin
  Writeln('HTTP/1.0 200 OK');
  Writeln('Server: Netscape-Communications/3.0');
  Writeln('Content-type: text/html');
  Writeln;
  Writeln;
  Writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
  Writeln('<html>');
  Writeln('  <head>');
  Writeln('    <meta content="text/html;charset=windows-1252" http-equiv="Content-Type">');
  Writeln('    <title>',title,'</title>');
  Writeln('  </head>');
  Writeln('  <body>');
  Writeln('    <table style="background-color: rgb(153, 153, 153); text-align: center; width:
100%;" border="0" cellpadding="2" cellspacing="2">');
  Writeln('      <tbody>');
  Writeln('        <tr>');
  Writeln('          <td style="font-size: 30px; font-weight: bold;">');
  Writeln('            ',cFaellesOverskrift);
  Writeln('          </td>');
  Writeln('        </tr>');
  Writeln('      </tbody>');
  Writeln('    </table>');
  Writeln('    <h2>');
  Writeln('      ',title);
  Writeln('    </h2>');
End;
Procedure PrintFooter;
Begin
  Writeln('    </p>');
  Writeln('  </body>');
  Writeln('</html>');
End;
Procedure FindOgListFiler(title,PartFileName:shortstring);
var
  Info:TSearchRec;
  flist:tstringlist;
  p:integer;
  date,s:shortstring;
Begin
  flist:=tstringlist.create;
  If FindFirst (PartFileName,faAnyFile,Info)=0 then       {Find f°rste fil}
    begin
      Repeat
        flist.add(Info.Name);                   {Find de f°lgende filer}
      Until FindNext(info)0;
    end;
    flist.sort;                                 {Soter filer}
    p:=flist.count;
    while p0 do                               {Print fil navn til HTML side}
    begin
      dec(p);
      s:=flist.Strings[p];
      date:=copy(s,succ(length(QUERY_STRING)),12);
      date:=title+
            copy(date,1,4)+
            ' d. '+copy(date,7,2)+
            '/'+copy(date,5,2)+
            ' Kl. '+copy(date,9,2)+
            ':'+copy(date,11,2);
     // WriteLn('<a href="../log/',s,'">',date,'</a>');
      WriteLn('<INPUT TYPE="BUTTON" VALUE="',date,'"
ONCLICK="window.location.href=''http:','../log/',s,'''"><br>');
      flist.delete(pred(flist.count));
    end;
  flist.free;
End;
begin
  if not uSpStatus.InitShaerdMemory(InitShaerdMemoryStr,false) then
  begin
    PrintHeader('Beas login');
    writeln ('Kan ikke initialisere shared memory<br>');
    PrintFooter;
    halt;
  end;
  if not ErBrugerOk then
    halt(1);
  QUERY_STRING:=getenv('QUERY_STRING');
  if debug.Listdir2html then
  begin
    writeln(stderr);
    writeln(stderr,'QUERY_STRING= ',QUERY_STRING);
  end;
  chdir('..');
  if pos('Statistik',QUERY_STRING)0 then
  begin
    PrintHeader('Historiske statistik data fra blandeanlµg');
    FindOgListFiler('Statistik fil fra ','./log/statistik*.html');
    PrintFooter;
  end;
  if pos('Blandelog',QUERY_STRING)0 then
  begin
    PrintHeader('Historiske  log data fra blandeanlµg');
    FindOgListFiler('Log fil fra ','./log/blandelog*.html');
    PrintFooter;
  end;
  if pos('Program',QUERY_STRING)0 then
  begin
    PrintHeader('Historiske  program data fra blandeanlµg');
    FindOgListFiler('Program fil fra ','./log/program*.html');
    PrintFooter;
  end;
  if pos('AlarmLog',QUERY_STRING)0 then
  begin
    PrintHeader('Historiske  alarm log data fra blandeanlµg');
    FindOgListFiler('Alarm log fil fra ','./log/alarmlog*.html');
    PrintFooter;
  end;
end.
Vannus
to Carsten Bager

Thats me up & running now - thanks for your help guys, I'll certainly poke around those CGI vars.

Carsten Bager wrote 156 lines
> How do i get command line vars? i tried GetENV() but it didn't understand GetEnv() (which unit is > it in?) Here is a small cgi program. Comments are in Danish but I hope you can get the meaning. Carsten {Program version 1.00} program listdir2html; {LONGSTRINGS OFF} {$INFO +---------------------------------+} {$INFO | Statistik projekt til Oegendahl |} {$INFO +---------------------------------+} uses dos, classes, uInit, sysutils, {System} uSpStatus, testbruger; var QUERY_STRING:shortstring; Procedure PrintHeader(title:shortstring); Begin Writeln('HTTP/1.0 200 OK'); Writeln('Server: Netscape-Communications/3.0'); Writeln('Content-type: text/html'); Writeln; Writeln; Writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'); Writeln('<html>'); Writeln(' <head>'); Writeln(' <meta content="text/html;charset=windows-1252" http-equiv="Content-Type">'); Writeln(' <title>',title,'</title>'); Writeln(' </head>'); Writeln(' <body>'); Writeln(' <table style="background-color: rgb(153, 153, 153); text-align: center; width: 100%;" border="0" cellpadding="2" cellspacing="2">'); Writeln(' <tbody>'); Writeln(' <tr>'); Writeln(' <td style="font-size: 30px; font-weight: bold;">'); Writeln(' ',cFaellesOverskrift); Writeln(' </td>'); Writeln(' </tr>'); Writeln(' </tbody>'); Writeln(' </table>'); Writeln(' <h2>'); Writeln(' ',title); Writeln(' </h2>'); End; Procedure PrintFooter; Begin Writeln(' </p>'); Writeln(' </body>'); Writeln('</html>'); End; Procedure FindOgListFiler(title,PartFileName:shortstring); var Info:TSearchRec; flist:tstringlist; p:integer; date,s:shortstring; Begin flist:=tstringlist.create; If FindFirst (PartFileName,faAnyFile,Info)=0 then {Find f°rste fil} begin Repeat flist.add(Info.Name); {Find de f°lgende filer} Until FindNext(info)0; end; flist.sort; {Soter filer} p:=flist.count; while p0 do {Print fil navn til HTML side} begin dec(p); s:=flist.Strings[p]; date:=copy(s,succ(length(QUERY_STRING)),12); date:=title+ copy(date,1,4)+ ' d. '+copy(date,7,2)+ '/'+copy(date,5,2)+ ' Kl. '+copy(date,9,2)+ ':'+copy(date,11,2); // WriteLn('<a href="../log/',s,'">',date,'</a>'); WriteLn('<INPUT TYPE="BUTTON" VALUE="',date,'" ONCLICK="window.location.href=''http:','../log/',s,'''"><br>'); flist.delete(pred(flist.count)); end; flist.free; End; begin if not uSpStatus.InitShaerdMemory(InitShaerdMemoryStr,false) then begin PrintHeader('Beas login'); writeln ('Kan ikke initialisere shared memory<br>'); PrintFooter; halt; end; if not ErBrugerOk then halt(1); QUERY_STRING:=getenv('QUERY_STRING'); if debug.Listdir2html then begin writeln(stderr); writeln(stderr,'QUERY_STRING= ',QUERY_STRING); end; chdir('..'); if pos('Statistik',QUERY_STRING)0 then begin PrintHeader('Historiske statistik data fra blandeanlµg'); FindOgListFiler('Statistik fil fra ','./log/statistik*.html'); PrintFooter; end; if pos('Blandelog',QUERY_STRING)0 then begin PrintHeader('Historiske log data fra blandeanlµg'); FindOgListFiler('Log fil fra ','./log/blandelog*.html'); PrintFooter; end; if pos('Program',QUERY_STRING)0 then begin PrintHeader('Historiske program data fra blandeanlµg'); FindOgListFiler('Program fil fra ','./log/program*.html'); PrintFooter; end; if pos('AlarmLog',QUERY_STRING)0 then begin PrintHeader('Historiske alarm log data fra blandeanlµg'); FindOgListFiler('Alarm log fil fra ','./log/alarmlog*.html'); PrintFooter; end; end. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Felipe Monteiro de Carvalho
to Jorge Aldo G. de F. Junior

Using powtils you can write something like the example bellow, which I consider cleaner and easier to maintain:

From: http://code.google.com/p/powtils/source/browse/dev#dev/examples/raw-template

program project1; {$IFDEF FPC}{$mode objfpc}{$H+}{$ENDIF} {$APPTYPE CONSOLE}
uses
  pwinit, pwmain;
var
  MyMacroVar: string;
begin
  MyMacroVar:= '<b>really, really</b> splendid and <i>nice</i> ';
  SetVar('MacroVar', MyMacroVar);
  { TemplateRaw is less secure than TemplateOut and allows special characters }
  TemplateRaw('template.htm');
end.

=========

<html>
<head>
</head>
<body>
Hello, isn't it $MacroVar today?
</body>
</html>

Felipe Monteiro de Carvalho