help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



Notes

Simple example of a web program

In version 1.7.X a basic web program is as follows:
 program demo;  
 uses pwinit, pwmain;
 begin
   out('hello');
 end.

In version 1.6.0.2 a basic web program is as follows:

 program demo;  
 uses pwumain;
 begin
   webwrite('hello');
 end.

Notes

In delphi, the {$APPTYPE CONSOLE} directive must be used.

Most of the Powtils units are prefixed with 'pw' so that they do not conflict with other projects. Some general units or older may not be prefixed with 'pw'

The program can run on linux/bsd/windows servers. The machine you compile it on is the one it runs on - but we have tools such as FPW/FPWSERV and compile-studio so that you can remotely control your server and create programs right on it.

Low Level Details Encapsulated

The webwrite() or out() and similar calls automatically set your HTTP headers for you. If you were not using the powerful web utilities framework, a simple hello world web application would require that you set web headers before making calls to writeln. With the powerful web utilities you simply call webwrite and all is handled automatically.

Out, OutLn, or WebWrite, TemplateOut, and similar output calls are all easy to use without you having to worry about the low level details of HTTP/CGI. You can however take full control and override Powtils to be however powerful you want.. with such functions as SetHeader, and plugin units such as pwinit, pwsdssess, etc.

Library Option

In version 1.6.0.2 (and some other versions), you can also use the Powtils DLL (dynamic link library).. on unix or windows servers.

A simple demo using the DLL in freepascal is as follows:

 program demo;  
 uses
   dynpwu;  // pwu-1.6.0.0 DLL must be uploaded
 begin
   webwrite('demo test');
 end;

Other examples

There are a many examples in the 1.7.x, ,1.6.x, and 1.5.x packages. One shows how to use templates:
 
 program tpltest;  
 uses
   pwumain; // note: use "pwu" in version 1.5
 var
   mymacro: string;
 begin
   mymacro:= 'Red Bike';
   setwebvar('macrovar', mymacro);
   webtemplateout('mytemplate.tpl', true);
 end.
Create a file called MyTemplate.tpl and store it in the cgi program directory..

In the template file, place a line like this:

   He found a {$macrovar}. He decided to ride it.
The macro var will get replaced with 'Red Bike'.

How to get a variable from a URL

Pretend you have a URL such as:
 http://example.com/cgi-bin/prog.cgi?var1=test&var2=5
To access these variables in the url such as VAR1 and VAR2, simply call GetCgiVar('var1') and GetCgiVar('var2'). These functions return the value of the variables.

Note that there is built in security when using GetCgiVar, so if some hacker tries to inject unix commands or javascript...

 http://example.com/cgi-bin/prog.cgi?var1=./ls|./cat<script>java</script>
 
...then characters like pipes, less than, greater than, and dots will get trimmed right out.

If you want to filter those pipes and dots into HTML equivalents.. then you should bypass security, and use the FilterHTML function or use the GetCgiVar_SafeHtml function.

To bypass security completely without any automatic filtering use the GetCgiVar_S function and specify zero (0) security as a parameter.

The reason GetCgiVar_S function contains a underscore suffix naming scheme, is so that it jumps_OUT at you when you are looking at that code months later. Bypassing the security needs to be made clear in the program. The extra zero param makes it even more obvious that you are setting security to none.






lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505