Wednesday, July 01, 2009

 

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.
Comments:
Thanks so much for the information!

Now I have a question: What effect does the keepalive variable in HTTPD2 have? I can find lots of google hits, but no clear explanation.
 
Did you read this?: http://en.wikipedia.org/wiki/HTTP_persistent_connection
 
Leonardo:
I have an Apache module that I made using your post. It was working fine until I compiled my module using delphi7 on windows7. The unique change is to compile on windows7 because I´ve compiled always with Delphi 7. Now I don´t have a clue of what is happening. Please, I appreciate your help.

Lester
 
@Dim:
Did you try compiling it with Delphi 2007 or newer?. Newer versions of Delphi have Windows Vista+ support, older version does not.
 
I fixed it!! there wasn´t necessary to compile using delphi 2007. I just redeclared one of the functions of one of my unit interfaces, which where returning a complex type (an object containing methods with null terminated String as params). I were making my Apache extension crash. Thanks anyway for your advise, you have a very useful blog.
 
Im looking for answer how can I setup my delphi written apache module (web broker applitation) for entire subdomain (virtual host) on my web server. When I'm trying to use < Location /> directive in httpd.conf or httpd-vhosts.conf it doesn't work. It seems, that Location can not be only '/', it must be non-empty path. I tryed also "SetHandler myapp-handler" i both config files without properly result. There is always displayed page for default acion, but never for other actions identified by pathinfo property of webaction.
 
hi, how do I deploy this .so on a linux apache server? I get a ELF header error when I try to start the server.

thanks!
 
You cannot deploy a .so created by Delphi, for this, please take a look here: http://wiki.lazarus.freepascal.org/FPC_and_Apache_Modules
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?