Habari Web Components – web application framework for Delphi

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

Online Demo

A demo web application is located at www.habariwebcomponents.de, which contains several small demo pages, including a live (Ajax powered) request statistics demo here.

Development Process

The development process for Delphi web applications with this framework is:

  1. create a new handler for HTTP requests by subclassing TdjWebComponent and override event handlers (for example OnGet and OnPost)
  2. map the class to URL paths, for example ‘index.html’, ‘login.html’, ‘*.html’ or ‘/secure/*’
  3. 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.

Web Contexts

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

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.

Additional Features

Static content

Static resources can be served as well. CSS, JavaScript and image files can be placed in a subfolder with the same name as the context. These files will be used when a request was not handled by any web component in this context. See screenshot below – the style sheet (CSS) and JavaScript files are served from a local directory.

Demo Download

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.

Demo running on Ubuntu 12.04 (compiled with Free pascal)

HTML form example

Mobile app demo

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s