CGI application

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

CGI application

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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

Michael Van Canneyt


On Wed, 19 May 2010, Vannus wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

Graeme Geldenhuys-2
In reply to this post by Vannus
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

Vannus
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.

On 19 May 2010 13:55, Graeme Geldenhuys <[hidden email]> wrote:
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

Graeme Geldenhuys-2
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/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

Carsten Bager
In reply to this post by Vannus
> 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 p<>0 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

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

On 19 May 2010 14:19, Carsten Bager <[hidden email]> wrote:
> 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 p<>0 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

Jorge Aldo G. de F. Junior
Try using powtils :

http://z505.com/powtils/idx.shtml
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: CGI application

Felipe Monteiro de Carvalho
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal