Official website for Linux User & Developer
FOLLOW US ON:
Oct
22

Develop Apache HTTP Server Modules

by Kunal Deo

Apache HTTP Server is one of the most iconic open source projects in human history. It’s also the world’s most used and respected web server. In this tutorial you’ll learn how to add your own features to it…

Now we will develop a Hello World module. For the Hello World module, we are developing a module called mod_lud.c which will log messages to @edit logs/errorlog on each HTTP request. This module will also support the configuration directive to customise the log message.
Every Apache module has to conform to the following data structure:

## Module Data Structure ##
module AP_MODULE_DECLARE_DATA some_module = {
 STANDARD20_MODULE_STUFF,
 dir_cfg,        /* per-directory config struct (create) */
 dir_merge,        /* per-directory config struct (merge) */
 server_cfg,        /* per-host config struct (create) */
 server_merge,        /* per-host config struct (merge) */
 commands,        /* configuration directives */
 hooks            /* hook registration with the core */
};

There are 14 slots in a standard Apache 2.0 module. The macro ‘STANDARD20_MODULE_STUFF’ predefines the first eight of these for us. The macro is defined in http_config.h. It mostly contains bookkeeping items such as the major and minor magic numbers for the Apache release.

Extension system in other web servers
Extensions aren’t unique to Apache HTTP Server but are also available in other browsers. Let’s have a very brief look at how other web servers support extensions:

Oracle iPlanet Web Server (formerly Sun Java Web Server): Apart from CGI, iPlanet Web Server supports NSAPI (Netscape Server Application Programming Interface) and Java  Servlets and JavaServer Pages (JSP).

Netscape Server Application Programming Interface (NSAPI) implements the functions and server calls when processing a request (Server Application Functions or SAFs), which provide the core and extended functionality of iPlanet Web Server. It allows the server to process requests and divide into small steps that can be arranged in a variety of ways for speed and
flexible configuration.

Java Servlets and JavaServer Pages extensions enable all servlet and JSP meta functions, including instantiation, initialisation, destruction, access from other components, and configuration management. Servlets and JSPs are reusable Java applications that run on a web server rather than in a web browser.

Microsoft Internet Information Services (IIS) Web Server: Apart from supporting CGI and various .NET languages, IIS uses ISAPI (Internet Server Application Programming Interface). ISAPI consists of two components: Extensions and Filters. These are the only two types of applications that can be developed using ISAPI. Both Filters and Extensions must be compiled into DLL files which are then registered with IIS to be run on the web server. ISAPI applications can be written using any language which allows the export of standard C functions – for instance C, C++, Delphi. You can also use Apache’s mod_isapi to use ISAPI-based modules with Apache.

Building Apache Module mod_lud.c
That’s the only theory we need before beginning. Let’s start coding. In the following steps we will be building mod_lud.c . Full source code for this can be downloaded here.

01 Importing Apache module functions
Our module only requires the following two header files which make up the core server components. Note that you will need to include additional files if you are planning to include additional functionalities.

#include “httpd.h”
#include “http_config.h”

module AP_MODULE_DECLARE_DATA lud_module; //Module declaration to the httpd core

02 Module settings fallback
Our module requires a configuration directive to show the message in the error log. But it is optional. The following code will check if the user has entered the configuration for our module – if not, it will display the default text.

#ifndef DEFAULT_MODlud_STRING
#define DEFAULT_MODlud_STRING “apache2_mod_lud: Default HTTP Request Alert.”
#endif

Continue to page 3

twitter follow us
Pages: 1 2 3 4
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.

    2 Comments »

    • minnie said:

      [link=http://www.google.com]google[/link]

    • Farrukh Shahzad said:

      Great Article!

      A good step by step article describing newbies like me :)

      Thanks

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields