mod_annot editor

Annotate Section

Managing a Module Configuration

No less than five out of the six (usable) elements of the Apache module struct are concerned with configuration:

module my_module = {
  STANDARD20_MODULE_STUFF,
  my_create_dir_conf,		/* Create config rec for Directory */
  my_merge_dir_conf, 		/* Merge config rec for Directory */
  my_create_svr_conf,		/* Create config rec for Host */
  my_merge_svr_conf, 		/* Merge config rec for Host */
  my_cmds,           		/* Configuration directives */
  my_hooks
} ;

It is up to each module whether and how to define each struct. Whenever a struct is defined, the module must implement an appropriate create function to allocate and (usually) initialise it:

typedef struct {
  ... ;
} my_svr_cfg ;

static void* my_create_svr_conf(apr_pool_t* pool, server_rec* svr) {
  my_svr_cfg* svr = apr_pcalloc(pool, sizeof(my_svr_cfg));
  /* Set up the default values for fields of svr */
  return svr ;
}

typedef struct {
  ... ;
} my_dir_cfg ;

static void* my_create_dir_conf(apr_pool_t* pool, char* x) {
  my_dir_cfg* dir = apr_pcalloc(pool, sizeof(my_dir_cfg));
  /* Set up the default values for fields of dir */
  return dir ;
}

At this point, just allocating and returning a struct of the right size is sufficient: Apache uses the return value. Now these values can be accessed at any time a server_rec or request_rec respectively is available:

my_svr_cfg* svr = ap_get_module_config(s->module_config, &my_module) ;
my_dir_cfg* dir = ap_get_module_config(r->per_dir_config, &my_module) ;