Initializer
void module_init(server_rec *pServer, pool *pPool) [1.3] int module_post_config(apr_pool_t *pPool, apr_pool_t *pLog, apr_pool_t *pTemp, server_rec *pServer) [2.0]
In 1.3 this is the init
hook, but in 2.0 it has
been renamed, more accurately, to post_config
.
In 2.0 the three pools provided are, in order,
pPool
, a pool that lasts until the configuration
is changed, corresponding to pPool
in 1.3;
pLog
, a pool that is cleared after each read of
the configuration file (remembering it is read twice for each
reconfiguration) intended for log files; and
ptemp
, a temporary pool that is cleared after
configuration is complete (and perhaps more often than that).
This function
is called after the server configuration files have been read but
before any requests are handled. Like the configuration functions, it
is called each time the server is reconfigured, so care must be taken
to make sure it behaves correctly on the second and subsequent calls.
This is the last function to be called before Apache forks the
request-handling children. pServer
is a pointer to
the server_rec
for the main host.
pPool
is a pool
that persists
until the server is reconfigured. Note that, at least in the current
version of Apache:
pServer->server_hostname
may not yet be initialized. If the module is going to add to the
version string with ap_add_version_component()
,
then this is a good place to do it.
It is possible to iterate through all the server configurations by
following the next
member of
pServer
, as in the following:
for( ; pServer ; pServer=pServer->next) ;
See Example 21-9 (1.3) for an excerpt from mod_mime.c.
Example 21-9. mod_mime.c
#define MIME_HASHSIZE (32) #define hash(i) (ap_tolower(i) % MIME_HASHSIZE) static table *hash_buckets[MIME_HASHSIZE]; static void init_mime(server_rec *s, pool *p) { configfile_t *f; char l[MAX_STRING_LEN]; int x; char *types_confname = ap_get_module_config(s->module_config, &mime_module); if (!types_confname) types_confname = TYPES_CONFIG_FILE; types_confname = ap_server_root_relative(p, types_confname); if (!(f = ap_pcfg_openfile(p, types_confname))) { ap_log_error(APLOG_MARK, APLOG_ERR, s, "could not open mime types log file %s.", types_confname); exit(1); } for (x = 0; x < MIME_HASHSIZE; x++) hash_buckets[x] = ap_make_table(p, 10); while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) { const char *ll = l, *ct; if (l[0] == '#') continue; ct = ap_getword_conf(p, &ll); while (ll[0]) { char *ext = ap_getword_conf(p, &ll); ap_str_tolower(ext); /* ??? */ ap_table_setn(hash_buckets[hash(ext[0])], ext, ct); } } ap_cfg_closefile(f); }
The same function in mod_mime.c uses a hash provided by APR instead of building its own, as shown in Example 21-10 (2.0).
Example 21-10. mod_mime.c
static apr_hash_t *mime_type_extensions; static int mime_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) { ap_configfile_t *f; char l[MAX_STRING_LEN]; const char *types_confname = ap_get_module_config(s->module_config, &mime_module); apr_status_t status; if (!types_confname) types_confname = AP_TYPES_CONFIG_FILE; types_confname = ap_server_root_relative(p, types_confname); if ((status = ap_pcfg_openfile(&f, ptemp, types_confname)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, status, s, "could not open mime types config file %s.", types_confname); return HTTP_INTERNAL_SERVER_ERROR; } mime_type_extensions = apr_hash_make(p); while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) { const char *ll = l, *ct; if (l[0] == '#') continue; ct = ap_getword_conf(p, &ll); while (ll[0]) { char *ext = ap_getword_conf(p, &ll); ap_str_tolower(ext); /* ??? */ apr_hash_set(mime_type_extensions, ext, APR_HASH_KEY_STRING, ct); } } ap_cfg_closefile(f); return OK; }