Writing Apache Modules
by L. Blunt Jackson


Example 1:
module MODULE_VAR_EXPORT hw_module = {
    STANDARD_MODULE_STUFF,
    hw_init,       /* initializer */
    NULL,          /* dir config creator */
    NULL,          /* dir merger --- default is to override */
    NULL,          /* server config */
    NULL,          /* merge server config */
    hw_cmds,       /* command table */
    hw_handlers,   /* response handler table */
    NULL,          /* filename translation */
    NULL,          /* check_user_id */
    NULL,          /* check auth */
    NULL,          /* check access */
    NULL,          /* type_checker */
    NULL,          /* fixups */
    NULL,          /* logger */
    NULL,          /* header parser */
    NULL,          /* child_init */
    NULL,          /* child_exit */
    NULL           /* post read-request */
};

Example 2:

static const char* greeting;
static const char*
set_greeting(cmd_parms *cmd, void* empty, char* text)
{
    greeting = text;
    return NULL;
}
command_rec hw_cmds[] = {
    { "SetGreeting", set_greeting, NULL, RSRC_CONF, TAKE1, "Set Greeting" },
    { NULL }
};


Example 3:

static int hw_handle_req ( request_rec *r )
{
    const char* ua = ap_table_get(r->headers_in, "User-Agent" );
    if (!ua || !*ua) ua = "No User-Agent.";
    /* set and send headers */
    r->content_type = "text/html";
    ap_send_http_header( r );
    /* send the content body */
    ap_rputs( "<HTML><HEAD><TITLE>Hi. Welcome!</TITLE></HEAD><BODY>\n" , r );
    ap_rprintf( r, "<P>%s <br>Using Browser: %s</P>\n", greeting, ua );
    ap_rputs( "</BODY></HTML>\n", r );
    return OK;
}
static const handler_rec hw_handlers[] = {
    { "hw-app", hw_handle_req },
    { NULL }
};


Example 4:

static int hw_handle_req(request_rec* r)
{
    const char* ua;
    if(strcmp(r->handler, "hw") != 0)
        return DECLINED;
    ua = apr_table_get (r->headers_in, "User-Agent");
    if (!ua || !*ua) ua = "No User Agent.";
    r->content_type = "text/html";
    ap_rputs  (    "<HTML><HEAD><TITLE>Hi!</TITLE></HEAD><BODY>\n" , r );
    ap_rprintf( r, "<P>%s <br>Using Browser: %s</P>\n", greeting, ua );
    ap_rputs  (    "</BODY></HTML>\n", r );
    return OK;
}


Listing One

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"

static const char* greeting;

/* command processing happens at startup, before all else */
static const char*
set_greeting(cmd_parms *cmd, void* empty, char* text)
{
    /* TAKE1 guarantees that text will not be null */
    greeting = text;
    /* If we wanted to return an error code, we would return a string.
       This would prevent Apache from running. */
    return NULL;
}
/* initialization is called at startup, after command processing
   we use this to set the greeting if no directives were placed
   in the config file. */
static void
hw_init (server_rec *s, pool *p)
{
    if (! greeting) set_greeting(NULL, NULL, "Init Greeting");
}
static int hw_handle_req ( request_rec *r )
{
    const char* ua = ap_table_get(r->headers_in, "User-Agent" );
    if (!ua || !*ua) ua = "No User-Agent.";

    /* set and send headers */
    r->content_type = "text/html";
    ap_send_http_header( r );

    /* send the content body */
    ap_rputs( "<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY>\n" , r );
    ap_rprintf( r, "<P>%s <br>Using Browser: %s</P>\n", greeting, ua );
    ap_rputs( "</BODY></HTML>\n", r );
    return OK;
}
command_rec hw_cmds[] = {
    { "SetGreeting", set_greeting, NULL, RSRC_CONF, TAKE1, "Set Greeting" },
    { NULL }
};
static const handler_rec hw_handlers[] = {
    { "hw-app", hw_handle_req },
    { NULL }
};
module MODULE_VAR_EXPORT hw_module = {
    STANDARD_MODULE_STUFF,
    hw_init,       /* initializer */
    NULL,          /* dir config creator */
    NULL,          /* dir merger --- default is to override */
    NULL,          /* server config */
    NULL,          /* merge server config */
    hw_cmds,       /* command table */
    hw_handlers,   /* response handlers */
    NULL,          /* filename translation */
    NULL,          /* check_user_id */
    NULL,          /* check auth */
    NULL,          /* check access */
    NULL,          /* type_checker */
    NULL,          /* fixups */
    NULL,          /* logger */
    NULL,          /* header parser */
    NULL,          /* child_init */
    NULL,          /* child_exit */
    NULL           /* post read-request */
};


Listing Two

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"

static const char* greeting;
static const char* set_greeting(cmd_parms* cmd, void* empty, char* text)
{
    greeting = text;
    return NULL;
}
static int
hw_init(apr_pool_t* p, apr_pool_t* plog, apr_pool_t* ptemp, server_rec* s)
{
    if (!greeting) set_greeting(NULL, NULL, "No Greeting at Init");
    return OK;
}
static int hw_handle_req(request_rec* r)
{
    const char* ua;
    /* note, we have to check to see if this handler is
       the one that has actually been requested */
    if(strcmp(r->handler, "hw") != 0)
        return DECLINED;
    ua = apr_table_get (r->headers_in, "User-Agent");
    if (!ua || !*ua) ua = "No User Agent.";
    /* note, we do not _send_ headers anymore */
    r->content_type = "text/html";
    ap_rputs  (    "<HTML><HEAD><TITLE>Hi!</TITLE></HEAD><BODY>\n" , r );
    ap_rprintf( r, "<P>%s <br>Using Browser: %s</P>\n", greeting, ua );
    ap_rputs  (    "</BODY></HTML>\n", r );
    return OK;
}
command_rec hw_cmds[] = {
    { "SetGreeting", set_greeting, NULL, RSRC_CONF, TAKE1, "Set Greeting" },
    { NULL }
};
static void hw_register_hooks(apr_pool_t* p)
{
    ap_hook_post_config (hw_init,       NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_handler     (hw_handle_req, NULL, NULL, APR_HOOK_FIRST );
}
module AP_MODULE_DECLARE_DATA hw_module =
{
    STANDARD20_MODULE_STUFF,
    NULL,    /* create per-directory config structures */
    NULL,    /* merge per-directory config structures  */
    NULL,    /* create per-server config structures    */
    NULL,    /* merge per-server config structures     */
    hw_cmds,            /* command handlers */
    hw_register_hooks   /* register hooks   */
};







4


