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

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值