Difference between revisions of "CGI Web Programming/fr"

From Lazarus wiki
Jump to navigationJump to search
(Reprise de →‎Debugging CGI)
Line 80: Line 80:
  
 
==Débogage de CGI==
 
==Débogage de CGI==
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.
 
  
One solution is to let the CGI application start a debugger:
+
Parce qu'une application CGI n'écrit pas sur l'écran, et est démarré depuis un autre processus (le serveru web), le débogage est plus difficile que pour une application de bureau.
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.  
 
  
A short "sleep" makes sure gdb breaks the program while still initializing.  
+
Une solution consiste à laisser l'application CGI démarrer un débogueur:
 +
Quand elle est utilisée par un programme, cette unité, comme partie de l'initialisation, lancera un nouveau processus qui ouvre un terminal (Windows:cmd, Linux:xterm...) et démarre le débogueur gdb en l'attachant au processus appelant.
  
It has been tested on
+
Un court "sleep" garantit que gdb interrompt le programme pendant l'initialisation.
* 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)
 
* on ubuntu 10.04 x64 with Apache 2.2
 
* on Debian unstable x64 with Apache 2 (Feb 2013)
 
  
Further instructions are in the unit.
+
A été testé sur :
 +
* Windows XP32 avaec Apache 2.2 s'exécutant comme une application ou un service (ne fonctionne plus sur les versions plus récentes de Windows, voir [http://forum.lazarus.freepascal.org/index.php/topic,21931.msg128849.html#msg128849].
 +
* Ubuntu 10.04 x64 avec Apache 2.2
 +
* Debian unstable x64 avec Apache 2 (Fév. 2013)
 +
 
 +
D'autres instructions sont dans l'unité:
  
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
Line 147: Line 148:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
Source: http://www.hu.freepascal.org/lists/fpc-pascal/2011-October/030752.html
+
[http://www.hu.freepascal.org/lists/fpc-pascal/2011-October/030752.html Source]
  
Other solutions are writing to a file or event log/syslog.
+
D'autres solutions sont l'écriture vers un fichier ou un événement log/syslog.
  
 
==Existing CGI Frameworks==
 
==Existing CGI Frameworks==

Revision as of 10:13, 30 April 2020

English (en) español (es) français (fr) Bahasa Indonesia (id) русский (ru) 中文(中国大陆)‎ (zh_CN)

Ceci est le début d'un tutorial relatif à la programmation cgi web. A remarquer que l'utilisation d'un cadre d'application (framework) sera sans doute plus productif. Tout le monde est invité à contribuer.

A propos de CGI

CGI (Common Gateway Interface) fonctionne simplement comme ceci :

Nous avons deux ordinateurs :

  • Ordinateur serveur - Cet ordinateur stockera les fichiers hébergés (pages web, images et CGI);
  • Ordinateur client - En général, cet ordinateur utilise un navigateur pour demander les fichiers hébergés sur l'ordinateur serveur.

Vous pouvez employer les CGI seulement si votre serveur d'hébergement supporte les CGI. Après avoir hébergé vos fichiers, le CGI est généralement utilisé ainsi : http://www.yourserver.com[.xx]/yourlogin/cgi-bin/cginame.exe

Notes:

  • Le fichier CGI n'a pas besoin de l'extension .exe.
  • Le CGI devra être (évidemment) compilé pour la plateforme de l'ordinateur serveur (Linux, Windows ...)

A propos des CGI FreePascal et comment tester vos CGI

Un CGI Free Pascal est un programme exécutable normal, comme tout programme sur votre ordinateur. Contrairement aux CGI en Perl, un CGI Free Pascal n'est pas interprété. C'est un programme exécutable indépendant.

Pour tester un CGI, vous devez avoir installé sur votre ordianteur un serveur HTTP avec support pour les CGI. Un bon, et recommandé, serveur HTTP est Apache, disponible pour Windows et Linux. Un autre serveur HTTP est Xitami qui supporte les CGI en perl et les 16bits (mais Xitami est vraiment archaïque).

Un exemple simple

Ceci est un exemple de programme CGI minimal de démonstration...

  1. Définir un cookie
  2. Préciser le type de contenu (c-à-d prévoir que le contenu devra être du texte pour HTTP)
  3. Lire les Cookies
  4. Lire les données du formulaire via GET
  5. Lire les données du formulaire via POST
program mini;

uses dos;

var
  a:string;
  c:char;
begin
  // set a cookie (must come before content-type line below)
  // don't forget to change the expires date
writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005  18:37:00 GMT');

  // output legal http page
  writeln('Content-Type:text/html',#10#13);

  // demonstrate get cookies
  a:= GetEnv('HTTP_COOKIE');
  writeln('cookies:',a);
  
  // demonstrate GET result
  a:='';
  a:= GetEnv('QUERY_STRING');
  writeln('GET: ',a);
	
  // demonstrate POST result 
  a:='';
  while not eof(input) do
  begin
     read(c);
     a:= a+c;
  end;	 
  writeln('POST: ',a);
end.

Un simple compteur de visites

L'application hitcounter présente un simple compteur de visite pour votre site web. Il est possible d'entrer des listes de blocages dans un fichier de configuration. Ainsi, les robots et votre propre ordinateur pourront être exclus des statistiques. Il y a encore beaucoup d'améliorations possibles bien sûr, comme un cookie afin que vous obteniez un seul hit par session, mais il donne une bonne introduction à la programmation CGI. Si vous utilisez le code et le modifiez, s'il vous plaît envoyez-moi un patch pour que je puisse le mettre à jour.

Vous pouvez trouver le code ici : [1]

Débogage de CGI

Parce qu'une application CGI n'écrit pas sur l'écran, et est démarré depuis un autre processus (le serveru web), le débogage est plus difficile que pour une application de bureau.

Une solution consiste à laisser l'application CGI démarrer un débogueur: Quand elle est utilisée par un programme, cette unité, comme partie de l'initialisation, lancera un nouveau processus qui ouvre un terminal (Windows:cmd, Linux:xterm...) et démarre le débogueur gdb en l'attachant au processus appelant.

Un court "sleep" garantit que gdb interrompt le programme pendant l'initialisation.

A été testé sur :

  • Windows XP32 avaec Apache 2.2 s'exécutant comme une application ou un service (ne fonctionne plus sur les versions plus récentes de Windows, voir [2].
  • Ubuntu 10.04 x64 avec Apache 2.2
  • Debian unstable x64 avec Apache 2 (Fév. 2013)

D'autres instructions sont dans l'unité:

unit SelfDebug;

{
Instructions:
-Include unit anywhere in the program.
-Change DEBUGGER constant to match your debugger (gdb, ddd,gdbtui...)
-For Windows, if the program to debug is running as a service (e.g. CGI application
 from Apache running as a service), make sure the service is configured with
 "Interact with desktop" checked. Failing to do so, the debugger will be started
 without a console, break the program and hang waiting for input. Killing the debugger
 is your only option.
-For Linux, if the program to debug is running as a different user (e.g. CGI application),
 run "xhost +" in a terminal to enable all users to connect to xserver.
 If needed, change DISPLAY to match your DISPLAY environment variable
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,Process;

implementation

const
  {$ifdef windows}
  DEBUGGER='C:\lazarus\mingw\bin\7.3\gdb.exe';
  {$endif}
  {$ifdef linux}
  DEBUGGER='gdb';
  DISPLAY=':0.0';
  {$endif}
  MSWAIT=2000;

var
  AProcess: TProcess;

initialization
AProcess := TProcess.Create(nil);
{$ifdef windows}
AProcess.CommandLine := format('cmd /C START "Debugging %s" /WAIT "%s" "%s" %d"',[paramstr(0),debugger,paramstr(0),GetProcessID]);
{$endif}
{$ifdef linux}
AProcess.CommandLine := format('xterm -display %s -T "Debugging %s" -e "%s" "%s" %d',[DISPLAY,paramstr(0),DEBUGGER,paramstr(0),GetProcessID]);
{$endif}
AProcess.Execute;
sleep(MSWAIT);
finalization
AProcess.Free;
end.

Source

D'autres solutions sont l'écriture vers un fichier ou un événement log/syslog.

Existing CGI Frameworks

There are existing CGI units and frameworks that make working with CGI trivial (and allow one to set cookies, sessions, retrieve POST and GET). See Powtils

There is also a CGIModule for Lazarus, and a few CGI and HTML units in the freepascal FCL.

Another CGI and FastCGI framework for FreePascal/Delphi is ExtPascal an Ext JS wrapper.