一.目的
寫(xiě)一個(gè)APACHE2.0的MOD模塊,讀取配置,并對所有后綴為.hello的請求進(jìn)行處理。
二.步驟
創(chuàng )建一個(gè)mod_hello.c文件
1. 定義一個(gè)模塊。
#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
};
說(shuō)明:
其中create_modhello_config函數為用來(lái)為自定義的結構分配空間,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}
};
參數結構的創(chuàng )建,由apache在裝載模塊時(shí)候調用。
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-s cript",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-s cript .hello
增加參數:
welcome "hello,world"
ModuleMaxProcess 5
3. 安裝
gcc -v
gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)
make.
四.測試。
訪(fǎng)問(wèn)http://xxx.xxx.xxx.xxx/a.hello, 屏幕上打印出 “hello,world”,同時(shí)LOG中也有打印信息。
五.參考資料
1. http://threebit.net/tutorials/apache2_modules/tut1/tutorial1.html
2. Writing.Apache Modules with Perl and C(Lincoln Stein and Doug MacEachern)
APACHE MOD 開(kāi)發(fā) Step 2 獲取用戶(hù)輸入
聯(lián)系客服