开发者

Why is request_rec::filename NULL when the ap_hook_type_checker function is called in my Apache 2.2 module?

I have an Apache module that works properly on CentOS, but fails on Ubuntu. I have tracked the problem down to the fact that the request record contains a NULL value for the filename in the request_rec struct that Apache is passing as an argument to the hook function I've defined in my module to check the file type for the file being processed.

i.e.,

extern "C" module AP_MODULE_DECLARE_DATA  MyModule = {
STANDARD20_MODULE_STUFF,     // initializer
NULL,                        // create per-dir config
NULL,                        // merge per-dir config
NULL, 开发者_JAVA百科                       // server config
NULL,                        // merge server config
MyCommandTable,              // command table
MyRegisterHooks              // register hooks
};

... with

static void MyRegisterHooks(apr_pool_t *p)
{
ap_hook_child_init(MyPluginInit, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_type_checker(MyCheckAppFileType, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(MyFileHandler,NULL,NULL,APR_HOOK_MIDDLE);
}

... and, finally, the culprit function:

static int MyCheckAppFileType(request_rec *ap_req)
{
if(ap_req == NULL)
{
    return DECLINED; // Not reached
}

if(ap_req->filename == NULL)
{
    return DECLINED; // HERE is the problem ... Why is ap_req->filename NULL?
                     // On CentOS it is not NULL, only on Ubuntu.
}

    // ...
}

I am using Apache 2.2 on both Ubuntu and on CentOS, and I have built the module from scratch on both systems independently.

FURTHER INFO ~3 months later: I have discovered that building the module on CentOS, and then copying the binary over to Ubuntu, and it works. Taking the identical code and building it on Ubuntu causes the above failure at runtime. Therefore, the code does not necessarily seem to be the problem -- or at least there is something being handled by the compiler differently on the two systems that is causing success with the CentOS build but not the Ubuntu build.


I ran into an almost identical problem just now. I'm using a module that someone else wrote. On one test machine the module was doing authN and authZ correctly. On a different machine r->filename was NULL inside of the auth checker hook. Your question here came up first in my google search.

In my case I tracked it down to an incorrect usage of ap_set_module_config. The code looked like:

ap_set_module_config(r, &auth_my_module, attrs);

The source code for ap_set_module_config says this:

/**
 * Generic accessors for other modules to set at their own module-specific
 * data
 * @param conf_vector The vector in which the modules configuration is stored.
 *        usually r->per_dir_config or s->module_config
 * @param m The module to set the data for.
 * @param val The module-specific data to set
 */
AP_DECLARE(void) ap_set_module_config(ap_conf_vector_t *cv, const module *m,
                                      void *val);

I changed the the call to ap_set_module_config to this:

ap_set_module_config(r->per_dir_config, &auth_my_module, attrs);

And just like magic, r->filename is no longer NULL in the auth checker hook. I'm new to writing apache modules so I need to find out exactly what this means, but I'm glad I figured out where the problem is. You might check your code to see how you call ap_set_module_config.

Edit: I should add that I tracked this down by adding debug statements that showed me exactly when the filename field of the request_rec struct became NULL. For me it went from valid to NULL inside a hook I defined.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜