From Lazarus wiki
Revision as of 11:42, 14 July 2010 by Sekelsenmat (talk | contribs) (Using fpWeb together with Lazarus)

Template:Web and Networking Programming

Using fpWeb together with Lazarus

Installing the fpWeb Lazarus Package

The first step to do is installing the package which comes in the path lazarus/components/fpweb/weblaz.lpg

Creating a cgi application

After the weblaz package is installed, a very simple cgi web application which displays an html page can be created by going to the Lazarus menu "File->New...". From the list of possible applications select "CGI Application" as in the image bellow, which will create a main cgi project file and a fpweb web module.

To add code to show the page a request handler should be added, by double clicking the OnRequest property in the object inspector, as in the image bellow:

In the event handler one should write the html code which will be displayed by the browser. To avoid mixing Pascal and HTML, this page can be loaded from the same directory of the cgi executable by using AResponse.Contents.LoadFromFile(). The type of the response should be set in AResponse.ContentType and for html pages it will have the value 'text/html;charset=utf-8'. In the end Handled should be set to True to indicate that the request was successfully handled. After adding this code, the web module should look like this:

<delphi> unit mainpage;

{$mode objfpc}{$H+}



 Classes, SysUtils, FileUtil, HTTPDefs, websession, fpHTTP, fpWeb; 


 { TFPWebModule1 }
 TFPWebModule1 = class(TFPWebModule)
   procedure DataModuleRequest(Sender: TObject; ARequest: TRequest;
     AResponse: TResponse; var Handled: Boolean);
   { private declarations }
   { public declarations }


 FPWebModule1: TFPWebModule1; 


{$R *.lfm}

{ TFPWebModule1 }

procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;

 AResponse: TResponse; var Handled: Boolean);


 AResponse.ContentType := 'text/html;charset=utf-8';
 AResponse.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'mainpage.html');
 Handled := True;



 RegisterHTTPModule('TFPWebModule1', TFPWebModule1);

end. </delphi>

Deploying this very simple application

Apache, the most popular web server, can be downloaded here: [1]

The default installation of Apache will thread all files located in it's cgi-bin directory as CGI Programs, so the user won't be able to access plain html files placed there. This directory can be set in the file httpd.conf in the following section:

   # ScriptAlias: This controls which directories contain server scripts.
   # ScriptAliases are essentially the same as Aliases, except that
   # documents in the target directory are treated as applications and
   # run by the server when requested rather than as documents sent to the
   # client.  The same rules about trailing "/" apply to ScriptAlias
   # directives as to Alias.
   ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

If you place an executable called "mywebpage.cgi" in this directory, then the page can be accessed as http://localhost/cgi-bin/mywebpage.cgi or with the corresponding domain name if accessed remotely.

Reading GET fields


The cgiapp unit is deprecated, please use fpcgi as much as possible.

The fastcgi, custfcgi, and fpfcgi units are not supported on Darwin at this time, because it uses a different mechanism than the MSG_NOSIGNAL option for recv() to indicate that no SIGPIPE should be raised in case the connection breaks. See http://lists.apple.com/archives/macnetworkprog/2002/Dec/msg00091.html for how this should be fixed.