Apache 2.2.x modules with Delphi II

After my "Apache 2.2.x modules with Delphi" article, I received many requests for an example, and here it is.


Creating the directory structure


Create a directory where your Delphi project will reside, for example C:\myModule, then copy the files HTTPD2.pas, ApacheTwoApp.pas and ApacheTwoHTTP.pas from your Delphi/Source directory to the newly created directory.

Patching HTTPD2.pas

Open the file c:\myModule\HTTPD2.pas and replace/fix the lines as shown in my previous post.

The code

This is a very small HelloWorld WebBroker example, it is composed of only three files, mod_helloworld.dpr, main.pas and main.dfm

mod_helloworld.dpr


library mod_helloworld;

uses
HTTPD2 in 'HTTPD2.pas',
ApacheTwoApp,
WebBroker,
main in 'main.pas' {WebModule1: TWebModule};

{$E so}

{$R *.res}

exports
apache_module name 'helloworld_module';

begin
Application.Initialize;
Application.CreateForm(TWebModule1, WebModule1);
Application.Run;
end.


main.pas


unit main;

interface

uses
SysUtils, Classes, HTTPApp,
HTTPProd;

type
TWebModule1 = class(TWebModule)
procedure WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
public
{ Public declarations }
end;

var
WebModule1: TWebModule1;

implementation

{$R *.dfm}

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.Content := 'Hello from Apache Module!';
end;

end.


main.dfm


object WebModule1: TWebModule1
OldCreateOrder = False
Actions = < default =" True" name =" 'WebActionItem1'" pathinfo =" '/test'" onaction =" WebModule1WebActionItem1Action">
Left = 679
Top = 385
Height = 150
Width = 215
end


Compilling the module

Save the three files in c:\myModule and open the .dpr with Delphi, then compile to your Apache2 modules directory, usually in C:\Program files\Apache Software Foundation\Apache2.2\modules.

Configuring Apache2

If the Apache 2.2 service it's running, please stop it, then open your C:\Program files\Apache Software Foundation\Apache2.2\conf\httpd.conf file and look for the "LoadModule" entries, add this after the last entry:


LoadModule helloworld_module modules/mod_helloworld.so


And below this line add this:


<Location /test>
SetHandler mod_helloworld-handler
</Location>


Testing the module

Restart your Apache 2.2 service and open a web browser, then type http://localhost:8080/test, it should show a page with the text "Hello from Apache Module!".

That's it.