Asked by
ram lal
in
Computers & Technology
at
5:35 AM on December 04, 2008
Sudipta Deb's Answer
In your module AP_MODULE_DECLARE_DATA structure, define a "register hooks" callback. This will be invoked as part of your module's initialisation, and gives a chance for your module to register for any of the processing hooks Apache makes available.
One of the hooks you should register for is ap_hook_insert_filter hook, something like this:
static ap_filter_rec_t * globalMyInputFilter ;
static ap_filter_rec_t * globalMyOutputFilter ;
...
static void myModuleRegisterHooks(apr_pool _t *p) {
ap_hook_insert_filter(myModule InsertFilters, NULL, NULL, APR_HOOK_MIDDLE) ;
globalMyInputFilter = ap_register_input_filter(myInp utFilterName, myInputFilter,
NULL, AP_FTYPE_RESOURCE) ;
globalMyOutputFilter = ap_register_output_filter(myOu tputFilterName, myOutputFilter,
NULL, AP_FTYPE_RESOURCE) ;
}
This will result in your function myModuleInsertFilters being invoked on each request. This code also registers the 2 filters, one input and one output, and saves the resultant pointers to Apache filter record structures, which makes the next step performed at a "per request" level more efficient...
Now, on each request, Apache will invoke your myModuleInsertFilters callback code. It has to decide whether it is interested in this request (maybe by looking at the request and configuration data), and if so, add the filters to the request.
Each filter can be associated with a "context", and by setting the same context on both filters, you make it easy for them to share data, status etc.
For example:
static void myModuleInsertFilters(request_ rec *r) {
MyPerRequestContext *ctx ;
MyPerServerConfig *myServerConfig = ap_get_module_config(r->server ->module_config,
&myFilter_module);
if(!myServerConfig->enabled) return ; // some server level enablement switch
if (.....) { // some other "are we interested?" type tests...
return ; // return without doing anything
}
ctx = apr_palloc(r->pool, sizeof(*ctx)) ; // allocate my "per request" context from the
// Apache-managed "per request" memory pool
ctx->status = 0 ; // initialise it...
....
// add the input and output handlers, sharing a context
ap_add_input_filter_handle(glo balMyInputFilter, ctx, r, r->connection) ;
ap_add_output_filter_handle(gl obalMyOutputFilter, ctx, r, r->connection) ;
}
Answered at
5:46 AM on December 04, 2008
Read all answers