六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 55|回复: 0

DSO开发指南晋级(APACHE2.0 MOD 模块开发)

[复制链接]

升级  28.8%

198

主题

198

主题

198

主题

进士

Rank: 4

积分
644
 楼主| 发表于 2013-1-16 02:19:57 | 显示全部楼层 |阅读模式
APACHE2.0  MOD 模块开发 STEP  1

一.目的
写一个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/
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表