c++ - 为什么在我的 Apache 2.2 模块中调用 ap_hook_type_checker 函数时 request_rec::filename NULL?

标签 c++ linux apache ubuntu

我有一个 Apache 模块可以在 CentOS 上正常工作,但在 Ubuntu 上会失败。我已经将问题追溯到这样一个事实,即请求记录在 request_rec 结构中包含文件名的 NULL 值,Apache 作为参数传递给我在模块中定义的 Hook 函数检查正在处理的文件的文件类型。

即,

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

...与

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);
}

... 最后是罪魁祸首函数:

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.
}

    // ...
}

我在 Ubuntu 和 CentOS 上都使用 Apache 2.2,并且我在两个系统上独立地从头构建了模块。

更多信息~3 个月后: 我发现在 CentOS 上构建模块,然后将二进制文件复制到 Ubuntu,并且它可以工作。采用相同的代码并在 Ubuntu 上构建它会在运行时导致上述故障。因此,代码似乎不一定是问题所在——或者至少编译器在两个系统上的处理方式不同导致 CentOS 构建成功但 Ubuntu 构建失败。

最佳答案

我刚才遇到了一个几乎相同的问题。我正在使用其他人编写的模块。在一台测试机器上,该模块正确执行 authN 和 authZ。在另一台机器上,r->filename 在 auth checker hook 中是 NULL。你的问题首先出现在我的谷歌搜索中。

在我的案例中,我将其归因于 ap_set_module_config 的错误使用。代码看起来像:

ap_set_module_config(r, &auth_my_module, attrs);

ap_set_module_config 的源代码是这样说的:

/**
 * 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);

我将对 ap_set_module_config 的调用更改为:

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

就像变魔术一样,r->filename 在 auth checker hook 中不再是 NULL。我是编写 apache 模块的新手,所以我需要弄清楚这到底是什么意思,但我很高兴我弄清楚了问题出在哪里。您可以检查您的代码以查看您如何调用 ap_set_module_config

编辑:我应该补充一点,我通过添加调试语句来跟踪这个问题,这些语句准确地显示了 request_rec 结构的 filename 字段变成了 NULL。对我来说,它在我定义的钩子(Hook)内从有效变为 NULL

祝你好运!

关于c++ - 为什么在我的 Apache 2.2 模块中调用 ap_hook_type_checker 函数时 request_rec::filename NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7115671/

相关文章:

c++ - boost_multi 数组太大? bad_alloc 错误

linux - ubuntu中安装的软件包在哪里?

git - Docker 上带有 Alpine Linux 的 Apache git 服务器

java - Tomcat 5.5 无法在 eclipse 中启动 - 5 分钟后超时

php - "Symbolic"链接以解决对 public_html 之外的文件夹的请求?

c++ - 通过嵌入式IWebBrowser2控件中的链接打开youtube搜索失败

c++ - C++11 <codecvt> header 在最新的 GCC 中可用吗?

c++ - 实现纯虚函数:时出现一些错误

linux - 如何确定 linux 中的定时器频率

linux - 使用命令 find,找到一个不包含某些元素的目录