Habari Web Components is a web application framework for Delphi 2009 and Free Pascal 2.6.0 or newer. It allows to compose web applications with these building blocks:
- a Web Component base class which provides HTTP method handlers (OnGet, OnPost, OnPut, OnDelete etc.)
- a HTTP server run time environment, based on Internet Direct (Indy) 10.6
The development process for Delphi web applications with this framework is:
- create a new handler for HTTP requests by subclassing TdjWebComponent and override event handlers (for example OnGet and OnPost)
- map the class to URL paths, for example ‘index.html’, ‘login.html’, ‘*.html’ or ‘/secure/*’
- register the class with an application context, for example ‘app’, ‘web’, or ‘domain1′
A handler will be executed when a HTTP clients hits a mapped resource URL with the matching HTTP method – for example a GET request on http://127.0.0.1/app/login.html.
The framework supports multiple application contexts:
- a context is the top-level directory of the web site (context myapp will appear as 127.0.0.1/myapp/index.html)
- context can hold any number of web components or handlers
- a dedicated webapps subfolder per context serves static files (when needed)
- context may be associated with HTTP ports, to run the main application on port 80 at http://myserver/myapp and its administration interface at http://myserver:8088/admin
Web Component implementation – Hello World example
This example responds to GET requests with a ‘Hello world’ response body:
type THelloPage = class(TdjWebComponent) public procedure OnGet(Request: TIdHTTPRequestInfo; Response: TIdHTTPResponseInfo); override; end; procedure THelloPage.OnGet; begin Response.ContentText := '<html>Hello world!</html>'; end;
Available event handler methods:
|OnPost||This method will answer to a HTTP POST request|
|OnGet||This method will answer to a HTTP GET request|
|OnPut||This method will answer to a HTTP PUT request|
|OnDelete||This method will answer to a HTTP DELETE request|
|OnHead||This method will answer to a HTTP HEAD request|
|OnOptions||This method will answer to a HTTP OPTIONS request|
|OnTrace||This method will answer to a HTTP TRACE request|
Context Creation and Component Deployment
Supported Mapping SYNTAX
Supported mapping styles in order of priority are
- absolute paths, for example ‘/mypage.html’
- prefix mappings, for example ‘/myfolder/subfolder/*’
- suffix mappings, for example ‘*.html’ or ‘*.page’
If two web components declare overlapping mappings, they will be processed in the order of their priority. Example
- WebCompontentA maps to ‘*.html’
- WebCompontentB maps to ‘/myfolder/*’
In this case, the resource /myfolder/index.html will be handled by WebComponentB because a prefix mapping (/myfolder/*) has a higher priority than an extension mapping.
Multiple mappings per component are supported. Example:
- WebCompontentA maps to ‘*.html’, ‘*.doc’ and ‘*.pdf’
- WebCompontentB maps to ‘/secure/*’ and ‘/protected/*’
With this mapping, resource /context/secure/example.pdf will be handled by WebComponentB, and resource /context/example.pdf will be handled by WebCompontentA.
A demo download is available, the archive contains a Delphi console app which launches a stand-alone server and the default browser to display the web application main page. From there you can navigate between demo pages and view the Delphi source code for the running Web Components in your browser.
The server app also logs its messages to the console window. The Log4D configuration file is located in config/log4d.properties.