Apache Server中的全局变量 [英] Global variables in Apache Server

查看:274
本文介绍了Apache Server中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C编写一些apache(2.2)模块,但是我还很新,所以我想知道:

I'm writing some apache (2.2) modules in C and I'm pretty new at it, so I was wondering:

我需要知道是否有可能创建一个全局变量,该变量将在apache服务器开始运行时启动.

I need to know if it's possible to create a global variable that will be initiated whenever the apache server starts to run.

看,我需要有一个主机名列表(将是特权"),以便我收到的每个请求都需要检查主机名是否出现在列表中(以检查它是否是有特权的") ).

See, I need to have a list of host names (that will be "privileged"), so that every request I get, I need to check if the host name appears in the list (to check if it's "previleged").

所以列表应该是全局的(以便每个服务器实例都具有列表的相同实例),我需要在开始时对其进行初始化.

So the list should be global (so that every server instance will have the same instance of the list), and I need to initialize it at the beginning.

如果可能的话,我该怎么办?

How do I do that, if it's at all possible?

谢谢!

推荐答案

虽然不是一个完整的答案,但我确实设法找到了具有全局变量的方法.

Although not a complete answer, I did manage to find a way to have global variables.

我在进程的全局池(pconf和pool)中使用了apr_pool_userdata_getapr_pool_userdata_set方法.

I used the apr_pool_userdata_get and apr_pool_userdata_set methods with the process's global pools (pconf and pool).

进一步参考:
http://apr.apache.org/docs/apr/0.9/group_ apr _pools.html

For further reference:
http://apr.apache.org/docs/apr/0.9/group_apr_pools.html

示例:

将静态全局数据附加到服务器进程池

attach static global data to server process pool

char *data = "this is some data";
apr_pool_userdata_setn ((void*) data, "myglobaldata_key", NULL, request->server->process->pool);

将已分配的堆数据附加到服务器进程池

attach malloced heap data to server process pool

char *data = strdup("this is some data");
apr_pool_userdata_setn ((void*) data, "myglobaldata_key", (apr_status_t(*)(void *))free, request->server->process->pool);

现在检索全局数据:

char *data;
apr_pool_userdata_get ((void**)&data, "myglobaldata_key", request->server->process->pool);
if (data == NULL) {
    // data not set...
}

这篇关于Apache Server中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆