Sign in Join
Wiki copytemporary

CGI Web Programming: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: CGI Web Programming

OldNewLine
1+This article contains some information about basic CGI programs. Note that using one of the frameworks will probably be much more productive.
2+
3+## About CGI
4+
5+[**CGI** (Common Gateway Interface)](http://en.wikipedia.org/wiki/Common_Gateway_Interface) works simply as follows:
6+
7+We have two computers:
8+
9+- Server Computer - This computer will store the hosted files (web pages, images and CGIs) and serve them using a web server.
10+- Client Computer - Generally, this computer has a browser to request the files hosted in server computer.
11+
12+You can use CGI only if your hosting web server has CGI support. After hosting your files, CGI can now be used normally as: **http://www.yourserver.com\[.xx\]/yourlogin/cgi-bin/cginame.exe**
13+
14+**Notes:**
15+
16+- The CGI file doesn't need the **.exe** extension.
17+- The CGI should be (obviously) compiled for the platform of the computer server (Linux, Windows...)
18+
19+## About FreePascal CGI and how you can test your CGIs
20+
21+A Free Pascal CGI executable is a normal executable program, like any program on your computer. Unlike the CGIs in Perl or Python, Free Pascal CGI isn't interpreted. It's an independently executable program.
22+
23+To test a CGI, you must have a **HTTP Server** with support for CGI installed on your PC. A good, and recommended [HTTP Server](http://en.wikipedia.org/wiki/HTTP_Server) is [Apache](http://en.wikipedia.org/wiki/Apache_HTTP_Server), available both for Windows and Linux.
24+
25+## A very basic example
26+
27+Here is an example of a minimal CGI program to demonstrate...
28+
29+1. Setting a cookie
30+2. Outputting the content-type (ie make it put out legal text for HTTP)
31+3. Reading Cookies
32+4. Reading form data via GET
33+5. Reading form data via POST
34+
35+``` pascal
36+program mini;
37+
38+uses dos;
39+
40+var
41+ a:string;
42+ c:char;
43+begin
44+ // set a cookie (must come before content-type line below)
45+ // don't forget to change the expires date
46+ writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005 18:37:00 GMT');
47+
48+ // output legal http page
49+ writeln('Content-Type:text/html',#10#13);
50+
51+ // demonstrate get cookies
52+ a:= GetEnv('HTTP_COOKIE');
53+ writeln('cookies:',a);
54+
55+ // demonstrate GET result
56+ a:='';
57+ a:= GetEnv('QUERY_STRING');
58+ writeln('GET: ',a);
59+
60+ // demonstrate POST result
61+ a:='';
62+ while not eof(input) do
63+ begin
64+ read(c);
65+ a:= a+c;
66+ end;
67+ writeln('POST: ',a);
68+end.
69+```
70+
71+## A simple hitcounter
72+
73+The application **hitcounter** presents a simple hitcounter for your website. It is possible to enter blocklists in a configfile so robots and perhaps your own computer are excluded from the statistics. There are still a lot of improvements possible ofcourse like setting a cookie so you get only one hit per session, but it gives a good introduction into CGI programming. If you use the code and modify it, please send me a patch so I can update it.
74+
75+You can find the code here: [1](http://sourceforge.net/p/modelbuilder/code/HEAD/tree/html/cgi/)
76+
77+## Debugging CGI
78+
79+Because a CGI application does not write to screen, and is started by another process (the web server), debugging is more difficult than desktop applications.
80+
81+One solution is to let the CGI application start a debugger: When used in a program this unit will as part of the initialization launch a new process which opens a terminal (windows:cmd, linux:xterm) and starts the gdb debugger attaching to the calling proces.
82+
83+A short "sleep" makes sure gdb breaks the program while still initializing.
84+
85+It has been tested on
86+
87+- Windows XP32 with apache 2.2 running as an application or as a service (does not work anymore is higher versions of Windows, see <http://forum.lazarus.freepascal.org/index.php/topic,21931.msg128849.html#msg128849>)
88+- on ubuntu 10.04 x64 with Apache 2.2
89+- on Debian unstable x64 with Apache 2 (Feb 2013)
90+
91+Further instructions are in the unit.
92+
93+``` pascal
94+unit SelfDebug;
95+
96+{
97+Instructions:
98+-Include unit anywhere in the program.
99+-Change DEBUGGER constant to match your debugger (gdb, ddd,gdbtui...)
100+-For Windows, if the program to debug is running as a service (e.g. CGI application
101+ from Apache running as a service), make sure the service is configured with
102+ "Interact with desktop" checked. Failing to do so, the debugger will be started
103+ without a console, break the program and hang waiting for input. Killing the debugger
104+ is your only option.
105+-For Linux, if the program to debug is running as a different user (e.g. CGI application),
106+ run "xhost +" in a terminal to enable all users to connect to xserver.
107+ If needed, change DISPLAY to match your DISPLAY environment variable
108+}
109+
110+{$mode objfpc}{$H+}
111+
112+interface
113+
114+uses
115+ Classes, SysUtils,Process;
116+
117+implementation
118+
119+const
120+ {$ifdef windows}
121+ DEBUGGER='C:\lazarus\mingw\bin\7.3\gdb.exe';
122+ {$endif}
123+ {$ifdef linux}
124+ DEBUGGER='gdb';
125+ DISPLAY=':0.0';
126+ {$endif}
127+ MSWAIT=2000;
128+
129+var
130+ AProcess: TProcess;
131+
132+initialization
133+AProcess := TProcess.Create(nil);
134+{$ifdef windows}
135+AProcess.CommandLine := format('cmd /C START "Debugging %s" /WAIT "%s" "%s" %d"',[paramstr(0),debugger,paramstr(0),GetProcessID]);
136+{$endif}
137+{$ifdef linux}
138+AProcess.CommandLine := format('xterm -display %s -T "Debugging %s" -e "%s" "%s" %d',[DISPLAY,paramstr(0),DEBUGGER,paramstr(0),GetProcessID]);
139+{$endif}
140+AProcess.Execute;
141+sleep(MSWAIT);
142+finalization
143+AProcess.Free;
144+end.
145+```
146+
147+Source: <http://www.hu.freepascal.org/lists/fpc-pascal/2011-October/030752.html>
148+
149+Other solutions are writing to a file or event log/syslog.
150+
151+## Existing CGI Frameworks
152+
153+There are existing CGI units and frameworks that make working with CGI trivial (and allow one to set cookies, sessions, retrieve POST and GET).
154+
155+- See [[Brook for Free Pascal]], the perfect Free Pascal framework for your web applications.
156+- See [FastPlaz](http://www.fastplaz.com/), Free Pascal framework for your web applications with MVC Style.
157+- See [[fcl-web]], an FPC and Lazarus (weblaz) framework
158+- See [Powtils](http://z505.com/powtils/idx.shtml)
159+- [ExtPascal](http://extpascal.googlecode.com) an Ext JS wrapper.
160+- See [Fano Framework](https://fanoframework.github.io/), Web application framework for modern Pascal programming language. It is written in Free Pascal.
161+
162+---
163+*From the Free Pascal wiki, page "CGI Web Programming", as it stood on 2020-06-26 after a change by Almuhandis, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history