Notice: Undefined index: order_next_posts in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 194

Notice: Undefined index: post_link_target in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 195

Notice: Undefined index: posts_featured_size in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 196
News

Develop Apache HTTP Server Modules

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…

03 Defining the module data structure
For our module we will be using server_cfg, commands and hooks.
[bash]module AP_MODULE_DECLARE_DATA mod_lud_apache =
{
STANDARD20_MODULE_STUFF, // standard
NULL,
NULL,
create_modlud_config, // create per-server configuration structures.
NULL,
mod_lud_cmds, // configuration directive handlers
mod_lud_register_hooks, // request handlers
};
[/bash]
04 Registering the module function with the server core
Apache will run this function at server startup. It will register the module’s processing functions with the server core, so that our module’s functions will subsequently be invoked on demand.
[bash]static void mod_lud_register_hooks (apr_pool_t *p)
{
ap_hook_handler(mod_lud_method_handler, NULL, NULL, APR_HOOK_LAST);
}
[/bash]
05 Implementing the handler function
The following is the callback function that will be invoked for every HTTP request.
[bash]static int mod_lud_method_handler (request_rec *r)
{
// Read the module configuration
modlud_config *s_cfg = ap_get_module_config(r->server->module_config, &mod_lud_apache);
// Send a message to the Apache log file.
fprintf(stderr,”%sn”,s_cfg->string);
// Flush the datastream so that it is written immediately.
fflush(stderr);

// Return DECLINED so that the Apache core will keep looking for
// other modules to handle this request.  This effectively makes
// this module completely transparent.
return DECLINED;
}
[/bash]
06 Module configuration data structure
The following struct will hold the custom string data used in the configuration of this module.
[bash]typedef struct {
char *string;
} modlud_config;
[/bash]
07 Creating module configuration
Module configuration function is called once by the httpd core to do the initial module configuration.
[bash]static void *create_modlud_config(apr_pool_t *p, server_rec *s)
{
modlud_config *newcfg;

// allocate memory for the modlud_config struct from the provided pool p.
newcfg = (modlud_config *) apr_pcalloc(p, sizeof(modlud_config));

// set the default value for the error string.
newcfg->string = DEFAULT_MODlud_STRING;

// return the new server configuration structure.
return (void *) newcfg;
}
[/bash]
APR (Apache Portable Runtime) provides the alloc function called ‘apr_pcalloc’, which allocates a block of memory. This facilitates automatic allocation and de‑allocation of memory by the Apache Portable Runtime and prevention of memory leaks.

Continue to page 4

[twitter username=”linuxusermag”]

×