目的写一个APACHE2.0
的MOD
模块,读取配置,并对所有后缀为.hello
的请求进行处理。
二.步骤
创建一个mod_hello.c
文件
1.
定义一个模块。
#include "httpd.h"
#include "http_config.h"
module AP_MODULE_DECLARE_DATA hello_module;
2.
定义接口。
module AP_MODULE_DECLARE_DATA hello_module =
{
STANDARD20_MODULE_STUFF, // standard stuff; no need to mess with this.
NULL, // create per-directory configuration structures - we do not.
NULL, // merge per-directory - no need to merge if we are not creating anything.
create_modhello_config, // create per-server configuration structures.
NULL, // merge per-server - hrm - examples I have been reading don't bother with this for trivial cases.
mod_hello_cmds, // configuration directive handlers
mod_hello_register_hooks, // request handlers
};
说明:
其中create_modhello_config
函数为用来为自定义的结构分配空间,mod_hello_cmds
定义了参数序列和参数的读取函数。mod_hello_register_hooks
定义了请求处理函数
3.
初始化配置,读取配置。
配置结构的定义:
typedef struct {
char
*welcome;
int
max_process;
} modhello_config;
参数的定义:
static const command_rec mod_hello_cmds[] =
{
AP_INIT_TAKE1(
"welcome",
set_modhello_string,
NULL,
RSRC_CONF,
"hello,apache"
),
AP_INIT_TAKE1(
"ModuleMaxProcess",
set_modhello_string,
NULL,
RSRC_CONF,
NULL
),
{NULL}
};
参数结构的创建,由apache
在装载模块时候调用。
static void *create_modhello_config(apr_pool_t *p, server_rec *s)
{
modhello_config *newcfg;
// allocate space for the configuration structure from the provided pool p.
newcfg = (modhello_config *) apr_pcalloc(p, sizeof(modhello_config));
// return the new server configuration structure.
return (void *) newcfg;
}
参数读取函数
static const char *set_modhello_string(cmd_parms *parms, void *mconfig, const char *arg)
{
modhello_config *s_cfg = ap_get_module_config(parms->server->module_config, &hello_module);
if(!strcmp(parms->cmd->name,"welcome")){
s_cfg->welcome= (char *) arg;
}else if(!strcmp(parms->cmd->name,"ModuleMaxProcess")){
s_cfg->max_process=atoi(arg);
}
// success
return NULL;
}
4.
处理请求。
注册请求。
static void mod_hello_register_hooks (apr_pool_t *p)
{
ap_hook_handler(mod_hello_method_handler, NULL, NULL, APR_HOOK_LAST);
}
请求处理函数
static int mod_hello_method_handler (request_rec *r)
{
modhello_config *s_cfg ;
if(strcmp("hello-script",r->handler)) return DECLINED;
s_cfg= ap_get_module_config(r->server->module_config, &hello_module);
fprintf(stderr,"%s,%s,%d\n",r->content_type,r->handler,s_cfg->max_process);
ap_rputs("hello,world!",r);
return 0;
}
三.安装。
1.
编译。
看Makefile.
all:
mod_hello.c
gcc -g -I/home/wee/apache2/include/ -fPIC -o mod_hello.o -c mod_hello.c
gcc -shared -I/home/wee/apache2/include/ -o libmodhello.so -lc mod_hello.o
cp *.so /home/wee/apache2/modules/
clean:
rm *.o *.so
2.
配置。
修改Httpd.conf
。
增加处理:
LoadModule hello_module
modules/libmodhello.so
AddHandler hello-script .hello
增加参数:
welcome
"hello,world"
ModuleMaxProcess
5
3.
安装
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)
make.
四.测试。
访问http://xxx.xxx.xxx.xxx/a.hello, 屏幕上打印出 “hello,world”,
同时LOG
中也有打印信息。
五.参考资料
1.
http://threebit.net/tutorials/apache2_modules/tut1/tutorial1.html
2.
Writing.Apache Modules with Perl and C
(Lincoln Stein and
Doug MacEachern)
3.
http://apache-modules.com/
4.
http://www.apache.org/